summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@cvs.openbsd.org>2011-01-23 11:03:44 +0000
committerNicholas Marriott <nicm@cvs.openbsd.org>2011-01-23 11:03:44 +0000
commit7e53eabddc6979e4f6c798fee98409cafa0e6a25 (patch)
tree877554c66d5900dcd763f49038c660f468261669 /usr.bin
parent7e1a400591be74655347d8b7d61e848954408757 (diff)
Set $TMUX without the session when background jobs are run.
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/tmux/cmd.c4
-rw-r--r--usr.bin/tmux/job.c14
-rw-r--r--usr.bin/tmux/server-fn.c24
-rw-r--r--usr.bin/tmux/tmux.c47
-rw-r--r--usr.bin/tmux/tmux.h8
5 files changed, 42 insertions, 55 deletions
diff --git a/usr.bin/tmux/cmd.c b/usr.bin/tmux/cmd.c
index 84346402b5c..f56358ab34d 100644
--- a/usr.bin/tmux/cmd.c
+++ b/usr.bin/tmux/cmd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: cmd.c,v 1.49 2011/01/04 00:42:47 nicm Exp $ */
+/* $OpenBSD: cmd.c,v 1.50 2011/01/23 11:03:43 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -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/usr.bin/tmux/job.c b/usr.bin/tmux/job.c
index 06413780c34..99527735335 100644
--- a/usr.bin/tmux/job.c
+++ b/usr.bin/tmux/job.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: job.c,v 1.22 2011/01/08 01:52:36 nicm Exp $ */
+/* $OpenBSD: job.c,v 1.23 2011/01/23 11:03:43 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -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/usr.bin/tmux/server-fn.c b/usr.bin/tmux/server-fn.c
index 1589cf7daa8..dde93cac0f0 100644
--- a/usr.bin/tmux/server-fn.c
+++ b/usr.bin/tmux/server-fn.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: server-fn.c,v 1.49 2011/01/01 01:12:09 nicm Exp $ */
+/* $OpenBSD: server-fn.c,v 1.50 2011/01/23 11:03:43 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -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;
-
- xsnprintf(tmuxvar, sizeof tmuxvar,
- "%s,%ld,%u", socket_path, (long) getpid(), s->idx);
- environ_set(env, "TMUX", tmuxvar);
-
- term = options_get_string(&s->options, "default-terminal");
- environ_set(env, "TERM", term);
+ char var[MAXPATHLEN], *term;
+ u_int idx;
+ long pid;
+
+ if (s != NULL) {
+ 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/usr.bin/tmux/tmux.c b/usr.bin/tmux/tmux.c
index a6a0a972f84..fedc8903e4b 100644
--- a/usr.bin/tmux/tmux.c
+++ b/usr.bin/tmux/tmux.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: tmux.c,v 1.102 2011/01/12 22:23:58 nicm Exp $ */
+/* $OpenBSD: tmux.c,v 1.103 2011/01/23 11:03:43 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -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/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h
index eaed4ce09db..db99b4b8763 100644
--- a/usr.bin/tmux/tmux.h
+++ b/usr.bin/tmux/tmux.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: tmux.h,v 1.266 2011/01/15 00:16:00 nicm Exp $ */
+/* $OpenBSD: tmux.h,v 1.267 2011/01/23 11:03:43 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -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 *);