tmux.c revision 1.106
1/* $OpenBSD: tmux.c,v 1.106 2011/10/23 08:03:27 nicm Exp $ */ 2 3/* 4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19#include <sys/types.h> 20#include <sys/stat.h> 21 22#include <errno.h> 23#include <event.h> 24#include <fcntl.h> 25#include <paths.h> 26#include <pwd.h> 27#include <stdlib.h> 28#include <string.h> 29#include <unistd.h> 30 31#include "tmux.h" 32 33#ifdef DEBUG 34extern char *malloc_options; 35#endif 36 37struct options global_options; /* server options */ 38struct options global_s_options; /* session options */ 39struct options global_w_options; /* window options */ 40struct environ global_environ; 41 42struct event_base *ev_base; 43 44char *cfg_file; 45char *shell_cmd; 46int debug_level; 47time_t start_time; 48char socket_path[MAXPATHLEN]; 49int login_shell; 50char *environ_path; 51pid_t environ_pid = -1; 52int environ_idx = -1; 53 54__dead void usage(void); 55void parseenvironment(void); 56char *makesocketpath(const char *); 57 58__dead void 59usage(void) 60{ 61 fprintf(stderr, 62 "usage: %s [-28lquv] [-c shell-command] [-f file] [-L socket-name]\n" 63 " [-S socket-path] [command [flags]]\n", 64 __progname); 65 exit(1); 66} 67 68void 69logfile(const char *name) 70{ 71 char *path; 72 73 log_close(); 74 if (debug_level > 0) { 75 xasprintf(&path, "tmux-%s-%ld.log", name, (long) getpid()); 76 log_open_file(debug_level, path); 77 xfree(path); 78 } 79} 80 81const char * 82getshell(void) 83{ 84 struct passwd *pw; 85 const char *shell; 86 87 shell = getenv("SHELL"); 88 if (checkshell(shell)) 89 return (shell); 90 91 pw = getpwuid(getuid()); 92 if (pw != NULL && checkshell(pw->pw_shell)) 93 return (pw->pw_shell); 94 95 return (_PATH_BSHELL); 96} 97 98int 99checkshell(const char *shell) 100{ 101 if (shell == NULL || *shell == '\0' || *shell != '/') 102 return (0); 103 if (areshell(shell)) 104 return (0); 105 if (access(shell, X_OK) != 0) 106 return (0); 107 return (1); 108} 109 110int 111areshell(const char *shell) 112{ 113 const char *progname, *ptr; 114 115 if ((ptr = strrchr(shell, '/')) != NULL) 116 ptr++; 117 else 118 ptr = shell; 119 progname = __progname; 120 if (*progname == '-') 121 progname++; 122 if (strcmp(ptr, progname) == 0) 123 return (1); 124 return (0); 125} 126 127void 128parseenvironment(void) 129{ 130 char *env, path[256]; 131 long pid; 132 int idx; 133 134 if ((env = getenv("TMUX")) == NULL) 135 return; 136 137 if (sscanf(env, "%255[^,],%ld,%d", path, &pid, &idx) != 3) 138 return; 139 environ_path = xstrdup(path); 140 environ_pid = pid; 141 environ_idx = idx; 142} 143 144char * 145makesocketpath(const char *label) 146{ 147 char base[MAXPATHLEN], *path, *s; 148 struct stat sb; 149 u_int uid; 150 151 uid = getuid(); 152 if ((s = getenv("TMPDIR")) == NULL || *s == '\0') 153 xsnprintf(base, sizeof base, "%s/tmux-%u", _PATH_TMP, uid); 154 else 155 xsnprintf(base, sizeof base, "%s/tmux-%u", s, uid); 156 157 if (mkdir(base, S_IRWXU) != 0 && errno != EEXIST) 158 return (NULL); 159 160 if (lstat(base, &sb) != 0) 161 return (NULL); 162 if (!S_ISDIR(sb.st_mode)) { 163 errno = ENOTDIR; 164 return (NULL); 165 } 166 if (sb.st_uid != uid || (sb.st_mode & (S_IRWXG|S_IRWXO)) != 0) { 167 errno = EACCES; 168 return (NULL); 169 } 170 171 xasprintf(&path, "%s/%s", base, label); 172 return (path); 173} 174 175void 176setblocking(int fd, int state) 177{ 178 int mode; 179 180 if ((mode = fcntl(fd, F_GETFL)) != -1) { 181 if (!state) 182 mode |= O_NONBLOCK; 183 else 184 mode &= ~O_NONBLOCK; 185 fcntl(fd, F_SETFL, mode); 186 } 187} 188 189__dead void 190shell_exec(const char *shell, const char *shellcmd) 191{ 192 const char *shellname, *ptr; 193 char *argv0; 194 195 ptr = strrchr(shell, '/'); 196 if (ptr != NULL && *(ptr + 1) != '\0') 197 shellname = ptr + 1; 198 else 199 shellname = shell; 200 if (login_shell) 201 xasprintf(&argv0, "-%s", shellname); 202 else 203 xasprintf(&argv0, "%s", shellname); 204 setenv("SHELL", shell, 1); 205 206 setblocking(STDIN_FILENO, 1); 207 setblocking(STDOUT_FILENO, 1); 208 setblocking(STDERR_FILENO, 1); 209 closefrom(STDERR_FILENO + 1); 210 211 execl(shell, argv0, "-c", shellcmd, (char *) NULL); 212 fatal("execl failed"); 213} 214 215int 216main(int argc, char **argv) 217{ 218 struct passwd *pw; 219 struct keylist *keylist; 220 char *s, *path, *label, *home, **var; 221 int opt, flags, quiet, keys; 222 223#ifdef DEBUG 224 malloc_options = (char *) "AFGJPX"; 225#endif 226 227 quiet = flags = 0; 228 label = path = NULL; 229 login_shell = (**argv == '-'); 230 while ((opt = getopt(argc, argv, "28c:df:lL:qS:uUv")) != -1) { 231 switch (opt) { 232 case '2': 233 flags |= IDENTIFY_256COLOURS; 234 flags &= ~IDENTIFY_88COLOURS; 235 break; 236 case '8': 237 flags |= IDENTIFY_88COLOURS; 238 flags &= ~IDENTIFY_256COLOURS; 239 break; 240 case 'c': 241 if (shell_cmd != NULL) 242 xfree(shell_cmd); 243 shell_cmd = xstrdup(optarg); 244 break; 245 case 'f': 246 if (cfg_file != NULL) 247 xfree(cfg_file); 248 cfg_file = xstrdup(optarg); 249 break; 250 case 'l': 251 login_shell = 1; 252 break; 253 case 'L': 254 if (label != NULL) 255 xfree(label); 256 label = xstrdup(optarg); 257 break; 258 case 'q': 259 quiet = 1; 260 break; 261 case 'S': 262 if (path != NULL) 263 xfree(path); 264 path = xstrdup(optarg); 265 break; 266 case 'u': 267 flags |= IDENTIFY_UTF8; 268 break; 269 case 'v': 270 debug_level++; 271 break; 272 default: 273 usage(); 274 } 275 } 276 argc -= optind; 277 argv += optind; 278 279 if (shell_cmd != NULL && argc != 0) 280 usage(); 281 282 log_open_tty(debug_level); 283 284 if (!(flags & IDENTIFY_UTF8)) { 285 /* 286 * If the user has set whichever of LC_ALL, LC_CTYPE or LANG 287 * exist (in that order) to contain UTF-8, it is a safe 288 * assumption that either they are using a UTF-8 terminal, or 289 * if not they know that output from UTF-8-capable programs may 290 * be wrong. 291 */ 292 if ((s = getenv("LC_ALL")) == NULL || *s == '\0') { 293 if ((s = getenv("LC_CTYPE")) == NULL || *s == '\0') 294 s = getenv("LANG"); 295 } 296 if (s != NULL && (strcasestr(s, "UTF-8") != NULL || 297 strcasestr(s, "UTF8") != NULL)) 298 flags |= IDENTIFY_UTF8; 299 } 300 301 environ_init(&global_environ); 302 for (var = environ; *var != NULL; var++) 303 environ_put(&global_environ, *var); 304 305 options_init(&global_options, NULL); 306 options_table_populate_tree(server_options_table, &global_options); 307 options_set_number(&global_options, "quiet", quiet); 308 309 options_init(&global_s_options, NULL); 310 options_table_populate_tree(session_options_table, &global_s_options); 311 options_set_string(&global_s_options, "default-shell", "%s", getshell()); 312 313 options_init(&global_w_options, NULL); 314 options_table_populate_tree(window_options_table, &global_w_options); 315 316 /* Set the prefix option (its a list, so not in the table). */ 317 keylist = xmalloc(sizeof *keylist); 318 ARRAY_INIT(keylist); 319 ARRAY_ADD(keylist, '\002'); 320 options_set_data(&global_s_options, "prefix", keylist, xfree); 321 322 /* Enable UTF-8 if the first client is on UTF-8 terminal. */ 323 if (flags & IDENTIFY_UTF8) { 324 options_set_number(&global_s_options, "status-utf8", 1); 325 options_set_number(&global_s_options, "mouse-utf8", 1); 326 options_set_number(&global_w_options, "utf8", 1); 327 } 328 329 /* Override keys to vi if VISUAL or EDITOR are set. */ 330 if ((s = getenv("VISUAL")) != NULL || (s = getenv("EDITOR")) != NULL) { 331 if (strrchr(s, '/') != NULL) 332 s = strrchr(s, '/') + 1; 333 if (strstr(s, "vi") != NULL) 334 keys = MODEKEY_VI; 335 else 336 keys = MODEKEY_EMACS; 337 options_set_number(&global_s_options, "status-keys", keys); 338 options_set_number(&global_w_options, "mode-keys", keys); 339 } 340 341 /* Locate the configuration file. */ 342 if (cfg_file == NULL) { 343 home = getenv("HOME"); 344 if (home == NULL || *home == '\0') { 345 pw = getpwuid(getuid()); 346 if (pw != NULL) 347 home = pw->pw_dir; 348 } 349 xasprintf(&cfg_file, "%s/%s", home, DEFAULT_CFG); 350 if (access(cfg_file, R_OK) != 0 && errno == ENOENT) { 351 xfree(cfg_file); 352 cfg_file = NULL; 353 } 354 } 355 356 /* 357 * Figure out the socket path. If specified on the command-line with -S 358 * or -L, use it, otherwise try $TMUX or assume -L default. 359 */ 360 parseenvironment(); 361 if (path == NULL) { 362 /* If no -L, use the environment. */ 363 if (label == NULL) { 364 if (environ_path != NULL) 365 path = xstrdup(environ_path); 366 else 367 label = xstrdup("default"); 368 } 369 370 /* -L or default set. */ 371 if (label != NULL) { 372 if ((path = makesocketpath(label)) == NULL) { 373 log_warn("can't create socket"); 374 exit(1); 375 } 376 } 377 } 378 if (label != NULL) 379 xfree(label); 380 if (realpath(path, socket_path) == NULL) 381 strlcpy(socket_path, path, sizeof socket_path); 382 xfree(path); 383 384 /* Set process title. */ 385 setproctitle("%s (%s)", __progname, socket_path); 386 387 /* Pass control to the client. */ 388 ev_base = event_init(); 389 exit(client_main(argc, argv, flags)); 390} 391