From: Vincent Bernat Date: Sun, 16 Nov 2008 20:38:30 +0000 (+0100) Subject: Fork earlier (before monitor creation); otherwise, the unprivileged X-Git-Tag: 0.2~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=eac2f38aab653d8abf7c2efceb88daabb537a1dc;p=thirdparty%2Flldpd.git Fork earlier (before monitor creation); otherwise, the unprivileged process will not be the child of the monitor after forking the monitor. --- diff --git a/src/lldpd.c b/src/lldpd.c index 98ed8d48..812e82fb 100644 --- a/src/lldpd.c +++ b/src/lldpd.c @@ -1349,6 +1349,22 @@ main(int argc, char *argv[]) log_init(debug); + if (!debug) { + int pid; + char *spid; + if (daemon(0, 0) != 0) + fatal("failed to detach daemon"); + if ((pid = open(LLDPD_PID_FILE, + O_TRUNC | O_CREAT | O_WRONLY)) == -1) + fatal("unable to open pid file " LLDPD_PID_FILE); + if (asprintf(&spid, "%d\n", getpid()) == -1) + fatal("unable to create pid file " LLDPD_PID_FILE); + if (write(pid, spid, strlen(spid)) == -1) + fatal("unable to write pid file " LLDPD_PID_FILE); + free(spid); + close(pid); + } + priv_init(PRIVSEP_CHROOT); if (probe == 0) probe = LLDPD_TTL; @@ -1398,9 +1414,6 @@ main(int argc, char *argv[]) TAILQ_INIT(&cfg->g_clients); gcfg = cfg; - if (!debug) { - priv_fork(); - } if (atexit(lldpd_exit) != 0) { close(cfg->g_ctl); priv_ctl_cleanup(); diff --git a/src/lldpd.h b/src/lldpd.h index f9ee0a6e..59cfdfe4 100644 --- a/src/lldpd.h +++ b/src/lldpd.h @@ -332,7 +332,6 @@ void client_handle_shutdown(struct lldpd *, struct hmsg *, /* priv.c */ void priv_init(char*); -void priv_fork(); int priv_ctl_create(); void priv_ctl_cleanup(); char *priv_gethostbyname(); diff --git a/src/priv.c b/src/priv.c index c74ddbe1..82444e42 100644 --- a/src/priv.c +++ b/src/priv.c @@ -42,7 +42,6 @@ enum { PRIV_PING, - PRIV_FORK, PRIV_CREATE_CTL_SOCKET, PRIV_DELETE_CTL_SOCKET, PRIV_GET_HOSTNAME, @@ -77,16 +76,6 @@ priv_ping() LLOG_DEBUG("monitor ready"); } -/* Proxy for fork */ -void -priv_fork() -{ - int cmd, rc; - cmd = PRIV_FORK; - must_write(remote, &cmd, sizeof(int)); - must_read(remote, &rc, sizeof(int)); -} - /* Proxy for ctl_create, no argument since this is the monitor that decides the * location of the socket */ int @@ -207,27 +196,6 @@ asroot_ping() must_write(remote, &rc, sizeof(int)); } -void -asroot_fork() -{ - int pid; - char *spid; - if (daemon(0, 0) != 0) - fatal("[priv]: failed to detach daemon"); - if ((pid = open(LLDPD_PID_FILE, - O_TRUNC | O_CREAT | O_WRONLY)) == -1) - fatal("[priv]: unable to open pid file " LLDPD_PID_FILE); - if (asprintf(&spid, "%d\n", getpid()) == -1) - fatal("[priv]: unable to create pid file " LLDPD_PID_FILE); - if (write(pid, spid, strlen(spid)) == -1) - fatal("[priv]: unable to write pid file " LLDPD_PID_FILE); - free(spid); - close(pid); - - /* Ack */ - must_write(remote, &pid, sizeof(int)); -} - void asroot_ctl_create() { @@ -462,7 +430,6 @@ struct dispatch_actions { struct dispatch_actions actions[] = { {PRIV_PING, asroot_ping}, - {PRIV_FORK, asroot_fork}, {PRIV_CREATE_CTL_SOCKET, asroot_ctl_create}, {PRIV_DELETE_CTL_SOCKET, asroot_ctl_cleanup}, {PRIV_GET_HOSTNAME, asroot_gethostbyname},