diff options
author | Nicholas Marriott <nicm@cvs.openbsd.org> | 2017-07-12 09:24:18 +0000 |
---|---|---|
committer | Nicholas Marriott <nicm@cvs.openbsd.org> | 2017-07-12 09:24:18 +0000 |
commit | a0abd8f9b95def0ebf3cdf38b71291594a970daf (patch) | |
tree | cab5f2792e4009b1bbea7e92a956d41df55504b1 /usr.bin/tmux/proc.c | |
parent | c9b01ab256541fdc54b723b8163431cc71fa516d (diff) |
Move signal code into proc.c.
Diffstat (limited to 'usr.bin/tmux/proc.c')
-rw-r--r-- | usr.bin/tmux/proc.c | 91 |
1 files changed, 68 insertions, 23 deletions
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) |