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

Annotation of src/usr.bin/tmux/spawn.c, Revision 1.11

1.11    ! nicm        1: /* $OpenBSD: spawn.c,v 1.10 2019/10/07 07:14:07 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2019 Nicholas Marriott <nicholas.marriott@gmail.com>
                      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:
                     21: #include <errno.h>
                     22: #include <paths.h>
                     23: #include <signal.h>
                     24: #include <stdlib.h>
                     25: #include <string.h>
                     26: #include <unistd.h>
                     27: #include <util.h>
                     28:
                     29: #include "tmux.h"
                     30:
                     31: /*
                     32:  * Set up the environment and create a new window and pane or a new pane.
                     33:  *
                     34:  * We need to set up the following items:
                     35:  *
                     36:  * - history limit, comes from the session;
                     37:  *
                     38:  * - base index, comes from the session;
                     39:  *
                     40:  * - current working directory, may be specified - if it isn't it comes from
                     41:  *   either the client or the session;
                     42:  *
                     43:  * - PATH variable, comes from the client if any, otherwise from the session
                     44:  *   environment;
                     45:  *
                     46:  * - shell, comes from default-shell;
                     47:  *
                     48:  * - termios, comes from the session;
                     49:  *
                     50:  * - remaining environment, comes from the session.
                     51:  */
                     52:
                     53: static void
                     54: spawn_log(const char *from, struct spawn_context *sc)
                     55: {
                     56:        struct session          *s = sc->s;
                     57:        struct winlink          *wl = sc->wl;
                     58:        struct window_pane      *wp0 = sc->wp0;
                     59:        char                     tmp[128];
                     60:        const char              *name;
                     61:
                     62:        log_debug("%s: %s, flags=%#x", from, sc->item->name, sc->flags);
                     63:
                     64:        if (wl != NULL && wp0 != NULL)
                     65:                xsnprintf(tmp, sizeof tmp, "wl=%d wp0=%%%u", wl->idx, wp0->id);
                     66:        else if (wl != NULL)
                     67:                xsnprintf(tmp, sizeof tmp, "wl=%d wp0=none", wl->idx);
                     68:        else if (wp0 != NULL)
                     69:                xsnprintf(tmp, sizeof tmp, "wl=none wp0=%%%u", wp0->id);
                     70:        else
                     71:                xsnprintf(tmp, sizeof tmp, "wl=none wp0=none");
                     72:        log_debug("%s: s=$%u %s idx=%d", from, s->id, tmp, sc->idx);
                     73:
                     74:        name = sc->name;
                     75:        if (name == NULL)
                     76:                name = "none";
                     77:        log_debug("%s: name=%s", from, name);
                     78: }
                     79:
                     80: struct winlink *
                     81: spawn_window(struct spawn_context *sc, char **cause)
                     82: {
                     83:        struct session          *s = sc->s;
                     84:        struct window           *w;
                     85:        struct window_pane      *wp;
                     86:        struct winlink          *wl;
                     87:        int                      idx = sc->idx;
                     88:        u_int                    sx, sy;
                     89:
                     90:        spawn_log(__func__, sc);
                     91:
                     92:        /*
                     93:         * If the window already exists, we are respawning, so destroy all the
                     94:         * panes except one.
                     95:         */
                     96:        if (sc->flags & SPAWN_RESPAWN) {
                     97:                w = sc->wl->window;
                     98:                if (~sc->flags & SPAWN_KILL) {
                     99:                        TAILQ_FOREACH(wp, &w->panes, entry) {
                    100:                                if (wp->fd != -1)
                    101:                                        break;
                    102:                        }
                    103:                        if (wp != NULL) {
                    104:                                xasprintf(cause, "window %s:%d still active",
                    105:                                    s->name, sc->wl->idx);
                    106:                                return (NULL);
                    107:                        }
                    108:                }
                    109:
                    110:                sc->wp0 = TAILQ_FIRST(&w->panes);
                    111:                TAILQ_REMOVE(&w->panes, sc->wp0, entry);
                    112:
                    113:                layout_free(w);
                    114:                window_destroy_panes(w);
                    115:
                    116:                TAILQ_INSERT_HEAD(&w->panes, sc->wp0, entry);
                    117:                window_pane_resize(sc->wp0, w->sx, w->sy);
                    118:
                    119:                layout_init(w, sc->wp0);
                    120:                window_set_active_pane(w, sc->wp0, 0);
                    121:        }
                    122:
                    123:        /*
                    124:         * Otherwise we have no window so we will need to create one. First
                    125:         * check if the given index already exists and destroy it if so.
                    126:         */
                    127:        if ((~sc->flags & SPAWN_RESPAWN) && idx != -1) {
                    128:                wl = winlink_find_by_index(&s->windows, idx);
                    129:                if (wl != NULL && (~sc->flags & SPAWN_KILL)) {
                    130:                        xasprintf(cause, "index %d in use", idx);
                    131:                        return (NULL);
                    132:                }
                    133:                if (wl != NULL) {
                    134:                        /*
                    135:                         * Can't use session_detach as it will destroy session
                    136:                         * if this makes it empty.
                    137:                         */
                    138:                        wl->flags &= ~WINLINK_ALERTFLAGS;
                    139:                        notify_session_window("window-unlinked", s, wl->window);
                    140:                        winlink_stack_remove(&s->lastw, wl);
                    141:                        winlink_remove(&s->windows, wl);
                    142:
                    143:                        if (s->curw == wl) {
                    144:                                s->curw = NULL;
                    145:                                sc->flags &= ~SPAWN_DETACHED;
                    146:                        }
                    147:                }
                    148:        }
                    149:
                    150:        /* Then create a window if needed. */
                    151:        if (~sc->flags & SPAWN_RESPAWN) {
                    152:                if (idx == -1)
                    153:                        idx = -1 - options_get_number(s->options, "base-index");
                    154:                if ((sc->wl = winlink_add(&s->windows, idx)) == NULL) {
                    155:                        xasprintf(cause, "couldn't add window %d", idx);
                    156:                        return (NULL);
                    157:                }
1.9       nicm      158:                default_window_size(sc->c, s, NULL, &sx, &sy, -1);
1.1       nicm      159:                if ((w = window_create(sx, sy)) == NULL) {
                    160:                        winlink_remove(&s->windows, sc->wl);
                    161:                        xasprintf(cause, "couldn't create window %d", idx);
                    162:                        return (NULL);
                    163:                }
1.5       nicm      164:                if (s->curw == NULL)
                    165:                        s->curw = sc->wl;
1.1       nicm      166:                sc->wl->session = s;
1.8       nicm      167:                w->latest = sc->c;
1.1       nicm      168:                winlink_set_window(sc->wl, w);
                    169:        } else
                    170:                w = NULL;
                    171:        sc->flags |= SPAWN_NONOTIFY;
                    172:
                    173:        /* Spawn the pane. */
                    174:        wp = spawn_pane(sc, cause);
                    175:        if (wp == NULL) {
1.6       nicm      176:                if (~sc->flags & SPAWN_RESPAWN)
1.1       nicm      177:                        winlink_remove(&s->windows, sc->wl);
                    178:                return (NULL);
                    179:        }
                    180:
                    181:        /* Set the name of the new window. */
                    182:        if (~sc->flags & SPAWN_RESPAWN) {
                    183:                if (sc->name != NULL) {
                    184:                        w->name = xstrdup(sc->name);
                    185:                        options_set_number(w->options, "automatic-rename", 0);
                    186:                } else
                    187:                        w->name = xstrdup(default_window_name(w));
                    188:        }
                    189:
                    190:        /* Switch to the new window if required. */
                    191:        if (~sc->flags & SPAWN_DETACHED)
                    192:                session_select(s, sc->wl->idx);
                    193:
                    194:        /* Fire notification if new window. */
                    195:        if (~sc->flags & SPAWN_RESPAWN)
                    196:                notify_session_window("window-linked", s, w);
                    197:
                    198:        session_group_synchronize_from(s);
                    199:        return (sc->wl);
                    200: }
                    201:
                    202: struct window_pane *
                    203: spawn_pane(struct spawn_context *sc, char **cause)
                    204: {
                    205:        struct cmdq_item         *item = sc->item;
                    206:        struct client            *c = item->client;
                    207:        struct session           *s = sc->s;
                    208:        struct window            *w = sc->wl->window;
                    209:        struct window_pane       *new_wp;
                    210:        struct environ           *child;
                    211:        struct environ_entry     *ee;
                    212:        char                    **argv, *cp, **argvp, *argv0, *cwd;
                    213:        const char               *cmd, *tmp;
                    214:        int                       argc;
                    215:        u_int                     idx;
                    216:        struct termios            now;
                    217:        u_int                     hlimit;
                    218:        struct winsize            ws;
                    219:        sigset_t                  set, oldset;
1.11    ! nicm      220:        key_code                  key;
1.1       nicm      221:
                    222:        spawn_log(__func__, sc);
                    223:
                    224:        /*
                    225:         * If we are respawning then get rid of the old process. Otherwise
                    226:         * either create a new cell or assign to the one we are given.
                    227:         */
                    228:        hlimit = options_get_number(s->options, "history-limit");
                    229:        if (sc->flags & SPAWN_RESPAWN) {
                    230:                if (sc->wp0->fd != -1 && (~sc->flags & SPAWN_KILL)) {
                    231:                        window_pane_index(sc->wp0, &idx);
                    232:                        xasprintf(cause, "pane %s:%d.%u still active",
                    233:                            s->name, sc->wl->idx, idx);
                    234:                        return (NULL);
                    235:                }
                    236:                if (sc->wp0->fd != -1) {
                    237:                        bufferevent_free(sc->wp0->event);
                    238:                        close(sc->wp0->fd);
                    239:                }
                    240:                window_pane_reset_mode_all(sc->wp0);
                    241:                screen_reinit(&sc->wp0->base);
                    242:                input_init(sc->wp0);
                    243:                new_wp = sc->wp0;
                    244:                new_wp->flags &= ~(PANE_STATUSREADY|PANE_STATUSDRAWN);
                    245:        } else if (sc->lc == NULL) {
                    246:                new_wp = window_add_pane(w, NULL, hlimit, sc->flags);
                    247:                layout_init(w, new_wp);
                    248:        } else {
                    249:                new_wp = window_add_pane(w, sc->wp0, hlimit, sc->flags);
                    250:                layout_assign_pane(sc->lc, new_wp);
                    251:        }
                    252:
                    253:        /*
                    254:         * Now we have a pane with nothing running in it ready for the new
                    255:         * process. Work out the command and arguments.
                    256:         */
1.10      nicm      257:        if (sc->argc == 0 && (~sc->flags & SPAWN_RESPAWN)) {
1.1       nicm      258:                cmd = options_get_string(s->options, "default-command");
                    259:                if (cmd != NULL && *cmd != '\0') {
                    260:                        argc = 1;
                    261:                        argv = (char **)&cmd;
                    262:                } else {
                    263:                        argc = 0;
                    264:                        argv = NULL;
                    265:                }
                    266:        } else {
                    267:                argc = sc->argc;
                    268:                argv = sc->argv;
                    269:        }
                    270:
                    271:        /*
                    272:         * Replace the stored arguments if there are new ones. If not, the
                    273:         * existing ones will be used (they will only exist for respawn).
                    274:         */
                    275:        if (argc > 0) {
                    276:                cmd_free_argv(new_wp->argc, new_wp->argv);
                    277:                new_wp->argc = argc;
                    278:                new_wp->argv = cmd_copy_argv(argc, argv);
                    279:        }
                    280:
                    281:        /*
                    282:         * Work out the current working directory. If respawning, use
                    283:         * the pane's stored one unless specified.
                    284:         */
                    285:        if (sc->cwd != NULL)
                    286:                cwd = format_single(item, sc->cwd, c, s, NULL, NULL);
                    287:        else if (~sc->flags & SPAWN_RESPAWN)
                    288:                cwd = xstrdup(server_client_get_cwd(c, s));
                    289:        else
                    290:                cwd = NULL;
                    291:        if (cwd != NULL) {
                    292:                free(new_wp->cwd);
                    293:                new_wp->cwd = cwd;
                    294:        }
                    295:
                    296:        /* Create an environment for this pane. */
                    297:        child = environ_for_session(s, 0);
1.2       nicm      298:        if (sc->environ != NULL)
                    299:                environ_copy(sc->environ, child);
1.1       nicm      300:        environ_set(child, "TMUX_PANE", "%%%u", new_wp->id);
                    301:
                    302:        /*
                    303:         * Then the PATH environment variable. The session one is replaced from
                    304:         * the client if there is one because otherwise running "tmux new
                    305:         * myprogram" wouldn't work if myprogram isn't in the session's path.
                    306:         */
                    307:        if (c != NULL && c->session == NULL) { /* only unattached clients */
                    308:                ee = environ_find(c->environ, "PATH");
                    309:                if (ee != NULL)
                    310:                        environ_set(child, "PATH", "%s", ee->value);
                    311:        }
                    312:        if (environ_find(child, "PATH") == NULL)
                    313:                environ_set(child, "%s", _PATH_DEFPATH);
                    314:
                    315:        /* Then the shell. If respawning, use the old one. */
                    316:        if (~sc->flags & SPAWN_RESPAWN) {
                    317:                tmp = options_get_string(s->options, "default-shell");
                    318:                if (*tmp == '\0' || areshell(tmp))
                    319:                        tmp = _PATH_BSHELL;
                    320:                free(new_wp->shell);
                    321:                new_wp->shell = xstrdup(tmp);
                    322:        }
                    323:        environ_set(child, "SHELL", "%s", new_wp->shell);
                    324:
                    325:        /* Log the arguments we are going to use. */
                    326:        log_debug("%s: shell=%s", __func__, new_wp->shell);
                    327:        if (new_wp->argc != 0) {
                    328:                cp = cmd_stringify_argv(new_wp->argc, new_wp->argv);
                    329:                log_debug("%s: cmd=%s", __func__, cp);
                    330:                free(cp);
                    331:        }
                    332:        if (cwd != NULL)
                    333:                log_debug("%s: cwd=%s", __func__, cwd);
1.4       nicm      334:        cmd_log_argv(new_wp->argc, new_wp->argv, "%s", __func__);
1.1       nicm      335:        environ_log(child, "%s: environment ", __func__);
                    336:
                    337:        /* Initialize the window size. */
                    338:        memset(&ws, 0, sizeof ws);
                    339:        ws.ws_col = screen_size_x(&new_wp->base);
                    340:        ws.ws_row = screen_size_y(&new_wp->base);
                    341:
                    342:        /* Block signals until fork has completed. */
                    343:        sigfillset(&set);
                    344:        sigprocmask(SIG_BLOCK, &set, &oldset);
1.7       nicm      345:
                    346:        /* If the command is empty, don't fork a child process. */
                    347:        if (sc->flags & SPAWN_EMPTY) {
                    348:                new_wp->flags |= PANE_EMPTY;
                    349:                new_wp->base.mode &= ~MODE_CURSOR;
                    350:                new_wp->base.mode |= MODE_CRLF;
                    351:                goto complete;
                    352:        }
1.1       nicm      353:
                    354:        /* Fork the new process. */
                    355:        new_wp->pid = fdforkpty(ptm_fd, &new_wp->fd, new_wp->tty, NULL, &ws);
                    356:        if (new_wp->pid == -1) {
                    357:                xasprintf(cause, "fork failed: %s", strerror(errno));
                    358:                new_wp->fd = -1;
                    359:                if (~sc->flags & SPAWN_RESPAWN) {
                    360:                        layout_close_pane(new_wp);
                    361:                        window_remove_pane(w, new_wp);
                    362:                }
                    363:                sigprocmask(SIG_SETMASK, &oldset, NULL);
                    364:                return (NULL);
                    365:        }
                    366:
                    367:        /* In the parent process, everything is done now. */
1.3       nicm      368:        if (new_wp->pid != 0)
                    369:                goto complete;
1.1       nicm      370:
                    371:        /*
                    372:         * Child process. Change to the working directory or home if that
                    373:         * fails.
                    374:         */
                    375:        if (chdir(new_wp->cwd) != 0) {
                    376:                if ((tmp = find_home()) == NULL || chdir(tmp) != 0)
                    377:                        chdir("/");
                    378:        }
                    379:
                    380:        /*
                    381:         * Update terminal escape characters from the session if available and
1.11    ! nicm      382:         * force VERASE to tmux's backspace.
1.1       nicm      383:         */
                    384:        if (tcgetattr(STDIN_FILENO, &now) != 0)
                    385:                _exit(1);
                    386:        if (s->tio != NULL)
                    387:                memcpy(now.c_cc, s->tio->c_cc, sizeof now.c_cc);
1.11    ! nicm      388:        key = options_get_number(global_options, "backspace");
        !           389:        if (key >= 0x7f)
        !           390:                now.c_cc[VERASE] = '\177';
        !           391:        else
        !           392:                now.c_cc[VERASE] = key;
1.1       nicm      393:        if (tcsetattr(STDIN_FILENO, TCSANOW, &now) != 0)
                    394:                _exit(1);
                    395:
                    396:        /* Clean up file descriptors and signals and update the environment. */
                    397:        closefrom(STDERR_FILENO + 1);
                    398:        proc_clear_signals(server_proc, 1);
                    399:        sigprocmask(SIG_SETMASK, &oldset, NULL);
                    400:        log_close();
                    401:        environ_push(child);
                    402:
                    403:        /*
                    404:         * If given multiple arguments, use execvp(). Copy the arguments to
                    405:         * ensure they end in a NULL.
                    406:         */
                    407:        if (new_wp->argc != 0 && new_wp->argc != 1) {
                    408:                argvp = cmd_copy_argv(new_wp->argc, new_wp->argv);
                    409:                execvp(argvp[0], argvp);
                    410:                _exit(1);
                    411:        }
                    412:
                    413:        /*
                    414:         * If one argument, pass it to $SHELL -c. Otherwise create a login
                    415:         * shell.
                    416:         */
                    417:        cp = strrchr(new_wp->shell, '/');
                    418:        if (new_wp->argc == 1) {
                    419:                tmp = new_wp->argv[0];
                    420:                if (cp != NULL && cp[1] != '\0')
                    421:                        xasprintf(&argv0, "%s", cp + 1);
                    422:                else
                    423:                        xasprintf(&argv0, "%s", new_wp->shell);
                    424:                execl(new_wp->shell, argv0, "-c", tmp, (char *)NULL);
                    425:                _exit(1);
                    426:        }
                    427:        if (cp != NULL && cp[1] != '\0')
                    428:                xasprintf(&argv0, "-%s", cp + 1);
                    429:        else
                    430:                xasprintf(&argv0, "-%s", new_wp->shell);
                    431:        execl(new_wp->shell, argv0, (char *)NULL);
                    432:        _exit(1);
1.3       nicm      433:
                    434: complete:
                    435:        new_wp->pipe_off = 0;
                    436:        new_wp->flags &= ~PANE_EXITED;
                    437:
                    438:        sigprocmask(SIG_SETMASK, &oldset, NULL);
                    439:        window_pane_set_event(new_wp);
                    440:
                    441:        if (sc->flags & SPAWN_RESPAWN)
                    442:                return (new_wp);
                    443:        if ((~sc->flags & SPAWN_DETACHED) || w->active == NULL) {
                    444:                if (sc->flags & SPAWN_NONOTIFY)
                    445:                        window_set_active_pane(w, new_wp, 0);
                    446:                else
                    447:                        window_set_active_pane(w, new_wp, 1);
                    448:        }
                    449:        if (~sc->flags & SPAWN_NONOTIFY)
                    450:                notify_window("window-layout-changed", w);
                    451:        return (new_wp);
1.1       nicm      452: }