Annotation of src/usr.bin/tmux/tmux.c, Revision 1.4
1.4 ! sobrado 1: /* $OpenBSD: tmux.c,v 1.3 2009/06/02 15:55:32 pyr 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;
45: struct options global_options;
46: struct options global_window_options;
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: 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':
1.2 ray 231: if (cfg_file)
232: xfree(cfg_file);
1.1 nicm 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", 1);
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:
303: options_init(&global_window_options, NULL);
304: options_set_number(&global_window_options, "aggressive-resize", 0);
305: options_set_number(&global_window_options, "automatic-rename", 1);
306: options_set_number(&global_window_options, "clock-mode-colour", 4);
307: options_set_number(&global_window_options, "clock-mode-style", 1);
308: options_set_number(&global_window_options, "force-height", 0);
309: options_set_number(&global_window_options, "force-width", 0);
310: options_set_number(
311: &global_window_options, "mode-attr", GRID_ATTR_REVERSE);
312: options_set_number(&global_window_options, "main-pane-width", 81);
313: options_set_number(&global_window_options, "main-pane-height", 24);
314: options_set_number(&global_window_options, "mode-bg", 3);
315: options_set_number(&global_window_options, "mode-fg", 0);
316: options_set_number(&global_window_options, "mode-keys", MODEKEY_EMACS);
317: options_set_number(&global_window_options, "monitor-activity", 0);
318: options_set_string(&global_window_options, "monitor-content", "%s", "");
319: options_set_number(&global_window_options, "utf8", 0);
320: options_set_number(&global_window_options, "window-status-attr", 0);
321: options_set_number(&global_window_options, "window-status-bg", 8);
322: options_set_number(&global_window_options, "window-status-fg", 8);
323: options_set_number(&global_window_options, "xterm-keys", 0);
324: options_set_number(&global_window_options, "remain-on-exit", 0);
325:
326: if (!(flags & IDENTIFY_UTF8)) {
327: /*
328: * If the user has set LANG to contain UTF-8, it is a safe
329: * assumption that either they are using a UTF-8 terminal, or
330: * if not they know that output from UTF-8-capable programs may
331: * be wrong.
332: */
333: if ((s = getenv("LANG")) != NULL && strstr(s, "UTF-8") != NULL)
334: flags |= IDENTIFY_UTF8;
335: }
336:
337: if (cfg_file == NULL) {
338: home = getenv("HOME");
339: if (home == NULL || *home == '\0') {
340: pw = getpwuid(getuid());
341: if (pw != NULL)
342: home = pw->pw_dir;
343: }
344: xasprintf(&cfg_file, "%s/%s", home, DEFAULT_CFG);
345: if (access(cfg_file, R_OK) != 0) {
346: xfree(cfg_file);
347: cfg_file = NULL;
348: }
349: } else {
350: if (access(cfg_file, R_OK) != 0) {
351: log_warn("%s", cfg_file);
352: exit(1);
353: }
354: }
355:
356: if (label == NULL)
357: label = xstrdup("default");
358: if (path == NULL && (path = makesockpath(label)) == NULL) {
359: log_warn("can't create socket");
360: exit(1);
361: }
362: xfree(label);
363:
364: shell = getenv("SHELL");
365: if (shell == NULL || *shell == '\0') {
366: pw = getpwuid(getuid());
367: if (pw != NULL)
368: shell = pw->pw_shell;
369: if (shell == NULL || *shell == '\0')
370: shell = _PATH_BSHELL;
371: }
372: options_set_string(
1.3 pyr 373: &global_options, "default-command", "exec %s -l", shell);
1.1 nicm 374:
375: if (getcwd(cwd, sizeof cwd) == NULL) {
376: log_warn("getcwd");
377: exit(1);
378: }
379: options_set_string(&global_options, "default-path", "%s", cwd);
380:
381: if (unlock) {
382: if (argc != 0) {
383: log_warnx("can't specify a command when unlocking");
384: exit(1);
385: }
386: cmdlist = NULL;
387: if ((pass = getpass("Password: ")) == NULL)
388: exit(1);
389: start_server = 0;
390: } else {
391: if (argc == 0) {
392: cmd = xmalloc(sizeof *cmd);
393: cmd->entry = &cmd_new_session_entry;
394: cmd->entry->init(cmd, 0);
395:
396: cmdlist = xmalloc(sizeof *cmdlist);
397: TAILQ_INIT(cmdlist);
398: TAILQ_INSERT_HEAD(cmdlist, cmd, qentry);
399: } else {
400: cmdlist = cmd_list_parse(argc, argv, &cause);
401: if (cmdlist == NULL) {
402: log_warnx("%s", cause);
403: exit(1);
404: }
405: }
406: start_server = 0;
407: TAILQ_FOREACH(cmd, cmdlist, qentry) {
408: if (cmd->entry->flags & CMD_STARTSERVER) {
409: start_server = 1;
410: break;
411: }
412: }
413: }
414:
415: memset(&cctx, 0, sizeof cctx);
416: if (client_init(path, &cctx, start_server, flags) != 0)
417: exit(1);
418: xfree(path);
419:
420: b = buffer_create(BUFSIZ);
421: if (unlock) {
422: cmd_send_string(b, pass);
423: client_write_server(
424: &cctx, MSG_UNLOCK, BUFFER_OUT(b), BUFFER_USED(b));
425: } else {
426: cmd_list_send(cmdlist, b);
427: cmd_list_free(cmdlist);
428: client_fill_session(&cmddata);
429: client_write_server2(&cctx, MSG_COMMAND,
430: &cmddata, sizeof cmddata, BUFFER_OUT(b), BUFFER_USED(b));
431: }
432: buffer_destroy(b);
433:
434: retcode = 0;
435: for (;;) {
436: pfd.fd = cctx.srv_fd;
437: pfd.events = POLLIN;
438: if (BUFFER_USED(cctx.srv_out) > 0)
439: pfd.events |= POLLOUT;
440:
441: if (poll(&pfd, 1, INFTIM) == -1) {
442: if (errno == EAGAIN || errno == EINTR)
443: continue;
444: fatal("poll failed");
445: }
446:
447: if (buffer_poll(&pfd, cctx.srv_in, cctx.srv_out) != 0)
448: goto out;
449:
450: restart:
451: if (BUFFER_USED(cctx.srv_in) < sizeof hdr)
452: continue;
453: memcpy(&hdr, BUFFER_OUT(cctx.srv_in), sizeof hdr);
454: if (BUFFER_USED(cctx.srv_in) < (sizeof hdr) + hdr.size)
455: continue;
456: buffer_remove(cctx.srv_in, sizeof hdr);
457:
458: switch (hdr.type) {
459: case MSG_EXIT:
460: case MSG_SHUTDOWN:
461: goto out;
462: case MSG_ERROR:
463: retcode = 1;
464: /* FALLTHROUGH */
465: case MSG_PRINT:
466: if (hdr.size > INT_MAX - 1)
467: fatalx("bad MSG_PRINT size");
468: log_info("%.*s",
469: (int) hdr.size, BUFFER_OUT(cctx.srv_in));
470: if (hdr.size != 0)
471: buffer_remove(cctx.srv_in, hdr.size);
472: goto restart;
473: case MSG_READY:
474: retcode = client_main(&cctx);
475: goto out;
476: default:
477: fatalx("unexpected command");
478: }
479: }
480:
481: out:
482: options_free(&global_options);
483: options_free(&global_window_options);
484:
485: close(cctx.srv_fd);
486: buffer_destroy(cctx.srv_in);
487: buffer_destroy(cctx.srv_out);
488:
489: return (retcode);
490: }