diff options
author | Jeremie Courreges-Anglas <jca@cvs.openbsd.org> | 2016-09-15 16:19:05 +0000 |
---|---|---|
committer | Jeremie Courreges-Anglas <jca@cvs.openbsd.org> | 2016-09-15 16:19:05 +0000 |
commit | 7e95faaae8697d0b6c2f10b98f5dbf98c974f5d0 (patch) | |
tree | 59552a467d22e7daa7383c827ebeb724b2bc909a /sbin/dhclient | |
parent | 59d8babc9d895cabb7c63196d27624ab1eec2987 (diff) |
Use rdaemon() in dhclient too.
dhclient already has code to pre-open /dev/null, in order to properly go
to the background after chroot(2). Use rdaemon() like in tftpd,
ftp-proxy, dhcrelay and rtadvd.
No objection krw@, ok dlg@
Diffstat (limited to 'sbin/dhclient')
-rw-r--r-- | sbin/dhclient/dhclient.c | 41 |
1 files changed, 29 insertions, 12 deletions
diff --git a/sbin/dhclient/dhclient.c b/sbin/dhclient/dhclient.c index 281ce9a123b..9642e4e2e95 100644 --- a/sbin/dhclient/dhclient.c +++ b/sbin/dhclient/dhclient.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dhclient.c,v 1.387 2016/09/04 11:21:24 jca Exp $ */ +/* $OpenBSD: dhclient.c,v 1.388 2016/09/15 16:19:04 jca Exp $ */ /* * Copyright 2004 Henning Brauer <henning@openbsd.org> @@ -167,6 +167,7 @@ char *lease_as_string(struct interface_info *, char *, struct client_lease *); struct client_lease *packet_to_lease(struct interface_info *, struct in_addr, struct option_data *); void go_daemon(void); +int rdaemon(int); #define ROUNDUP(a) \ ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) @@ -2057,17 +2058,8 @@ go_daemon(void) /* Stop logging to stderr. */ log_perror = 0; - if (daemon(1, 0) == -1) - error("daemon"); - - /* we are chrooted, daemon(3) fails to open /dev/null */ - if (nullfd != -1) { - dup2(nullfd, STDIN_FILENO); - dup2(nullfd, STDOUT_FILENO); - dup2(nullfd, STDERR_FILENO); - close(nullfd); - nullfd = -1; - } + if (rdaemon(nullfd) == -1) + error("rdaemon"); /* Catch stuff that might be trying to terminate the program. */ signal(SIGHUP, sighdlr); @@ -2080,6 +2072,31 @@ go_daemon(void) } int +rdaemon(int devnull) +{ + + switch (fork()) { + case -1: + return (-1); + case 0: + break; + default: + _exit(0); + } + + if (setsid() == -1) + return (-1); + + (void)dup2(devnull, STDIN_FILENO); + (void)dup2(devnull, STDOUT_FILENO); + (void)dup2(devnull, STDERR_FILENO); + if (devnull > 2) + (void)close(devnull); + + return (0); +} + +int res_hnok(const char *name) { const char *dn = name; |