xref: /onnv-gate/usr/src/lib/libresolv2/common/bsd/daemon.c (revision 0:68f95e015346)
1 /*
2  * Copyright (c) 1997-2000 by Sun Microsystems, Inc.
3  * All rights reserved.
4  */
5 
6 #if defined(LIBC_SCCS) && !defined(lint)
7 static const char sccsid[] = "@(#)daemon.c	8.1 (Berkeley) 6/4/93";
8 static const char rcsid[] = "$Id: daemon.c,v 8.2 1999/10/13 16:39:20 vixie Exp $";
9 #endif /* LIBC_SCCS and not lint */
10 
11 /*
12  * Copyright (c) 1990, 1993
13  *	The Regents of the University of California.  All rights reserved.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. All advertising materials mentioning features or use of this software
24  *    must display the following acknowledgement:
25  *	This product includes software developed by the University of
26  *	California, Berkeley and its contributors.
27  * 4. Neither the name of the University nor the names of its contributors
28  *    may be used to endorse or promote products derived from this software
29  *    without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41  * SUCH DAMAGE.
42  */
43 
44 #pragma ident	"%Z%%M%	%I%	%E% SMI"
45 
46 #include "port_before.h"
47 
48 #include <fcntl.h>
49 #include <paths.h>
50 #include <unistd.h>
51 
52 #include "port_after.h"
53 
54 #ifndef NEED_DAEMON
55 int __bind_daemon__;
56 #else
57 
58 int
59 daemon(int nochdir, int noclose) {
60 	int fd;
61 
62 	switch (fork()) {
63 	case -1:
64 		return (-1);
65 	case 0:
66 		break;
67 	default:
68 		_exit(0);
69 	}
70 
71 	if (setsid() == -1)
72 		return (-1);
73 
74 	if (!nochdir)
75 		(void)chdir("/");
76 
77 	if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
78 		(void)dup2(fd, STDIN_FILENO);
79 		(void)dup2(fd, STDOUT_FILENO);
80 		(void)dup2(fd, STDERR_FILENO);
81 		if (fd > 2)
82 			(void)close (fd);
83 	}
84 	return (0);
85 }
86 #endif
87