xref: /original-bsd/lib/libc/gen/daemon.c (revision 92ab646d)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)daemon.c	5.2 (Berkeley) 06/29/90";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <sys/file.h>
13 
14 daemon(nochdir, noclose)
15 	int nochdir, noclose;
16 {
17 	int cpid;
18 
19 	if ((cpid = fork()) == -1)
20 		return (-1);
21 	if (cpid)
22 		exit(0);
23 	(void) setsid();
24 	if (!nochdir)
25 		(void) chdir("/");
26 	if (!noclose) {
27 		int devnull = open("/dev/null", O_RDWR, 0);
28 
29 		if (devnull != -1) {
30 			(void) dup2(devnull, 0);
31 			(void) dup2(devnull, 1);
32 			(void) dup2(devnull, 2);
33 			if (devnull > 2)
34 				(void) close(devnull);
35 		}
36 	}
37 }
38