tmux.c revision 1.50
1/* $OpenBSD: tmux.c,v 1.50 2009/10/21 20:11:47 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 <paths.h> 24#include <pwd.h> 25#include <signal.h> 26#include <stdlib.h> 27#include <string.h> 28#include <syslog.h> 29#include <unistd.h> 30 31#include "tmux.h" 32 33#ifdef DEBUG 34const char *malloc_options = "AFGJPX"; 35#endif 36 37volatile sig_atomic_t sigwinch; 38volatile sig_atomic_t sigterm; 39volatile sig_atomic_t sigcont; 40volatile sig_atomic_t sigchld; 41volatile sig_atomic_t sigusr1; 42volatile sig_atomic_t sigusr2; 43 44char *cfg_file; 45struct options global_s_options; /* session options */ 46struct options global_w_options; /* window options */ 47struct environ global_environ; 48 49int debug_level; 50int be_quiet; 51time_t start_time; 52char *socket_path; 53int login_shell; 54 55__dead void usage(void); 56void fill_session(struct msg_command_data *); 57char *makesockpath(const char *); 58int prepare_cmd(enum msgtype *, void **, size_t *, int, char **); 59int dispatch_imsg(struct imsgbuf *, const char *, int *); 60__dead void shell_exec(const char *, const char *); 61 62__dead void 63usage(void) 64{ 65 fprintf(stderr, 66 "usage: %s [-28dlquv] [-c shell-command] [-f file] [-L socket-name]\n" 67 " [-S socket-path] [command [flags]]\n", 68 __progname); 69 exit(1); 70} 71 72void 73logfile(const char *name) 74{ 75 char *path; 76 77 log_close(); 78 if (debug_level > 0) { 79 xasprintf(&path, "tmux-%s-%ld.log", name, (long) getpid()); 80 log_open_file(debug_level, path); 81 xfree(path); 82 } 83} 84 85void 86sighandler(int sig) 87{ 88 int saved_errno; 89 90 saved_errno = errno; 91 switch (sig) { 92 case SIGWINCH: 93 sigwinch = 1; 94 break; 95 case SIGTERM: 96 sigterm = 1; 97 break; 98 case SIGCHLD: 99 sigchld = 1; 100 break; 101 case SIGCONT: 102 sigcont = 1; 103 break; 104 case SIGUSR1: 105 sigusr1 = 1; 106 break; 107 case SIGUSR2: 108 sigusr2 = 1; 109 break; 110 } 111 errno = saved_errno; 112} 113 114void 115siginit(void) 116{ 117 struct sigaction act; 118 119 memset(&act, 0, sizeof act); 120 sigemptyset(&act.sa_mask); 121 act.sa_flags = SA_RESTART; 122 123 act.sa_handler = SIG_IGN; 124 if (sigaction(SIGPIPE, &act, NULL) != 0) 125 fatal("sigaction failed"); 126 if (sigaction(SIGINT, &act, NULL) != 0) 127 fatal("sigaction failed"); 128 if (sigaction(SIGTSTP, &act, NULL) != 0) 129 fatal("sigaction failed"); 130 if (sigaction(SIGQUIT, &act, NULL) != 0) 131 fatal("sigaction failed"); 132 133 act.sa_handler = sighandler; 134 if (sigaction(SIGWINCH, &act, NULL) != 0) 135 fatal("sigaction failed"); 136 if (sigaction(SIGTERM, &act, NULL) != 0) 137 fatal("sigaction failed"); 138 if (sigaction(SIGCHLD, &act, NULL) != 0) 139 fatal("sigaction failed"); 140 if (sigaction(SIGUSR1, &act, NULL) != 0) 141 fatal("sigaction failed"); 142 if (sigaction(SIGUSR2, &act, NULL) != 0) 143 fatal("sigaction failed"); 144} 145 146void 147sigreset(void) 148{ 149 struct sigaction act; 150 151 memset(&act, 0, sizeof act); 152 sigemptyset(&act.sa_mask); 153 154 act.sa_handler = SIG_DFL; 155 if (sigaction(SIGPIPE, &act, NULL) != 0) 156 fatal("sigaction failed"); 157 if (sigaction(SIGUSR1, &act, NULL) != 0) 158 fatal("sigaction failed"); 159 if (sigaction(SIGUSR2, &act, NULL) != 0) 160 fatal("sigaction failed"); 161 if (sigaction(SIGINT, &act, NULL) != 0) 162 fatal("sigaction failed"); 163 if (sigaction(SIGTSTP, &act, NULL) != 0) 164 fatal("sigaction failed"); 165 if (sigaction(SIGQUIT, &act, NULL) != 0) 166 fatal("sigaction failed"); 167 if (sigaction(SIGWINCH, &act, NULL) != 0) 168 fatal("sigaction failed"); 169 if (sigaction(SIGTERM, &act, NULL) != 0) 170 fatal("sigaction failed"); 171 if (sigaction(SIGCHLD, &act, NULL) != 0) 172 fatal("sigaction failed"); 173} 174 175const char * 176getshell(void) 177{ 178 struct passwd *pw; 179 const char *shell; 180 181 shell = getenv("SHELL"); 182 if (checkshell(shell)) 183 return (shell); 184 185 pw = getpwuid(getuid()); 186 if (pw != NULL && checkshell(pw->pw_shell)) 187 return (pw->pw_shell); 188 189 return (_PATH_BSHELL); 190} 191 192int 193checkshell(const char *shell) 194{ 195 if (shell == NULL || *shell == '\0' || areshell(shell)) 196 return (0); 197 if (access(shell, X_OK) != 0) 198 return (0); 199 return (1); 200} 201 202int 203areshell(const char *shell) 204{ 205 const char *progname, *ptr; 206 207 if ((ptr = strrchr(shell, '/')) != NULL) 208 ptr++; 209 else 210 ptr = shell; 211 progname = __progname; 212 if (*progname == '-') 213 progname++; 214 if (strcmp(ptr, progname) == 0) 215 return (1); 216 return (0); 217} 218 219void 220fill_session(struct msg_command_data *data) 221{ 222 char *env, *ptr1, *ptr2, buf[256]; 223 size_t len; 224 const char *errstr; 225 long long ll; 226 227 data->pid = -1; 228 if ((env = getenv("TMUX")) == NULL) 229 return; 230 231 if ((ptr2 = strrchr(env, ',')) == NULL || ptr2 == env) 232 return; 233 for (ptr1 = ptr2 - 1; ptr1 > env && *ptr1 != ','; ptr1--) 234 ; 235 if (*ptr1 != ',') 236 return; 237 ptr1++; 238 ptr2++; 239 240 len = ptr2 - ptr1 - 1; 241 if (len > (sizeof buf) - 1) 242 return; 243 memcpy(buf, ptr1, len); 244 buf[len] = '\0'; 245 246 ll = strtonum(buf, 0, LONG_MAX, &errstr); 247 if (errstr != NULL) 248 return; 249 data->pid = ll; 250 251 ll = strtonum(ptr2, 0, UINT_MAX, &errstr); 252 if (errstr != NULL) 253 return; 254 data->idx = ll; 255} 256 257char * 258makesockpath(const char *label) 259{ 260 char base[MAXPATHLEN], *path; 261 struct stat sb; 262 u_int uid; 263 264 uid = getuid(); 265 xsnprintf(base, MAXPATHLEN, "%s/tmux-%d", _PATH_TMP, uid); 266 267 if (mkdir(base, S_IRWXU) != 0 && errno != EEXIST) 268 return (NULL); 269 270 if (lstat(base, &sb) != 0) 271 return (NULL); 272 if (!S_ISDIR(sb.st_mode)) { 273 errno = ENOTDIR; 274 return (NULL); 275 } 276 if (sb.st_uid != uid || (sb.st_mode & (S_IRWXG|S_IRWXO)) != 0) { 277 errno = EACCES; 278 return (NULL); 279 } 280 281 xasprintf(&path, "%s/%s", base, label); 282 return (path); 283} 284 285int 286prepare_cmd(enum msgtype *msg, void **buf, size_t *len, int argc, char **argv) 287{ 288 static struct msg_command_data cmddata; 289 290 fill_session(&cmddata); 291 292 cmddata.argc = argc; 293 if (cmd_pack_argv(argc, argv, cmddata.argv, sizeof cmddata.argv) != 0) { 294 log_warnx("command too long"); 295 return (-1); 296 } 297 298 *buf = &cmddata; 299 *len = sizeof cmddata; 300 301 *msg = MSG_COMMAND; 302 return (0); 303} 304 305int 306main(int argc, char **argv) 307{ 308 struct cmd_list *cmdlist; 309 struct cmd *cmd; 310 struct pollfd pfd; 311 enum msgtype msg; 312 struct passwd *pw; 313 struct options *so, *wo; 314 struct keylist *keylist; 315 struct imsgbuf *ibuf; 316 char *s, *shellcmd, *path, *label, *home, *cause; 317 char cwd[MAXPATHLEN], **var; 318 void *buf; 319 size_t len; 320 int nfds, retcode, opt, flags, cmdflags = 0; 321 322 flags = 0; 323 shellcmd = label = path = NULL; 324 login_shell = (**argv == '-'); 325 while ((opt = getopt(argc, argv, "28c:df:lL:qS:uUv")) != -1) { 326 switch (opt) { 327 case '2': 328 flags |= IDENTIFY_256COLOURS; 329 flags &= ~IDENTIFY_88COLOURS; 330 break; 331 case '8': 332 flags |= IDENTIFY_88COLOURS; 333 flags &= ~IDENTIFY_256COLOURS; 334 break; 335 case 'c': 336 if (shellcmd != NULL) 337 xfree(shellcmd); 338 shellcmd = xstrdup(optarg); 339 break; 340 case 'd': 341 flags |= IDENTIFY_HASDEFAULTS; 342 break; 343 case 'f': 344 if (cfg_file != NULL) 345 xfree(cfg_file); 346 cfg_file = xstrdup(optarg); 347 break; 348 case 'l': 349 login_shell = 1; 350 break; 351 case 'L': 352 if (label != NULL) 353 xfree(label); 354 label = xstrdup(optarg); 355 break; 356 case 'q': 357 be_quiet = 1; 358 break; 359 case 'S': 360 if (path != NULL) 361 xfree(path); 362 path = xstrdup(optarg); 363 break; 364 case 'u': 365 flags |= IDENTIFY_UTF8; 366 break; 367 case 'v': 368 debug_level++; 369 break; 370 default: 371 usage(); 372 } 373 } 374 argc -= optind; 375 argv += optind; 376 377 if (shellcmd != NULL && argc != 0) 378 usage(); 379 380 log_open_tty(debug_level); 381 siginit(); 382 383 if (!(flags & IDENTIFY_UTF8)) { 384 /* 385 * If the user has set whichever of LC_ALL, LC_CTYPE or LANG 386 * exist (in that order) to contain UTF-8, it is a safe 387 * assumption that either they are using a UTF-8 terminal, or 388 * if not they know that output from UTF-8-capable programs may 389 * be wrong. 390 */ 391 if ((s = getenv("LC_ALL")) == NULL) { 392 if ((s = getenv("LC_CTYPE")) == NULL) 393 s = getenv("LANG"); 394 } 395 if (s != NULL && (strcasestr(s, "UTF-8") != NULL || 396 strcasestr(s, "UTF8") != NULL)) 397 flags |= IDENTIFY_UTF8; 398 } 399 400 environ_init(&global_environ); 401 for (var = environ; *var != NULL; var++) 402 environ_put(&global_environ, *var); 403 404 options_init(&global_s_options, NULL); 405 so = &global_s_options; 406 options_set_number(so, "base-index", 0); 407 options_set_number(so, "bell-action", BELL_ANY); 408 options_set_number(so, "buffer-limit", 9); 409 options_set_string(so, "default-command", "%s", ""); 410 options_set_string(so, "default-shell", "%s", getshell()); 411 options_set_string(so, "default-terminal", "screen"); 412 options_set_number(so, "display-panes-colour", 4); 413 options_set_number(so, "display-panes-time", 1000); 414 options_set_number(so, "display-time", 750); 415 options_set_number(so, "history-limit", 2000); 416 options_set_number(so, "lock-after-time", 0); 417 options_set_string(so, "lock-command", "lock -np"); 418 options_set_number(so, "lock-server", 1); 419 options_set_number(so, "message-attr", 0); 420 options_set_number(so, "message-bg", 3); 421 options_set_number(so, "message-fg", 0); 422 options_set_number(so, "mouse-select-pane", 0); 423 options_set_number(so, "repeat-time", 500); 424 options_set_number(so, "set-remain-on-exit", 0); 425 options_set_number(so, "set-titles", 0); 426 options_set_string(so, "set-titles-string", "#S:#I:#W - \"#T\""); 427 options_set_number(so, "status", 1); 428 options_set_number(so, "status-attr", 0); 429 options_set_number(so, "status-bg", 2); 430 options_set_number(so, "status-fg", 0); 431 options_set_number(so, "status-interval", 15); 432 options_set_number(so, "status-justify", 0); 433 options_set_number(so, "status-keys", MODEKEY_EMACS); 434 options_set_string(so, "status-left", "[#S]"); 435 options_set_number(so, "status-left-attr", 0); 436 options_set_number(so, "status-left-bg", 8); 437 options_set_number(so, "status-left-fg", 8); 438 options_set_number(so, "status-left-length", 10); 439 options_set_string(so, "status-right", "\"#22T\" %%H:%%M %%d-%%b-%%y"); 440 options_set_number(so, "status-right-attr", 0); 441 options_set_number(so, "status-right-bg", 8); 442 options_set_number(so, "status-right-fg", 8); 443 options_set_number(so, "status-right-length", 40); 444 options_set_string(so, "terminal-overrides", 445 "*88col*:colors=88,*256col*:colors=256"); 446 options_set_string(so, "update-environment", "DISPLAY " 447 "WINDOWID SSH_ASKPASS SSH_AUTH_SOCK SSH_AGENT_PID SSH_CONNECTION"); 448 options_set_number(so, "visual-activity", 0); 449 options_set_number(so, "visual-bell", 0); 450 options_set_number(so, "visual-content", 0); 451 452 keylist = xmalloc(sizeof *keylist); 453 ARRAY_INIT(keylist); 454 ARRAY_ADD(keylist, '\002'); 455 options_set_data(so, "prefix", keylist, xfree); 456 457 options_init(&global_w_options, NULL); 458 wo = &global_w_options; 459 options_set_number(wo, "aggressive-resize", 0); 460 options_set_number(wo, "automatic-rename", 1); 461 options_set_number(wo, "clock-mode-colour", 4); 462 options_set_number(wo, "clock-mode-style", 1); 463 options_set_number(wo, "force-height", 0); 464 options_set_number(wo, "force-width", 0); 465 options_set_number(wo, "main-pane-height", 24); 466 options_set_number(wo, "main-pane-width", 81); 467 options_set_number(wo, "mode-attr", 0); 468 options_set_number(wo, "mode-bg", 3); 469 options_set_number(wo, "mode-fg", 0); 470 options_set_number(wo, "mode-keys", MODEKEY_EMACS); 471 options_set_number(wo, "mode-mouse", 0); 472 options_set_number(wo, "monitor-activity", 0); 473 options_set_string(wo, "monitor-content", "%s", ""); 474 options_set_number(wo, "window-status-attr", 0); 475 options_set_number(wo, "window-status-bg", 8); 476 options_set_number(wo, "window-status-current-attr", 0); 477 options_set_number(wo, "window-status-current-bg", 8); 478 options_set_number(wo, "window-status-current-fg", 8); 479 options_set_number(wo, "window-status-fg", 8); 480 options_set_number(wo, "xterm-keys", 0); 481 options_set_number(wo, "remain-on-exit", 0); 482 options_set_number(wo, "synchronize-panes", 0); 483 484 if (flags & IDENTIFY_UTF8) { 485 options_set_number(so, "status-utf8", 1); 486 options_set_number(wo, "utf8", 1); 487 } else { 488 options_set_number(so, "status-utf8", 0); 489 options_set_number(wo, "utf8", 0); 490 } 491 492 if (getcwd(cwd, sizeof cwd) == NULL) { 493 pw = getpwuid(getuid()); 494 if (pw->pw_dir != NULL && *pw->pw_dir != '\0') 495 strlcpy(cwd, pw->pw_dir, sizeof cwd); 496 else 497 strlcpy(cwd, "/", sizeof cwd); 498 } 499 options_set_string(so, "default-path", "%s", cwd); 500 501 if (cfg_file == NULL) { 502 home = getenv("HOME"); 503 if (home == NULL || *home == '\0') { 504 pw = getpwuid(getuid()); 505 if (pw != NULL) 506 home = pw->pw_dir; 507 } 508 xasprintf(&cfg_file, "%s/%s", home, DEFAULT_CFG); 509 if (access(cfg_file, R_OK) != 0) { 510 xfree(cfg_file); 511 cfg_file = NULL; 512 } 513 } else { 514 if (access(cfg_file, R_OK) != 0) { 515 log_warn("%s", cfg_file); 516 exit(1); 517 } 518 } 519 520 if (label == NULL) 521 label = xstrdup("default"); 522 if (path == NULL && (path = makesockpath(label)) == NULL) { 523 log_warn("can't create socket"); 524 exit(1); 525 } 526 xfree(label); 527 528 if (shellcmd != NULL) { 529 msg = MSG_SHELL; 530 buf = NULL; 531 len = 0; 532 } else if (prepare_cmd(&msg, &buf, &len, argc, argv) != 0) 533 exit(1); 534 535 if (shellcmd != NULL) 536 cmdflags |= CMD_STARTSERVER; 537 else if (argc == 0) /* new-session is the default */ 538 cmdflags |= CMD_STARTSERVER|CMD_SENDENVIRON; 539 else { 540 /* 541 * It sucks parsing the command string twice (in client and 542 * later in server) but it is necessary to get the start server 543 * flag. 544 */ 545 if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) { 546 log_warnx("%s", cause); 547 exit(1); 548 } 549 cmdflags &= ~CMD_STARTSERVER; 550 TAILQ_FOREACH(cmd, cmdlist, qentry) { 551 if (cmd->entry->flags & CMD_STARTSERVER) 552 cmdflags |= CMD_STARTSERVER; 553 if (cmd->entry->flags & CMD_SENDENVIRON) 554 cmdflags |= CMD_SENDENVIRON; 555 } 556 cmd_list_free(cmdlist); 557 } 558 559 if ((ibuf = client_init(path, cmdflags, flags)) == NULL) 560 exit(1); 561 xfree(path); 562 563 imsg_compose(ibuf, msg, PROTOCOL_VERSION, -1, -1, buf, len); 564 565 retcode = 0; 566 for (;;) { 567 pfd.fd = ibuf->fd; 568 pfd.events = POLLIN; 569 if (ibuf->w.queued != 0) 570 pfd.events |= POLLOUT; 571 572 if ((nfds = poll(&pfd, 1, INFTIM)) == -1) { 573 if (errno == EAGAIN || errno == EINTR) 574 continue; 575 fatal("poll failed"); 576 } 577 if (nfds == 0) 578 continue; 579 580 if (pfd.revents & (POLLERR|POLLHUP|POLLNVAL)) 581 fatalx("socket error"); 582 583 if (pfd.revents & POLLIN) { 584 if (dispatch_imsg(ibuf, shellcmd, &retcode) != 0) 585 break; 586 } 587 588 if (pfd.revents & POLLOUT) { 589 if (msgbuf_write(&ibuf->w) < 0) 590 fatalx("msgbuf_write failed"); 591 } 592 } 593 594 options_free(&global_s_options); 595 options_free(&global_w_options); 596 597 return (retcode); 598} 599 600int 601dispatch_imsg(struct imsgbuf *ibuf, const char *shellcmd, int *retcode) 602{ 603 struct imsg imsg; 604 ssize_t n, datalen; 605 struct msg_print_data printdata; 606 struct msg_shell_data shelldata; 607 608 if ((n = imsg_read(ibuf)) == -1 || n == 0) 609 fatalx("imsg_read failed"); 610 611 for (;;) { 612 if ((n = imsg_get(ibuf, &imsg)) == -1) 613 fatalx("imsg_get failed"); 614 if (n == 0) 615 return (0); 616 datalen = imsg.hdr.len - IMSG_HEADER_SIZE; 617 618 switch (imsg.hdr.type) { 619 case MSG_EXIT: 620 case MSG_SHUTDOWN: 621 if (datalen != 0) 622 fatalx("bad MSG_EXIT size"); 623 624 return (-1); 625 case MSG_ERROR: 626 *retcode = 1; 627 /* FALLTHROUGH */ 628 case MSG_PRINT: 629 if (datalen != sizeof printdata) 630 fatalx("bad MSG_PRINT size"); 631 memcpy(&printdata, imsg.data, sizeof printdata); 632 printdata.msg[(sizeof printdata.msg) - 1] = '\0'; 633 634 log_info("%s", printdata.msg); 635 break; 636 case MSG_READY: 637 if (datalen != 0) 638 fatalx("bad MSG_READY size"); 639 640 client_main(); /* doesn't return */ 641 case MSG_VERSION: 642 if (datalen != 0) 643 fatalx("bad MSG_VERSION size"); 644 645 log_warnx("protocol version mismatch (client %u, " 646 "server %u)", PROTOCOL_VERSION, imsg.hdr.peerid); 647 *retcode = 1; 648 return (-1); 649 case MSG_SHELL: 650 if (datalen != sizeof shelldata) 651 fatalx("bad MSG_SHELL size"); 652 memcpy(&shelldata, imsg.data, sizeof shelldata); 653 shelldata.shell[(sizeof shelldata.shell) - 1] = '\0'; 654 655 shell_exec(shelldata.shell, shellcmd); 656 default: 657 fatalx("unexpected message"); 658 } 659 660 imsg_free(&imsg); 661 } 662} 663 664__dead void 665shell_exec(const char *shell, const char *shellcmd) 666{ 667 const char *shellname, *ptr; 668 char *argv0; 669 670 sigreset(); 671 672 ptr = strrchr(shell, '/'); 673 if (ptr != NULL && *(ptr + 1) != '\0') 674 shellname = ptr + 1; 675 else 676 shellname = shell; 677 if (login_shell) 678 xasprintf(&argv0, "-%s", shellname); 679 else 680 xasprintf(&argv0, "%s", shellname); 681 setenv("SHELL", shell, 1); 682 683 execl(shell, argv0, "-c", shellcmd, (char *) NULL); 684 fatal("execl failed"); 685} 686