tmux.c revision 1.8
1/* $OpenBSD: tmux.c,v 1.8 2009/06/05 07:22:23 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_options; 46struct options global_window_options; 47 48int server_locked; 49char *server_password; 50time_t server_activity; 51 52int debug_level; 53int be_quiet; 54time_t start_time; 55char *socket_path; 56 57__dead void usage(void); 58char *makesockpath(const char *); 59 60__dead void 61usage(void) 62{ 63 fprintf(stderr, 64 "usage: %s [-28dqUuv] [-f file] [-L socket-name] [-S socket-path]\n" 65 " [command [flags]]\n", 66 __progname); 67 exit(1); 68} 69 70void 71logfile(const char *name) 72{ 73 char *path; 74 75 log_close(); 76 if (debug_level > 0) { 77 xasprintf( 78 &path, "%s-%s-%ld.log", __progname, name, (long) getpid()); 79 log_open_file(debug_level, path); 80 xfree(path); 81 } 82} 83 84void 85sighandler(int sig) 86{ 87 int saved_errno; 88 89 saved_errno = errno; 90 switch (sig) { 91 case SIGWINCH: 92 sigwinch = 1; 93 break; 94 case SIGTERM: 95 sigterm = 1; 96 break; 97 case SIGCHLD: 98 sigchld = 1; 99 break; 100 case SIGCONT: 101 sigcont = 1; 102 break; 103 case SIGUSR1: 104 sigusr1 = 1; 105 break; 106 case SIGUSR2: 107 sigusr2 = 1; 108 break; 109 } 110 errno = saved_errno; 111} 112 113void 114siginit(void) 115{ 116 struct sigaction act; 117 118 memset(&act, 0, sizeof act); 119 sigemptyset(&act.sa_mask); 120 act.sa_flags = SA_RESTART; 121 122 act.sa_handler = SIG_IGN; 123 if (sigaction(SIGPIPE, &act, NULL) != 0) 124 fatal("sigaction failed"); 125 if (sigaction(SIGINT, &act, NULL) != 0) 126 fatal("sigaction failed"); 127 if (sigaction(SIGTSTP, &act, NULL) != 0) 128 fatal("sigaction failed"); 129 if (sigaction(SIGQUIT, &act, NULL) != 0) 130 fatal("sigaction failed"); 131 132 act.sa_handler = sighandler; 133 if (sigaction(SIGWINCH, &act, NULL) != 0) 134 fatal("sigaction failed"); 135 if (sigaction(SIGTERM, &act, NULL) != 0) 136 fatal("sigaction failed"); 137 if (sigaction(SIGCHLD, &act, NULL) != 0) 138 fatal("sigaction failed"); 139 if (sigaction(SIGUSR1, &act, NULL) != 0) 140 fatal("sigaction failed"); 141 if (sigaction(SIGUSR2, &act, NULL) != 0) 142 fatal("sigaction failed"); 143} 144 145void 146sigreset(void) 147{ 148 struct sigaction act; 149 150 memset(&act, 0, sizeof act); 151 sigemptyset(&act.sa_mask); 152 153 act.sa_handler = SIG_DFL; 154 if (sigaction(SIGPIPE, &act, NULL) != 0) 155 fatal("sigaction failed"); 156 if (sigaction(SIGUSR1, &act, NULL) != 0) 157 fatal("sigaction failed"); 158 if (sigaction(SIGUSR2, &act, NULL) != 0) 159 fatal("sigaction failed"); 160 if (sigaction(SIGINT, &act, NULL) != 0) 161 fatal("sigaction failed"); 162 if (sigaction(SIGTSTP, &act, NULL) != 0) 163 fatal("sigaction failed"); 164 if (sigaction(SIGQUIT, &act, NULL) != 0) 165 fatal("sigaction failed"); 166 if (sigaction(SIGWINCH, &act, NULL) != 0) 167 fatal("sigaction failed"); 168 if (sigaction(SIGTERM, &act, NULL) != 0) 169 fatal("sigaction failed"); 170 if (sigaction(SIGCHLD, &act, NULL) != 0) 171 fatal("sigaction failed"); 172} 173 174char * 175makesockpath(const char *label) 176{ 177 char base[MAXPATHLEN], *path; 178 struct stat sb; 179 u_int uid; 180 181 uid = getuid(); 182 xsnprintf(base, MAXPATHLEN, "%s/%s-%d", _PATH_TMP, __progname, uid); 183 184 if (mkdir(base, S_IRWXU) != 0 && errno != EEXIST) 185 return (NULL); 186 187 if (lstat(base, &sb) != 0) 188 return (NULL); 189 if (!S_ISDIR(sb.st_mode)) { 190 errno = ENOTDIR; 191 return (NULL); 192 } 193 if (sb.st_uid != uid || (sb.st_mode & (S_IRWXG|S_IRWXO)) != 0) { 194 errno = EACCES; 195 return (NULL); 196 } 197 198 xasprintf(&path, "%s/%s", base, label); 199 return (path); 200} 201 202int 203main(int argc, char **argv) 204{ 205 struct client_ctx cctx; 206 struct msg_command_data cmddata; 207 struct buffer *b; 208 struct cmd_list *cmdlist; 209 struct cmd *cmd; 210 struct pollfd pfd; 211 struct hdr hdr; 212 const char *shell; 213 struct passwd *pw; 214 char *s, *path, *label, *cause, *home, *pass = NULL; 215 char cwd[MAXPATHLEN]; 216 int retcode, opt, flags, unlock, start_server; 217 218 unlock = flags = 0; 219 label = path = NULL; 220 while ((opt = getopt(argc, argv, "28df:L:qS:uUv")) != -1) { 221 switch (opt) { 222 case '2': 223 flags |= IDENTIFY_256COLOURS; 224 flags &= ~IDENTIFY_88COLOURS; 225 break; 226 case '8': 227 flags |= IDENTIFY_88COLOURS; 228 flags &= ~IDENTIFY_256COLOURS; 229 break; 230 case 'f': 231 if (cfg_file) 232 xfree(cfg_file); 233 cfg_file = xstrdup(optarg); 234 break; 235 case 'L': 236 if (path != NULL) { 237 log_warnx("-L and -S cannot be used together"); 238 exit(1); 239 } 240 if (label != NULL) 241 xfree(label); 242 label = xstrdup(optarg); 243 break; 244 case 'S': 245 if (label != NULL) { 246 log_warnx("-L and -S cannot be used together"); 247 exit(1); 248 } 249 if (path != NULL) 250 xfree(path); 251 path = xstrdup(optarg); 252 break; 253 case 'q': 254 be_quiet = 1; 255 break; 256 case 'u': 257 flags |= IDENTIFY_UTF8; 258 break; 259 case 'U': 260 unlock = 1; 261 break; 262 case 'd': 263 flags |= IDENTIFY_HASDEFAULTS; 264 break; 265 case 'v': 266 debug_level++; 267 break; 268 default: 269 usage(); 270 } 271 } 272 argc -= optind; 273 argv += optind; 274 275 log_open_tty(debug_level); 276 siginit(); 277 278 options_init(&global_options, NULL); 279 options_set_number(&global_options, "bell-action", BELL_ANY); 280 options_set_number(&global_options, "buffer-limit", 9); 281 options_set_number(&global_options, "display-time", 750); 282 options_set_number(&global_options, "history-limit", 2000); 283 options_set_number(&global_options, "lock-after-time", 0); 284 options_set_number(&global_options, "message-attr", GRID_ATTR_REVERSE); 285 options_set_number(&global_options, "message-bg", 3); 286 options_set_number(&global_options, "message-fg", 0); 287 options_set_number(&global_options, "prefix", '\002'); 288 options_set_number(&global_options, "repeat-time", 500); 289 options_set_number(&global_options, "set-remain-on-exit", 0); 290 options_set_number(&global_options, "set-titles", 0); 291 options_set_number(&global_options, "status", 1); 292 options_set_number(&global_options, "status-attr", GRID_ATTR_REVERSE); 293 options_set_number(&global_options, "status-bg", 2); 294 options_set_number(&global_options, "status-fg", 0); 295 options_set_number(&global_options, "status-interval", 15); 296 options_set_number(&global_options, "status-keys", MODEKEY_EMACS); 297 options_set_number(&global_options, "status-left-length", 10); 298 options_set_number(&global_options, "status-right-length", 40); 299 options_set_string(&global_options, "status-left", "[#S]"); 300 options_set_string( 301 &global_options, "status-right", "\"#24T\" %%H:%%M %%d-%%b-%%y"); 302 options_set_number(&global_options, "status-utf8", 0); 303 304 options_init(&global_window_options, NULL); 305 options_set_number(&global_window_options, "aggressive-resize", 0); 306 options_set_number(&global_window_options, "automatic-rename", 1); 307 options_set_number(&global_window_options, "clock-mode-colour", 4); 308 options_set_number(&global_window_options, "clock-mode-style", 1); 309 options_set_number(&global_window_options, "force-height", 0); 310 options_set_number(&global_window_options, "force-width", 0); 311 options_set_number( 312 &global_window_options, "mode-attr", GRID_ATTR_REVERSE); 313 options_set_number(&global_window_options, "main-pane-width", 81); 314 options_set_number(&global_window_options, "main-pane-height", 24); 315 options_set_number(&global_window_options, "mode-bg", 3); 316 options_set_number(&global_window_options, "mode-fg", 0); 317 options_set_number(&global_window_options, "mode-keys", MODEKEY_EMACS); 318 options_set_number(&global_window_options, "monitor-activity", 0); 319 options_set_string(&global_window_options, "monitor-content", "%s", ""); 320 options_set_number(&global_window_options, "utf8", 0); 321 options_set_number(&global_window_options, "window-status-attr", 0); 322 options_set_number(&global_window_options, "window-status-bg", 8); 323 options_set_number(&global_window_options, "window-status-fg", 8); 324 options_set_number(&global_window_options, "xterm-keys", 0); 325 options_set_number(&global_window_options, "remain-on-exit", 0); 326 327 if (!(flags & IDENTIFY_UTF8)) { 328 /* 329 * If the user has set whichever of LC_ALL, LC_CTYPE or LANG 330 * exist (in that order) to contain UTF-8, it is a safe 331 * assumption that either they are using a UTF-8 terminal, or 332 * if not they know that output from UTF-8-capable programs may 333 * be wrong. 334 */ 335 if ((s = getenv("LC_CTYPE")) == NULL) { 336 if ((s = getenv("LC_ALL")) == NULL) 337 s = getenv("LANG"); 338 } 339 if (s != NULL && strcasestr(s, "UTF-8") != NULL) 340 flags |= IDENTIFY_UTF8; 341 } 342 343 if (cfg_file == NULL) { 344 home = getenv("HOME"); 345 if (home == NULL || *home == '\0') { 346 pw = getpwuid(getuid()); 347 if (pw != NULL) 348 home = pw->pw_dir; 349 } 350 xasprintf(&cfg_file, "%s/%s", home, DEFAULT_CFG); 351 if (access(cfg_file, R_OK) != 0) { 352 xfree(cfg_file); 353 cfg_file = NULL; 354 } 355 } else { 356 if (access(cfg_file, R_OK) != 0) { 357 log_warn("%s", cfg_file); 358 exit(1); 359 } 360 } 361 362 if (label == NULL) 363 label = xstrdup("default"); 364 if (path == NULL && (path = makesockpath(label)) == NULL) { 365 log_warn("can't create socket"); 366 exit(1); 367 } 368 xfree(label); 369 370 shell = getenv("SHELL"); 371 if (shell == NULL || *shell == '\0') { 372 pw = getpwuid(getuid()); 373 if (pw != NULL) 374 shell = pw->pw_shell; 375 if (shell == NULL || *shell == '\0') 376 shell = _PATH_BSHELL; 377 } 378 options_set_string( 379 &global_options, "default-command", "exec %s -l", shell); 380 381 if (getcwd(cwd, sizeof cwd) == NULL) { 382 log_warn("getcwd"); 383 exit(1); 384 } 385 options_set_string(&global_options, "default-path", "%s", cwd); 386 387 if (unlock) { 388 if (argc != 0) { 389 log_warnx("can't specify a command when unlocking"); 390 exit(1); 391 } 392 cmdlist = NULL; 393 if ((pass = getpass("Password: ")) == NULL) 394 exit(1); 395 start_server = 0; 396 } else { 397 if (argc == 0) { 398 cmd = xmalloc(sizeof *cmd); 399 cmd->entry = &cmd_new_session_entry; 400 cmd->entry->init(cmd, 0); 401 402 cmdlist = xmalloc(sizeof *cmdlist); 403 TAILQ_INIT(cmdlist); 404 TAILQ_INSERT_HEAD(cmdlist, cmd, qentry); 405 } else { 406 cmdlist = cmd_list_parse(argc, argv, &cause); 407 if (cmdlist == NULL) { 408 log_warnx("%s", cause); 409 exit(1); 410 } 411 } 412 start_server = 0; 413 TAILQ_FOREACH(cmd, cmdlist, qentry) { 414 if (cmd->entry->flags & CMD_STARTSERVER) { 415 start_server = 1; 416 break; 417 } 418 } 419 } 420 421 memset(&cctx, 0, sizeof cctx); 422 if (client_init(path, &cctx, start_server, flags) != 0) 423 exit(1); 424 xfree(path); 425 426 b = buffer_create(BUFSIZ); 427 if (unlock) { 428 cmd_send_string(b, pass); 429 memset(pass, 0, strlen(pass)); 430 client_write_server( 431 &cctx, MSG_UNLOCK, BUFFER_OUT(b), BUFFER_USED(b)); 432 } else { 433 cmd_list_send(cmdlist, b); 434 cmd_list_free(cmdlist); 435 client_fill_session(&cmddata); 436 client_write_server2(&cctx, MSG_COMMAND, 437 &cmddata, sizeof cmddata, BUFFER_OUT(b), BUFFER_USED(b)); 438 } 439 buffer_destroy(b); 440 441 retcode = 0; 442 for (;;) { 443 pfd.fd = cctx.srv_fd; 444 pfd.events = POLLIN; 445 if (BUFFER_USED(cctx.srv_out) > 0) 446 pfd.events |= POLLOUT; 447 448 if (poll(&pfd, 1, INFTIM) == -1) { 449 if (errno == EAGAIN || errno == EINTR) 450 continue; 451 fatal("poll failed"); 452 } 453 454 if (buffer_poll(&pfd, cctx.srv_in, cctx.srv_out) != 0) 455 goto out; 456 457 restart: 458 if (BUFFER_USED(cctx.srv_in) < sizeof hdr) 459 continue; 460 memcpy(&hdr, BUFFER_OUT(cctx.srv_in), sizeof hdr); 461 if (BUFFER_USED(cctx.srv_in) < (sizeof hdr) + hdr.size) 462 continue; 463 buffer_remove(cctx.srv_in, sizeof hdr); 464 465 switch (hdr.type) { 466 case MSG_EXIT: 467 case MSG_SHUTDOWN: 468 goto out; 469 case MSG_ERROR: 470 retcode = 1; 471 /* FALLTHROUGH */ 472 case MSG_PRINT: 473 if (hdr.size > INT_MAX - 1) 474 fatalx("bad MSG_PRINT size"); 475 log_info("%.*s", 476 (int) hdr.size, BUFFER_OUT(cctx.srv_in)); 477 if (hdr.size != 0) 478 buffer_remove(cctx.srv_in, hdr.size); 479 goto restart; 480 case MSG_READY: 481 retcode = client_main(&cctx); 482 goto out; 483 default: 484 fatalx("unexpected command"); 485 } 486 } 487 488out: 489 options_free(&global_options); 490 options_free(&global_window_options); 491 492 close(cctx.srv_fd); 493 buffer_destroy(cctx.srv_in); 494 buffer_destroy(cctx.srv_out); 495 496 return (retcode); 497} 498