tmux.c revision 1.18
1/* $OpenBSD: tmux.c,v 1.18 2009/07/20 14:32:09 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 */ 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 struct passwd *pw; 213 char *s, *path, *label, *cause, *home, *pass = NULL; 214 char cwd[MAXPATHLEN]; 215 int retcode, opt, flags, unlock, start_server; 216 217 unlock = flags = 0; 218 label = path = NULL; 219 while ((opt = getopt(argc, argv, "28df:L:qS:uUv")) != -1) { 220 switch (opt) { 221 case '2': 222 flags |= IDENTIFY_256COLOURS; 223 flags &= ~IDENTIFY_88COLOURS; 224 break; 225 case '8': 226 flags |= IDENTIFY_88COLOURS; 227 flags &= ~IDENTIFY_256COLOURS; 228 break; 229 case 'f': 230 if (cfg_file) 231 xfree(cfg_file); 232 cfg_file = xstrdup(optarg); 233 break; 234 case 'L': 235 if (label != NULL) 236 xfree(label); 237 label = xstrdup(optarg); 238 break; 239 case 'S': 240 if (path != NULL) 241 xfree(path); 242 path = xstrdup(optarg); 243 break; 244 case 'q': 245 be_quiet = 1; 246 break; 247 case 'u': 248 flags |= IDENTIFY_UTF8; 249 break; 250 case 'U': 251 unlock = 1; 252 break; 253 case 'd': 254 flags |= IDENTIFY_HASDEFAULTS; 255 break; 256 case 'v': 257 debug_level++; 258 break; 259 default: 260 usage(); 261 } 262 } 263 argc -= optind; 264 argv += optind; 265 266 log_open_tty(debug_level); 267 siginit(); 268 269 if (!(flags & IDENTIFY_UTF8)) { 270 /* 271 * If the user has set whichever of LC_ALL, LC_CTYPE or LANG 272 * exist (in that order) to contain UTF-8, it is a safe 273 * assumption that either they are using a UTF-8 terminal, or 274 * if not they know that output from UTF-8-capable programs may 275 * be wrong. 276 */ 277 if ((s = getenv("LC_ALL")) == NULL) { 278 if ((s = getenv("LC_CTYPE")) == NULL) 279 s = getenv("LANG"); 280 } 281 if (s != NULL && strcasestr(s, "UTF-8") != NULL) 282 flags |= IDENTIFY_UTF8; 283 } 284 285 options_init(&global_s_options, NULL); 286 options_set_number(&global_s_options, "bell-action", BELL_ANY); 287 options_set_number(&global_s_options, "buffer-limit", 9); 288 options_set_string(&global_s_options, "default-command", "%s", ""); 289 options_set_string(&global_s_options, "default-terminal", "screen"); 290 options_set_number(&global_s_options, "display-time", 750); 291 options_set_number(&global_s_options, "history-limit", 2000); 292 options_set_number(&global_s_options, "lock-after-time", 0); 293 options_set_number(&global_s_options, "message-attr", GRID_ATTR_REVERSE); 294 options_set_number(&global_s_options, "message-bg", 3); 295 options_set_number(&global_s_options, "message-fg", 0); 296 options_set_number(&global_s_options, "prefix", '\002'); 297 options_set_number(&global_s_options, "repeat-time", 500); 298 options_set_number(&global_s_options, "set-remain-on-exit", 0); 299 options_set_number(&global_s_options, "set-titles", 0); 300 options_set_number(&global_s_options, "status", 1); 301 options_set_number(&global_s_options, "status-attr", GRID_ATTR_REVERSE); 302 options_set_number(&global_s_options, "status-bg", 2); 303 options_set_number(&global_s_options, "status-fg", 0); 304 options_set_number(&global_s_options, "status-interval", 15); 305 options_set_number(&global_s_options, "status-keys", MODEKEY_EMACS); 306 options_set_number(&global_s_options, "status-justify", 0); 307 options_set_number(&global_s_options, "status-left-length", 10); 308 options_set_number(&global_s_options, "status-right-length", 40); 309 options_set_string(&global_s_options, "status-left", "[#S]"); 310 options_set_string( 311 &global_s_options, "status-right", "\"#24T\" %%H:%%M %%d-%%b-%%y"); 312 if (flags & IDENTIFY_UTF8) 313 options_set_number(&global_s_options, "status-utf8", 1); 314 else 315 options_set_number(&global_s_options, "status-utf8", 0); 316 options_set_number(&global_s_options, "visual-activity", 0); 317 options_set_number(&global_s_options, "visual-bell", 0); 318 options_set_number(&global_s_options, "visual-content", 0); 319 320 options_init(&global_w_options, NULL); 321 options_set_number(&global_w_options, "aggressive-resize", 0); 322 options_set_number(&global_w_options, "automatic-rename", 1); 323 options_set_number(&global_w_options, "clock-mode-colour", 4); 324 options_set_number(&global_w_options, "clock-mode-style", 1); 325 options_set_number(&global_w_options, "force-height", 0); 326 options_set_number(&global_w_options, "force-width", 0); 327 options_set_number(&global_w_options, "mode-attr", GRID_ATTR_REVERSE); 328 options_set_number(&global_w_options, "main-pane-width", 81); 329 options_set_number(&global_w_options, "main-pane-height", 24); 330 options_set_number(&global_w_options, "mode-bg", 3); 331 options_set_number(&global_w_options, "mode-fg", 0); 332 options_set_number(&global_w_options, "mode-keys", MODEKEY_EMACS); 333 options_set_number(&global_w_options, "monitor-activity", 0); 334 options_set_string(&global_w_options, "monitor-content", "%s", ""); 335 if (flags & IDENTIFY_UTF8) 336 options_set_number(&global_w_options, "utf8", 1); 337 else 338 options_set_number(&global_w_options, "utf8", 0); 339 options_set_number(&global_w_options, "window-status-attr", 0); 340 options_set_number(&global_w_options, "window-status-bg", 8); 341 options_set_number(&global_w_options, "window-status-fg", 8); 342 options_set_number(&global_w_options, "window-status-current-attr", 0); 343 options_set_number(&global_w_options, "window-status-current-bg", 8); 344 options_set_number(&global_w_options, "window-status-current-fg", 8); 345 options_set_number(&global_w_options, "xterm-keys", 0); 346 options_set_number(&global_w_options, "remain-on-exit", 0); 347 348 if (cfg_file == NULL) { 349 home = getenv("HOME"); 350 if (home == NULL || *home == '\0') { 351 pw = getpwuid(getuid()); 352 if (pw != NULL) 353 home = pw->pw_dir; 354 } 355 xasprintf(&cfg_file, "%s/%s", home, DEFAULT_CFG); 356 if (access(cfg_file, R_OK) != 0) { 357 xfree(cfg_file); 358 cfg_file = NULL; 359 } 360 } else { 361 if (access(cfg_file, R_OK) != 0) { 362 log_warn("%s", cfg_file); 363 exit(1); 364 } 365 } 366 367 if (label == NULL) 368 label = xstrdup("default"); 369 if (path == NULL && (path = makesockpath(label)) == NULL) { 370 log_warn("can't create socket"); 371 exit(1); 372 } 373 xfree(label); 374 375 if (getcwd(cwd, sizeof cwd) == NULL) { 376 pw = getpwuid(getuid()); 377 if (pw->pw_dir != NULL && *pw->pw_dir != '\0') 378 strlcpy(cwd, pw->pw_dir, sizeof cwd); 379 else 380 strlcpy(cwd, "/", sizeof cwd); 381 } 382 options_set_string(&global_s_options, "default-path", "%s", cwd); 383 384 if (unlock) { 385 if (argc != 0) { 386 log_warnx("can't specify a command when unlocking"); 387 exit(1); 388 } 389 cmdlist = NULL; 390 if ((pass = getpass("Password: ")) == NULL) 391 exit(1); 392 start_server = 0; 393 } else { 394 if (argc == 0) { 395 cmd = xmalloc(sizeof *cmd); 396 cmd->entry = &cmd_new_session_entry; 397 cmd->entry->init(cmd, 0); 398 399 cmdlist = xmalloc(sizeof *cmdlist); 400 TAILQ_INIT(cmdlist); 401 TAILQ_INSERT_HEAD(cmdlist, cmd, qentry); 402 } else { 403 cmdlist = cmd_list_parse(argc, argv, &cause); 404 if (cmdlist == NULL) { 405 log_warnx("%s", cause); 406 exit(1); 407 } 408 } 409 start_server = 0; 410 TAILQ_FOREACH(cmd, cmdlist, qentry) { 411 if (cmd->entry->flags & CMD_STARTSERVER) { 412 start_server = 1; 413 break; 414 } 415 } 416 } 417 418 memset(&cctx, 0, sizeof cctx); 419 if (client_init(path, &cctx, start_server, flags) != 0) 420 exit(1); 421 xfree(path); 422 423 b = buffer_create(BUFSIZ); 424 if (unlock) { 425 cmd_send_string(b, pass); 426 memset(pass, 0, strlen(pass)); 427 client_write_server( 428 &cctx, MSG_UNLOCK, BUFFER_OUT(b), BUFFER_USED(b)); 429 } else { 430 cmd_list_send(cmdlist, b); 431 cmd_list_free(cmdlist); 432 client_fill_session(&cmddata); 433 client_write_server2(&cctx, MSG_COMMAND, 434 &cmddata, sizeof cmddata, BUFFER_OUT(b), BUFFER_USED(b)); 435 } 436 buffer_destroy(b); 437 438 retcode = 0; 439 for (;;) { 440 pfd.fd = cctx.srv_fd; 441 pfd.events = POLLIN; 442 if (BUFFER_USED(cctx.srv_out) > 0) 443 pfd.events |= POLLOUT; 444 445 if (poll(&pfd, 1, INFTIM) == -1) { 446 if (errno == EAGAIN || errno == EINTR) 447 continue; 448 fatal("poll failed"); 449 } 450 451 if (buffer_poll(&pfd, cctx.srv_in, cctx.srv_out) != 0) 452 goto out; 453 454 restart: 455 if (BUFFER_USED(cctx.srv_in) < sizeof hdr) 456 continue; 457 memcpy(&hdr, BUFFER_OUT(cctx.srv_in), sizeof hdr); 458 if (BUFFER_USED(cctx.srv_in) < (sizeof hdr) + hdr.size) 459 continue; 460 buffer_remove(cctx.srv_in, sizeof hdr); 461 462 switch (hdr.type) { 463 case MSG_EXIT: 464 case MSG_SHUTDOWN: 465 goto out; 466 case MSG_ERROR: 467 retcode = 1; 468 /* FALLTHROUGH */ 469 case MSG_PRINT: 470 if (hdr.size > INT_MAX - 1) 471 fatalx("bad MSG_PRINT size"); 472 log_info("%.*s", 473 (int) hdr.size, BUFFER_OUT(cctx.srv_in)); 474 if (hdr.size != 0) 475 buffer_remove(cctx.srv_in, hdr.size); 476 goto restart; 477 case MSG_READY: 478 retcode = client_main(&cctx); 479 goto out; 480 default: 481 fatalx("unexpected command"); 482 } 483 } 484 485out: 486 options_free(&global_s_options); 487 options_free(&global_w_options); 488 489 close(cctx.srv_fd); 490 buffer_destroy(cctx.srv_in); 491 buffer_destroy(cctx.srv_out); 492 493 return (retcode); 494} 495