[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.91

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