From: tedu Date: Fri, 16 Oct 2015 20:12:06 +0000 (+0000) Subject: naddy would like the child to exit when the parent dies. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=c452184f6a6e3c19f4b2f829a2d3ff97c12b3910;p=openbsd naddy would like the child to exit when the parent dies. hook up a pipe between them and watch for eof in the child. --- diff --git a/usr.sbin/rebound/rebound.c b/usr.sbin/rebound/rebound.c index dcad25a35c1..2c30a28530d 100644 --- a/usr.sbin/rebound/rebound.c +++ b/usr.sbin/rebound/rebound.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rebound.c,v 1.23 2015/10/16 18:47:52 tedu Exp $ */ +/* $OpenBSD: rebound.c,v 1.24 2015/10/16 20:12:06 tedu Exp $ */ /* * Copyright (c) 2015 Ted Unangst * @@ -356,7 +356,7 @@ readconfig(FILE *conf, struct sockaddr_storage *remoteaddr) } static int -launch(const char *confname, int ud, int ld, int kq) +launch(const char *confname, int ud, int ld, int kq, int *pfd) { struct sockaddr_storage remoteaddr; struct kevent chlist[2], kev[4]; @@ -377,6 +377,7 @@ launch(const char *confname, int ud, int ld, int kq) if (!debug) { if ((child = fork())) return child; + close(pfd[1]); } if (!(pwd = getpwnam("_rebound"))) @@ -406,7 +407,8 @@ launch(const char *confname, int ud, int ld, int kq) EV_SET(&kev[0], ud, EVFILT_READ, EV_ADD, 0, 0, NULL); EV_SET(&kev[1], ld, EVFILT_READ, EV_ADD, 0, 0, NULL); EV_SET(&kev[2], SIGHUP, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL); - kevent(kq, kev, 3, NULL, 0, NULL); + EV_SET(&kev[3], pfd[0], EVFILT_READ, EV_ADD, 0, 0, NULL); + kevent(kq, kev, 4, NULL, 0, NULL); signal(SIGHUP, SIG_IGN); logmsg(LOG_INFO, "worker process going to work"); while (1) { @@ -420,6 +422,11 @@ launch(const char *confname, int ud, int ld, int kq) if (kev[i].filter == EVFILT_SIGNAL) { logmsg(LOG_INFO, "hupped, exiting"); exit(0); + } else if (kev[i].ident == pfd[0]) { + if (kev[i].flags & EV_EOF) { + logmsg(LOG_INFO, "parent died"); + exit(0); + } } else if (kev[i].ident == ud) { req = newrequest(ud, (struct sockaddr *)&remoteaddr); @@ -515,6 +522,7 @@ main(int argc, char **argv) struct sockaddr_in bindaddr; int r, kq, ld, ud, ch; int one; + int pfd[2]; int childdead, hupped; pid_t child; struct kevent kev; @@ -570,8 +578,10 @@ main(int argc, char **argv) if (listen(ld, 10) == -1) err(1, "listen"); + pipe(pfd); + if (debug) { - launch(conffile, ud, ld, -1); + launch(conffile, ud, ld, -1, pfd); return 1; } @@ -583,7 +593,7 @@ main(int argc, char **argv) while (1) { hupped = 0; childdead = 0; - child = launch(conffile, ud, ld, kq); + child = launch(conffile, ud, ld, kq, pfd); if (child == -1) logerr("failed to launch");