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