xref: /openbsd-src/usr.bin/tmux/spawn.c (revision a0aa010a1e376f08e9dd6752e6f4510e98bd0daa)
1*a0aa010aSnicm /* $OpenBSD: spawn.c,v 1.33 2023/07/10 09:24:53 nicm Exp $ */
2c26c4f79Snicm 
3c26c4f79Snicm /*
4c26c4f79Snicm  * Copyright (c) 2019 Nicholas Marriott <nicholas.marriott@gmail.com>
5c26c4f79Snicm  *
6c26c4f79Snicm  * Permission to use, copy, modify, and distribute this software for any
7c26c4f79Snicm  * purpose with or without fee is hereby granted, provided that the above
8c26c4f79Snicm  * copyright notice and this permission notice appear in all copies.
9c26c4f79Snicm  *
10c26c4f79Snicm  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11c26c4f79Snicm  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12c26c4f79Snicm  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13c26c4f79Snicm  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14c26c4f79Snicm  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15c26c4f79Snicm  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16c26c4f79Snicm  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17c26c4f79Snicm  */
18c26c4f79Snicm 
19c26c4f79Snicm #include <sys/types.h>
20c26c4f79Snicm 
21c26c4f79Snicm #include <errno.h>
22c26c4f79Snicm #include <paths.h>
23c26c4f79Snicm #include <signal.h>
24c26c4f79Snicm #include <stdlib.h>
25c26c4f79Snicm #include <string.h>
26c26c4f79Snicm #include <unistd.h>
27c26c4f79Snicm #include <util.h>
28c26c4f79Snicm 
29c26c4f79Snicm #include "tmux.h"
30c26c4f79Snicm 
31c26c4f79Snicm /*
32c26c4f79Snicm  * Set up the environment and create a new window and pane or a new pane.
33c26c4f79Snicm  *
34c26c4f79Snicm  * We need to set up the following items:
35c26c4f79Snicm  *
36c26c4f79Snicm  * - history limit, comes from the session;
37c26c4f79Snicm  *
38c26c4f79Snicm  * - base index, comes from the session;
39c26c4f79Snicm  *
40c26c4f79Snicm  * - current working directory, may be specified - if it isn't it comes from
41c26c4f79Snicm  *   either the client or the session;
42c26c4f79Snicm  *
43c26c4f79Snicm  * - PATH variable, comes from the client if any, otherwise from the session
44c26c4f79Snicm  *   environment;
45c26c4f79Snicm  *
46c26c4f79Snicm  * - shell, comes from default-shell;
47c26c4f79Snicm  *
48c26c4f79Snicm  * - termios, comes from the session;
49c26c4f79Snicm  *
50c26c4f79Snicm  * - remaining environment, comes from the session.
51c26c4f79Snicm  */
52c26c4f79Snicm 
53c26c4f79Snicm static void
spawn_log(const char * from,struct spawn_context * sc)54c26c4f79Snicm spawn_log(const char *from, struct spawn_context *sc)
55c26c4f79Snicm {
56c26c4f79Snicm 	struct session		*s = sc->s;
57c26c4f79Snicm 	struct winlink		*wl = sc->wl;
58c26c4f79Snicm 	struct window_pane	*wp0 = sc->wp0;
59040343aeSnicm 	const char		*name = cmdq_get_name(sc->item);
60c26c4f79Snicm 	char			 tmp[128];
61c26c4f79Snicm 
62040343aeSnicm 	log_debug("%s: %s, flags=%#x", from, name, sc->flags);
63c26c4f79Snicm 
64c26c4f79Snicm 	if (wl != NULL && wp0 != NULL)
65c26c4f79Snicm 		xsnprintf(tmp, sizeof tmp, "wl=%d wp0=%%%u", wl->idx, wp0->id);
66c26c4f79Snicm 	else if (wl != NULL)
67c26c4f79Snicm 		xsnprintf(tmp, sizeof tmp, "wl=%d wp0=none", wl->idx);
68c26c4f79Snicm 	else if (wp0 != NULL)
69c26c4f79Snicm 		xsnprintf(tmp, sizeof tmp, "wl=none wp0=%%%u", wp0->id);
70c26c4f79Snicm 	else
71c26c4f79Snicm 		xsnprintf(tmp, sizeof tmp, "wl=none wp0=none");
72c26c4f79Snicm 	log_debug("%s: s=$%u %s idx=%d", from, s->id, tmp, sc->idx);
73040343aeSnicm 	log_debug("%s: name=%s", from, sc->name == NULL ? "none" : sc->name);
74c26c4f79Snicm }
75c26c4f79Snicm 
76c26c4f79Snicm struct winlink *
spawn_window(struct spawn_context * sc,char ** cause)77c26c4f79Snicm spawn_window(struct spawn_context *sc, char **cause)
78c26c4f79Snicm {
79ae88e52dSnicm 	struct cmdq_item	*item = sc->item;
80040343aeSnicm 	struct client		*c = cmdq_get_client(item);
81c26c4f79Snicm 	struct session		*s = sc->s;
82c26c4f79Snicm 	struct window		*w;
83c26c4f79Snicm 	struct window_pane	*wp;
84c26c4f79Snicm 	struct winlink		*wl;
85c26c4f79Snicm 	int			 idx = sc->idx;
864a8b0ea5Snicm 	u_int			 sx, sy, xpixel, ypixel;
87c26c4f79Snicm 
88c26c4f79Snicm 	spawn_log(__func__, sc);
89c26c4f79Snicm 
90c26c4f79Snicm 	/*
91c26c4f79Snicm 	 * If the window already exists, we are respawning, so destroy all the
92c26c4f79Snicm 	 * panes except one.
93c26c4f79Snicm 	 */
94c26c4f79Snicm 	if (sc->flags & SPAWN_RESPAWN) {
95c26c4f79Snicm 		w = sc->wl->window;
96c26c4f79Snicm 		if (~sc->flags & SPAWN_KILL) {
97c26c4f79Snicm 			TAILQ_FOREACH(wp, &w->panes, entry) {
98c26c4f79Snicm 				if (wp->fd != -1)
99c26c4f79Snicm 					break;
100c26c4f79Snicm 			}
101c26c4f79Snicm 			if (wp != NULL) {
102c26c4f79Snicm 				xasprintf(cause, "window %s:%d still active",
103c26c4f79Snicm 				    s->name, sc->wl->idx);
104c26c4f79Snicm 				return (NULL);
105c26c4f79Snicm 			}
106c26c4f79Snicm 		}
107c26c4f79Snicm 
108c26c4f79Snicm 		sc->wp0 = TAILQ_FIRST(&w->panes);
109c26c4f79Snicm 		TAILQ_REMOVE(&w->panes, sc->wp0, entry);
110c26c4f79Snicm 
111c26c4f79Snicm 		layout_free(w);
112c26c4f79Snicm 		window_destroy_panes(w);
113c26c4f79Snicm 
114c26c4f79Snicm 		TAILQ_INSERT_HEAD(&w->panes, sc->wp0, entry);
115c26c4f79Snicm 		window_pane_resize(sc->wp0, w->sx, w->sy);
116c26c4f79Snicm 
117c26c4f79Snicm 		layout_init(w, sc->wp0);
118*a0aa010aSnicm 		w->active = NULL;
119c26c4f79Snicm 		window_set_active_pane(w, sc->wp0, 0);
120c26c4f79Snicm 	}
121c26c4f79Snicm 
122c26c4f79Snicm 	/*
123c26c4f79Snicm 	 * Otherwise we have no window so we will need to create one. First
124c26c4f79Snicm 	 * check if the given index already exists and destroy it if so.
125c26c4f79Snicm 	 */
126c26c4f79Snicm 	if ((~sc->flags & SPAWN_RESPAWN) && idx != -1) {
127c26c4f79Snicm 		wl = winlink_find_by_index(&s->windows, idx);
128c26c4f79Snicm 		if (wl != NULL && (~sc->flags & SPAWN_KILL)) {
129c26c4f79Snicm 			xasprintf(cause, "index %d in use", idx);
130c26c4f79Snicm 			return (NULL);
131c26c4f79Snicm 		}
132c26c4f79Snicm 		if (wl != NULL) {
133c26c4f79Snicm 			/*
134c26c4f79Snicm 			 * Can't use session_detach as it will destroy session
135c26c4f79Snicm 			 * if this makes it empty.
136c26c4f79Snicm 			 */
137c26c4f79Snicm 			wl->flags &= ~WINLINK_ALERTFLAGS;
138c26c4f79Snicm 			notify_session_window("window-unlinked", s, wl->window);
139c26c4f79Snicm 			winlink_stack_remove(&s->lastw, wl);
140c26c4f79Snicm 			winlink_remove(&s->windows, wl);
141c26c4f79Snicm 
142c26c4f79Snicm 			if (s->curw == wl) {
143c26c4f79Snicm 				s->curw = NULL;
144c26c4f79Snicm 				sc->flags &= ~SPAWN_DETACHED;
145c26c4f79Snicm 			}
146c26c4f79Snicm 		}
147c26c4f79Snicm 	}
148c26c4f79Snicm 
149c26c4f79Snicm 	/* Then create a window if needed. */
150c26c4f79Snicm 	if (~sc->flags & SPAWN_RESPAWN) {
151c26c4f79Snicm 		if (idx == -1)
152c26c4f79Snicm 			idx = -1 - options_get_number(s->options, "base-index");
153c26c4f79Snicm 		if ((sc->wl = winlink_add(&s->windows, idx)) == NULL) {
154c26c4f79Snicm 			xasprintf(cause, "couldn't add window %d", idx);
155c26c4f79Snicm 			return (NULL);
156c26c4f79Snicm 		}
157035dc73dSnicm 		default_window_size(sc->tc, s, NULL, &sx, &sy, &xpixel, &ypixel,
1584a8b0ea5Snicm 		    -1);
1594a8b0ea5Snicm 		if ((w = window_create(sx, sy, xpixel, ypixel)) == NULL) {
160c26c4f79Snicm 			winlink_remove(&s->windows, sc->wl);
161c26c4f79Snicm 			xasprintf(cause, "couldn't create window %d", idx);
162c26c4f79Snicm 			return (NULL);
163c26c4f79Snicm 		}
164587f8a81Snicm 		if (s->curw == NULL)
165587f8a81Snicm 			s->curw = sc->wl;
166c26c4f79Snicm 		sc->wl->session = s;
167035dc73dSnicm 		w->latest = sc->tc;
168c26c4f79Snicm 		winlink_set_window(sc->wl, w);
169c26c4f79Snicm 	} else
170c26c4f79Snicm 		w = NULL;
171c26c4f79Snicm 	sc->flags |= SPAWN_NONOTIFY;
172c26c4f79Snicm 
173c26c4f79Snicm 	/* Spawn the pane. */
174c26c4f79Snicm 	wp = spawn_pane(sc, cause);
175c26c4f79Snicm 	if (wp == NULL) {
1767ecc8255Snicm 		if (~sc->flags & SPAWN_RESPAWN)
177c26c4f79Snicm 			winlink_remove(&s->windows, sc->wl);
178c26c4f79Snicm 		return (NULL);
179c26c4f79Snicm 	}
180c26c4f79Snicm 
181c26c4f79Snicm 	/* Set the name of the new window. */
182c26c4f79Snicm 	if (~sc->flags & SPAWN_RESPAWN) {
1831d297f78Snicm 		free(w->name);
184c26c4f79Snicm 		if (sc->name != NULL) {
185ae88e52dSnicm 			w->name = format_single(item, sc->name, c, s, NULL,
186ae88e52dSnicm 			    NULL);
187c26c4f79Snicm 			options_set_number(w->options, "automatic-rename", 0);
188c26c4f79Snicm 		} else
189c6ae5393Snicm 			w->name = default_window_name(w);
190c26c4f79Snicm 	}
191c26c4f79Snicm 
192c26c4f79Snicm 	/* Switch to the new window if required. */
193c26c4f79Snicm 	if (~sc->flags & SPAWN_DETACHED)
194c26c4f79Snicm 		session_select(s, sc->wl->idx);
195c26c4f79Snicm 
196c26c4f79Snicm 	/* Fire notification if new window. */
197c26c4f79Snicm 	if (~sc->flags & SPAWN_RESPAWN)
198c26c4f79Snicm 		notify_session_window("window-linked", s, w);
199c26c4f79Snicm 
200c26c4f79Snicm 	session_group_synchronize_from(s);
201c26c4f79Snicm 	return (sc->wl);
202c26c4f79Snicm }
203c26c4f79Snicm 
204c26c4f79Snicm struct window_pane *
spawn_pane(struct spawn_context * sc,char ** cause)205c26c4f79Snicm spawn_pane(struct spawn_context *sc, char **cause)
206c26c4f79Snicm {
207c26c4f79Snicm 	struct cmdq_item	 *item = sc->item;
208040343aeSnicm 	struct cmd_find_state	 *target = cmdq_get_target(item);
209040343aeSnicm 	struct client		 *c = cmdq_get_client(item);
210c26c4f79Snicm 	struct session		 *s = sc->s;
211c26c4f79Snicm 	struct window		 *w = sc->wl->window;
212c26c4f79Snicm 	struct window_pane	 *new_wp;
213c26c4f79Snicm 	struct environ		 *child;
214c26c4f79Snicm 	struct environ_entry	 *ee;
215428f955dSnicm 	char			**argv, *cp, **argvp, *argv0, *cwd, *new_cwd;
216c26c4f79Snicm 	const char		 *cmd, *tmp;
217c26c4f79Snicm 	int			  argc;
218c26c4f79Snicm 	u_int			  idx;
219c26c4f79Snicm 	struct termios		  now;
220c26c4f79Snicm 	u_int			  hlimit;
221c26c4f79Snicm 	struct winsize		  ws;
222c26c4f79Snicm 	sigset_t		  set, oldset;
223fdd8dc91Snicm 	key_code		  key;
224c26c4f79Snicm 
225c26c4f79Snicm 	spawn_log(__func__, sc);
226c26c4f79Snicm 
227c26c4f79Snicm 	/*
228c71c2530Snicm 	 * Work out the current working directory. If respawning, use
229c71c2530Snicm 	 * the pane's stored one unless specified.
230c71c2530Snicm 	 */
231428f955dSnicm 	if (sc->cwd != NULL) {
232040343aeSnicm 		cwd = format_single(item, sc->cwd, c, target->s, NULL, NULL);
233428f955dSnicm 		if (*cwd != '/') {
234428f955dSnicm 			xasprintf(&new_cwd, "%s/%s", server_client_get_cwd(c,
235428f955dSnicm 			    target->s), cwd);
236428f955dSnicm 			free(cwd);
237428f955dSnicm 			cwd = new_cwd;
238428f955dSnicm 		}
239428f955dSnicm 	} else if (~sc->flags & SPAWN_RESPAWN)
240040343aeSnicm 		cwd = xstrdup(server_client_get_cwd(c, target->s));
241c71c2530Snicm 	else
242c71c2530Snicm 		cwd = NULL;
243c71c2530Snicm 
244c71c2530Snicm 	/*
245c26c4f79Snicm 	 * If we are respawning then get rid of the old process. Otherwise
246c26c4f79Snicm 	 * either create a new cell or assign to the one we are given.
247c26c4f79Snicm 	 */
248c26c4f79Snicm 	hlimit = options_get_number(s->options, "history-limit");
249c26c4f79Snicm 	if (sc->flags & SPAWN_RESPAWN) {
250c26c4f79Snicm 		if (sc->wp0->fd != -1 && (~sc->flags & SPAWN_KILL)) {
251c26c4f79Snicm 			window_pane_index(sc->wp0, &idx);
252c26c4f79Snicm 			xasprintf(cause, "pane %s:%d.%u still active",
253c26c4f79Snicm 			    s->name, sc->wl->idx, idx);
254c71c2530Snicm 			free(cwd);
255c26c4f79Snicm 			return (NULL);
256c26c4f79Snicm 		}
257c26c4f79Snicm 		if (sc->wp0->fd != -1) {
258c26c4f79Snicm 			bufferevent_free(sc->wp0->event);
259c26c4f79Snicm 			close(sc->wp0->fd);
260c26c4f79Snicm 		}
261c26c4f79Snicm 		window_pane_reset_mode_all(sc->wp0);
262c26c4f79Snicm 		screen_reinit(&sc->wp0->base);
26329ebed37Snicm 		input_free(sc->wp0->ictx);
264cf6b0e8fSnicm 		sc->wp0->ictx = NULL;
265c26c4f79Snicm 		new_wp = sc->wp0;
266c26c4f79Snicm 		new_wp->flags &= ~(PANE_STATUSREADY|PANE_STATUSDRAWN);
267c26c4f79Snicm 	} else if (sc->lc == NULL) {
268c26c4f79Snicm 		new_wp = window_add_pane(w, NULL, hlimit, sc->flags);
269c26c4f79Snicm 		layout_init(w, new_wp);
270c26c4f79Snicm 	} else {
271c26c4f79Snicm 		new_wp = window_add_pane(w, sc->wp0, hlimit, sc->flags);
272baddd6b2Snicm 		if (sc->flags & SPAWN_ZOOM)
273baddd6b2Snicm 			layout_assign_pane(sc->lc, new_wp, 1);
274baddd6b2Snicm 		else
275baddd6b2Snicm 			layout_assign_pane(sc->lc, new_wp, 0);
276c26c4f79Snicm 	}
277c26c4f79Snicm 
278c26c4f79Snicm 	/*
279309e6403Snicm 	 * Now we have a pane with nothing running in it ready for the new
280309e6403Snicm 	 * process. Work out the command and arguments and store the working
281309e6403Snicm 	 * directory.
282c26c4f79Snicm 	 */
283a74bab06Snicm 	if (sc->argc == 0 && (~sc->flags & SPAWN_RESPAWN)) {
284c26c4f79Snicm 		cmd = options_get_string(s->options, "default-command");
285c26c4f79Snicm 		if (cmd != NULL && *cmd != '\0') {
286c26c4f79Snicm 			argc = 1;
287c26c4f79Snicm 			argv = (char **)&cmd;
288c26c4f79Snicm 		} else {
289c26c4f79Snicm 			argc = 0;
290c26c4f79Snicm 			argv = NULL;
291c26c4f79Snicm 		}
292c26c4f79Snicm 	} else {
293c26c4f79Snicm 		argc = sc->argc;
294c26c4f79Snicm 		argv = sc->argv;
295c26c4f79Snicm 	}
296c71c2530Snicm 	if (cwd != NULL) {
297c71c2530Snicm 		free(new_wp->cwd);
298c71c2530Snicm 		new_wp->cwd = cwd;
299c71c2530Snicm 	}
300c26c4f79Snicm 
301c26c4f79Snicm 	/*
302c26c4f79Snicm 	 * Replace the stored arguments if there are new ones. If not, the
303c26c4f79Snicm 	 * existing ones will be used (they will only exist for respawn).
304c26c4f79Snicm 	 */
305c26c4f79Snicm 	if (argc > 0) {
306c26c4f79Snicm 		cmd_free_argv(new_wp->argc, new_wp->argv);
307c26c4f79Snicm 		new_wp->argc = argc;
308c26c4f79Snicm 		new_wp->argv = cmd_copy_argv(argc, argv);
309c26c4f79Snicm 	}
310c26c4f79Snicm 
311c26c4f79Snicm 	/* Create an environment for this pane. */
312c26c4f79Snicm 	child = environ_for_session(s, 0);
313d0772b58Snicm 	if (sc->environ != NULL)
314d0772b58Snicm 		environ_copy(sc->environ, child);
315d6f6a5d2Snicm 	environ_set(child, "TMUX_PANE", 0, "%%%u", new_wp->id);
316c26c4f79Snicm 
317c26c4f79Snicm 	/*
318c26c4f79Snicm 	 * Then the PATH environment variable. The session one is replaced from
319c26c4f79Snicm 	 * the client if there is one because otherwise running "tmux new
320c26c4f79Snicm 	 * myprogram" wouldn't work if myprogram isn't in the session's path.
321c26c4f79Snicm 	 */
322c26c4f79Snicm 	if (c != NULL && c->session == NULL) { /* only unattached clients */
323c26c4f79Snicm 		ee = environ_find(c->environ, "PATH");
324c26c4f79Snicm 		if (ee != NULL)
325d6f6a5d2Snicm 			environ_set(child, "PATH", 0, "%s", ee->value);
326c26c4f79Snicm 	}
327c26c4f79Snicm 	if (environ_find(child, "PATH") == NULL)
328d6f6a5d2Snicm 		environ_set(child, "PATH", 0, "%s", _PATH_DEFPATH);
329c26c4f79Snicm 
330c26c4f79Snicm 	/* Then the shell. If respawning, use the old one. */
331c26c4f79Snicm 	if (~sc->flags & SPAWN_RESPAWN) {
332c26c4f79Snicm 		tmp = options_get_string(s->options, "default-shell");
333e8bf1467Snicm 		if (!checkshell(tmp))
334c26c4f79Snicm 			tmp = _PATH_BSHELL;
335c26c4f79Snicm 		free(new_wp->shell);
336c26c4f79Snicm 		new_wp->shell = xstrdup(tmp);
337c26c4f79Snicm 	}
338d6f6a5d2Snicm 	environ_set(child, "SHELL", 0, "%s", new_wp->shell);
339c26c4f79Snicm 
340c26c4f79Snicm 	/* Log the arguments we are going to use. */
341c26c4f79Snicm 	log_debug("%s: shell=%s", __func__, new_wp->shell);
342c26c4f79Snicm 	if (new_wp->argc != 0) {
343c26c4f79Snicm 		cp = cmd_stringify_argv(new_wp->argc, new_wp->argv);
344c26c4f79Snicm 		log_debug("%s: cmd=%s", __func__, cp);
345c26c4f79Snicm 		free(cp);
346c26c4f79Snicm 	}
347428f955dSnicm 	log_debug("%s: cwd=%s", __func__, new_wp->cwd);
3482732babfSnicm 	cmd_log_argv(new_wp->argc, new_wp->argv, "%s", __func__);
349c26c4f79Snicm 	environ_log(child, "%s: environment ", __func__);
350c26c4f79Snicm 
351c26c4f79Snicm 	/* Initialize the window size. */
352c26c4f79Snicm 	memset(&ws, 0, sizeof ws);
353c26c4f79Snicm 	ws.ws_col = screen_size_x(&new_wp->base);
354c26c4f79Snicm 	ws.ws_row = screen_size_y(&new_wp->base);
3554a8b0ea5Snicm 	ws.ws_xpixel = w->xpixel * ws.ws_col;
3564a8b0ea5Snicm 	ws.ws_ypixel = w->ypixel * ws.ws_row;
357c26c4f79Snicm 
358c26c4f79Snicm 	/* Block signals until fork has completed. */
359c26c4f79Snicm 	sigfillset(&set);
360c26c4f79Snicm 	sigprocmask(SIG_BLOCK, &set, &oldset);
361c26c4f79Snicm 
3623d28e8e7Snicm 	/* If the command is empty, don't fork a child process. */
3633d28e8e7Snicm 	if (sc->flags & SPAWN_EMPTY) {
3643d28e8e7Snicm 		new_wp->flags |= PANE_EMPTY;
3653d28e8e7Snicm 		new_wp->base.mode &= ~MODE_CURSOR;
3663d28e8e7Snicm 		new_wp->base.mode |= MODE_CRLF;
3673d28e8e7Snicm 		goto complete;
3683d28e8e7Snicm 	}
3693d28e8e7Snicm 
370c26c4f79Snicm 	/* Fork the new process. */
371c26c4f79Snicm 	new_wp->pid = fdforkpty(ptm_fd, &new_wp->fd, new_wp->tty, NULL, &ws);
372c26c4f79Snicm 	if (new_wp->pid == -1) {
373c26c4f79Snicm 		xasprintf(cause, "fork failed: %s", strerror(errno));
374c26c4f79Snicm 		new_wp->fd = -1;
375c26c4f79Snicm 		if (~sc->flags & SPAWN_RESPAWN) {
376249e1654Snicm 			server_client_remove_pane(new_wp);
377c26c4f79Snicm 			layout_close_pane(new_wp);
378c26c4f79Snicm 			window_remove_pane(w, new_wp);
379c26c4f79Snicm 		}
380c26c4f79Snicm 		sigprocmask(SIG_SETMASK, &oldset, NULL);
381928e56dcSnicm 		environ_free(child);
382c26c4f79Snicm 		return (NULL);
383c26c4f79Snicm 	}
384c26c4f79Snicm 
385c26c4f79Snicm 	/* In the parent process, everything is done now. */
386aab3c1a6Snicm 	if (new_wp->pid != 0)
387aab3c1a6Snicm 		goto complete;
388c26c4f79Snicm 
389c26c4f79Snicm 	/*
390c26c4f79Snicm 	 * Child process. Change to the working directory or home if that
391c26c4f79Snicm 	 * fails.
392c26c4f79Snicm 	 */
393428f955dSnicm 	if (chdir(new_wp->cwd) == 0)
394428f955dSnicm 		environ_set(child, "PWD", 0, "%s", new_wp->cwd);
395917085d6Snicm 	else if ((tmp = find_home()) != NULL && chdir(tmp) == 0)
396428f955dSnicm 		environ_set(child, "PWD", 0, "%s", tmp);
397428f955dSnicm 	else if (chdir("/") == 0)
398428f955dSnicm 		environ_set(child, "PWD", 0, "/");
399428f955dSnicm 	else
400f93f4929Snicm 		fatal("chdir failed");
401c26c4f79Snicm 
402c26c4f79Snicm 	/*
403c26c4f79Snicm 	 * Update terminal escape characters from the session if available and
404fdd8dc91Snicm 	 * force VERASE to tmux's backspace.
405c26c4f79Snicm 	 */
406c26c4f79Snicm 	if (tcgetattr(STDIN_FILENO, &now) != 0)
407c26c4f79Snicm 		_exit(1);
408c26c4f79Snicm 	if (s->tio != NULL)
409c26c4f79Snicm 		memcpy(now.c_cc, s->tio->c_cc, sizeof now.c_cc);
410fdd8dc91Snicm 	key = options_get_number(global_options, "backspace");
411fdd8dc91Snicm 	if (key >= 0x7f)
412c26c4f79Snicm 		now.c_cc[VERASE] = '\177';
413fdd8dc91Snicm 	else
414fdd8dc91Snicm 		now.c_cc[VERASE] = key;
415c26c4f79Snicm 	if (tcsetattr(STDIN_FILENO, TCSANOW, &now) != 0)
416c26c4f79Snicm 		_exit(1);
417c26c4f79Snicm 
418c26c4f79Snicm 	/* Clean up file descriptors and signals and update the environment. */
419c26c4f79Snicm 	proc_clear_signals(server_proc, 1);
420b3dc7a2cSnicm 	closefrom(STDERR_FILENO + 1);
421c26c4f79Snicm 	sigprocmask(SIG_SETMASK, &oldset, NULL);
422c26c4f79Snicm 	log_close();
423c26c4f79Snicm 	environ_push(child);
424c26c4f79Snicm 
425c26c4f79Snicm 	/*
426c26c4f79Snicm 	 * If given multiple arguments, use execvp(). Copy the arguments to
427c26c4f79Snicm 	 * ensure they end in a NULL.
428c26c4f79Snicm 	 */
429c26c4f79Snicm 	if (new_wp->argc != 0 && new_wp->argc != 1) {
430c26c4f79Snicm 		argvp = cmd_copy_argv(new_wp->argc, new_wp->argv);
431c26c4f79Snicm 		execvp(argvp[0], argvp);
432c26c4f79Snicm 		_exit(1);
433c26c4f79Snicm 	}
434c26c4f79Snicm 
435c26c4f79Snicm 	/*
436c26c4f79Snicm 	 * If one argument, pass it to $SHELL -c. Otherwise create a login
437c26c4f79Snicm 	 * shell.
438c26c4f79Snicm 	 */
439c26c4f79Snicm 	cp = strrchr(new_wp->shell, '/');
440c26c4f79Snicm 	if (new_wp->argc == 1) {
441c26c4f79Snicm 		tmp = new_wp->argv[0];
442c26c4f79Snicm 		if (cp != NULL && cp[1] != '\0')
443c26c4f79Snicm 			xasprintf(&argv0, "%s", cp + 1);
444c26c4f79Snicm 		else
445c26c4f79Snicm 			xasprintf(&argv0, "%s", new_wp->shell);
446c26c4f79Snicm 		execl(new_wp->shell, argv0, "-c", tmp, (char *)NULL);
447c26c4f79Snicm 		_exit(1);
448c26c4f79Snicm 	}
449c26c4f79Snicm 	if (cp != NULL && cp[1] != '\0')
450c26c4f79Snicm 		xasprintf(&argv0, "-%s", cp + 1);
451c26c4f79Snicm 	else
452c26c4f79Snicm 		xasprintf(&argv0, "-%s", new_wp->shell);
453c26c4f79Snicm 	execl(new_wp->shell, argv0, (char *)NULL);
454c26c4f79Snicm 	_exit(1);
455aab3c1a6Snicm 
456aab3c1a6Snicm complete:
457aab3c1a6Snicm 	new_wp->flags &= ~PANE_EXITED;
458aab3c1a6Snicm 
459aab3c1a6Snicm 	sigprocmask(SIG_SETMASK, &oldset, NULL);
460aab3c1a6Snicm 	window_pane_set_event(new_wp);
461aab3c1a6Snicm 
462928e56dcSnicm 	environ_free(child);
463928e56dcSnicm 
464aab3c1a6Snicm 	if (sc->flags & SPAWN_RESPAWN)
465aab3c1a6Snicm 		return (new_wp);
466aab3c1a6Snicm 	if ((~sc->flags & SPAWN_DETACHED) || w->active == NULL) {
467aab3c1a6Snicm 		if (sc->flags & SPAWN_NONOTIFY)
468aab3c1a6Snicm 			window_set_active_pane(w, new_wp, 0);
469aab3c1a6Snicm 		else
470aab3c1a6Snicm 			window_set_active_pane(w, new_wp, 1);
471aab3c1a6Snicm 	}
472aab3c1a6Snicm 	if (~sc->flags & SPAWN_NONOTIFY)
473aab3c1a6Snicm 		notify_window("window-layout-changed", w);
474aab3c1a6Snicm 	return (new_wp);
475c26c4f79Snicm }
476