[BACK]Return to tmux.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/tmux.c, Revision 1.67

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