Module Name:    src
Committed By:   gson
Date:           Sat Mar 29 16:10:54 UTC 2014

Modified Files:
        src/tests/lib/libc/net: h_dns_server.c

Log Message:
Use daemon(3) instead of the experimental daemon2_fork() and
daemon2_detach().  Now that newer versions of daemon2_fork() and
daemon2_detach() are used in nfsd, where two-stage daemonization is
actually needed, there is no longer any reason for h_dns_server to
serve as a test case and example of their use.


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/tests/lib/libc/net/h_dns_server.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/lib/libc/net/h_dns_server.c
diff -u src/tests/lib/libc/net/h_dns_server.c:1.3 src/tests/lib/libc/net/h_dns_server.c:1.4
--- src/tests/lib/libc/net/h_dns_server.c:1.3	Thu Jan  9 02:18:10 2014
+++ src/tests/lib/libc/net/h_dns_server.c	Sat Mar 29 16:10:54 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: h_dns_server.c,v 1.3 2014/01/09 02:18:10 christos Exp $	*/
+/*	$NetBSD: h_dns_server.c,v 1.4 2014/03/29 16:10:54 gson Exp $	*/
 
 /*-
  * Copyright (c) 2013 The NetBSD Foundation, Inc.
@@ -35,14 +35,13 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: h_dns_server.c,v 1.3 2014/01/09 02:18:10 christos Exp $");
+__RCSID("$NetBSD: h_dns_server.c,v 1.4 2014/03/29 16:10:54 gson Exp $");
 
 #include <ctype.h>
 #include <err.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <memory.h>
-#include <paths.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -165,104 +164,6 @@ name2str(const void *v, char *buf, size_
 }
 #endif
 
-/* XXX the daemon2_* functions should be in a library */
-
-int __daemon2_detach_pipe[2];
-
-static int
-daemon2_fork(void)
-{
-	int r;
-	int fd;
-	int i;
-
-	/*
-	 * Set up the pipe, making sure the write end does not
-	 * get allocated one of the file descriptors that will
-	 * be closed in daemon2_detach().
-	 */
-	for (i = 0; i < 3; i++) {
-	    r = pipe(__daemon2_detach_pipe);
-	    if (r < 0)
-		    return -1;
-	    if (__daemon2_detach_pipe[1] <= STDERR_FILENO &&
-		(fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
-		    (void)dup2(fd, __daemon2_detach_pipe[0]);
-		    (void)dup2(fd, __daemon2_detach_pipe[1]);
-		    if (fd > STDERR_FILENO)
-			    (void)close(fd);
-		    continue;
-	    }
-	    break;
-	}
-
-	r = fork();
-	if (r < 0) {
-		return -1;
-	} else if (r == 0) {
-		/* child */
-		close(__daemon2_detach_pipe[0]);
-		return 0;
-       }
-       /* Parent */
-
-       (void) close(__daemon2_detach_pipe[1]);
-
-       for (;;) {
-	       char dummy;
-	       r = read(__daemon2_detach_pipe[0], &dummy, 1);
-	       if (r < 0) {
-		       if (errno == EINTR)
-			       continue;
-		       _exit(1);
-	       } else if (r == 0) {
-		       _exit(1);
-	       } else { /* r > 0 */
-		       _exit(0);
-	       }
-       }
-}
-
-static int
-daemon2_detach(int nochdir, int noclose)
-{
-	int r;
-	int fd;
-
-	if (setsid() == -1)
-		return -1;
-
-	if (!nochdir)
-		(void)chdir("/");
-
-	if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
-		(void)dup2(fd, STDIN_FILENO);
-		(void)dup2(fd, STDOUT_FILENO);
-		(void)dup2(fd, STDERR_FILENO);
-		if (fd > STDERR_FILENO)
-			(void)close(fd);
-	}
-
-	while (1) {
-		r = write(__daemon2_detach_pipe[1], "", 1);
-		if (r < 0) {
-			if (errno == EINTR)
-				continue;
-			/* May get "broken pipe" here if parent is killed */
-			return -1;
-		} else if (r == 0) {
-			/* Should not happen */
-			return -1;
-		} else {
-			break;
-		}
-	}
-
-	(void) close(__daemon2_detach_pipe[1]);
-
-	return 0;
-}
-
 int main(int argc, char **argv) {
 	int s, r, protocol;
 	union sockaddr_either saddr;
@@ -275,8 +176,6 @@ int main(int argc, char **argv) {
 	char buf1[1024], buf2[1024];
 #endif
 
-	daemon2_fork();
-
 	if (argc < 2 || ((protocol = argv[1][0]) != '4' && protocol != '6'))
 		errx(1, "usage: dns_server 4 | 6");
 	s = socket(protocol == '4' ? PF_INET : PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
@@ -314,9 +213,9 @@ int main(int argc, char **argv) {
 	fprintf(f, "%d", getpid());
 	fclose(f);
 #ifdef DEBUG
-	daemon2_detach(0, 1);
+	daemon(0, 1);
 #else
-	daemon2_detach(0, 0);
+	daemon(0, 0);
 #endif
 
 	for (;;) {

Reply via email to