diff options
-rw-r--r-- | usr.bin/tmux/Makefile | 3 | ||||
-rw-r--r-- | usr.bin/tmux/client.c | 11 | ||||
-rw-r--r-- | usr.bin/tmux/cmd-pipe-pane.c | 4 | ||||
-rw-r--r-- | usr.bin/tmux/job.c | 4 | ||||
-rw-r--r-- | usr.bin/tmux/proc.c | 91 | ||||
-rw-r--r-- | usr.bin/tmux/server.c | 21 | ||||
-rw-r--r-- | usr.bin/tmux/signal.c | 111 | ||||
-rw-r--r-- | usr.bin/tmux/tmux.h | 14 | ||||
-rw-r--r-- | usr.bin/tmux/window.c | 4 |
9 files changed, 104 insertions, 159 deletions
diff --git a/usr.bin/tmux/Makefile b/usr.bin/tmux/Makefile index 1d3fca65c3e..7742621e219 100644 --- a/usr.bin/tmux/Makefile +++ b/usr.bin/tmux/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.88 2017/05/30 21:44:59 nicm Exp $ +# $OpenBSD: Makefile,v 1.89 2017/07/12 09:24:17 nicm Exp $ PROG= tmux SRCS= alerts.c \ @@ -102,7 +102,6 @@ SRCS= alerts.c \ server-fn.c \ server.c \ session.c \ - signal.c \ status.c \ style.c \ tmux.c \ diff --git a/usr.bin/tmux/client.c b/usr.bin/tmux/client.c index 85d7d93a1f9..3dced4d95af 100644 --- a/usr.bin/tmux/client.c +++ b/usr.bin/tmux/client.c @@ -1,4 +1,4 @@ -/* $OpenBSD: client.c,v 1.121 2017/07/12 09:21:25 nicm Exp $ */ +/* $OpenBSD: client.c,v 1.122 2017/07/12 09:24:17 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> @@ -157,7 +157,7 @@ retry: close(lockfd); return (-1); } - fd = server_start(base, lockfd, lockfile); + fd = server_start(client_proc, base, lockfd, lockfile); } if (locked && lockfd >= 0) { @@ -261,7 +261,8 @@ client_main(struct event_base *base, int argc, char **argv, int flags) } /* Create client process structure (starts logging). */ - client_proc = proc_start("client", base, 0, client_signal); + client_proc = proc_start("client"); + proc_set_signals(client_proc, client_signal); /* Initialize the client socket and start the server. */ fd = client_connect(base, socket_path, cmdflags & CMD_STARTSERVER); @@ -364,7 +365,7 @@ client_main(struct event_base *base, int argc, char **argv, int flags) if (client_exittype == MSG_EXEC) { if (client_flags & CLIENT_CONTROLCONTROL) tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio); - clear_signals(0); + proc_clear_signals(client_proc); client_exec(client_execshell, client_execcmd); } @@ -627,7 +628,7 @@ client_dispatch_wait(struct imsg *imsg) if (datalen == 0 || data[datalen - 1] != '\0') fatalx("bad MSG_SHELL string"); - clear_signals(0); + proc_clear_signals(client_proc); client_exec(data, shell_command); /* NOTREACHED */ case MSG_DETACH: diff --git a/usr.bin/tmux/cmd-pipe-pane.c b/usr.bin/tmux/cmd-pipe-pane.c index 501b858ced2..49f49b6c065 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.43 2017/07/03 08:16:03 nicm Exp $ */ +/* $OpenBSD: cmd-pipe-pane.c,v 1.44 2017/07/12 09:24:17 nicm Exp $ */ /* * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com> @@ -111,7 +111,7 @@ cmd_pipe_pane_exec(struct cmd *self, struct cmdq_item *item) case 0: /* Child process. */ close(pipe_fd[0]); - clear_signals(1); + proc_clear_signals(server_proc); 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 7d2432b316f..028706d89bb 100644 --- a/usr.bin/tmux/job.c +++ b/usr.bin/tmux/job.c @@ -1,4 +1,4 @@ -/* $OpenBSD: job.c,v 1.45 2017/05/31 17:56:48 nicm Exp $ */ +/* $OpenBSD: job.c,v 1.46 2017/07/12 09:24:17 nicm Exp $ */ /* * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com> @@ -68,7 +68,7 @@ job_run(const char *cmd, struct session *s, const char *cwd, close(out[1]); return (NULL); case 0: /* child */ - clear_signals(1); + proc_clear_signals(server_proc); if (cwd == NULL || chdir(cwd) != 0) { if ((home = find_home()) == NULL || chdir(home) != 0) diff --git a/usr.bin/tmux/proc.c b/usr.bin/tmux/proc.c index 0d9625314e7..693d6cbb87e 100644 --- a/usr.bin/tmux/proc.c +++ b/usr.bin/tmux/proc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: proc.c,v 1.11 2017/07/12 09:07:52 nicm Exp $ */ +/* $OpenBSD: proc.c,v 1.12 2017/07/12 09:24:17 nicm Exp $ */ /* * Copyright (c) 2015 Nicholas Marriott <nicholas.marriott@gmail.com> @@ -24,6 +24,7 @@ #include <errno.h> #include <event.h> #include <imsg.h> +#include <signal.h> #include <stdlib.h> #include <string.h> #include <unistd.h> @@ -35,6 +36,14 @@ struct tmuxproc { int exit; void (*signalcb)(int); + + struct event ev_sighup; + struct event ev_sigchld; + struct event ev_sigcont; + struct event ev_sigterm; + struct event ev_sigusr1; + struct event ev_sigusr2; + struct event ev_sigwinch; }; struct tmuxpeer { @@ -162,29 +171,11 @@ proc_send(struct tmuxpeer *peer, enum msgtype type, int fd, const void *buf, } struct tmuxproc * -proc_start(const char *name, struct event_base *base, int forkflag, - void (*signalcb)(int)) +proc_start(const char *name) { struct tmuxproc *tp; struct utsname u; - if (forkflag) { - switch (fork()) { - case -1: - fatal("fork failed"); - case 0: - break; - default: - return (NULL); - } - if (daemon(1, 0) != 0) - fatal("daemon failed"); - - clear_signals(0); - if (event_reinit(base) != 0) - fatalx("event_reinit failed"); - } - log_open(name); setproctitle("%s (%s)", name, socket_path); @@ -199,9 +190,6 @@ proc_start(const char *name, struct event_base *base, int forkflag, tp = xcalloc(1, sizeof *tp); tp->name = xstrdup(name); - tp->signalcb = signalcb; - set_signals(proc_signal_cb, tp); - return (tp); } @@ -221,6 +209,63 @@ proc_exit(struct tmuxproc *tp) tp->exit = 1; } +void +proc_set_signals(struct tmuxproc *tp, void (*signalcb)(int)) +{ + struct sigaction sa; + + tp->signalcb = signalcb; + + memset(&sa, 0, sizeof sa); + sigemptyset(&sa.sa_mask); + sa.sa_flags = SA_RESTART; + sa.sa_handler = SIG_IGN; + + sigaction(SIGINT, &sa, NULL); + sigaction(SIGPIPE, &sa, NULL); + sigaction(SIGUSR2, &sa, NULL); + sigaction(SIGTSTP, &sa, NULL); + + signal_set(&tp->ev_sighup, SIGHUP, proc_signal_cb, tp); + signal_add(&tp->ev_sighup, NULL); + signal_set(&tp->ev_sigchld, SIGCHLD, proc_signal_cb, tp); + signal_add(&tp->ev_sigchld, NULL); + signal_set(&tp->ev_sigcont, SIGCONT, proc_signal_cb, tp); + signal_add(&tp->ev_sigcont, NULL); + signal_set(&tp->ev_sigterm, SIGTERM, proc_signal_cb, tp); + signal_add(&tp->ev_sigterm, NULL); + signal_set(&tp->ev_sigusr1, SIGUSR1, proc_signal_cb, tp); + signal_add(&tp->ev_sigusr1, NULL); + signal_set(&tp->ev_sigusr2, SIGUSR2, proc_signal_cb, tp); + signal_add(&tp->ev_sigusr2, NULL); + signal_set(&tp->ev_sigwinch, SIGWINCH, proc_signal_cb, tp); + signal_add(&tp->ev_sigwinch, NULL); +} + +void +proc_clear_signals(struct tmuxproc *tp) +{ + struct sigaction sa; + + memset(&sa, 0, sizeof sa); + sigemptyset(&sa.sa_mask); + sa.sa_flags = SA_RESTART; + sa.sa_handler = SIG_DFL; + + sigaction(SIGINT, &sa, NULL); + sigaction(SIGPIPE, &sa, NULL); + sigaction(SIGUSR2, &sa, NULL); + sigaction(SIGTSTP, &sa, NULL); + + event_del(&tp->ev_sighup); + event_del(&tp->ev_sigchld); + event_del(&tp->ev_sigcont); + event_del(&tp->ev_sigterm); + event_del(&tp->ev_sigusr1); + event_del(&tp->ev_sigusr2); + event_del(&tp->ev_sigwinch); +} + struct tmuxpeer * proc_add_peer(struct tmuxproc *tp, int fd, void (*dispatchcb)(struct imsg *, void *), void *arg) diff --git a/usr.bin/tmux/server.c b/usr.bin/tmux/server.c index 1b73f315c1f..2ca53f77839 100644 --- a/usr.bin/tmux/server.c +++ b/usr.bin/tmux/server.c @@ -1,4 +1,4 @@ -/* $OpenBSD: server.c,v 1.173 2017/07/09 22:33:09 nicm Exp $ */ +/* $OpenBSD: server.c,v 1.174 2017/07/12 09:24:17 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> @@ -136,7 +136,8 @@ server_create_socket(void) /* Fork new server. */ int -server_start(struct event_base *base, int lockfd, char *lockfile) +server_start(struct tmuxproc *client, struct event_base *base, int lockfd, + char *lockfile) { int pair[2]; struct job *job; @@ -144,13 +145,25 @@ server_start(struct event_base *base, int lockfd, char *lockfile) if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0) fatal("socketpair failed"); - server_proc = proc_start("server", base, 1, server_signal); - if (server_proc == NULL) { + switch (fork()) { + case -1: + fatal("fork failed"); + case 0: + break; + default: close(pair[1]); return (pair[0]); } close(pair[0]); + if (daemon(1, 0) != 0) + fatal("daemon failed"); + proc_clear_signals(client); + if (event_reinit(base) != 0) + fatalx("event_reinit failed"); + server_proc = proc_start("server"); + proc_set_signals(server_proc, server_signal); + if (log_get_level() > 1) tty_create_log(); if (pledge("stdio rpath wpath cpath fattr unix getpw recvfd proc exec " diff --git a/usr.bin/tmux/signal.c b/usr.bin/tmux/signal.c deleted file mode 100644 index e34c57f0d3c..00000000000 --- a/usr.bin/tmux/signal.c +++ /dev/null @@ -1,111 +0,0 @@ -/* $OpenBSD: signal.c,v 1.11 2017/06/04 08:25:57 nicm Exp $ */ - -/* - * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> - * 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 <string.h> -#include <signal.h> - -#include "tmux.h" - -static struct event ev_sighup; -static struct event ev_sigchld; -static struct event ev_sigcont; -static struct event ev_sigterm; -static struct event ev_sigusr1; -static struct event ev_sigusr2; -static struct event ev_sigwinch; - -void -set_signals(void (*handler)(int, short, void *), void *arg) -{ - 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"); - - signal_set(&ev_sighup, SIGHUP, handler, arg); - signal_add(&ev_sighup, NULL); - signal_set(&ev_sigchld, SIGCHLD, handler, arg); - signal_add(&ev_sigchld, NULL); - signal_set(&ev_sigcont, SIGCONT, handler, arg); - signal_add(&ev_sigcont, NULL); - signal_set(&ev_sigterm, SIGTERM, handler, arg); - signal_add(&ev_sigterm, NULL); - signal_set(&ev_sigusr1, SIGUSR1, handler, arg); - signal_add(&ev_sigusr1, NULL); - signal_set(&ev_sigusr2, SIGUSR2, handler, arg); - signal_add(&ev_sigusr2, NULL); - signal_set(&ev_sigwinch, SIGWINCH, handler, arg); - signal_add(&ev_sigwinch, NULL); -} - -void -clear_signals(int after_fork) -{ - 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 (after_fork) { - if (sigaction(SIGHUP, &sigact, NULL) != 0) - fatal("sigaction failed"); - if (sigaction(SIGCHLD, &sigact, NULL) != 0) - fatal("sigaction failed"); - if (sigaction(SIGCONT, &sigact, NULL) != 0) - fatal("sigaction failed"); - if (sigaction(SIGTERM, &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(SIGWINCH, &sigact, NULL) != 0) - fatal("sigaction failed"); - } else { - event_del(&ev_sighup); - event_del(&ev_sigchld); - event_del(&ev_sigcont); - event_del(&ev_sigterm); - event_del(&ev_sigusr1); - event_del(&ev_sigusr2); - event_del(&ev_sigwinch); - } -} diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h index 7140564e83b..ed4a46eb201 100644 --- a/usr.bin/tmux/tmux.h +++ b/usr.bin/tmux/tmux.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.h,v 1.794 2017/07/12 09:21:25 nicm Exp $ */ +/* $OpenBSD: tmux.h,v 1.795 2017/07/12 09:24:17 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> @@ -1485,6 +1485,7 @@ extern struct options *global_w_options; extern struct environ *global_environ; extern struct timeval start_time; extern const char *socket_path; +extern const char *shell_command; extern int ptm_fd; extern const char *shell_command; int areshell(const char *); @@ -1494,10 +1495,11 @@ const char *find_home(void); /* proc.c */ struct imsg; int proc_send(struct tmuxpeer *, enum msgtype, int, const void *, size_t); -struct tmuxproc *proc_start(const char *, struct event_base *, int, - void (*)(int)); +struct tmuxproc *proc_start(const char *); void proc_loop(struct tmuxproc *, int (*)(void)); void proc_exit(struct tmuxproc *); +void proc_set_signals(struct tmuxproc *, void(*)(int)); +void proc_clear_signals(struct tmuxproc *); struct tmuxpeer *proc_add_peer(struct tmuxproc *, int, void (*)(struct imsg *, void *), void *); void proc_remove_peer(struct tmuxpeer *); @@ -1857,7 +1859,7 @@ void server_clear_marked(void); int server_is_marked(struct session *, struct winlink *, struct window_pane *); int server_check_marked(void); -int server_start(struct event_base *, int, char *); +int server_start(struct tmuxproc *, struct event_base *, int, char *); void server_update_socket(void); void server_add_accept(int); @@ -2257,10 +2259,6 @@ void check_window_name(struct window *); char *default_window_name(struct window *); char *parse_window_name(const char *); -/* signal.c */ -void set_signals(void(*)(int, short, void *), void *); -void clear_signals(int); - /* control.c */ void control_callback(struct client *, int, void *); void printflike(2, 3) control_write(struct client *, const char *, ...); diff --git a/usr.bin/tmux/window.c b/usr.bin/tmux/window.c index f2f2d139ca3..083aaca03bb 100644 --- a/usr.bin/tmux/window.c +++ b/usr.bin/tmux/window.c @@ -1,4 +1,4 @@ -/* $OpenBSD: window.c,v 1.201 2017/07/03 12:38:50 nicm Exp $ */ +/* $OpenBSD: window.c,v 1.202 2017/07/12 09:24:17 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> @@ -943,7 +943,7 @@ window_pane_spawn(struct window_pane *wp, int argc, char **argv, environ_set(env, "TMUX_PANE", "%%%u", wp->id); environ_push(env); - clear_signals(1); + proc_clear_signals(server_proc); log_close(); setenv("SHELL", wp->shell, 1); |