diff --git a/cmd.c b/cmd.c index 6c4a06f4..5333a023 100644 --- a/cmd.c +++ b/cmd.c @@ -356,7 +356,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 c7181964..ce7961a1 100644 --- a/job.c +++ b/job.c @@ -137,7 +137,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); @@ -145,13 +146,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"); @@ -174,6 +181,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 7064c1ed..002d56ac 100644 --- a/server-fn.c +++ b/server-fn.c @@ -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 0225f5f6..31070f00 100644 --- a/tmux.c +++ b/tmux.c @@ -48,8 +48,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); @@ -125,45 +125,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 4eab285c..f6a45d33 100644 --- a/tmux.h +++ b/tmux.h @@ -383,8 +383,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]; @@ -1301,7 +1301,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 *);