xref: /netbsd-src/external/bsd/tmux/dist/spawn.c (revision f844e94ef29eebc7999c12636b87f541bb86868b)
16483eba0Schristos /* $OpenBSD$ */
26483eba0Schristos 
36483eba0Schristos /*
46483eba0Schristos  * Copyright (c) 2019 Nicholas Marriott <nicholas.marriott@gmail.com>
56483eba0Schristos  *
66483eba0Schristos  * Permission to use, copy, modify, and distribute this software for any
76483eba0Schristos  * purpose with or without fee is hereby granted, provided that the above
86483eba0Schristos  * copyright notice and this permission notice appear in all copies.
96483eba0Schristos  *
106483eba0Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
116483eba0Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
126483eba0Schristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
136483eba0Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
146483eba0Schristos  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
156483eba0Schristos  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
166483eba0Schristos  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
176483eba0Schristos  */
186483eba0Schristos 
196483eba0Schristos #include <sys/types.h>
206483eba0Schristos 
216483eba0Schristos #include <errno.h>
226483eba0Schristos #include <signal.h>
236483eba0Schristos #include <stdlib.h>
246483eba0Schristos #include <string.h>
256483eba0Schristos #include <unistd.h>
266483eba0Schristos 
276483eba0Schristos #include "tmux.h"
286483eba0Schristos 
296483eba0Schristos /*
306483eba0Schristos  * Set up the environment and create a new window and pane or a new pane.
316483eba0Schristos  *
326483eba0Schristos  * We need to set up the following items:
336483eba0Schristos  *
346483eba0Schristos  * - history limit, comes from the session;
356483eba0Schristos  *
366483eba0Schristos  * - base index, comes from the session;
376483eba0Schristos  *
386483eba0Schristos  * - current working directory, may be specified - if it isn't it comes from
396483eba0Schristos  *   either the client or the session;
406483eba0Schristos  *
416483eba0Schristos  * - PATH variable, comes from the client if any, otherwise from the session
426483eba0Schristos  *   environment;
436483eba0Schristos  *
446483eba0Schristos  * - shell, comes from default-shell;
456483eba0Schristos  *
466483eba0Schristos  * - termios, comes from the session;
476483eba0Schristos  *
486483eba0Schristos  * - remaining environment, comes from the session.
496483eba0Schristos  */
506483eba0Schristos 
516483eba0Schristos static void
spawn_log(const char * from,struct spawn_context * sc)526483eba0Schristos spawn_log(const char *from, struct spawn_context *sc)
536483eba0Schristos {
546483eba0Schristos 	struct session		*s = sc->s;
556483eba0Schristos 	struct winlink		*wl = sc->wl;
566483eba0Schristos 	struct window_pane	*wp0 = sc->wp0;
57e271dbb8Schristos 	const char		*name = cmdq_get_name(sc->item);
586483eba0Schristos 	char			 tmp[128];
596483eba0Schristos 
60e271dbb8Schristos 	log_debug("%s: %s, flags=%#x", from, name, sc->flags);
616483eba0Schristos 
626483eba0Schristos 	if (wl != NULL && wp0 != NULL)
636483eba0Schristos 		xsnprintf(tmp, sizeof tmp, "wl=%d wp0=%%%u", wl->idx, wp0->id);
646483eba0Schristos 	else if (wl != NULL)
656483eba0Schristos 		xsnprintf(tmp, sizeof tmp, "wl=%d wp0=none", wl->idx);
666483eba0Schristos 	else if (wp0 != NULL)
676483eba0Schristos 		xsnprintf(tmp, sizeof tmp, "wl=none wp0=%%%u", wp0->id);
686483eba0Schristos 	else
696483eba0Schristos 		xsnprintf(tmp, sizeof tmp, "wl=none wp0=none");
706483eba0Schristos 	log_debug("%s: s=$%u %s idx=%d", from, s->id, tmp, sc->idx);
71e271dbb8Schristos 	log_debug("%s: name=%s", from, sc->name == NULL ? "none" : sc->name);
726483eba0Schristos }
736483eba0Schristos 
746483eba0Schristos struct winlink *
spawn_window(struct spawn_context * sc,char ** cause)756483eba0Schristos spawn_window(struct spawn_context *sc, char **cause)
766483eba0Schristos {
7768e6ba84Schristos 	struct cmdq_item	*item = sc->item;
78e271dbb8Schristos 	struct client		*c = cmdq_get_client(item);
796483eba0Schristos 	struct session		*s = sc->s;
806483eba0Schristos 	struct window		*w;
816483eba0Schristos 	struct window_pane	*wp;
826483eba0Schristos 	struct winlink		*wl;
836483eba0Schristos 	int			 idx = sc->idx;
8468e6ba84Schristos 	u_int			 sx, sy, xpixel, ypixel;
856483eba0Schristos 
866483eba0Schristos 	spawn_log(__func__, sc);
876483eba0Schristos 
886483eba0Schristos 	/*
896483eba0Schristos 	 * If the window already exists, we are respawning, so destroy all the
906483eba0Schristos 	 * panes except one.
916483eba0Schristos 	 */
926483eba0Schristos 	if (sc->flags & SPAWN_RESPAWN) {
936483eba0Schristos 		w = sc->wl->window;
946483eba0Schristos 		if (~sc->flags & SPAWN_KILL) {
956483eba0Schristos 			TAILQ_FOREACH(wp, &w->panes, entry) {
966483eba0Schristos 				if (wp->fd != -1)
976483eba0Schristos 					break;
986483eba0Schristos 			}
996483eba0Schristos 			if (wp != NULL) {
1006483eba0Schristos 				xasprintf(cause, "window %s:%d still active",
1016483eba0Schristos 				    s->name, sc->wl->idx);
1026483eba0Schristos 				return (NULL);
1036483eba0Schristos 			}
1046483eba0Schristos 		}
1056483eba0Schristos 
1066483eba0Schristos 		sc->wp0 = TAILQ_FIRST(&w->panes);
1076483eba0Schristos 		TAILQ_REMOVE(&w->panes, sc->wp0, entry);
1086483eba0Schristos 
1096483eba0Schristos 		layout_free(w);
1106483eba0Schristos 		window_destroy_panes(w);
1116483eba0Schristos 
1126483eba0Schristos 		TAILQ_INSERT_HEAD(&w->panes, sc->wp0, entry);
1136483eba0Schristos 		window_pane_resize(sc->wp0, w->sx, w->sy);
1146483eba0Schristos 
1156483eba0Schristos 		layout_init(w, sc->wp0);
116*f844e94eSwiz 		w->active = NULL;
1176483eba0Schristos 		window_set_active_pane(w, sc->wp0, 0);
1186483eba0Schristos 	}
1196483eba0Schristos 
1206483eba0Schristos 	/*
1216483eba0Schristos 	 * Otherwise we have no window so we will need to create one. First
1226483eba0Schristos 	 * check if the given index already exists and destroy it if so.
1236483eba0Schristos 	 */
1246483eba0Schristos 	if ((~sc->flags & SPAWN_RESPAWN) && idx != -1) {
1256483eba0Schristos 		wl = winlink_find_by_index(&s->windows, idx);
1266483eba0Schristos 		if (wl != NULL && (~sc->flags & SPAWN_KILL)) {
1276483eba0Schristos 			xasprintf(cause, "index %d in use", idx);
1286483eba0Schristos 			return (NULL);
1296483eba0Schristos 		}
1306483eba0Schristos 		if (wl != NULL) {
1316483eba0Schristos 			/*
1326483eba0Schristos 			 * Can't use session_detach as it will destroy session
1336483eba0Schristos 			 * if this makes it empty.
1346483eba0Schristos 			 */
1356483eba0Schristos 			wl->flags &= ~WINLINK_ALERTFLAGS;
1366483eba0Schristos 			notify_session_window("window-unlinked", s, wl->window);
1376483eba0Schristos 			winlink_stack_remove(&s->lastw, wl);
1386483eba0Schristos 			winlink_remove(&s->windows, wl);
1396483eba0Schristos 
1406483eba0Schristos 			if (s->curw == wl) {
1416483eba0Schristos 				s->curw = NULL;
1426483eba0Schristos 				sc->flags &= ~SPAWN_DETACHED;
1436483eba0Schristos 			}
1446483eba0Schristos 		}
1456483eba0Schristos 	}
1466483eba0Schristos 
1476483eba0Schristos 	/* Then create a window if needed. */
1486483eba0Schristos 	if (~sc->flags & SPAWN_RESPAWN) {
1496483eba0Schristos 		if (idx == -1)
1506483eba0Schristos 			idx = -1 - options_get_number(s->options, "base-index");
1516483eba0Schristos 		if ((sc->wl = winlink_add(&s->windows, idx)) == NULL) {
1526483eba0Schristos 			xasprintf(cause, "couldn't add window %d", idx);
1536483eba0Schristos 			return (NULL);
1546483eba0Schristos 		}
155e271dbb8Schristos 		default_window_size(sc->tc, s, NULL, &sx, &sy, &xpixel, &ypixel,
15668e6ba84Schristos 		    -1);
15768e6ba84Schristos 		if ((w = window_create(sx, sy, xpixel, ypixel)) == NULL) {
1586483eba0Schristos 			winlink_remove(&s->windows, sc->wl);
1596483eba0Schristos 			xasprintf(cause, "couldn't create window %d", idx);
1606483eba0Schristos 			return (NULL);
1616483eba0Schristos 		}
1626483eba0Schristos 		if (s->curw == NULL)
1636483eba0Schristos 			s->curw = sc->wl;
1646483eba0Schristos 		sc->wl->session = s;
165e271dbb8Schristos 		w->latest = sc->tc;
1666483eba0Schristos 		winlink_set_window(sc->wl, w);
1676483eba0Schristos 	} else
1686483eba0Schristos 		w = NULL;
1696483eba0Schristos 	sc->flags |= SPAWN_NONOTIFY;
1706483eba0Schristos 
1716483eba0Schristos 	/* Spawn the pane. */
1726483eba0Schristos 	wp = spawn_pane(sc, cause);
1736483eba0Schristos 	if (wp == NULL) {
1746483eba0Schristos 		if (~sc->flags & SPAWN_RESPAWN)
1756483eba0Schristos 			winlink_remove(&s->windows, sc->wl);
1766483eba0Schristos 		return (NULL);
1776483eba0Schristos 	}
1786483eba0Schristos 
1796483eba0Schristos 	/* Set the name of the new window. */
1806483eba0Schristos 	if (~sc->flags & SPAWN_RESPAWN) {
18146548964Swiz 		free(w->name);
1826483eba0Schristos 		if (sc->name != NULL) {
18368e6ba84Schristos 			w->name = format_single(item, sc->name, c, s, NULL,
18468e6ba84Schristos 			    NULL);
1856483eba0Schristos 			options_set_number(w->options, "automatic-rename", 0);
1866483eba0Schristos 		} else
187e271dbb8Schristos 			w->name = default_window_name(w);
1886483eba0Schristos 	}
1896483eba0Schristos 
1906483eba0Schristos 	/* Switch to the new window if required. */
1916483eba0Schristos 	if (~sc->flags & SPAWN_DETACHED)
1926483eba0Schristos 		session_select(s, sc->wl->idx);
1936483eba0Schristos 
1946483eba0Schristos 	/* Fire notification if new window. */
1956483eba0Schristos 	if (~sc->flags & SPAWN_RESPAWN)
1966483eba0Schristos 		notify_session_window("window-linked", s, w);
1976483eba0Schristos 
1986483eba0Schristos 	session_group_synchronize_from(s);
1996483eba0Schristos 	return (sc->wl);
2006483eba0Schristos }
2016483eba0Schristos 
2026483eba0Schristos struct window_pane *
spawn_pane(struct spawn_context * sc,char ** cause)2036483eba0Schristos spawn_pane(struct spawn_context *sc, char **cause)
2046483eba0Schristos {
2056483eba0Schristos 	struct cmdq_item	 *item = sc->item;
206e271dbb8Schristos 	struct cmd_find_state	 *target = cmdq_get_target(item);
207e271dbb8Schristos 	struct client		 *c = cmdq_get_client(item);
2086483eba0Schristos 	struct session		 *s = sc->s;
2096483eba0Schristos 	struct window		 *w = sc->wl->window;
2106483eba0Schristos 	struct window_pane	 *new_wp;
2116483eba0Schristos 	struct environ		 *child;
2126483eba0Schristos 	struct environ_entry	 *ee;
21346548964Swiz 	char			**argv, *cp, **argvp, *argv0, *cwd, *new_cwd;
2146483eba0Schristos 	const char		 *cmd, *tmp;
2156483eba0Schristos 	int			  argc;
2166483eba0Schristos 	u_int			  idx;
2176483eba0Schristos 	struct termios		  now;
2186483eba0Schristos 	u_int			  hlimit;
2196483eba0Schristos 	struct winsize		  ws;
2206483eba0Schristos 	sigset_t		  set, oldset;
22168e6ba84Schristos 	key_code		  key;
2226483eba0Schristos 
2236483eba0Schristos 	spawn_log(__func__, sc);
2246483eba0Schristos 
2256483eba0Schristos 	/*
22668e6ba84Schristos 	 * Work out the current working directory. If respawning, use
22768e6ba84Schristos 	 * the pane's stored one unless specified.
22868e6ba84Schristos 	 */
22946548964Swiz 	if (sc->cwd != NULL) {
230e271dbb8Schristos 		cwd = format_single(item, sc->cwd, c, target->s, NULL, NULL);
23146548964Swiz 		if (*cwd != '/') {
23246548964Swiz 			xasprintf(&new_cwd, "%s/%s", server_client_get_cwd(c,
23346548964Swiz 			    target->s), cwd);
23446548964Swiz 			free(cwd);
23546548964Swiz 			cwd = new_cwd;
23646548964Swiz 		}
23746548964Swiz 	} else if (~sc->flags & SPAWN_RESPAWN)
238e271dbb8Schristos 		cwd = xstrdup(server_client_get_cwd(c, target->s));
23968e6ba84Schristos 	else
24068e6ba84Schristos 		cwd = NULL;
24168e6ba84Schristos 
24268e6ba84Schristos 	/*
2436483eba0Schristos 	 * If we are respawning then get rid of the old process. Otherwise
2446483eba0Schristos 	 * either create a new cell or assign to the one we are given.
2456483eba0Schristos 	 */
2466483eba0Schristos 	hlimit = options_get_number(s->options, "history-limit");
2476483eba0Schristos 	if (sc->flags & SPAWN_RESPAWN) {
2486483eba0Schristos 		if (sc->wp0->fd != -1 && (~sc->flags & SPAWN_KILL)) {
2496483eba0Schristos 			window_pane_index(sc->wp0, &idx);
2506483eba0Schristos 			xasprintf(cause, "pane %s:%d.%u still active",
2516483eba0Schristos 			    s->name, sc->wl->idx, idx);
25268e6ba84Schristos 			free(cwd);
2536483eba0Schristos 			return (NULL);
2546483eba0Schristos 		}
2556483eba0Schristos 		if (sc->wp0->fd != -1) {
2566483eba0Schristos 			bufferevent_free(sc->wp0->event);
2576483eba0Schristos 			close(sc->wp0->fd);
2586483eba0Schristos 		}
2596483eba0Schristos 		window_pane_reset_mode_all(sc->wp0);
2606483eba0Schristos 		screen_reinit(&sc->wp0->base);
261e271dbb8Schristos 		input_free(sc->wp0->ictx);
262e271dbb8Schristos 		sc->wp0->ictx = NULL;
2636483eba0Schristos 		new_wp = sc->wp0;
2646483eba0Schristos 		new_wp->flags &= ~(PANE_STATUSREADY|PANE_STATUSDRAWN);
2656483eba0Schristos 	} else if (sc->lc == NULL) {
2666483eba0Schristos 		new_wp = window_add_pane(w, NULL, hlimit, sc->flags);
2676483eba0Schristos 		layout_init(w, new_wp);
2686483eba0Schristos 	} else {
2696483eba0Schristos 		new_wp = window_add_pane(w, sc->wp0, hlimit, sc->flags);
270e271dbb8Schristos 		if (sc->flags & SPAWN_ZOOM)
271e271dbb8Schristos 			layout_assign_pane(sc->lc, new_wp, 1);
272e271dbb8Schristos 		else
273e271dbb8Schristos 			layout_assign_pane(sc->lc, new_wp, 0);
2746483eba0Schristos 	}
2756483eba0Schristos 
2766483eba0Schristos 	/*
277e271dbb8Schristos 	 * Now we have a pane with nothing running in it ready for the new
278e271dbb8Schristos 	 * process. Work out the command and arguments and store the working
279e271dbb8Schristos 	 * directory.
2806483eba0Schristos 	 */
2816483eba0Schristos 	if (sc->argc == 0 && (~sc->flags & SPAWN_RESPAWN)) {
2826483eba0Schristos 		cmd = options_get_string(s->options, "default-command");
2836483eba0Schristos 		if (cmd != NULL && *cmd != '\0') {
2846483eba0Schristos 			argc = 1;
28530744affSchristos 			argv = __UNCONST(&cmd);
2866483eba0Schristos 		} else {
2876483eba0Schristos 			argc = 0;
2886483eba0Schristos 			argv = NULL;
2896483eba0Schristos 		}
2906483eba0Schristos 	} else {
2916483eba0Schristos 		argc = sc->argc;
2926483eba0Schristos 		argv = sc->argv;
2936483eba0Schristos 	}
29468e6ba84Schristos 	if (cwd != NULL) {
29568e6ba84Schristos 		free(new_wp->cwd);
29668e6ba84Schristos 		new_wp->cwd = cwd;
29768e6ba84Schristos 	}
2986483eba0Schristos 
2996483eba0Schristos 	/*
3006483eba0Schristos 	 * Replace the stored arguments if there are new ones. If not, the
3016483eba0Schristos 	 * existing ones will be used (they will only exist for respawn).
3026483eba0Schristos 	 */
3036483eba0Schristos 	if (argc > 0) {
3046483eba0Schristos 		cmd_free_argv(new_wp->argc, new_wp->argv);
3056483eba0Schristos 		new_wp->argc = argc;
3066483eba0Schristos 		new_wp->argv = cmd_copy_argv(argc, argv);
3076483eba0Schristos 	}
3086483eba0Schristos 
3096483eba0Schristos 	/* Create an environment for this pane. */
3106483eba0Schristos 	child = environ_for_session(s, 0);
3116483eba0Schristos 	if (sc->environ != NULL)
3126483eba0Schristos 		environ_copy(sc->environ, child);
313e271dbb8Schristos 	environ_set(child, "TMUX_PANE", 0, "%%%u", new_wp->id);
3146483eba0Schristos 
3156483eba0Schristos 	/*
3166483eba0Schristos 	 * Then the PATH environment variable. The session one is replaced from
3176483eba0Schristos 	 * the client if there is one because otherwise running "tmux new
3186483eba0Schristos 	 * myprogram" wouldn't work if myprogram isn't in the session's path.
3196483eba0Schristos 	 */
3206483eba0Schristos 	if (c != NULL && c->session == NULL) { /* only unattached clients */
3216483eba0Schristos 		ee = environ_find(c->environ, "PATH");
3226483eba0Schristos 		if (ee != NULL)
323e271dbb8Schristos 			environ_set(child, "PATH", 0, "%s", ee->value);
3246483eba0Schristos 	}
3256483eba0Schristos 	if (environ_find(child, "PATH") == NULL)
326e271dbb8Schristos 		environ_set(child, "PATH", 0, "%s", _PATH_DEFPATH);
3276483eba0Schristos 
3286483eba0Schristos 	/* Then the shell. If respawning, use the old one. */
3296483eba0Schristos 	if (~sc->flags & SPAWN_RESPAWN) {
3306483eba0Schristos 		tmp = options_get_string(s->options, "default-shell");
331e271dbb8Schristos 		if (!checkshell(tmp))
3326483eba0Schristos 			tmp = _PATH_BSHELL;
3336483eba0Schristos 		free(new_wp->shell);
3346483eba0Schristos 		new_wp->shell = xstrdup(tmp);
3356483eba0Schristos 	}
336e271dbb8Schristos 	environ_set(child, "SHELL", 0, "%s", new_wp->shell);
3376483eba0Schristos 
3386483eba0Schristos 	/* Log the arguments we are going to use. */
3396483eba0Schristos 	log_debug("%s: shell=%s", __func__, new_wp->shell);
3406483eba0Schristos 	if (new_wp->argc != 0) {
3416483eba0Schristos 		cp = cmd_stringify_argv(new_wp->argc, new_wp->argv);
3426483eba0Schristos 		log_debug("%s: cmd=%s", __func__, cp);
3436483eba0Schristos 		free(cp);
3446483eba0Schristos 	}
34546548964Swiz 	log_debug("%s: cwd=%s", __func__, new_wp->cwd);
3466483eba0Schristos 	cmd_log_argv(new_wp->argc, new_wp->argv, "%s", __func__);
3476483eba0Schristos 	environ_log(child, "%s: environment ", __func__);
3486483eba0Schristos 
3496483eba0Schristos 	/* Initialize the window size. */
3506483eba0Schristos 	memset(&ws, 0, sizeof ws);
3516483eba0Schristos 	ws.ws_col = screen_size_x(&new_wp->base);
3526483eba0Schristos 	ws.ws_row = screen_size_y(&new_wp->base);
35368e6ba84Schristos 	ws.ws_xpixel = w->xpixel * ws.ws_col;
35468e6ba84Schristos 	ws.ws_ypixel = w->ypixel * ws.ws_row;
3556483eba0Schristos 
3566483eba0Schristos 	/* Block signals until fork has completed. */
3576483eba0Schristos 	sigfillset(&set);
3586483eba0Schristos 	sigprocmask(SIG_BLOCK, &set, &oldset);
3596483eba0Schristos 
3606483eba0Schristos 	/* If the command is empty, don't fork a child process. */
3616483eba0Schristos 	if (sc->flags & SPAWN_EMPTY) {
3626483eba0Schristos 		new_wp->flags |= PANE_EMPTY;
3636483eba0Schristos 		new_wp->base.mode &= ~MODE_CURSOR;
3646483eba0Schristos 		new_wp->base.mode |= MODE_CRLF;
3656483eba0Schristos 		goto complete;
3666483eba0Schristos 	}
3676483eba0Schristos 
3686483eba0Schristos 	/* Fork the new process. */
3696483eba0Schristos 	new_wp->pid = fdforkpty(ptm_fd, &new_wp->fd, new_wp->tty, NULL, &ws);
3706483eba0Schristos 	if (new_wp->pid == -1) {
3716483eba0Schristos 		xasprintf(cause, "fork failed: %s", strerror(errno));
3726483eba0Schristos 		new_wp->fd = -1;
3736483eba0Schristos 		if (~sc->flags & SPAWN_RESPAWN) {
374e271dbb8Schristos 			server_client_remove_pane(new_wp);
3756483eba0Schristos 			layout_close_pane(new_wp);
3766483eba0Schristos 			window_remove_pane(w, new_wp);
3776483eba0Schristos 		}
3786483eba0Schristos 		sigprocmask(SIG_SETMASK, &oldset, NULL);
379e271dbb8Schristos 		environ_free(child);
3806483eba0Schristos 		return (NULL);
3816483eba0Schristos 	}
3826483eba0Schristos 
3836483eba0Schristos 	/* In the parent process, everything is done now. */
384*f844e94eSwiz 	if (new_wp->pid != 0) {
385*f844e94eSwiz #if defined(HAVE_SYSTEMD) && defined(ENABLE_CGROUPS)
386*f844e94eSwiz 		/*
387*f844e94eSwiz 		 * Move the child process into a new cgroup for systemd-oomd
388*f844e94eSwiz 		 * isolation.
389*f844e94eSwiz 		 */
390*f844e94eSwiz 		if (systemd_move_pid_to_new_cgroup(new_wp->pid, cause) < 0) {
391*f844e94eSwiz 			log_debug("%s: moving pane to new cgroup failed: %s",
392*f844e94eSwiz 			    __func__, *cause);
393*f844e94eSwiz 			free (*cause);
394*f844e94eSwiz 		}
395*f844e94eSwiz #endif
3966483eba0Schristos 		goto complete;
397*f844e94eSwiz 	}
3986483eba0Schristos 
3996483eba0Schristos 	/*
4006483eba0Schristos 	 * Child process. Change to the working directory or home if that
4016483eba0Schristos 	 * fails.
4026483eba0Schristos 	 */
40346548964Swiz 	if (chdir(new_wp->cwd) == 0)
40446548964Swiz 		environ_set(child, "PWD", 0, "%s", new_wp->cwd);
405*f844e94eSwiz 	else if ((tmp = find_home()) != NULL && chdir(tmp) == 0)
40646548964Swiz 		environ_set(child, "PWD", 0, "%s", tmp);
40746548964Swiz 	else if (chdir("/") == 0)
40846548964Swiz 		environ_set(child, "PWD", 0, "/");
40946548964Swiz 	else
410e271dbb8Schristos 		fatal("chdir failed");
4116483eba0Schristos 
4126483eba0Schristos 	/*
4136483eba0Schristos 	 * Update terminal escape characters from the session if available and
41468e6ba84Schristos 	 * force VERASE to tmux's backspace.
4156483eba0Schristos 	 */
4166483eba0Schristos 	if (tcgetattr(STDIN_FILENO, &now) != 0)
4176483eba0Schristos 		_exit(1);
4186483eba0Schristos 	if (s->tio != NULL)
4196483eba0Schristos 		memcpy(now.c_cc, s->tio->c_cc, sizeof now.c_cc);
42068e6ba84Schristos 	key = options_get_number(global_options, "backspace");
42168e6ba84Schristos 	if (key >= 0x7f)
4226483eba0Schristos 		now.c_cc[VERASE] = '\177';
42368e6ba84Schristos 	else
42468e6ba84Schristos 		now.c_cc[VERASE] = key;
425e271dbb8Schristos #ifdef IUTF8
426e271dbb8Schristos 	now.c_iflag |= IUTF8;
427e271dbb8Schristos #endif
4286483eba0Schristos 	if (tcsetattr(STDIN_FILENO, TCSANOW, &now) != 0)
4296483eba0Schristos 		_exit(1);
4306483eba0Schristos 
4316483eba0Schristos 	/* Clean up file descriptors and signals and update the environment. */
4326483eba0Schristos 	proc_clear_signals(server_proc, 1);
433*f844e94eSwiz 	closefrom(STDERR_FILENO + 1);
4346483eba0Schristos 	sigprocmask(SIG_SETMASK, &oldset, NULL);
4356483eba0Schristos 	log_close();
4366483eba0Schristos 	environ_push(child);
4376483eba0Schristos 
4386483eba0Schristos 	/*
4396483eba0Schristos 	 * If given multiple arguments, use execvp(). Copy the arguments to
4406483eba0Schristos 	 * ensure they end in a NULL.
4416483eba0Schristos 	 */
4426483eba0Schristos 	if (new_wp->argc != 0 && new_wp->argc != 1) {
4436483eba0Schristos 		argvp = cmd_copy_argv(new_wp->argc, new_wp->argv);
4446483eba0Schristos 		execvp(argvp[0], argvp);
4456483eba0Schristos 		_exit(1);
4466483eba0Schristos 	}
4476483eba0Schristos 
4486483eba0Schristos 	/*
4496483eba0Schristos 	 * If one argument, pass it to $SHELL -c. Otherwise create a login
4506483eba0Schristos 	 * shell.
4516483eba0Schristos 	 */
4526483eba0Schristos 	cp = strrchr(new_wp->shell, '/');
4536483eba0Schristos 	if (new_wp->argc == 1) {
4546483eba0Schristos 		tmp = new_wp->argv[0];
4556483eba0Schristos 		if (cp != NULL && cp[1] != '\0')
4566483eba0Schristos 			xasprintf(&argv0, "%s", cp + 1);
4576483eba0Schristos 		else
4586483eba0Schristos 			xasprintf(&argv0, "%s", new_wp->shell);
4596483eba0Schristos 		execl(new_wp->shell, argv0, "-c", tmp, (char *)NULL);
4606483eba0Schristos 		_exit(1);
4616483eba0Schristos 	}
4626483eba0Schristos 	if (cp != NULL && cp[1] != '\0')
4636483eba0Schristos 		xasprintf(&argv0, "-%s", cp + 1);
4646483eba0Schristos 	else
4656483eba0Schristos 		xasprintf(&argv0, "-%s", new_wp->shell);
4666483eba0Schristos 	execl(new_wp->shell, argv0, (char *)NULL);
4676483eba0Schristos 	_exit(1);
4686483eba0Schristos 
4696483eba0Schristos complete:
47068e6ba84Schristos #ifdef HAVE_UTEMPTER
47168e6ba84Schristos 	if (~new_wp->flags & PANE_EMPTY) {
47268e6ba84Schristos 		xasprintf(&cp, "tmux(%lu).%%%u", (long)getpid(), new_wp->id);
47368e6ba84Schristos 		utempter_add_record(new_wp->fd, cp);
47468e6ba84Schristos 		kill(getpid(), SIGCHLD);
47568e6ba84Schristos 		free(cp);
47668e6ba84Schristos 	}
47768e6ba84Schristos #endif
47868e6ba84Schristos 
4796483eba0Schristos 	new_wp->flags &= ~PANE_EXITED;
4806483eba0Schristos 
4816483eba0Schristos 	sigprocmask(SIG_SETMASK, &oldset, NULL);
4826483eba0Schristos 	window_pane_set_event(new_wp);
4836483eba0Schristos 
484e271dbb8Schristos 	environ_free(child);
485e271dbb8Schristos 
4866483eba0Schristos 	if (sc->flags & SPAWN_RESPAWN)
4876483eba0Schristos 		return (new_wp);
4886483eba0Schristos 	if ((~sc->flags & SPAWN_DETACHED) || w->active == NULL) {
4896483eba0Schristos 		if (sc->flags & SPAWN_NONOTIFY)
4906483eba0Schristos 			window_set_active_pane(w, new_wp, 0);
4916483eba0Schristos 		else
4926483eba0Schristos 			window_set_active_pane(w, new_wp, 1);
4936483eba0Schristos 	}
4946483eba0Schristos 	if (~sc->flags & SPAWN_NONOTIFY)
4956483eba0Schristos 		notify_window("window-layout-changed", w);
4966483eba0Schristos 	return (new_wp);
4976483eba0Schristos }
498