summaryrefslogtreecommitdiff
path: root/usr.sbin
diff options
context:
space:
mode:
authorJun-ichiro itojun Hagino <itojun@cvs.openbsd.org>2002-01-11 03:51:09 +0000
committerJun-ichiro itojun Hagino <itojun@cvs.openbsd.org>2002-01-11 03:51:09 +0000
commit1e286bf79bcff9bd04964a6d4b7b0773a1e7afa6 (patch)
tree0fbc05365ca874a6326cdf81a3b8732218b12bfa /usr.sbin
parent9f1463a8c6c1b28e4c9707469ac02a119f30e7d6 (diff)
call daemon(3) prior to socket setups to avoid file descriptor close mistakes.
noted by markus, sync with kame
Diffstat (limited to 'usr.sbin')
-rw-r--r--usr.sbin/faithd/faithd.c26
-rw-r--r--usr.sbin/route6d/route6d.c27
-rw-r--r--usr.sbin/rtadvd/rtadvd.c5
-rw-r--r--usr.sbin/rtsold/rtsold.c26
4 files changed, 43 insertions, 41 deletions
diff --git a/usr.sbin/faithd/faithd.c b/usr.sbin/faithd/faithd.c
index 156dc9670d6..c9eaab84c7e 100644
--- a/usr.sbin/faithd/faithd.c
+++ b/usr.sbin/faithd/faithd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: faithd.c,v 1.16 2001/11/21 04:54:51 itojun Exp $ */
+/* $OpenBSD: faithd.c,v 1.17 2002/01/11 03:51:08 itojun Exp $ */
/* $KAME: faithd.c,v 1.40 2001/07/02 14:36:48 itojun Exp $ */
/*
@@ -319,6 +319,8 @@ daemon_main(int argc, char **argv)
break;
}
+ start_daemon();
+
/*
* Opening wild card socket for this service.
*/
@@ -330,17 +332,17 @@ daemon_main(int argc, char **argv)
hints.ai_protocol = 0;
error = getaddrinfo(NULL, service, &hints, &res);
if (error)
- exit_stderr("getaddrinfo: %s", gai_strerror(error));
+ exit_failure("getaddrinfo: %s", gai_strerror(error));
s_wld = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (s_wld == -1)
- exit_stderr("socket: %s", strerror(errno));
+ exit_failure("socket: %s", strerror(errno));
#ifdef IPV6_FAITH
if (res->ai_family == AF_INET6) {
error = setsockopt(s_wld, IPPROTO_IPV6, IPV6_FAITH, &on, sizeof(on));
if (error == -1)
- exit_stderr("setsockopt(IPV6_FAITH): %s",
+ exit_failure("setsockopt(IPV6_FAITH): %s",
strerror(errno));
}
#endif
@@ -349,7 +351,7 @@ daemon_main(int argc, char **argv)
if (res->ai_family == AF_INET) {
error = setsockopt(s_wld, IPPROTO_IP, IP_FAITH, &on, sizeof(on));
if (error == -1)
- exit_stderr("setsockopt(IP_FAITH): %s",
+ exit_failure("setsockopt(IP_FAITH): %s",
strerror(errno));
}
#endif
@@ -357,24 +359,24 @@ daemon_main(int argc, char **argv)
error = setsockopt(s_wld, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if (error == -1)
- exit_stderr("setsockopt(SO_REUSEADDR): %s", strerror(errno));
+ exit_failure("setsockopt(SO_REUSEADDR): %s", strerror(errno));
error = setsockopt(s_wld, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on));
if (error == -1)
- exit_stderr("setsockopt(SO_OOBINLINE): %s", strerror(errno));
+ exit_failure("setsockopt(SO_OOBINLINE): %s", strerror(errno));
error = bind(s_wld, (struct sockaddr *)res->ai_addr, res->ai_addrlen);
if (error == -1)
- exit_stderr("bind: %s", strerror(errno));
+ exit_failure("bind: %s", strerror(errno));
error = listen(s_wld, 5);
if (error == -1)
- exit_stderr("listen: %s", strerror(errno));
+ exit_failure("listen: %s", strerror(errno));
#ifdef USE_ROUTE
sockfd = socket(PF_ROUTE, SOCK_RAW, PF_UNSPEC);
if (sockfd < 0) {
- exit_stderr("socket(PF_ROUTE): %s", strerror(errno));
+ exit_failure("socket(PF_ROUTE): %s", strerror(errno));
/*NOTREACHED*/
}
#endif
@@ -383,8 +385,6 @@ daemon_main(int argc, char **argv)
* Everything is OK.
*/
- start_daemon();
-
snprintf(logname, sizeof(logname), "faithd %s", service);
snprintf(procname, sizeof(procname), "accepting port %s", service);
openlog(logname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
@@ -777,7 +777,7 @@ sig_child(int sig)
struct syslog_data sdata = SYSLOG_DATA_INIT;
pid_t pid;
- while ((pid = wait3(&status, WNOHANG, (struct rusage *)0)) != -1) {
+ while ((pid = wait3(&status, WNOHANG, (struct rusage *)0)) > 0) {
if (pid && WEXITSTATUS(status))
syslog_r(LOG_WARNING, &sdata,
"child %d exit status 0x%x", pid, status);
diff --git a/usr.sbin/route6d/route6d.c b/usr.sbin/route6d/route6d.c
index 6e110d3ddaa..48ed2e12590 100644
--- a/usr.sbin/route6d/route6d.c
+++ b/usr.sbin/route6d/route6d.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: route6d.c,v 1.21 2001/12/01 23:27:23 miod Exp $ */
+/* $OpenBSD: route6d.c,v 1.22 2002/01/11 03:51:08 itojun Exp $ */
/* $KAME: route6d.c,v 1.73 2001/09/05 01:12:34 itojun Exp $ */
/*
@@ -31,7 +31,7 @@
*/
#if 0
-static char _rcsid[] = "$OpenBSD: route6d.c,v 1.21 2001/12/01 23:27:23 miod Exp $";
+static char _rcsid[] = "$OpenBSD: route6d.c,v 1.22 2002/01/11 03:51:08 itojun Exp $";
#endif
#include <stdio.h>
@@ -349,6 +349,14 @@ main(argc, argv)
nflag = 1;
fprintf(stderr, "No kernel update is allowed\n");
}
+
+ if (dflag == 0) {
+ if (daemon(0, 0) < 0) {
+ fatal("daemon");
+ /*NOTREACHED*/
+ }
+ }
+
openlog(progname, LOG_NDELAY|LOG_PID, LOG_DAEMON);
logopened++;
@@ -384,21 +392,6 @@ main(argc, argv)
if (dflag)
ifrtdump(0);
- if (dflag == 0) {
-#if 1
- if (daemon(0, 0) < 0) {
- fatal("daemon");
- /*NOTREACHED*/
- }
-#else
- if (fork())
- exit(0);
- if (setsid() < 0) {
- fatal("setid");
- /*NOTREACHED*/
- }
-#endif
- }
pidfile(NULL);
if ((ripbuf = (struct rip6 *)malloc(RIP6_MAXMTU)) == NULL) {
diff --git a/usr.sbin/rtadvd/rtadvd.c b/usr.sbin/rtadvd/rtadvd.c
index f0234f89de5..83342fbcd61 100644
--- a/usr.sbin/rtadvd/rtadvd.c
+++ b/usr.sbin/rtadvd/rtadvd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: rtadvd.c,v 1.12 2001/12/01 23:27:23 miod Exp $ */
+/* $OpenBSD: rtadvd.c,v 1.13 2002/01/11 03:51:08 itojun Exp $ */
/* $KAME: rtadvd.c,v 1.50 2001/02/04 06:15:15 itojun Exp $ */
/*
@@ -234,11 +234,12 @@ main(argc, argv)
fprintf(stderr, "fatal: inet_pton failed\n");
exit(1);
}
- sock_open();
if (!fflag)
daemon(1, 0);
+ sock_open();
+
/* record the current PID */
if (pidfile(NULL) < 0) {
syslog(LOG_ERR,
diff --git a/usr.sbin/rtsold/rtsold.c b/usr.sbin/rtsold/rtsold.c
index b8443763675..e17fc79cea2 100644
--- a/usr.sbin/rtsold/rtsold.c
+++ b/usr.sbin/rtsold/rtsold.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: rtsold.c,v 1.13 2001/12/01 23:27:24 miod Exp $ */
+/* $OpenBSD: rtsold.c,v 1.14 2002/01/11 03:51:08 itojun Exp $ */
/* $KAME: rtsold.c,v 1.32 2001/07/09 22:34:07 itojun Exp $ */
/*
@@ -217,19 +217,24 @@ main(argc, argv)
/*NOTREACHED*/
}
+ if (!fflag)
+ daemon(0, 0); /* act as a daemon */
+
/*
* Open a socket for sending RS and receiving RA.
* This should be done before calling ifinit(), since the function
* uses the socket.
*/
if ((s = sockopen()) < 0) {
- errx(1, "failed to open a socket");
+ warnmsg(LOG_ERR, __FUNCTION__, "failed to open a socket");
+ exit(1);
/*NOTREACHED*/
}
maxfd = s;
#if 0
if ((rtsock = rtsock_open()) < 0) {
- errx(1, "failed to open a socket");
+ warnmsg(LOG_ERR, __FUNCTION__, "failed to open a socket");
+ exit(1);
/*NOTREACHED*/
}
if (rtsock > maxfd)
@@ -238,12 +243,16 @@ main(argc, argv)
/* configuration per interface */
if (ifinit()) {
- errx(1, "failed to initilizatoin interfaces");
+ warnmsg(LOG_ERR, __FUNCTION__,
+ "failed to initilizatoin interfaces");
+ exit(1);
/*NOTREACHED*/
}
while (argc--) {
if (ifconfig(*argv)) {
- errx(1, "failed to initialize %s", *argv);
+ warnmsg(LOG_ERR, __FUNCTION__,
+ "failed to initialize %s", *argv);
+ exit(1);
/*NOTREACHED*/
}
argv++;
@@ -251,13 +260,12 @@ main(argc, argv)
/* setup for probing default routers */
if (probe_init()) {
- errx(1, "failed to setup for probing routers");
+ warnmsg(LOG_ERR, __FUNCTION__,
+ "failed to setup for probing routers");
+ exit(1);
/*NOTREACHED*/
}
- if (!fflag)
- daemon(0, 0); /* act as a daemon */
-
/* dump the current pid */
if (!once) {
if (pidfile(NULL) < 0) {