summaryrefslogtreecommitdiff
path: root/usr.bin/tmux
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@cvs.openbsd.org>2010-05-03 16:06:33 +0000
committerNicholas Marriott <nicm@cvs.openbsd.org>2010-05-03 16:06:33 +0000
commit72a6f023642c24ed068b6564472df9d7ccec30cf (patch)
tree3dd1b2f8b660cfe7c21ab37745c45e4aceb91e44 /usr.bin/tmux
parentb216f11b658d51a71040b8ed7718b8f019686022 (diff)
Make signal handler setup/teardown two common functions instead of six,
and reset SIGCHLD after fork to fix problems with some shells. From Romain Francois.
Diffstat (limited to 'usr.bin/tmux')
-rw-r--r--usr.bin/tmux/Makefile4
-rw-r--r--usr.bin/tmux/client.c27
-rw-r--r--usr.bin/tmux/cmd-pipe-pane.c4
-rw-r--r--usr.bin/tmux/job.c4
-rw-r--r--usr.bin/tmux/server.c70
-rw-r--r--usr.bin/tmux/signal.c90
-rw-r--r--usr.bin/tmux/tmux.c81
-rw-r--r--usr.bin/tmux/tmux.h9
-rw-r--r--usr.bin/tmux/window.c4
9 files changed, 131 insertions, 162 deletions
diff --git a/usr.bin/tmux/Makefile b/usr.bin/tmux/Makefile
index 6add1c2b079..1e952f6a492 100644
--- a/usr.bin/tmux/Makefile
+++ b/usr.bin/tmux/Makefile
@@ -1,4 +1,4 @@
-# $OpenBSD: Makefile,v 1.37 2010/04/06 21:35:44 nicm Exp $
+# $OpenBSD: Makefile,v 1.38 2010/05/03 16:06:32 nicm Exp $
PROG= tmux
SRCS= attributes.c cfg.c client.c clock.c \
@@ -35,7 +35,7 @@ SRCS= attributes.c cfg.c client.c clock.c \
layout-set.c layout-string.c layout.c log.c job.c \
mode-key.c names.c options.c paste.c procname.c \
resize.c screen-redraw.c screen-write.c screen.c session.c status.c \
- server-fn.c server.c server-client.c server-window.c \
+ server-fn.c server.c server-client.c server-window.c signal.c \
tmux.c tty-keys.c tty-term.c tty.c utf8.c \
window-choose.c window-clock.c window-copy.c window.c \
xterm-keys.c xmalloc.c
diff --git a/usr.bin/tmux/client.c b/usr.bin/tmux/client.c
index d49e63e73ab..036fe1a45a6 100644
--- a/usr.bin/tmux/client.c
+++ b/usr.bin/tmux/client.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: client.c,v 1.35 2009/12/03 22:50:09 nicm Exp $ */
+/* $OpenBSD: client.c,v 1.36 2010/05/03 16:06:32 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -172,35 +172,12 @@ client_update_event(void)
__dead void
client_main(void)
{
- struct event ev_sigcont, ev_sigterm, ev_sigwinch;
- struct sigaction sigact;
-
logfile("client");
/* Note: event_init() has already been called. */
/* Set up signals. */
- memset(&sigact, 0, sizeof sigact);
- sigemptyset(&sigact.sa_mask);
- sigact.sa_flags = SA_RESTART;
- sigact.sa_handler = SIG_IGN;
- if (sigaction(SIGINT, &sigact, NULL) != 0)
- fatal("sigaction failed");
- if (sigaction(SIGPIPE, &sigact, NULL) != 0)
- fatal("sigaction failed");
- if (sigaction(SIGUSR1, &sigact, NULL) != 0)
- fatal("sigaction failed");
- if (sigaction(SIGUSR2, &sigact, NULL) != 0)
- fatal("sigaction failed");
- if (sigaction(SIGTSTP, &sigact, NULL) != 0)
- fatal("sigaction failed");
-
- signal_set(&ev_sigcont, SIGCONT, client_signal, NULL);
- signal_add(&ev_sigcont, NULL);
- signal_set(&ev_sigterm, SIGTERM, client_signal, NULL);
- signal_add(&ev_sigterm, NULL);
- signal_set(&ev_sigwinch, SIGWINCH, client_signal, NULL);
- signal_add(&ev_sigwinch, NULL);
+ set_signals(client_signal);
/*
* imsg_read in the first client poll loop (before the terminal has
diff --git a/usr.bin/tmux/cmd-pipe-pane.c b/usr.bin/tmux/cmd-pipe-pane.c
index 926bc927dab..c5aceba7b0c 100644
--- a/usr.bin/tmux/cmd-pipe-pane.c
+++ b/usr.bin/tmux/cmd-pipe-pane.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: cmd-pipe-pane.c,v 1.9 2009/12/03 22:50:10 nicm Exp $ */
+/* $OpenBSD: cmd-pipe-pane.c,v 1.10 2010/05/03 16:06:32 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -91,7 +91,7 @@ cmd_pipe_pane_exec(struct cmd *self, struct cmd_ctx *ctx)
case 0:
/* Child process. */
close(pipe_fd[0]);
- server_signal_clear();
+ clear_signals();
if (dup2(pipe_fd[1], STDIN_FILENO) == -1)
_exit(1);
diff --git a/usr.bin/tmux/job.c b/usr.bin/tmux/job.c
index fd2174e07f7..c37acc7478e 100644
--- a/usr.bin/tmux/job.c
+++ b/usr.bin/tmux/job.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: job.c,v 1.16 2010/04/04 19:02:09 nicm Exp $ */
+/* $OpenBSD: job.c,v 1.17 2010/05/03 16:06:32 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -149,7 +149,7 @@ job_run(struct job *job)
case -1:
return (-1);
case 0: /* child */
- server_signal_clear();
+ clear_signals();
environ_push(&global_environ);
diff --git a/usr.bin/tmux/server.c b/usr.bin/tmux/server.c
index 470d5703f87..ba059815660 100644
--- a/usr.bin/tmux/server.c
+++ b/usr.bin/tmux/server.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: server.c,v 1.85 2010/04/06 21:35:44 nicm Exp $ */
+/* $OpenBSD: server.c,v 1.86 2010/05/03 16:06:32 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -49,9 +49,6 @@ struct clients dead_clients;
int server_fd;
int server_shutdown;
struct event server_ev_accept;
-struct event server_ev_sigterm;
-struct event server_ev_sigusr1;
-struct event server_ev_sigchld;
struct event server_ev_second;
int server_create_socket(void);
@@ -134,6 +131,12 @@ server_start(char *path)
}
close(pair[0]);
+ /* event_init() was called by the parent, need to reinit. */
+ if (event_reinit(ev_base) != 0)
+ fatal("event_reinit failed");
+
+ clear_signals();
+
/*
* Must daemonise before loading configuration as the PID changes so
* $TMUX would be wrong for sessions created in the config file.
@@ -162,8 +165,6 @@ server_start(char *path)
log_debug("socket path %s", socket_path);
setproctitle("server (%s)", rpathbuf);
- event_init();
-
server_fd = server_create_socket();
server_client_create(pair[1]);
@@ -202,7 +203,7 @@ server_start(char *path)
evtimer_set(&server_ev_second, server_second_callback, NULL);
evtimer_add(&server_ev_second, &tv);
- server_signal_set();
+ set_signals(server_signal_callback);
server_loop();
exit(0);
}
@@ -341,61 +342,6 @@ server_accept_callback(int fd, short events, unused void *data)
server_client_create(newfd);
}
-/* Set up server signal handling. */
-void
-server_signal_set(void)
-{
- struct sigaction sigact;
-
- memset(&sigact, 0, sizeof sigact);
- sigemptyset(&sigact.sa_mask);
- sigact.sa_flags = SA_RESTART;
- sigact.sa_handler = SIG_IGN;
- if (sigaction(SIGINT, &sigact, NULL) != 0)
- fatal("sigaction failed");
- if (sigaction(SIGPIPE, &sigact, NULL) != 0)
- fatal("sigaction failed");
- if (sigaction(SIGUSR2, &sigact, NULL) != 0)
- fatal("sigaction failed");
- if (sigaction(SIGTSTP, &sigact, NULL) != 0)
- fatal("sigaction failed");
- if (sigaction(SIGHUP, &sigact, NULL) != 0)
- fatal("sigaction failed");
-
- signal_set(&server_ev_sigchld, SIGCHLD, server_signal_callback, NULL);
- signal_add(&server_ev_sigchld, NULL);
- signal_set(&server_ev_sigterm, SIGTERM, server_signal_callback, NULL);
- signal_add(&server_ev_sigterm, NULL);
- signal_set(&server_ev_sigusr1, SIGUSR1, server_signal_callback, NULL);
- signal_add(&server_ev_sigusr1, NULL);
-}
-
-/* Destroy server signal events. */
-void
-server_signal_clear(void)
-{
- struct sigaction sigact;
-
- memset(&sigact, 0, sizeof sigact);
- sigemptyset(&sigact.sa_mask);
- sigact.sa_flags = SA_RESTART;
- sigact.sa_handler = SIG_DFL;
- if (sigaction(SIGINT, &sigact, NULL) != 0)
- fatal("sigaction failed");
- if (sigaction(SIGPIPE, &sigact, NULL) != 0)
- fatal("sigaction failed");
- if (sigaction(SIGUSR2, &sigact, NULL) != 0)
- fatal("sigaction failed");
- if (sigaction(SIGTSTP, &sigact, NULL) != 0)
- fatal("sigaction failed");
- if (sigaction(SIGHUP, &sigact, NULL) != 0)
- fatal("sigaction failed");
-
- signal_del(&server_ev_sigchld);
- signal_del(&server_ev_sigterm);
- signal_del(&server_ev_sigusr1);
-}
-
/* Signal handler. */
/* ARGSUSED */
void
diff --git a/usr.bin/tmux/signal.c b/usr.bin/tmux/signal.c
new file mode 100644
index 00000000000..8e34243e265
--- /dev/null
+++ b/usr.bin/tmux/signal.c
@@ -0,0 +1,90 @@
+/* $OpenBSD: signal.c,v 1.1 2010/05/03 16:06:32 nicm Exp $ */
+
+/*
+ * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
+ * Copyright (c) 2010 Romain Francoise <rfrancoise@debian.org>
+ *
+ * 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 <signal.h>
+#include <string.h>
+
+#include "tmux.h"
+
+struct event ev_sigchld;
+struct event ev_sigcont;
+struct event ev_sigterm;
+struct event ev_sigusr1;
+struct event ev_sigwinch;
+
+void
+set_signals(void(*handler)(int, short, void *))
+{
+ struct sigaction sigact;
+
+ memset(&sigact, 0, sizeof sigact);
+ sigemptyset(&sigact.sa_mask);
+ sigact.sa_flags = SA_RESTART;
+ sigact.sa_handler = SIG_IGN;
+ if (sigaction(SIGINT, &sigact, NULL) != 0)
+ fatal("sigaction failed");
+ if (sigaction(SIGPIPE, &sigact, NULL) != 0)
+ fatal("sigaction failed");
+ if (sigaction(SIGUSR2, &sigact, NULL) != 0)
+ fatal("sigaction failed");
+ if (sigaction(SIGTSTP, &sigact, NULL) != 0)
+ fatal("sigaction failed");
+ if (sigaction(SIGHUP, &sigact, NULL) != 0)
+ fatal("sigaction failed");
+
+ signal_set(&ev_sigchld, SIGCHLD, handler, NULL);
+ signal_add(&ev_sigchld, NULL);
+ signal_set(&ev_sigcont, SIGCONT, handler, NULL);
+ signal_add(&ev_sigcont, NULL);
+ signal_set(&ev_sigterm, SIGTERM, handler, NULL);
+ signal_add(&ev_sigterm, NULL);
+ signal_set(&ev_sigusr1, SIGUSR1, handler, NULL);
+ signal_add(&ev_sigusr1, NULL);
+ signal_set(&ev_sigwinch, SIGWINCH, handler, NULL);
+ signal_add(&ev_sigwinch, NULL);
+}
+
+void
+clear_signals(void)
+{
+ struct sigaction sigact;
+
+ memset(&sigact, 0, sizeof sigact);
+ sigemptyset(&sigact.sa_mask);
+ sigact.sa_flags = SA_RESTART;
+ sigact.sa_handler = SIG_DFL;
+ if (sigaction(SIGINT, &sigact, NULL) != 0)
+ fatal("sigaction failed");
+ if (sigaction(SIGPIPE, &sigact, NULL) != 0)
+ fatal("sigaction failed");
+ if (sigaction(SIGUSR2, &sigact, NULL) != 0)
+ fatal("sigaction failed");
+ if (sigaction(SIGTSTP, &sigact, NULL) != 0)
+ fatal("sigaction failed");
+ if (sigaction(SIGHUP, &sigact, NULL) != 0)
+ fatal("sigaction failed");
+
+ event_del(&ev_sigchld);
+ event_del(&ev_sigcont);
+ event_del(&ev_sigterm);
+ event_del(&ev_sigusr1);
+ event_del(&ev_sigwinch);
+}
diff --git a/usr.bin/tmux/tmux.c b/usr.bin/tmux/tmux.c
index 1af7849c447..13d7889849b 100644
--- a/usr.bin/tmux/tmux.c
+++ b/usr.bin/tmux/tmux.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: tmux.c,v 1.75 2010/04/28 18:22:32 nicm Exp $ */
+/* $OpenBSD: tmux.c,v 1.76 2010/05/03 16:06:32 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -18,6 +18,7 @@
#include <sys/types.h>
#include <sys/stat.h>
+#include <sys/wait.h>
#include <errno.h>
#include <event.h>
@@ -41,6 +42,8 @@ struct options global_s_options; /* session options */
struct options global_w_options; /* window options */
struct environ global_environ;
+struct event_base *ev_base;
+
int debug_level;
time_t start_time;
char *socket_path;
@@ -58,11 +61,8 @@ char *makesockpath(const char *);
__dead void shell_exec(const char *, const char *);
struct imsgbuf *main_ibuf;
-struct event main_ev_sigterm;
int main_exitval;
-void main_set_signals(void);
-void main_clear_signals(void);
void main_signal(int, short, unused void *);
void main_callback(int, short, void *);
void main_dispatch(const char *);
@@ -238,7 +238,6 @@ main(int argc, char **argv)
struct keylist *keylist;
struct env_data envdata;
struct msg_command_data cmddata;
- struct sigaction sigact;
char *s, *shellcmd, *path, *label, *home, *cause;
char cwd[MAXPATHLEN], **var;
void *buf;
@@ -538,23 +537,21 @@ main(int argc, char **argv)
exit(1);
}
- /* Catch SIGCHLD to avoid a zombie when starting the server. */
- memset(&sigact, 0, sizeof sigact);
- sigemptyset(&sigact.sa_mask);
- sigact.sa_handler = SIG_IGN;
- if (sigaction(SIGCHLD, &sigact, NULL) != 0)
- fatal("sigaction failed");
-
/* Initialise the client socket/start the server. */
if ((main_ibuf = client_init(path, cmdflags, flags)) == NULL)
exit(1);
xfree(path);
- event_init();
+ ev_base = event_init();
- imsg_compose(main_ibuf, msg, PROTOCOL_VERSION, -1, -1, buf, len);
+ set_signals(main_signal);
- main_set_signals();
+ /* Initialise the client socket/start the server. */
+ if ((main_ibuf = client_init(path, cmdflags, flags)) == NULL)
+ exit(1);
+ xfree(path);
+
+ imsg_compose(main_ibuf, msg, PROTOCOL_VERSION, -1, -1, buf, len);
events = EV_READ;
if (main_ibuf->w.queued > 0)
@@ -564,58 +561,11 @@ main(int argc, char **argv)
main_exitval = 0;
event_dispatch();
- main_clear_signals();
+ clear_signals();
client_main(); /* doesn't return */
}
-void
-main_set_signals(void)
-{
- struct sigaction sigact;
-
- memset(&sigact, 0, sizeof sigact);
- sigemptyset(&sigact.sa_mask);
- sigact.sa_flags = SA_RESTART;
- sigact.sa_handler = SIG_IGN;
- if (sigaction(SIGINT, &sigact, NULL) != 0)
- fatal("sigaction failed");
- if (sigaction(SIGPIPE, &sigact, NULL) != 0)
- fatal("sigaction failed");
- if (sigaction(SIGUSR1, &sigact, NULL) != 0)
- fatal("sigaction failed");
- if (sigaction(SIGUSR2, &sigact, NULL) != 0)
- fatal("sigaction failed");
- if (sigaction(SIGTSTP, &sigact, NULL) != 0)
- fatal("sigaction failed");
-
- signal_set(&main_ev_sigterm, SIGTERM, main_signal, NULL);
- signal_add(&main_ev_sigterm, NULL);
-}
-
-void
-main_clear_signals(void)
-{
- struct sigaction sigact;
-
- memset(&sigact, 0, sizeof sigact);
- sigemptyset(&sigact.sa_mask);
- sigact.sa_flags = SA_RESTART;
- sigact.sa_handler = SIG_DFL;
- if (sigaction(SIGINT, &sigact, NULL) != 0)
- fatal("sigaction failed");
- if (sigaction(SIGPIPE, &sigact, NULL) != 0)
- fatal("sigaction failed");
- if (sigaction(SIGUSR1, &sigact, NULL) != 0)
- fatal("sigaction failed");
- if (sigaction(SIGUSR2, &sigact, NULL) != 0)
- fatal("sigaction failed");
- if (sigaction(SIGTSTP, &sigact, NULL) != 0)
- fatal("sigaction failed");
-
- event_del(&main_ev_sigterm);
-}
-
/* ARGSUSED */
void
main_signal(int sig, unused short events, unused void *data)
@@ -623,6 +573,9 @@ main_signal(int sig, unused short events, unused void *data)
switch (sig) {
case SIGTERM:
exit(1);
+ case SIGCHLD:
+ waitpid(WAIT_ANY, NULL, WNOHANG);
+ break;
}
}
@@ -701,7 +654,7 @@ main_dispatch(const char *shellcmd)
memcpy(&shelldata, imsg.data, sizeof shelldata);
shelldata.shell[(sizeof shelldata.shell) - 1] = '\0';
- main_clear_signals();
+ clear_signals();
shell_exec(shelldata.shell, shellcmd);
default:
diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h
index 8877b8848a8..f65e6629974 100644
--- a/usr.bin/tmux/tmux.h
+++ b/usr.bin/tmux/tmux.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: tmux.h,v 1.216 2010/04/06 21:35:44 nicm Exp $ */
+/* $OpenBSD: tmux.h,v 1.217 2010/05/03 16:06:32 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -1253,6 +1253,7 @@ extern struct options global_options;
extern struct options global_s_options;
extern struct options global_w_options;
extern struct environ global_environ;
+extern struct event_base *ev_base;
extern char *cfg_file;
extern int debug_level;
extern int be_quiet;
@@ -1583,8 +1584,6 @@ const char *key_string_lookup_key(int);
extern struct clients clients;
extern struct clients dead_clients;
int server_start(char *);
-void server_signal_set(void);
-void server_signal_clear(void);
void server_update_socket(void);
/* server-client.c */
@@ -1900,6 +1899,10 @@ void window_choose_ready(struct window_pane *,
void queue_window_name(struct window *);
char *default_window_name(struct window *);
+/* signal.c */
+void set_signals(void(*)(int, short, void *));
+void clear_signals(void);
+
/* session.c */
extern struct sessions sessions;
extern struct sessions dead_sessions;
diff --git a/usr.bin/tmux/window.c b/usr.bin/tmux/window.c
index 47f705a43a0..73506aa8726 100644
--- a/usr.bin/tmux/window.c
+++ b/usr.bin/tmux/window.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: window.c,v 1.47 2010/04/04 19:02:09 nicm Exp $ */
+/* $OpenBSD: window.c,v 1.48 2010/05/03 16:06:32 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -529,7 +529,7 @@ window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell,
environ_push(env);
- server_signal_clear();
+ clear_signals();
log_close();
if (*wp->cmd != '\0') {