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