diff options
author | Reyk Floeter <reyk@cvs.openbsd.org> | 2016-09-02 11:51:51 +0000 |
---|---|---|
committer | Reyk Floeter <reyk@cvs.openbsd.org> | 2016-09-02 11:51:51 +0000 |
commit | a71eba83bff47c5e296f087fc0d3a9aa362bbfb5 (patch) | |
tree | fcec88e2d493a2854b95cfd321726cb2901ec37a /usr.sbin | |
parent | e6e8d4ae80736a4441a87c35e6ea4743afe88f28 (diff) |
Terminate relayd using the socket status instead of watching SIGCHLD
or killing child processes. - Based on rzalamena@'s diff for httpd.
OK deraadt@ rzalamena@
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/relayd/ca.c | 6 | ||||
-rw-r--r-- | usr.sbin/relayd/hce.c | 6 | ||||
-rw-r--r-- | usr.sbin/relayd/pfe.c | 6 | ||||
-rw-r--r-- | usr.sbin/relayd/proc.c | 47 | ||||
-rw-r--r-- | usr.sbin/relayd/relay.c | 9 | ||||
-rw-r--r-- | usr.sbin/relayd/relayd.c | 51 | ||||
-rw-r--r-- | usr.sbin/relayd/relayd.h | 15 |
7 files changed, 51 insertions, 89 deletions
diff --git a/usr.sbin/relayd/ca.c b/usr.sbin/relayd/ca.c index 5fbcd3605ce..cf98615317c 100644 --- a/usr.sbin/relayd/ca.c +++ b/usr.sbin/relayd/ca.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ca.c,v 1.17 2016/09/01 10:40:38 claudio Exp $ */ +/* $OpenBSD: ca.c,v 1.18 2016/09/02 11:51:49 reyk Exp $ */ /* * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org> @@ -63,12 +63,12 @@ static struct privsep_proc procs[] = { { "relay", PROC_RELAY, ca_dispatch_relay }, }; -pid_t +void ca(struct privsep *ps, struct privsep_proc *p) { env = ps->ps_env; - return (proc_run(ps, p, procs, nitems(procs), ca_init, NULL)); + proc_run(ps, p, procs, nitems(procs), ca_init, NULL); } void diff --git a/usr.sbin/relayd/hce.c b/usr.sbin/relayd/hce.c index 3772faa405d..84b81154574 100644 --- a/usr.sbin/relayd/hce.c +++ b/usr.sbin/relayd/hce.c @@ -1,4 +1,4 @@ -/* $OpenBSD: hce.c,v 1.72 2016/01/11 21:31:42 benno Exp $ */ +/* $OpenBSD: hce.c,v 1.73 2016/09/02 11:51:49 reyk Exp $ */ /* * Copyright (c) 2006 Pierre-Yves Ritschard <pyr@openbsd.org> @@ -48,7 +48,7 @@ static struct privsep_proc procs[] = { { "relay", PROC_RELAY, hce_dispatch_relay }, }; -pid_t +void hce(struct privsep *ps, struct privsep_proc *p) { env = ps->ps_env; @@ -56,7 +56,7 @@ hce(struct privsep *ps, struct privsep_proc *p) /* this is needed for icmp tests */ icmp_init(env); - return (proc_run(ps, p, procs, nitems(procs), hce_init, NULL)); + proc_run(ps, p, procs, nitems(procs), hce_init, NULL); } void diff --git a/usr.sbin/relayd/pfe.c b/usr.sbin/relayd/pfe.c index 36898d010a6..2ed98b81ed5 100644 --- a/usr.sbin/relayd/pfe.c +++ b/usr.sbin/relayd/pfe.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pfe.c,v 1.83 2015/12/03 16:12:16 benno Exp $ */ +/* $OpenBSD: pfe.c,v 1.84 2016/09/02 11:51:49 reyk Exp $ */ /* * Copyright (c) 2006 Pierre-Yves Ritschard <pyr@openbsd.org> @@ -48,12 +48,12 @@ static struct privsep_proc procs[] = { { "hce", PROC_HCE, pfe_dispatch_hce } }; -pid_t +void pfe(struct privsep *ps, struct privsep_proc *p) { env = ps->ps_env; - return (proc_run(ps, p, procs, nitems(procs), pfe_init, NULL)); + proc_run(ps, p, procs, nitems(procs), pfe_init, NULL); } void diff --git a/usr.sbin/relayd/proc.c b/usr.sbin/relayd/proc.c index c498685daaa..02f4bd3444e 100644 --- a/usr.sbin/relayd/proc.c +++ b/usr.sbin/relayd/proc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: proc.c,v 1.27 2015/12/07 16:05:56 reyk Exp $ */ +/* $OpenBSD: proc.c,v 1.28 2016/09/02 11:51:49 reyk Exp $ */ /* * Copyright (c) 2010 - 2014 Reyk Floeter <reyk@openbsd.org> @@ -103,7 +103,6 @@ proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc) privsep_process = PROC_PARENT; ps->ps_instances[PROC_PARENT] = 1; ps->ps_title[PROC_PARENT] = "parent"; - ps->ps_pid[PROC_PARENT] = getpid(); ps->ps_pp = &ps->ps_pipes[privsep_process][0]; for (i = 0; i < nproc; i++) { @@ -117,29 +116,45 @@ proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc) /* Engage! */ for (i = 0; i < nproc; i++) - ps->ps_pid[procs[i].p_id] = (*procs[i].p_init)(ps, &procs[i]); + (*procs[i].p_init)(ps, &procs[i]); } void proc_kill(struct privsep *ps) { + char *cause; pid_t pid; - unsigned int i; + int len, status; if (privsep_process != PROC_PARENT) return; - for (i = 0; i < PROC_MAX; i++) { - if (ps->ps_pid[i] == 0) - continue; - killpg(ps->ps_pid[i], SIGTERM); - } + proc_close(ps); do { - pid = waitpid(WAIT_ANY, NULL, 0); - } while (pid != -1 || (pid == -1 && errno == EINTR)); + pid = waitpid(WAIT_ANY, &status, 0); + if (pid <= 0) + continue; - proc_close(ps); + if (WIFSIGNALED(status)) { + len = asprintf(&cause, "terminated; signal %d", + WTERMSIG(status)); + } else if (WIFEXITED(status)) { + if (WEXITSTATUS(status) != 0) + len = asprintf(&cause, "exited abnormally"); + else + len = 0; + } else + len = -1; + + if (len == 0) { + /* child exited OK, don't print a warning message */ + } else if (len != -1) { + log_warnx("lost child: pid %u %s", pid, cause); + free(cause); + } else + log_warnx("lost child: pid %u", pid); + } while (pid != -1 || (pid == -1 && errno == EINTR)); } void @@ -326,7 +341,7 @@ proc_sig_handler(int sig, short event, void *arg) } } -pid_t +void proc_run(struct privsep *ps, struct privsep_proc *p, struct privsep_proc *procs, unsigned int nproc, void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg) @@ -338,7 +353,7 @@ proc_run(struct privsep *ps, struct privsep_proc *p, unsigned int n; if (ps->ps_noaction) - return (0); + return; proc_open(ps, p, procs, nproc); @@ -353,7 +368,7 @@ proc_run(struct privsep *ps, struct privsep_proc *p, setpgid(0, 0); break; default: - return (pid); + return; } pw = ps->ps_pw; @@ -432,8 +447,6 @@ proc_run(struct privsep *ps, struct privsep_proc *p, event_dispatch(); proc_shutdown(p); - - return (0); } void diff --git a/usr.sbin/relayd/relay.c b/usr.sbin/relayd/relay.c index 33a49782f50..1c809ebec62 100644 --- a/usr.sbin/relayd/relay.c +++ b/usr.sbin/relayd/relay.c @@ -1,4 +1,4 @@ -/* $OpenBSD: relay.c,v 1.207 2016/09/01 10:49:48 claudio Exp $ */ +/* $OpenBSD: relay.c,v 1.208 2016/09/02 11:51:49 reyk Exp $ */ /* * Copyright (c) 2006 - 2014 Reyk Floeter <reyk@openbsd.org> @@ -108,15 +108,12 @@ static struct privsep_proc procs[] = { { "hce", PROC_HCE, relay_dispatch_hce }, }; -pid_t +void relay(struct privsep *ps, struct privsep_proc *p) { - pid_t pid; - env = ps->ps_env; - pid = proc_run(ps, p, procs, nitems(procs), relay_init, NULL); + proc_run(ps, p, procs, nitems(procs), relay_init, NULL); relay_http(env); - return (pid); } void diff --git a/usr.sbin/relayd/relayd.c b/usr.sbin/relayd/relayd.c index abb226d74af..4d8fbb292f2 100644 --- a/usr.sbin/relayd/relayd.c +++ b/usr.sbin/relayd/relayd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: relayd.c,v 1.156 2016/09/01 10:49:48 claudio Exp $ */ +/* $OpenBSD: relayd.c,v 1.157 2016/09/02 11:51:50 reyk Exp $ */ /* * Copyright (c) 2007 - 2016 Reyk Floeter <reyk@openbsd.org> @@ -78,56 +78,11 @@ void parent_sig_handler(int sig, short event, void *arg) { struct privsep *ps = arg; - int die = 0, status, fail, id; - pid_t pid; - char *cause; switch (sig) { case SIGTERM: case SIGINT: - die = 1; - /* FALLTHROUGH */ - case SIGCHLD: - do { - int len; - - pid = waitpid(WAIT_ANY, &status, WNOHANG); - if (pid <= 0) - continue; - - fail = 0; - if (WIFSIGNALED(status)) { - fail = 1; - len = asprintf(&cause, "terminated; signal %d", - WTERMSIG(status)); - } else if (WIFEXITED(status)) { - if (WEXITSTATUS(status) != 0) { - fail = 1; - len = asprintf(&cause, - "exited abnormally"); - } else - len = asprintf(&cause, "exited okay"); - } else - fatalx("unexpected cause of SIGCHLD"); - - if (len == -1) - fatal("asprintf"); - - die = 1; - - for (id = 0; id < PROC_MAX; id++) - if (pid == ps->ps_pid[id]) { - if (fail) - log_warnx("lost child: %s %s", - ps->ps_title[id], cause); - break; - } - - free(cause); - } while (pid > 0 || (pid == -1 && errno == EINTR)); - - if (die) - parent_shutdown(ps->ps_env); + parent_shutdown(ps->ps_env); break; case SIGHUP: log_info("%s: reload requested with SIGHUP", __func__); @@ -253,14 +208,12 @@ main(int argc, char *argv[]) signal_set(&ps->ps_evsigint, SIGINT, parent_sig_handler, ps); signal_set(&ps->ps_evsigterm, SIGTERM, parent_sig_handler, ps); - signal_set(&ps->ps_evsigchld, SIGCHLD, parent_sig_handler, ps); signal_set(&ps->ps_evsighup, SIGHUP, parent_sig_handler, ps); signal_set(&ps->ps_evsigpipe, SIGPIPE, parent_sig_handler, ps); signal_set(&ps->ps_evsigusr1, SIGUSR1, parent_sig_handler, ps); signal_add(&ps->ps_evsigint, NULL); signal_add(&ps->ps_evsigterm, NULL); - signal_add(&ps->ps_evsigchld, NULL); signal_add(&ps->ps_evsighup, NULL); signal_add(&ps->ps_evsigpipe, NULL); signal_add(&ps->ps_evsigusr1, NULL); diff --git a/usr.sbin/relayd/relayd.h b/usr.sbin/relayd/relayd.h index 31b3211fa5d..e713ec0e55e 100644 --- a/usr.sbin/relayd/relayd.h +++ b/usr.sbin/relayd/relayd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: relayd.h,v 1.227 2016/09/01 10:49:48 claudio Exp $ */ +/* $OpenBSD: relayd.h,v 1.228 2016/09/02 11:51:50 reyk Exp $ */ /* * Copyright (c) 2006 - 2016 Reyk Floeter <reyk@openbsd.org> @@ -1000,7 +1000,6 @@ struct privsep { struct imsgev *ps_ievs[PROC_MAX]; const char *ps_title[PROC_MAX]; - pid_t ps_pid[PROC_MAX]; u_int8_t ps_what[PROC_MAX]; u_int ps_instances[PROC_MAX]; @@ -1028,7 +1027,7 @@ struct privsep_proc { enum privsep_procid p_id; int (*p_cb)(int, struct privsep_proc *, struct imsg *); - pid_t (*p_init)(struct privsep *, + void (*p_init)(struct privsep *, struct privsep_proc *); void (*p_shutdown)(void); u_int p_instance; @@ -1128,7 +1127,7 @@ const char *printb_flags(const u_int32_t, const char *); void getmonotime(struct timeval *); /* pfe.c */ -pid_t pfe(struct privsep *, struct privsep_proc *); +void pfe(struct privsep *, struct privsep_proc *); void show(struct ctl_conn *); void show_sessions(struct ctl_conn *); int enable_rdr(struct ctl_conn *, struct ctl_id *); @@ -1155,11 +1154,11 @@ void sync_routes(struct relayd *, struct router *); int pfe_route(struct relayd *, struct ctl_netroute *); /* hce.c */ -pid_t hce(struct privsep *, struct privsep_proc *); +void hce(struct privsep *, struct privsep_proc *); void hce_notify_done(struct host *, enum host_error); /* relay.c */ -pid_t relay(struct privsep *, struct privsep_proc *); +void relay(struct privsep *, struct privsep_proc *); int relay_privinit(struct relay *); void relay_notify_done(struct host *, const char *); int relay_session_cmp(struct rsession *, struct rsession *); @@ -1252,7 +1251,7 @@ int ssl_ctx_fake_private_key(SSL_CTX *, const void *, size_t, char *, off_t, X509 **, EVP_PKEY **); /* ca.c */ -pid_t ca(struct privsep *, struct privsep_proc *); +void ca(struct privsep *, struct privsep_proc *); void ca_engine_init(struct relayd *); /* relayd.c */ @@ -1372,7 +1371,7 @@ void proc_init(struct privsep *, struct privsep_proc *, u_int); void proc_kill(struct privsep *); void proc_listen(struct privsep *, struct privsep_proc *, size_t); void proc_dispatch(int, short event, void *); -pid_t proc_run(struct privsep *, struct privsep_proc *, +void proc_run(struct privsep *, struct privsep_proc *, struct privsep_proc *, u_int, void (*)(struct privsep *, struct privsep_proc *, void *), void *); void proc_range(struct privsep *, enum privsep_procid, int *, int *); |