From d0d1c0e486dc13b6e5db93819ca1b634d384f283 Mon Sep 17 00:00:00 2001 From: Tiago Cunha Date: Mon, 14 Feb 2011 23:11:33 +0000 Subject: [PATCH] Sync OpenBSD patchset 848: Set $TMUX without the session when background jobs are run. --- cmd.c | 4 ++-- job.c | 14 +++++++++++--- server-fn.c | 20 +++++++++++++------- tmux.c | 47 ++++++++++------------------------------------- tmux.h | 8 ++++---- 5 files changed, 40 insertions(+), 53 deletions(-) diff --git a/cmd.c b/cmd.c index 4577fb1b..ef781aa0 100644 --- a/cmd.c +++ b/cmd.c @@ -1,4 +1,4 @@ -/* $Id: cmd.c,v 1.148 2011-01-07 14:45:34 tcunha Exp $ */ +/* $Id: cmd.c,v 1.149 2011-02-14 23:11:33 tcunha Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -355,7 +355,7 @@ cmd_current_session(struct cmd_ctx *ctx) } /* Use the session from the TMUX environment variable. */ - if (data != NULL && data->pid == getpid()) { + if (data != NULL && data->pid == getpid() && data->idx != -1) { s = session_find_by_index(data->idx); if (s != NULL) return (s); diff --git a/job.c b/job.c index e2605b65..c3cf7179 100644 --- a/job.c +++ b/job.c @@ -1,4 +1,4 @@ -/* $Id: job.c,v 1.20 2011-01-21 23:44:13 tcunha Exp $ */ +/* $Id: job.c,v 1.21 2011-02-14 23:11:33 tcunha Exp $ */ /* * Copyright (c) 2009 Nicholas Marriott @@ -136,7 +136,8 @@ job_free(struct job *job) int job_run(struct job *job) { - int nullfd, out[2]; + struct environ env; + int nullfd, out[2]; if (job->fd != -1 || job->pid != -1) return (0); @@ -144,13 +145,19 @@ job_run(struct job *job) if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, out) != 0) return (-1); + environ_init(&env); + environ_copy(&global_environ, &env); + server_fill_environ(NULL, &env); + switch (job->pid = fork()) { case -1: + environ_free(&env); return (-1); case 0: /* child */ clear_signals(1); - environ_push(&global_environ); + environ_push(&env); + environ_free(&env); if (dup2(out[1], STDOUT_FILENO) == -1) fatal("dup2 failed"); @@ -173,6 +180,7 @@ job_run(struct job *job) execl(_PATH_BSHELL, "sh", "-c", job->cmd, (char *) NULL); fatal("execl failed"); default: /* parent */ + environ_free(&env); close(out[1]); job->fd = out[0]; diff --git a/server-fn.c b/server-fn.c index fa470e5a..b66d4dc3 100644 --- a/server-fn.c +++ b/server-fn.c @@ -1,4 +1,4 @@ -/* $Id: server-fn.c,v 1.118 2011-01-03 23:27:54 tcunha Exp $ */ +/* $Id: server-fn.c,v 1.119 2011-02-14 23:11:33 tcunha Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -30,14 +30,20 @@ void server_callback_identify(int, short, void *); void server_fill_environ(struct session *s, struct environ *env) { - char tmuxvar[MAXPATHLEN], *term; + char var[MAXPATHLEN], *term; + u_int idx; + long pid; - xsnprintf(tmuxvar, sizeof tmuxvar, - "%s,%ld,%u", socket_path, (long) getpid(), s->idx); - environ_set(env, "TMUX", tmuxvar); + if (s != NULL) { + term = options_get_string(&s->options, "default-terminal"); + environ_set(env, "TERM", term); - term = options_get_string(&s->options, "default-terminal"); - environ_set(env, "TERM", term); + idx = s->idx; + } else + idx = -1; + pid = getpid(); + xsnprintf(var, sizeof var, "%s,%ld,%d", socket_path, pid, idx); + environ_set(env, "TMUX", var); } void diff --git a/tmux.c b/tmux.c index 5a2fb01b..5b036c30 100644 --- a/tmux.c +++ b/tmux.c @@ -1,4 +1,4 @@ -/* $Id: tmux.c,v 1.235 2011-01-21 23:46:50 tcunha Exp $ */ +/* $Id: tmux.c,v 1.236 2011-02-14 23:11:33 tcunha Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -47,8 +47,8 @@ time_t start_time; char socket_path[MAXPATHLEN]; int login_shell; char *environ_path; -pid_t environ_pid; -u_int environ_idx; +pid_t environ_pid = -1; +int environ_idx = -1; __dead void usage(void); void parseenvironment(void); @@ -128,45 +128,18 @@ areshell(const char *shell) void parseenvironment(void) { - char *env, *path_pid, *pid_idx, buf[256]; - size_t len; - const char *errstr; - long long ll; + char *env, path[256]; + long pid; + int idx; - environ_pid = -1; if ((env = getenv("TMUX")) == NULL) return; - if ((path_pid = strchr(env, ',')) == NULL || path_pid == env) + if (sscanf(env, "%255s,%ld,%d", path, &pid, &idx) != 3) return; - if ((pid_idx = strchr(path_pid + 1, ',')) == NULL) - return; - if ((pid_idx == path_pid + 1 || pid_idx[1] == '\0')) - return; - - /* path */ - len = path_pid - env; - environ_path = xmalloc(len + 1); - memcpy(environ_path, env, len); - environ_path[len] = '\0'; - - /* pid */ - len = pid_idx - path_pid - 1; - if (len > (sizeof buf) - 1) - return; - memcpy(buf, path_pid + 1, len); - buf[len] = '\0'; - - ll = strtonum(buf, 0, LONG_MAX, &errstr); - if (errstr != NULL) - return; - environ_pid = ll; - - /* idx */ - ll = strtonum(pid_idx + 1, 0, UINT_MAX, &errstr); - if (errstr != NULL) - return; - environ_idx = ll; + environ_path = xstrdup(path); + environ_pid = pid; + environ_idx = idx; } char * diff --git a/tmux.h b/tmux.h index af353b21..6e9abd1c 100644 --- a/tmux.h +++ b/tmux.h @@ -1,4 +1,4 @@ -/* $Id: tmux.h,v 1.605 2011-01-21 23:56:11 tcunha Exp $ */ +/* $Id: tmux.h,v 1.606 2011-02-14 23:11:33 tcunha Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -379,8 +379,8 @@ enum msgtype { * Don't forget to bump PROTOCOL_VERSION if any of these change! */ struct msg_command_data { - pid_t pid; /* pid from $TMUX or -1 */ - u_int idx; /* index from $TMUX */ + pid_t pid; /* PID from $TMUX or -1 */ + int idx; /* index from $TMUX or -1 */ int argc; char argv[COMMAND_LENGTH]; @@ -1297,7 +1297,7 @@ extern char socket_path[MAXPATHLEN]; extern int login_shell; extern char *environ_path; extern pid_t environ_pid; -extern u_int environ_idx; +extern int environ_idx; void logfile(const char *); const char *getshell(void); int checkshell(const char *);