Annotation of src/usr.bin/tmux/tmux.c, Revision 1.72
1.72 ! nicm 1: /* $OpenBSD: tmux.c,v 1.71 2010/02/06 18:47:41 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>
1.55 nicm 23: #include <event.h>
1.1 nicm 24: #include <paths.h>
25: #include <pwd.h>
26: #include <signal.h>
27: #include <stdlib.h>
28: #include <string.h>
29: #include <syslog.h>
30: #include <unistd.h>
31:
32: #include "tmux.h"
33:
34: #ifdef DEBUG
1.54 nicm 35: extern char *malloc_options;
1.1 nicm 36: #endif
37:
38: char *cfg_file;
1.63 nicm 39: struct options global_options; /* server options */
1.12 nicm 40: struct options global_s_options; /* session options */
41: struct options global_w_options; /* window options */
1.29 nicm 42: struct environ global_environ;
1.1 nicm 43:
44: int debug_level;
45: time_t start_time;
46: char *socket_path;
1.41 nicm 47: int login_shell;
1.1 nicm 48:
1.70 nicm 49: struct env_data {
50: char *path;
51: pid_t pid;
52: u_int idx;
53: };
54:
1.1 nicm 55: __dead void usage(void);
1.70 nicm 56: void parse_env(struct env_data *);
1.1 nicm 57: char *makesockpath(const char *);
1.46 nicm 58: __dead void shell_exec(const char *, const char *);
1.1 nicm 59:
1.55 nicm 60: struct imsgbuf *main_ibuf;
61: struct event main_ev_sigterm;
62: int main_exitval;
63:
64: void main_set_signals(void);
65: void main_clear_signals(void);
66: void main_signal(int, short, unused void *);
67: void main_callback(int, short, void *);
68: void main_dispatch(const char *);
69:
1.1 nicm 70: __dead void
71: usage(void)
72: {
1.4 sobrado 73: fprintf(stderr,
1.52 nicm 74: "usage: %s [-28lquv] [-c shell-command] [-f file] [-L socket-name]\n"
1.41 nicm 75: " [-S socket-path] [command [flags]]\n",
1.1 nicm 76: __progname);
77: exit(1);
78: }
79:
80: void
81: logfile(const char *name)
82: {
83: char *path;
84:
85: log_close();
86: if (debug_level > 0) {
1.32 nicm 87: xasprintf(&path, "tmux-%s-%ld.log", name, (long) getpid());
1.1 nicm 88: log_open_file(debug_level, path);
89: xfree(path);
90: }
91: }
92:
1.39 nicm 93: const char *
94: getshell(void)
95: {
96: struct passwd *pw;
97: const char *shell;
98:
99: shell = getenv("SHELL");
100: if (checkshell(shell))
101: return (shell);
102:
103: pw = getpwuid(getuid());
104: if (pw != NULL && checkshell(pw->pw_shell))
105: return (pw->pw_shell);
106:
107: return (_PATH_BSHELL);
108: }
109:
110: int
111: checkshell(const char *shell)
112: {
113: if (shell == NULL || *shell == '\0' || areshell(shell))
114: return (0);
115: if (access(shell, X_OK) != 0)
116: return (0);
117: return (1);
118: }
119:
120: int
121: areshell(const char *shell)
122: {
123: const char *progname, *ptr;
124:
125: if ((ptr = strrchr(shell, '/')) != NULL)
126: ptr++;
127: else
128: ptr = shell;
129: progname = __progname;
130: if (*progname == '-')
131: progname++;
132: if (strcmp(ptr, progname) == 0)
133: return (1);
134: return (0);
135: }
136:
1.50 nicm 137: void
1.70 nicm 138: parse_env(struct env_data *data)
1.50 nicm 139: {
1.70 nicm 140: char *env, *path_pid, *pid_idx, buf[256];
1.50 nicm 141: size_t len;
142: const char *errstr;
143: long long ll;
144:
145: data->pid = -1;
146: if ((env = getenv("TMUX")) == NULL)
147: return;
148:
1.70 nicm 149: if ((path_pid = strchr(env, ',')) == NULL || path_pid == env)
1.50 nicm 150: return;
1.70 nicm 151: if ((pid_idx = strchr(path_pid + 1, ',')) == NULL)
1.50 nicm 152: return;
1.70 nicm 153: if ((pid_idx == path_pid + 1 || pid_idx[1] == '\0'))
154: return;
155:
156: /* path */
157: len = path_pid - env;
158: data->path = xmalloc (len + 1);
159: memcpy(data->path, env, len);
160: data->path[len] = '\0';
1.50 nicm 161:
1.70 nicm 162: /* pid */
163: len = pid_idx - path_pid - 1;
1.50 nicm 164: if (len > (sizeof buf) - 1)
165: return;
1.70 nicm 166: memcpy(buf, path_pid + 1, len);
1.50 nicm 167: buf[len] = '\0';
168:
169: ll = strtonum(buf, 0, LONG_MAX, &errstr);
170: if (errstr != NULL)
171: return;
172: data->pid = ll;
173:
1.70 nicm 174: /* idx */
175: ll = strtonum(pid_idx+1, 0, UINT_MAX, &errstr);
1.50 nicm 176: if (errstr != NULL)
177: return;
178: data->idx = ll;
179: }
180:
1.1 nicm 181: char *
182: makesockpath(const char *label)
183: {
184: char base[MAXPATHLEN], *path;
185: struct stat sb;
186: u_int uid;
187:
188: uid = getuid();
1.32 nicm 189: xsnprintf(base, MAXPATHLEN, "%s/tmux-%d", _PATH_TMP, uid);
1.1 nicm 190:
191: if (mkdir(base, S_IRWXU) != 0 && errno != EEXIST)
192: return (NULL);
193:
194: if (lstat(base, &sb) != 0)
195: return (NULL);
196: if (!S_ISDIR(sb.st_mode)) {
197: errno = ENOTDIR;
198: return (NULL);
199: }
200: if (sb.st_uid != uid || (sb.st_mode & (S_IRWXG|S_IRWXO)) != 0) {
201: errno = EACCES;
202: return (NULL);
203: }
204:
205: xasprintf(&path, "%s/%s", base, label);
206: return (path);
207: }
208:
1.55 nicm 209: __dead void
210: shell_exec(const char *shell, const char *shellcmd)
211: {
212: const char *shellname, *ptr;
213: char *argv0;
214:
215: ptr = strrchr(shell, '/');
216: if (ptr != NULL && *(ptr + 1) != '\0')
217: shellname = ptr + 1;
218: else
219: shellname = shell;
220: if (login_shell)
221: xasprintf(&argv0, "-%s", shellname);
222: else
223: xasprintf(&argv0, "%s", shellname);
224: setenv("SHELL", shell, 1);
225:
226: execl(shell, argv0, "-c", shellcmd, (char *) NULL);
227: fatal("execl failed");
228: }
229:
1.1 nicm 230: int
231: main(int argc, char **argv)
232: {
1.51 nicm 233: struct cmd_list *cmdlist;
1.62 nicm 234: struct cmd *cmd;
1.51 nicm 235: enum msgtype msg;
236: struct passwd *pw;
1.63 nicm 237: struct options *oo, *so, *wo;
1.51 nicm 238: struct keylist *keylist;
1.70 nicm 239: struct env_data envdata;
1.51 nicm 240: struct msg_command_data cmddata;
241: char *s, *shellcmd, *path, *label, *home, *cause;
242: char cwd[MAXPATHLEN], **var;
243: void *buf;
244: size_t len;
1.64 nicm 245: int opt, flags, quiet = 0, cmdflags = 0;
1.55 nicm 246: short events;
1.54 nicm 247:
248: #ifdef DEBUG
249: malloc_options = (char *) "AFGJPX";
250: #endif
1.1 nicm 251:
1.45 nicm 252: flags = 0;
1.46 nicm 253: shellcmd = label = path = NULL;
1.70 nicm 254: envdata.path = NULL;
1.41 nicm 255: login_shell = (**argv == '-');
1.46 nicm 256: while ((opt = getopt(argc, argv, "28c:df:lL:qS:uUv")) != -1) {
1.41 nicm 257: switch (opt) {
1.1 nicm 258: case '2':
259: flags |= IDENTIFY_256COLOURS;
260: flags &= ~IDENTIFY_88COLOURS;
261: break;
262: case '8':
263: flags |= IDENTIFY_88COLOURS;
264: flags &= ~IDENTIFY_256COLOURS;
265: break;
1.46 nicm 266: case 'c':
267: if (shellcmd != NULL)
268: xfree(shellcmd);
269: shellcmd = xstrdup(optarg);
1.37 nicm 270: break;
1.1 nicm 271: case 'f':
1.42 nicm 272: if (cfg_file != NULL)
1.2 ray 273: xfree(cfg_file);
1.1 nicm 274: cfg_file = xstrdup(optarg);
1.41 nicm 275: break;
276: case 'l':
277: login_shell = 1;
1.1 nicm 278: break;
279: case 'L':
280: if (label != NULL)
281: xfree(label);
282: label = xstrdup(optarg);
283: break;
1.37 nicm 284: case 'q':
1.63 nicm 285: quiet = 1;
1.37 nicm 286: break;
1.1 nicm 287: case 'S':
288: if (path != NULL)
289: xfree(path);
290: path = xstrdup(optarg);
291: break;
292: case 'u':
293: flags |= IDENTIFY_UTF8;
294: break;
295: case 'v':
296: debug_level++;
297: break;
1.53 deraadt 298: default:
1.1 nicm 299: usage();
1.53 deraadt 300: }
301: }
1.1 nicm 302: argc -= optind;
303: argv += optind;
304:
1.46 nicm 305: if (shellcmd != NULL && argc != 0)
306: usage();
307:
1.1 nicm 308: log_open_tty(debug_level);
309:
1.15 nicm 310: if (!(flags & IDENTIFY_UTF8)) {
311: /*
312: * If the user has set whichever of LC_ALL, LC_CTYPE or LANG
313: * exist (in that order) to contain UTF-8, it is a safe
314: * assumption that either they are using a UTF-8 terminal, or
315: * if not they know that output from UTF-8-capable programs may
316: * be wrong.
317: */
318: if ((s = getenv("LC_ALL")) == NULL) {
319: if ((s = getenv("LC_CTYPE")) == NULL)
320: s = getenv("LANG");
321: }
1.26 nicm 322: if (s != NULL && (strcasestr(s, "UTF-8") != NULL ||
323: strcasestr(s, "UTF8") != NULL))
1.15 nicm 324: flags |= IDENTIFY_UTF8;
325: }
326:
1.42 nicm 327: environ_init(&global_environ);
1.62 nicm 328: for (var = environ; *var != NULL; var++)
1.42 nicm 329: environ_put(&global_environ, *var);
1.63 nicm 330:
331: options_init(&global_options, NULL);
332: oo = &global_options;
1.64 nicm 333: options_set_number(oo, "quiet", quiet);
1.65 nicm 334: options_set_number(oo, "escape-time", 500);
1.42 nicm 335:
1.12 nicm 336: options_init(&global_s_options, NULL);
1.42 nicm 337: so = &global_s_options;
338: options_set_number(so, "base-index", 0);
339: options_set_number(so, "bell-action", BELL_ANY);
340: options_set_number(so, "buffer-limit", 9);
341: options_set_string(so, "default-command", "%s", "");
342: options_set_string(so, "default-shell", "%s", getshell());
343: options_set_string(so, "default-terminal", "screen");
344: options_set_number(so, "display-panes-colour", 4);
1.67 nicm 345: options_set_number(so, "display-panes-active-colour", 1);
1.42 nicm 346: options_set_number(so, "display-panes-time", 1000);
347: options_set_number(so, "display-time", 750);
348: options_set_number(so, "history-limit", 2000);
349: options_set_number(so, "lock-after-time", 0);
1.45 nicm 350: options_set_string(so, "lock-command", "lock -np");
1.48 nicm 351: options_set_number(so, "lock-server", 1);
1.42 nicm 352: options_set_number(so, "message-attr", 0);
353: options_set_number(so, "message-bg", 3);
354: options_set_number(so, "message-fg", 0);
1.58 nicm 355: options_set_number(so, "message-limit", 20);
1.49 nicm 356: options_set_number(so, "mouse-select-pane", 0);
1.66 nicm 357: options_set_number(so, "pane-active-border-bg", 2);
358: options_set_number(so, "pane-active-border-fg", 8);
359: options_set_number(so, "pane-border-bg", 8);
360: options_set_number(so, "pane-border-fg", 8);
1.42 nicm 361: options_set_number(so, "repeat-time", 500);
362: options_set_number(so, "set-remain-on-exit", 0);
363: options_set_number(so, "set-titles", 0);
1.43 nicm 364: options_set_string(so, "set-titles-string", "#S:#I:#W - \"#T\"");
1.42 nicm 365: options_set_number(so, "status", 1);
366: options_set_number(so, "status-attr", 0);
367: options_set_number(so, "status-bg", 2);
368: options_set_number(so, "status-fg", 0);
369: options_set_number(so, "status-interval", 15);
370: options_set_number(so, "status-justify", 0);
371: options_set_number(so, "status-keys", MODEKEY_EMACS);
372: options_set_string(so, "status-left", "[#S]");
373: options_set_number(so, "status-left-attr", 0);
374: options_set_number(so, "status-left-bg", 8);
375: options_set_number(so, "status-left-fg", 8);
376: options_set_number(so, "status-left-length", 10);
377: options_set_string(so, "status-right", "\"#22T\" %%H:%%M %%d-%%b-%%y");
378: options_set_number(so, "status-right-attr", 0);
379: options_set_number(so, "status-right-bg", 8);
380: options_set_number(so, "status-right-fg", 8);
381: options_set_number(so, "status-right-length", 40);
382: options_set_string(so, "terminal-overrides",
383: "*88col*:colors=88,*256col*:colors=256");
384: options_set_string(so, "update-environment", "DISPLAY "
1.35 nicm 385: "WINDOWID SSH_ASKPASS SSH_AUTH_SOCK SSH_AGENT_PID SSH_CONNECTION");
1.42 nicm 386: options_set_number(so, "visual-activity", 0);
387: options_set_number(so, "visual-bell", 0);
388: options_set_number(so, "visual-content", 0);
1.44 nicm 389:
390: keylist = xmalloc(sizeof *keylist);
391: ARRAY_INIT(keylist);
392: ARRAY_ADD(keylist, '\002');
393: options_set_data(so, "prefix", keylist, xfree);
1.1 nicm 394:
1.12 nicm 395: options_init(&global_w_options, NULL);
1.42 nicm 396: wo = &global_w_options;
397: options_set_number(wo, "aggressive-resize", 0);
1.72 ! nicm 398: options_set_number(wo, "alternate-screen", 1);
1.42 nicm 399: options_set_number(wo, "automatic-rename", 1);
400: options_set_number(wo, "clock-mode-colour", 4);
401: options_set_number(wo, "clock-mode-style", 1);
402: options_set_number(wo, "force-height", 0);
403: options_set_number(wo, "force-width", 0);
404: options_set_number(wo, "main-pane-height", 24);
405: options_set_number(wo, "main-pane-width", 81);
406: options_set_number(wo, "mode-attr", 0);
407: options_set_number(wo, "mode-bg", 3);
408: options_set_number(wo, "mode-fg", 0);
409: options_set_number(wo, "mode-keys", MODEKEY_EMACS);
410: options_set_number(wo, "mode-mouse", 0);
411: options_set_number(wo, "monitor-activity", 0);
412: options_set_string(wo, "monitor-content", "%s", "");
413: options_set_number(wo, "window-status-attr", 0);
414: options_set_number(wo, "window-status-bg", 8);
415: options_set_number(wo, "window-status-current-attr", 0);
416: options_set_number(wo, "window-status-current-bg", 8);
417: options_set_number(wo, "window-status-current-fg", 8);
418: options_set_number(wo, "window-status-fg", 8);
1.60 nicm 419: options_set_string(wo, "window-status-format", "#I:#W#F");
420: options_set_string(wo, "window-status-current-format", "#I:#W#F");
1.59 nicm 421: options_set_number(wo, "xterm-keys", 0);
1.62 nicm 422: options_set_number(wo, "remain-on-exit", 0);
1.47 nicm 423: options_set_number(wo, "synchronize-panes", 0);
1.42 nicm 424:
1.62 nicm 425: if (flags & IDENTIFY_UTF8) {
1.42 nicm 426: options_set_number(so, "status-utf8", 1);
427: options_set_number(wo, "utf8", 1);
428: } else {
429: options_set_number(so, "status-utf8", 0);
430: options_set_number(wo, "utf8", 0);
431: }
432:
433: if (getcwd(cwd, sizeof cwd) == NULL) {
434: pw = getpwuid(getuid());
435: if (pw->pw_dir != NULL && *pw->pw_dir != '\0')
436: strlcpy(cwd, pw->pw_dir, sizeof cwd);
437: else
438: strlcpy(cwd, "/", sizeof cwd);
439: }
440: options_set_string(so, "default-path", "%s", cwd);
1.1 nicm 441:
442: if (cfg_file == NULL) {
443: home = getenv("HOME");
444: if (home == NULL || *home == '\0') {
445: pw = getpwuid(getuid());
446: if (pw != NULL)
447: home = pw->pw_dir;
448: }
449: xasprintf(&cfg_file, "%s/%s", home, DEFAULT_CFG);
1.69 nicm 450: if (access(cfg_file, R_OK) != 0 && errno == ENOENT) {
1.1 nicm 451: xfree(cfg_file);
452: cfg_file = NULL;
453: }
454: }
1.62 nicm 455:
1.68 nicm 456: /*
457: * Figure out the socket path. If specified on the command-line with
458: * -S or -L, use it, otherwise try $TMUX or assume -L default.
459: */
1.70 nicm 460: parse_env(&envdata);
1.68 nicm 461: if (path == NULL) {
462: /* No -L. Try $TMUX, or default. */
463: if (label == NULL) {
1.70 nicm 464: path = envdata.path;
465: if (path == NULL)
1.68 nicm 466: label = xstrdup("default");
467: }
468:
469: /* -L or default set. */
470: if (label != NULL) {
471: if ((path = makesockpath(label)) == NULL) {
472: log_warn("can't create socket");
473: exit(1);
474: }
475: }
1.1 nicm 476: }
1.68 nicm 477: if (label != NULL)
478: xfree(label);
1.1 nicm 479:
1.46 nicm 480: if (shellcmd != NULL) {
481: msg = MSG_SHELL;
482: buf = NULL;
483: len = 0;
1.51 nicm 484: } else {
1.70 nicm 485: cmddata.pid = envdata.pid;
486: cmddata.idx = envdata.idx;
1.62 nicm 487:
1.71 nicm 488: /* Prepare command for server. */
1.51 nicm 489: cmddata.argc = argc;
490: if (cmd_pack_argv(
491: argc, argv, cmddata.argv, sizeof cmddata.argv) != 0) {
492: log_warnx("command too long");
493: exit(1);
494: }
1.62 nicm 495:
1.51 nicm 496: msg = MSG_COMMAND;
497: buf = &cmddata;
498: len = sizeof cmddata;
499: }
1.21 nicm 500:
1.46 nicm 501: if (shellcmd != NULL)
502: cmdflags |= CMD_STARTSERVER;
503: else if (argc == 0) /* new-session is the default */
1.71 nicm 504: cmdflags |= CMD_STARTSERVER|CMD_SENDENVIRON|CMD_CANTNEST;
1.21 nicm 505: else {
506: /*
507: * It sucks parsing the command string twice (in client and
508: * later in server) but it is necessary to get the start server
509: * flag.
510: */
511: if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
512: log_warnx("%s", cause);
513: exit(1);
1.1 nicm 514: }
1.21 nicm 515: cmdflags &= ~CMD_STARTSERVER;
1.1 nicm 516: TAILQ_FOREACH(cmd, cmdlist, qentry) {
1.29 nicm 517: if (cmd->entry->flags & CMD_STARTSERVER)
1.20 nicm 518: cmdflags |= CMD_STARTSERVER;
1.29 nicm 519: if (cmd->entry->flags & CMD_SENDENVIRON)
520: cmdflags |= CMD_SENDENVIRON;
1.71 nicm 521: if (cmd->entry->flags & CMD_CANTNEST)
522: cmdflags |= CMD_CANTNEST;
1.1 nicm 523: }
1.21 nicm 524: cmd_list_free(cmdlist);
1.71 nicm 525: }
526:
527: /*
528: * Check if this could be a nested session, if the command can't nest:
529: * if the socket path matches $TMUX, this is probably the same server.
530: */
531: if (shellcmd == NULL && envdata.path != NULL &&
532: cmdflags & CMD_CANTNEST &&
533: (path == envdata.path || strcmp(path, envdata.path) == 0)) {
534: log_warnx("sessions should be nested with care. "
535: "unset $TMUX to force.");
536: exit(1);
1.1 nicm 537: }
538:
1.55 nicm 539: if ((main_ibuf = client_init(path, cmdflags, flags)) == NULL)
1.1 nicm 540: exit(1);
541: xfree(path);
542:
1.55 nicm 543: event_init();
544:
1.62 nicm 545: imsg_compose(main_ibuf, msg, PROTOCOL_VERSION, -1, -1, buf, len);
1.55 nicm 546:
547: main_set_signals();
548:
549: events = EV_READ;
550: if (main_ibuf->w.queued > 0)
551: events |= EV_WRITE;
552: event_once(main_ibuf->fd, events, main_callback, shellcmd, NULL);
553:
554: main_exitval = 0;
555: event_dispatch();
1.1 nicm 556:
1.55 nicm 557: main_clear_signals();
558:
559: client_main(); /* doesn't return */
560: }
1.31 nicm 561:
1.55 nicm 562: void
563: main_set_signals(void)
564: {
565: struct sigaction sigact;
566:
567: memset(&sigact, 0, sizeof sigact);
568: sigemptyset(&sigact.sa_mask);
569: sigact.sa_flags = SA_RESTART;
570: sigact.sa_handler = SIG_IGN;
571: if (sigaction(SIGINT, &sigact, NULL) != 0)
572: fatal("sigaction failed");
573: if (sigaction(SIGPIPE, &sigact, NULL) != 0)
574: fatal("sigaction failed");
575: if (sigaction(SIGUSR1, &sigact, NULL) != 0)
576: fatal("sigaction failed");
577: if (sigaction(SIGUSR2, &sigact, NULL) != 0)
578: fatal("sigaction failed");
579: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
580: fatal("sigaction failed");
1.62 nicm 581:
1.55 nicm 582: signal_set(&main_ev_sigterm, SIGTERM, main_signal, NULL);
583: signal_add(&main_ev_sigterm, NULL);
584: }
585:
586: void
587: main_clear_signals(void)
588: {
589: struct sigaction sigact;
590:
591: memset(&sigact, 0, sizeof sigact);
592: sigemptyset(&sigact.sa_mask);
593: sigact.sa_flags = SA_RESTART;
594: sigact.sa_handler = SIG_DFL;
595: if (sigaction(SIGINT, &sigact, NULL) != 0)
596: fatal("sigaction failed");
597: if (sigaction(SIGPIPE, &sigact, NULL) != 0)
598: fatal("sigaction failed");
599: if (sigaction(SIGUSR1, &sigact, NULL) != 0)
600: fatal("sigaction failed");
601: if (sigaction(SIGUSR2, &sigact, NULL) != 0)
602: fatal("sigaction failed");
603: if (sigaction(SIGTSTP, &sigact, NULL) != 0)
604: fatal("sigaction failed");
1.62 nicm 605:
1.55 nicm 606: event_del(&main_ev_sigterm);
607: }
1.31 nicm 608:
1.61 nicm 609: /* ARGSUSED */
1.55 nicm 610: void
611: main_signal(int sig, unused short events, unused void *data)
612: {
613: switch (sig) {
614: case SIGTERM:
615: exit(1);
1.31 nicm 616: }
1.55 nicm 617: }
1.31 nicm 618:
1.61 nicm 619: /* ARGSUSED */
1.55 nicm 620: void
621: main_callback(unused int fd, short events, void *data)
622: {
623: char *shellcmd = data;
624:
625: if (events & EV_READ)
626: main_dispatch(shellcmd);
1.62 nicm 627:
1.55 nicm 628: if (events & EV_WRITE) {
629: if (msgbuf_write(&main_ibuf->w) < 0)
630: fatalx("msgbuf_write failed");
631: }
1.31 nicm 632:
1.55 nicm 633: events = EV_READ;
634: if (main_ibuf->w.queued > 0)
635: events |= EV_WRITE;
636: event_once(main_ibuf->fd, events, main_callback, shellcmd, NULL);
1.31 nicm 637: }
638:
1.55 nicm 639: void
640: main_dispatch(const char *shellcmd)
1.31 nicm 641: {
642: struct imsg imsg;
643: ssize_t n, datalen;
644: struct msg_print_data printdata;
1.46 nicm 645: struct msg_shell_data shelldata;
1.1 nicm 646:
1.55 nicm 647: if ((n = imsg_read(main_ibuf)) == -1 || n == 0)
1.31 nicm 648: fatalx("imsg_read failed");
1.1 nicm 649:
1.31 nicm 650: for (;;) {
1.55 nicm 651: if ((n = imsg_get(main_ibuf, &imsg)) == -1)
1.31 nicm 652: fatalx("imsg_get failed");
653: if (n == 0)
1.55 nicm 654: return;
1.31 nicm 655: datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1.1 nicm 656:
1.31 nicm 657: switch (imsg.hdr.type) {
1.1 nicm 658: case MSG_EXIT:
659: case MSG_SHUTDOWN:
1.31 nicm 660: if (datalen != 0)
661: fatalx("bad MSG_EXIT size");
662:
1.55 nicm 663: exit(main_exitval);
1.1 nicm 664: case MSG_ERROR:
665: case MSG_PRINT:
1.31 nicm 666: if (datalen != sizeof printdata)
1.1 nicm 667: fatalx("bad MSG_PRINT size");
1.31 nicm 668: memcpy(&printdata, imsg.data, sizeof printdata);
669: printdata.msg[(sizeof printdata.msg) - 1] = '\0';
1.21 nicm 670:
671: log_info("%s", printdata.msg);
1.55 nicm 672: if (imsg.hdr.type == MSG_ERROR)
673: main_exitval = 1;
1.31 nicm 674: break;
1.1 nicm 675: case MSG_READY:
1.31 nicm 676: if (datalen != 0)
677: fatalx("bad MSG_READY size");
678:
1.55 nicm 679: event_loopexit(NULL); /* move to client_main() */
680: break;
1.31 nicm 681: case MSG_VERSION:
682: if (datalen != 0)
683: fatalx("bad MSG_VERSION size");
684:
685: log_warnx("protocol version mismatch (client %u, "
686: "server %u)", PROTOCOL_VERSION, imsg.hdr.peerid);
1.55 nicm 687: exit(1);
1.46 nicm 688: case MSG_SHELL:
689: if (datalen != sizeof shelldata)
690: fatalx("bad MSG_SHELL size");
691: memcpy(&shelldata, imsg.data, sizeof shelldata);
692: shelldata.shell[(sizeof shelldata.shell) - 1] = '\0';
1.55 nicm 693:
694: main_clear_signals();
695:
1.46 nicm 696: shell_exec(shelldata.shell, shellcmd);
1.1 nicm 697: default:
1.31 nicm 698: fatalx("unexpected message");
1.1 nicm 699: }
1.31 nicm 700:
701: imsg_free(&imsg);
1.1 nicm 702: }
703: }