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