2019-04-07 12:11:55 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Nicholas Marriott <nicholas.marriott@gmail.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the environment and create a new window and pane or a new pane.
|
|
|
|
*
|
|
|
|
* We need to set up the following items:
|
|
|
|
*
|
|
|
|
* - history limit, comes from the session;
|
|
|
|
*
|
|
|
|
* - base index, comes from the session;
|
|
|
|
*
|
|
|
|
* - current working directory, may be specified - if it isn't it comes from
|
|
|
|
* either the client or the session;
|
|
|
|
*
|
|
|
|
* - PATH variable, comes from the client if any, otherwise from the session
|
|
|
|
* environment;
|
|
|
|
*
|
|
|
|
* - shell, comes from default-shell;
|
|
|
|
*
|
|
|
|
* - termios, comes from the session;
|
|
|
|
*
|
|
|
|
* - remaining environment, comes from the session.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
spawn_log(const char *from, struct spawn_context *sc)
|
|
|
|
{
|
|
|
|
struct session *s = sc->s;
|
|
|
|
struct winlink *wl = sc->wl;
|
|
|
|
struct window_pane *wp0 = sc->wp0;
|
2020-04-13 10:59:58 +00:00
|
|
|
const char *name = cmdq_get_name(sc->item);
|
2019-04-07 12:11:55 +00:00
|
|
|
char tmp[128];
|
|
|
|
|
2020-04-13 10:59:58 +00:00
|
|
|
log_debug("%s: %s, flags=%#x", from, name, sc->flags);
|
2019-04-07 12:11:55 +00:00
|
|
|
|
|
|
|
if (wl != NULL && wp0 != NULL)
|
|
|
|
xsnprintf(tmp, sizeof tmp, "wl=%d wp0=%%%u", wl->idx, wp0->id);
|
|
|
|
else if (wl != NULL)
|
|
|
|
xsnprintf(tmp, sizeof tmp, "wl=%d wp0=none", wl->idx);
|
|
|
|
else if (wp0 != NULL)
|
|
|
|
xsnprintf(tmp, sizeof tmp, "wl=none wp0=%%%u", wp0->id);
|
|
|
|
else
|
|
|
|
xsnprintf(tmp, sizeof tmp, "wl=none wp0=none");
|
|
|
|
log_debug("%s: s=$%u %s idx=%d", from, s->id, tmp, sc->idx);
|
2020-04-13 10:59:58 +00:00
|
|
|
log_debug("%s: name=%s", from, sc->name == NULL ? "none" : sc->name);
|
2019-04-07 12:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct winlink *
|
|
|
|
spawn_window(struct spawn_context *sc, char **cause)
|
|
|
|
{
|
2020-01-01 21:51:33 +00:00
|
|
|
struct cmdq_item *item = sc->item;
|
2020-04-13 10:59:58 +00:00
|
|
|
struct client *c = cmdq_get_client(item);
|
2019-04-07 12:11:55 +00:00
|
|
|
struct session *s = sc->s;
|
|
|
|
struct window *w;
|
|
|
|
struct window_pane *wp;
|
|
|
|
struct winlink *wl;
|
|
|
|
int idx = sc->idx;
|
2019-11-28 09:45:15 +00:00
|
|
|
u_int sx, sy, xpixel, ypixel;
|
2019-04-07 12:11:55 +00:00
|
|
|
|
|
|
|
spawn_log(__func__, sc);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the window already exists, we are respawning, so destroy all the
|
|
|
|
* panes except one.
|
|
|
|
*/
|
|
|
|
if (sc->flags & SPAWN_RESPAWN) {
|
|
|
|
w = sc->wl->window;
|
|
|
|
if (~sc->flags & SPAWN_KILL) {
|
|
|
|
TAILQ_FOREACH(wp, &w->panes, entry) {
|
|
|
|
if (wp->fd != -1)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (wp != NULL) {
|
|
|
|
xasprintf(cause, "window %s:%d still active",
|
|
|
|
s->name, sc->wl->idx);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sc->wp0 = TAILQ_FIRST(&w->panes);
|
|
|
|
TAILQ_REMOVE(&w->panes, sc->wp0, entry);
|
|
|
|
|
|
|
|
layout_free(w);
|
|
|
|
window_destroy_panes(w);
|
|
|
|
|
|
|
|
TAILQ_INSERT_HEAD(&w->panes, sc->wp0, entry);
|
|
|
|
window_pane_resize(sc->wp0, w->sx, w->sy);
|
|
|
|
|
|
|
|
layout_init(w, sc->wp0);
|
2023-07-10 09:24:53 +00:00
|
|
|
w->active = NULL;
|
2019-04-07 12:11:55 +00:00
|
|
|
window_set_active_pane(w, sc->wp0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Otherwise we have no window so we will need to create one. First
|
|
|
|
* check if the given index already exists and destroy it if so.
|
|
|
|
*/
|
|
|
|
if ((~sc->flags & SPAWN_RESPAWN) && idx != -1) {
|
|
|
|
wl = winlink_find_by_index(&s->windows, idx);
|
|
|
|
if (wl != NULL && (~sc->flags & SPAWN_KILL)) {
|
|
|
|
xasprintf(cause, "index %d in use", idx);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
if (wl != NULL) {
|
|
|
|
/*
|
|
|
|
* Can't use session_detach as it will destroy session
|
|
|
|
* if this makes it empty.
|
|
|
|
*/
|
|
|
|
wl->flags &= ~WINLINK_ALERTFLAGS;
|
|
|
|
notify_session_window("window-unlinked", s, wl->window);
|
|
|
|
winlink_stack_remove(&s->lastw, wl);
|
|
|
|
winlink_remove(&s->windows, wl);
|
|
|
|
|
|
|
|
if (s->curw == wl) {
|
|
|
|
s->curw = NULL;
|
|
|
|
sc->flags &= ~SPAWN_DETACHED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then create a window if needed. */
|
|
|
|
if (~sc->flags & SPAWN_RESPAWN) {
|
|
|
|
if (idx == -1)
|
|
|
|
idx = -1 - options_get_number(s->options, "base-index");
|
|
|
|
if ((sc->wl = winlink_add(&s->windows, idx)) == NULL) {
|
|
|
|
xasprintf(cause, "couldn't add window %d", idx);
|
|
|
|
return (NULL);
|
|
|
|
}
|
2020-04-13 20:51:57 +00:00
|
|
|
default_window_size(sc->tc, s, NULL, &sx, &sy, &xpixel, &ypixel,
|
2019-11-28 09:45:15 +00:00
|
|
|
-1);
|
|
|
|
if ((w = window_create(sx, sy, xpixel, ypixel)) == NULL) {
|
2019-04-07 12:11:55 +00:00
|
|
|
winlink_remove(&s->windows, sc->wl);
|
|
|
|
xasprintf(cause, "couldn't create window %d", idx);
|
|
|
|
return (NULL);
|
|
|
|
}
|
2019-05-27 12:48:52 +00:00
|
|
|
if (s->curw == NULL)
|
|
|
|
s->curw = sc->wl;
|
2019-04-07 12:11:55 +00:00
|
|
|
sc->wl->session = s;
|
2020-04-13 20:51:57 +00:00
|
|
|
w->latest = sc->tc;
|
2019-04-07 12:11:55 +00:00
|
|
|
winlink_set_window(sc->wl, w);
|
|
|
|
} else
|
|
|
|
w = NULL;
|
|
|
|
sc->flags |= SPAWN_NONOTIFY;
|
|
|
|
|
|
|
|
/* Spawn the pane. */
|
|
|
|
wp = spawn_pane(sc, cause);
|
|
|
|
if (wp == NULL) {
|
2019-06-30 19:21:53 +00:00
|
|
|
if (~sc->flags & SPAWN_RESPAWN)
|
2019-04-07 12:11:55 +00:00
|
|
|
winlink_remove(&s->windows, sc->wl);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the name of the new window. */
|
|
|
|
if (~sc->flags & SPAWN_RESPAWN) {
|
2021-08-23 11:04:21 +00:00
|
|
|
free(w->name);
|
2019-04-07 12:11:55 +00:00
|
|
|
if (sc->name != NULL) {
|
2020-01-01 21:51:33 +00:00
|
|
|
w->name = format_single(item, sc->name, c, s, NULL,
|
|
|
|
NULL);
|
2019-04-07 12:11:55 +00:00
|
|
|
options_set_number(w->options, "automatic-rename", 0);
|
|
|
|
} else
|
2021-03-02 11:00:38 +00:00
|
|
|
w->name = default_window_name(w);
|
2019-04-07 12:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Switch to the new window if required. */
|
|
|
|
if (~sc->flags & SPAWN_DETACHED)
|
|
|
|
session_select(s, sc->wl->idx);
|
|
|
|
|
|
|
|
/* Fire notification if new window. */
|
|
|
|
if (~sc->flags & SPAWN_RESPAWN)
|
|
|
|
notify_session_window("window-linked", s, w);
|
|
|
|
|
|
|
|
session_group_synchronize_from(s);
|
|
|
|
return (sc->wl);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct window_pane *
|
|
|
|
spawn_pane(struct spawn_context *sc, char **cause)
|
|
|
|
{
|
|
|
|
struct cmdq_item *item = sc->item;
|
2020-04-13 10:59:58 +00:00
|
|
|
struct cmd_find_state *target = cmdq_get_target(item);
|
|
|
|
struct client *c = cmdq_get_client(item);
|
2019-04-07 12:11:55 +00:00
|
|
|
struct session *s = sc->s;
|
|
|
|
struct window *w = sc->wl->window;
|
|
|
|
struct window_pane *new_wp;
|
|
|
|
struct environ *child;
|
|
|
|
struct environ_entry *ee;
|
2022-05-20 07:33:57 +00:00
|
|
|
char **argv, *cp, **argvp, *argv0, *cwd, *new_cwd;
|
2019-04-07 12:11:55 +00:00
|
|
|
const char *cmd, *tmp;
|
2019-04-10 06:27:21 +00:00
|
|
|
int argc;
|
2019-04-07 12:11:55 +00:00
|
|
|
u_int idx;
|
|
|
|
struct termios now;
|
|
|
|
u_int hlimit;
|
|
|
|
struct winsize ws;
|
|
|
|
sigset_t set, oldset;
|
2019-11-14 07:55:01 +00:00
|
|
|
key_code key;
|
2019-04-07 12:11:55 +00:00
|
|
|
|
|
|
|
spawn_log(__func__, sc);
|
|
|
|
|
2020-01-28 10:21:21 +00:00
|
|
|
/*
|
|
|
|
* Work out the current working directory. If respawning, use
|
|
|
|
* the pane's stored one unless specified.
|
|
|
|
*/
|
2022-05-20 07:33:57 +00:00
|
|
|
if (sc->cwd != NULL) {
|
2020-04-13 10:59:58 +00:00
|
|
|
cwd = format_single(item, sc->cwd, c, target->s, NULL, NULL);
|
2022-05-20 07:33:57 +00:00
|
|
|
if (*cwd != '/') {
|
|
|
|
xasprintf(&new_cwd, "%s/%s", server_client_get_cwd(c,
|
|
|
|
target->s), cwd);
|
|
|
|
free(cwd);
|
|
|
|
cwd = new_cwd;
|
|
|
|
}
|
|
|
|
} else if (~sc->flags & SPAWN_RESPAWN)
|
2020-04-13 10:59:58 +00:00
|
|
|
cwd = xstrdup(server_client_get_cwd(c, target->s));
|
2020-01-28 10:21:21 +00:00
|
|
|
else
|
|
|
|
cwd = NULL;
|
|
|
|
|
2019-04-07 12:11:55 +00:00
|
|
|
/*
|
|
|
|
* If we are respawning then get rid of the old process. Otherwise
|
|
|
|
* either create a new cell or assign to the one we are given.
|
|
|
|
*/
|
|
|
|
hlimit = options_get_number(s->options, "history-limit");
|
|
|
|
if (sc->flags & SPAWN_RESPAWN) {
|
|
|
|
if (sc->wp0->fd != -1 && (~sc->flags & SPAWN_KILL)) {
|
|
|
|
window_pane_index(sc->wp0, &idx);
|
|
|
|
xasprintf(cause, "pane %s:%d.%u still active",
|
|
|
|
s->name, sc->wl->idx, idx);
|
2020-01-28 10:21:21 +00:00
|
|
|
free(cwd);
|
2019-04-07 12:11:55 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
if (sc->wp0->fd != -1) {
|
|
|
|
bufferevent_free(sc->wp0->event);
|
|
|
|
close(sc->wp0->fd);
|
|
|
|
}
|
|
|
|
window_pane_reset_mode_all(sc->wp0);
|
|
|
|
screen_reinit(&sc->wp0->base);
|
2020-03-19 14:03:48 +00:00
|
|
|
input_free(sc->wp0->ictx);
|
2020-03-31 06:35:38 +00:00
|
|
|
sc->wp0->ictx = NULL;
|
2019-04-07 12:11:55 +00:00
|
|
|
new_wp = sc->wp0;
|
|
|
|
new_wp->flags &= ~(PANE_STATUSREADY|PANE_STATUSDRAWN);
|
|
|
|
} else if (sc->lc == NULL) {
|
|
|
|
new_wp = window_add_pane(w, NULL, hlimit, sc->flags);
|
|
|
|
layout_init(w, new_wp);
|
|
|
|
} else {
|
|
|
|
new_wp = window_add_pane(w, sc->wp0, hlimit, sc->flags);
|
2021-03-11 06:31:05 +00:00
|
|
|
if (sc->flags & SPAWN_ZOOM)
|
|
|
|
layout_assign_pane(sc->lc, new_wp, 1);
|
|
|
|
else
|
|
|
|
layout_assign_pane(sc->lc, new_wp, 0);
|
2019-04-07 12:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-03-02 10:56:45 +00:00
|
|
|
* Now we have a pane with nothing running in it ready for the new
|
|
|
|
* process. Work out the command and arguments and store the working
|
|
|
|
* directory.
|
2019-04-07 12:11:55 +00:00
|
|
|
*/
|
2019-10-07 07:14:07 +00:00
|
|
|
if (sc->argc == 0 && (~sc->flags & SPAWN_RESPAWN)) {
|
2019-04-07 12:11:55 +00:00
|
|
|
cmd = options_get_string(s->options, "default-command");
|
|
|
|
if (cmd != NULL && *cmd != '\0') {
|
|
|
|
argc = 1;
|
|
|
|
argv = (char **)&cmd;
|
|
|
|
} else {
|
|
|
|
argc = 0;
|
|
|
|
argv = NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
argc = sc->argc;
|
|
|
|
argv = sc->argv;
|
|
|
|
}
|
2020-01-28 10:21:21 +00:00
|
|
|
if (cwd != NULL) {
|
|
|
|
free(new_wp->cwd);
|
|
|
|
new_wp->cwd = cwd;
|
|
|
|
}
|
2019-04-07 12:11:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Replace the stored arguments if there are new ones. If not, the
|
|
|
|
* existing ones will be used (they will only exist for respawn).
|
|
|
|
*/
|
|
|
|
if (argc > 0) {
|
|
|
|
cmd_free_argv(new_wp->argc, new_wp->argv);
|
|
|
|
new_wp->argc = argc;
|
|
|
|
new_wp->argv = cmd_copy_argv(argc, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create an environment for this pane. */
|
|
|
|
child = environ_for_session(s, 0);
|
2019-04-28 20:05:50 +00:00
|
|
|
if (sc->environ != NULL)
|
|
|
|
environ_copy(sc->environ, child);
|
2020-03-31 17:14:40 +00:00
|
|
|
environ_set(child, "TMUX_PANE", 0, "%%%u", new_wp->id);
|
2019-04-07 12:11:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Then the PATH environment variable. The session one is replaced from
|
|
|
|
* the client if there is one because otherwise running "tmux new
|
|
|
|
* myprogram" wouldn't work if myprogram isn't in the session's path.
|
|
|
|
*/
|
|
|
|
if (c != NULL && c->session == NULL) { /* only unattached clients */
|
|
|
|
ee = environ_find(c->environ, "PATH");
|
|
|
|
if (ee != NULL)
|
2020-03-31 17:14:40 +00:00
|
|
|
environ_set(child, "PATH", 0, "%s", ee->value);
|
2019-04-07 12:11:55 +00:00
|
|
|
}
|
|
|
|
if (environ_find(child, "PATH") == NULL)
|
2020-03-31 17:14:40 +00:00
|
|
|
environ_set(child, "PATH", 0, "%s", _PATH_DEFPATH);
|
2019-04-07 12:11:55 +00:00
|
|
|
|
|
|
|
/* Then the shell. If respawning, use the old one. */
|
|
|
|
if (~sc->flags & SPAWN_RESPAWN) {
|
|
|
|
tmp = options_get_string(s->options, "default-shell");
|
2020-03-17 11:10:12 +00:00
|
|
|
if (!checkshell(tmp))
|
2019-04-07 12:11:55 +00:00
|
|
|
tmp = _PATH_BSHELL;
|
|
|
|
free(new_wp->shell);
|
|
|
|
new_wp->shell = xstrdup(tmp);
|
|
|
|
}
|
2020-03-31 17:14:40 +00:00
|
|
|
environ_set(child, "SHELL", 0, "%s", new_wp->shell);
|
2019-04-07 12:11:55 +00:00
|
|
|
|
|
|
|
/* Log the arguments we are going to use. */
|
|
|
|
log_debug("%s: shell=%s", __func__, new_wp->shell);
|
2019-04-07 12:16:55 +00:00
|
|
|
if (new_wp->argc != 0) {
|
|
|
|
cp = cmd_stringify_argv(new_wp->argc, new_wp->argv);
|
|
|
|
log_debug("%s: cmd=%s", __func__, cp);
|
|
|
|
free(cp);
|
|
|
|
}
|
2022-05-20 07:33:57 +00:00
|
|
|
log_debug("%s: cwd=%s", __func__, new_wp->cwd);
|
2019-05-25 06:58:10 +00:00
|
|
|
cmd_log_argv(new_wp->argc, new_wp->argv, "%s", __func__);
|
2019-04-07 12:11:55 +00:00
|
|
|
environ_log(child, "%s: environment ", __func__);
|
|
|
|
|
|
|
|
/* Initialize the window size. */
|
|
|
|
memset(&ws, 0, sizeof ws);
|
|
|
|
ws.ws_col = screen_size_x(&new_wp->base);
|
|
|
|
ws.ws_row = screen_size_y(&new_wp->base);
|
2019-11-28 09:45:15 +00:00
|
|
|
ws.ws_xpixel = w->xpixel * ws.ws_col;
|
|
|
|
ws.ws_ypixel = w->ypixel * ws.ws_row;
|
2019-04-07 12:11:55 +00:00
|
|
|
|
|
|
|
/* Block signals until fork has completed. */
|
|
|
|
sigfillset(&set);
|
|
|
|
sigprocmask(SIG_BLOCK, &set, &oldset);
|
|
|
|
|
2019-09-18 11:37:58 +00:00
|
|
|
/* If the command is empty, don't fork a child process. */
|
|
|
|
if (sc->flags & SPAWN_EMPTY) {
|
|
|
|
new_wp->flags |= PANE_EMPTY;
|
|
|
|
new_wp->base.mode &= ~MODE_CURSOR;
|
|
|
|
new_wp->base.mode |= MODE_CRLF;
|
|
|
|
goto complete;
|
|
|
|
}
|
|
|
|
|
2019-04-07 12:11:55 +00:00
|
|
|
/* Fork the new process. */
|
|
|
|
new_wp->pid = fdforkpty(ptm_fd, &new_wp->fd, new_wp->tty, NULL, &ws);
|
|
|
|
if (new_wp->pid == -1) {
|
|
|
|
xasprintf(cause, "fork failed: %s", strerror(errno));
|
|
|
|
new_wp->fd = -1;
|
|
|
|
if (~sc->flags & SPAWN_RESPAWN) {
|
2020-05-14 10:18:19 +00:00
|
|
|
server_client_remove_pane(new_wp);
|
2019-04-07 12:11:55 +00:00
|
|
|
layout_close_pane(new_wp);
|
|
|
|
window_remove_pane(w, new_wp);
|
|
|
|
}
|
|
|
|
sigprocmask(SIG_SETMASK, &oldset, NULL);
|
2020-04-23 05:48:42 +00:00
|
|
|
environ_free(child);
|
2019-04-07 12:11:55 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* In the parent process, everything is done now. */
|
2023-04-03 07:50:49 +00:00
|
|
|
if (new_wp->pid != 0) {
|
|
|
|
#if defined(HAVE_SYSTEMD) && defined(ENABLE_CGROUPS)
|
|
|
|
/*
|
|
|
|
* Move the child process into a new cgroup for systemd-oomd
|
|
|
|
* isolation.
|
|
|
|
*/
|
|
|
|
if (systemd_move_pid_to_new_cgroup(new_wp->pid, cause) < 0) {
|
|
|
|
log_debug("%s: moving pane to new cgroup failed: %s",
|
|
|
|
__func__, *cause);
|
|
|
|
free (*cause);
|
|
|
|
}
|
|
|
|
#endif
|
2019-05-03 20:44:24 +00:00
|
|
|
goto complete;
|
2023-04-03 07:50:49 +00:00
|
|
|
}
|
2019-04-07 12:11:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Child process. Change to the working directory or home if that
|
|
|
|
* fails.
|
|
|
|
*/
|
2022-05-20 07:33:57 +00:00
|
|
|
if (chdir(new_wp->cwd) == 0)
|
|
|
|
environ_set(child, "PWD", 0, "%s", new_wp->cwd);
|
2022-08-10 14:03:59 +00:00
|
|
|
else if ((tmp = find_home()) != NULL && chdir(tmp) == 0)
|
2022-05-20 07:33:57 +00:00
|
|
|
environ_set(child, "PWD", 0, "%s", tmp);
|
|
|
|
else if (chdir("/") == 0)
|
|
|
|
environ_set(child, "PWD", 0, "/");
|
|
|
|
else
|
2021-02-19 09:09:16 +00:00
|
|
|
fatal("chdir failed");
|
2019-04-07 12:11:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Update terminal escape characters from the session if available and
|
2019-11-14 07:55:01 +00:00
|
|
|
* force VERASE to tmux's backspace.
|
2019-04-07 12:11:55 +00:00
|
|
|
*/
|
|
|
|
if (tcgetattr(STDIN_FILENO, &now) != 0)
|
|
|
|
_exit(1);
|
|
|
|
if (s->tio != NULL)
|
|
|
|
memcpy(now.c_cc, s->tio->c_cc, sizeof now.c_cc);
|
2019-11-14 07:55:01 +00:00
|
|
|
key = options_get_number(global_options, "backspace");
|
|
|
|
if (key >= 0x7f)
|
|
|
|
now.c_cc[VERASE] = '\177';
|
|
|
|
else
|
|
|
|
now.c_cc[VERASE] = key;
|
2020-05-26 05:15:13 +00:00
|
|
|
#ifdef IUTF8
|
|
|
|
now.c_iflag |= IUTF8;
|
|
|
|
#endif
|
2019-04-07 12:11:55 +00:00
|
|
|
if (tcsetattr(STDIN_FILENO, TCSANOW, &now) != 0)
|
|
|
|
_exit(1);
|
|
|
|
|
|
|
|
/* Clean up file descriptors and signals and update the environment. */
|
|
|
|
proc_clear_signals(server_proc, 1);
|
2023-07-09 22:54:52 +00:00
|
|
|
closefrom(STDERR_FILENO + 1);
|
2019-04-07 12:11:55 +00:00
|
|
|
sigprocmask(SIG_SETMASK, &oldset, NULL);
|
|
|
|
log_close();
|
|
|
|
environ_push(child);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If given multiple arguments, use execvp(). Copy the arguments to
|
|
|
|
* ensure they end in a NULL.
|
|
|
|
*/
|
|
|
|
if (new_wp->argc != 0 && new_wp->argc != 1) {
|
|
|
|
argvp = cmd_copy_argv(new_wp->argc, new_wp->argv);
|
|
|
|
execvp(argvp[0], argvp);
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If one argument, pass it to $SHELL -c. Otherwise create a login
|
|
|
|
* shell.
|
|
|
|
*/
|
|
|
|
cp = strrchr(new_wp->shell, '/');
|
|
|
|
if (new_wp->argc == 1) {
|
|
|
|
tmp = new_wp->argv[0];
|
|
|
|
if (cp != NULL && cp[1] != '\0')
|
|
|
|
xasprintf(&argv0, "%s", cp + 1);
|
|
|
|
else
|
|
|
|
xasprintf(&argv0, "%s", new_wp->shell);
|
Support use as a Windows program outside of Cygwin
If `getenv("SHELL")` is `NULL`, use the function `win32_setenv_shell()`
added to `osdep-win32-cpp.cpp` to set it using the command line of the
parent process, which is retrieved using WMI WIN32 API.
This is a C++ file because the OLE/WMI API is only available for C++.
Adjust the autotools code to add this file and link the necessary
Windows DLLs.
Include some fixed/missing MINGW headers necessary to compile this file.
This will be fixed in the relevant places and they will be removed.
Add a new macro WIN32_PLATFORM for Windows specific functionality, currently
Cygwin and MSYS2, in the future for the native port as well.
When spawning commands using the shell, check for cmd.exe on Windows and
use the `/c` switch, otherwise use `-c` which works for PowerShell,
MSYS2, Cygwin and Git Bash etc..
Adjust code that uses `/tmp/` to use
`$env:USERPROFILE/AppData/Local/Temp/` outside of a Cygwin virtual
filesystem when `/tmp/` is not available, add the function
`win32_get_tmpdir()` and related functions to `osdep-win32.c` for this.
Use `getenv("USERPROFILE")` when `getenv("HOME")` is `NULL`.
When outside of a Cygwin virtual filesystem, use
`C:\ProgramData\tmux\tmux.conf:$USERPROFILE\.tmux.conf:$LOCALAPPDATA\tmux\tmux.conf`
as the config search order.
Use the ncurses term-driver with `TERM="#win32con"` when a terminfo
database is not available. This will require patches to ncurses as well
as MSYS2 and Cygwin to work.
Signed-off-by: Rafael Kitover <rkitover@gmail.com>
2024-08-24 05:44:45 +00:00
|
|
|
execl(new_wp->shell, argv0, SHELL_CMD_SWITCH(new_wp->shell), tmp, (char *)NULL);
|
2019-04-07 12:11:55 +00:00
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
if (cp != NULL && cp[1] != '\0')
|
|
|
|
xasprintf(&argv0, "-%s", cp + 1);
|
|
|
|
else
|
|
|
|
xasprintf(&argv0, "-%s", new_wp->shell);
|
|
|
|
execl(new_wp->shell, argv0, (char *)NULL);
|
|
|
|
_exit(1);
|
2019-05-03 20:44:24 +00:00
|
|
|
|
|
|
|
complete:
|
2019-12-18 15:58:06 +00:00
|
|
|
#ifdef HAVE_UTEMPTER
|
|
|
|
if (~new_wp->flags & PANE_EMPTY) {
|
|
|
|
xasprintf(&cp, "tmux(%lu).%%%u", (long)getpid(), new_wp->id);
|
|
|
|
utempter_add_record(new_wp->fd, cp);
|
|
|
|
kill(getpid(), SIGCHLD);
|
|
|
|
free(cp);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-05-03 20:44:24 +00:00
|
|
|
new_wp->flags &= ~PANE_EXITED;
|
|
|
|
|
|
|
|
sigprocmask(SIG_SETMASK, &oldset, NULL);
|
|
|
|
window_pane_set_event(new_wp);
|
|
|
|
|
2020-04-23 05:48:42 +00:00
|
|
|
environ_free(child);
|
|
|
|
|
2019-05-03 20:44:24 +00:00
|
|
|
if (sc->flags & SPAWN_RESPAWN)
|
|
|
|
return (new_wp);
|
|
|
|
if ((~sc->flags & SPAWN_DETACHED) || w->active == NULL) {
|
|
|
|
if (sc->flags & SPAWN_NONOTIFY)
|
|
|
|
window_set_active_pane(w, new_wp, 0);
|
|
|
|
else
|
|
|
|
window_set_active_pane(w, new_wp, 1);
|
|
|
|
}
|
|
|
|
if (~sc->flags & SPAWN_NONOTIFY)
|
|
|
|
notify_window("window-layout-changed", w);
|
|
|
|
return (new_wp);
|
2019-04-07 12:11:55 +00:00
|
|
|
}
|