Annotation of src/usr.bin/tmux/tmux.c, Revision 1.17
1.17 ! nicm 1: /* $OpenBSD: tmux.c,v 1.16 2009/07/18 14:59:25 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.1 nicm 47:
48: int server_locked;
49: char *server_password;
50: time_t server_activity;
51:
52: int debug_level;
53: int be_quiet;
54: time_t start_time;
55: char *socket_path;
56:
57: __dead void usage(void);
58: char *makesockpath(const char *);
59:
60: __dead void
61: usage(void)
62: {
1.4 sobrado 63: fprintf(stderr,
64: "usage: %s [-28dqUuv] [-f file] [-L socket-name] [-S socket-path]\n"
65: " [command [flags]]\n",
1.1 nicm 66: __progname);
67: exit(1);
68: }
69:
70: void
71: logfile(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:
84: void
85: sighandler(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:
113: void
114: siginit(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:
145: void
146: sigreset(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:
174: char *
175: makesockpath(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:
202: int
203: main(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':
1.2 ray 230: if (cfg_file)
231: xfree(cfg_file);
1.1 nicm 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:
1.15 nicm 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:
1.12 nicm 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);
1.13 nicm 288: options_set_string(&global_s_options, "default-command", "%s", "");
1.14 nicm 289: options_set_string(&global_s_options, "default-terminal", "screen");
1.12 nicm 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-left-length", 10);
307: options_set_number(&global_s_options, "status-right-length", 40);
308: options_set_string(&global_s_options, "status-left", "[#S]");
1.1 nicm 309: options_set_string(
1.12 nicm 310: &global_s_options, "status-right", "\"#24T\" %%H:%%M %%d-%%b-%%y");
1.15 nicm 311: if (flags & IDENTIFY_UTF8)
312: options_set_number(&global_s_options, "status-utf8", 1);
313: else
314: options_set_number(&global_s_options, "status-utf8", 0);
1.16 nicm 315: options_set_number(&global_s_options, "visual-activity", 0);
316: options_set_number(&global_s_options, "visual-bell", 0);
317: options_set_number(&global_s_options, "visual-content", 0);
1.1 nicm 318:
1.12 nicm 319: options_init(&global_w_options, NULL);
320: options_set_number(&global_w_options, "aggressive-resize", 0);
321: options_set_number(&global_w_options, "automatic-rename", 1);
322: options_set_number(&global_w_options, "clock-mode-colour", 4);
323: options_set_number(&global_w_options, "clock-mode-style", 1);
324: options_set_number(&global_w_options, "force-height", 0);
325: options_set_number(&global_w_options, "force-width", 0);
326: options_set_number(&global_w_options, "mode-attr", GRID_ATTR_REVERSE);
327: options_set_number(&global_w_options, "main-pane-width", 81);
328: options_set_number(&global_w_options, "main-pane-height", 24);
329: options_set_number(&global_w_options, "mode-bg", 3);
330: options_set_number(&global_w_options, "mode-fg", 0);
331: options_set_number(&global_w_options, "mode-keys", MODEKEY_EMACS);
332: options_set_number(&global_w_options, "monitor-activity", 0);
333: options_set_string(&global_w_options, "monitor-content", "%s", "");
1.15 nicm 334: if (flags & IDENTIFY_UTF8)
335: options_set_number(&global_w_options, "utf8", 1);
336: else
337: options_set_number(&global_w_options, "utf8", 0);
1.12 nicm 338: options_set_number(&global_w_options, "window-status-attr", 0);
339: options_set_number(&global_w_options, "window-status-bg", 8);
340: options_set_number(&global_w_options, "window-status-fg", 8);
1.17 ! nicm 341: options_set_number(&global_w_options, "window-status-current-attr", 0);
! 342: options_set_number(&global_w_options, "window-status-current-bg", 8);
! 343: options_set_number(&global_w_options, "window-status-current-fg", 8);
1.12 nicm 344: options_set_number(&global_w_options, "xterm-keys", 0);
345: options_set_number(&global_w_options, "remain-on-exit", 0);
1.1 nicm 346:
347: if (cfg_file == NULL) {
348: home = getenv("HOME");
349: if (home == NULL || *home == '\0') {
350: pw = getpwuid(getuid());
351: if (pw != NULL)
352: home = pw->pw_dir;
353: }
354: xasprintf(&cfg_file, "%s/%s", home, DEFAULT_CFG);
355: if (access(cfg_file, R_OK) != 0) {
356: xfree(cfg_file);
357: cfg_file = NULL;
358: }
359: } else {
360: if (access(cfg_file, R_OK) != 0) {
361: log_warn("%s", cfg_file);
362: exit(1);
363: }
364: }
365:
366: if (label == NULL)
367: label = xstrdup("default");
368: if (path == NULL && (path = makesockpath(label)) == NULL) {
369: log_warn("can't create socket");
370: exit(1);
371: }
372: xfree(label);
373:
374: if (getcwd(cwd, sizeof cwd) == NULL) {
1.11 nicm 375: pw = getpwuid(getuid());
376: if (pw->pw_dir != NULL && *pw->pw_dir != '\0')
377: strlcpy(cwd, pw->pw_dir, sizeof cwd);
378: else
379: strlcpy(cwd, "/", sizeof cwd);
1.1 nicm 380: }
1.12 nicm 381: options_set_string(&global_s_options, "default-path", "%s", cwd);
1.1 nicm 382:
383: if (unlock) {
384: if (argc != 0) {
385: log_warnx("can't specify a command when unlocking");
386: exit(1);
387: }
388: cmdlist = NULL;
389: if ((pass = getpass("Password: ")) == NULL)
390: exit(1);
391: start_server = 0;
392: } else {
393: if (argc == 0) {
394: cmd = xmalloc(sizeof *cmd);
395: cmd->entry = &cmd_new_session_entry;
396: cmd->entry->init(cmd, 0);
397:
398: cmdlist = xmalloc(sizeof *cmdlist);
399: TAILQ_INIT(cmdlist);
400: TAILQ_INSERT_HEAD(cmdlist, cmd, qentry);
401: } else {
402: cmdlist = cmd_list_parse(argc, argv, &cause);
403: if (cmdlist == NULL) {
404: log_warnx("%s", cause);
405: exit(1);
406: }
407: }
408: start_server = 0;
409: TAILQ_FOREACH(cmd, cmdlist, qentry) {
410: if (cmd->entry->flags & CMD_STARTSERVER) {
411: start_server = 1;
412: break;
413: }
414: }
415: }
416:
417: memset(&cctx, 0, sizeof cctx);
418: if (client_init(path, &cctx, start_server, flags) != 0)
419: exit(1);
420: xfree(path);
421:
422: b = buffer_create(BUFSIZ);
423: if (unlock) {
424: cmd_send_string(b, pass);
1.7 nicm 425: memset(pass, 0, strlen(pass));
1.1 nicm 426: client_write_server(
427: &cctx, MSG_UNLOCK, BUFFER_OUT(b), BUFFER_USED(b));
428: } else {
429: cmd_list_send(cmdlist, b);
430: cmd_list_free(cmdlist);
431: client_fill_session(&cmddata);
432: client_write_server2(&cctx, MSG_COMMAND,
433: &cmddata, sizeof cmddata, BUFFER_OUT(b), BUFFER_USED(b));
434: }
435: buffer_destroy(b);
436:
437: retcode = 0;
438: for (;;) {
439: pfd.fd = cctx.srv_fd;
440: pfd.events = POLLIN;
441: if (BUFFER_USED(cctx.srv_out) > 0)
442: pfd.events |= POLLOUT;
443:
444: if (poll(&pfd, 1, INFTIM) == -1) {
445: if (errno == EAGAIN || errno == EINTR)
446: continue;
447: fatal("poll failed");
448: }
449:
450: if (buffer_poll(&pfd, cctx.srv_in, cctx.srv_out) != 0)
451: goto out;
452:
453: restart:
454: if (BUFFER_USED(cctx.srv_in) < sizeof hdr)
455: continue;
456: memcpy(&hdr, BUFFER_OUT(cctx.srv_in), sizeof hdr);
457: if (BUFFER_USED(cctx.srv_in) < (sizeof hdr) + hdr.size)
458: continue;
459: buffer_remove(cctx.srv_in, sizeof hdr);
460:
461: switch (hdr.type) {
462: case MSG_EXIT:
463: case MSG_SHUTDOWN:
464: goto out;
465: case MSG_ERROR:
466: retcode = 1;
467: /* FALLTHROUGH */
468: case MSG_PRINT:
469: if (hdr.size > INT_MAX - 1)
470: fatalx("bad MSG_PRINT size");
471: log_info("%.*s",
472: (int) hdr.size, BUFFER_OUT(cctx.srv_in));
473: if (hdr.size != 0)
474: buffer_remove(cctx.srv_in, hdr.size);
475: goto restart;
476: case MSG_READY:
477: retcode = client_main(&cctx);
478: goto out;
479: default:
480: fatalx("unexpected command");
481: }
482: }
483:
484: out:
1.12 nicm 485: options_free(&global_s_options);
486: options_free(&global_w_options);
1.1 nicm 487:
488: close(cctx.srv_fd);
489: buffer_destroy(cctx.srv_in);
490: buffer_destroy(cctx.srv_out);
491:
492: return (retcode);
493: }