Move signal code into proc.c.

This commit is contained in:
nicm 2017-07-12 09:24:17 +00:00
parent ed3cfaafb2
commit 0453ad0146
9 changed files with 96 additions and 151 deletions

View File

@ -102,7 +102,6 @@ SRCS= alerts.c \
server-fn.c \
server.c \
session.c \
signal.c \
status.c \
style.c \
tmux.c \

View File

@ -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:

View File

@ -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);

2
job.c
View File

@ -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)

89
proc.c
View File

@ -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)

View File

@ -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 "

111
signal.c
View File

@ -1,111 +0,0 @@
/* $OpenBSD$ */
/*
* 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);
}
}

12
tmux.h
View File

@ -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 *, ...);

View File

@ -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);