daemon.c revision 98937
1217309Snwhitehorn/*-
2251843Sbapt * Copyright (c) 1990, 1993
3217309Snwhitehorn *	The Regents of the University of California.  All rights reserved.
4224014Snwhitehorn *
5217309Snwhitehorn * Redistribution and use in source and binary forms, with or without
6251843Sbapt * modification, are permitted provided that the following conditions
7217309Snwhitehorn * are met:
8217309Snwhitehorn * 1. Redistributions of source code must retain the above copyright
9217309Snwhitehorn *    notice, this list of conditions and the following disclaimer.
10217309Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright
11217309Snwhitehorn *    notice, this list of conditions and the following disclaimer in the
12217309Snwhitehorn *    documentation and/or other materials provided with the distribution.
13217309Snwhitehorn * 3. All advertising materials mentioning features or use of this software
14217309Snwhitehorn *    must display the following acknowledgement:
15217309Snwhitehorn *	This product includes software developed by the University of
16217309Snwhitehorn *	California, Berkeley and its contributors.
17217309Snwhitehorn * 4. Neither the name of the University nor the names of its contributors
18217309Snwhitehorn *    may be used to endorse or promote products derived from this software
19217309Snwhitehorn *    without specific prior written permission.
20217309Snwhitehorn *
21217309Snwhitehorn * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22217309Snwhitehorn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23217309Snwhitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24217309Snwhitehorn * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25217309Snwhitehorn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26251843Sbapt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27217309Snwhitehorn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28217309Snwhitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29217309Snwhitehorn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30217309Snwhitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31217309Snwhitehorn * SUCH DAMAGE.
32217309Snwhitehorn */
33217309Snwhitehorn
34217309Snwhitehorn#include "includes.h"
35251843Sbapt
36217309Snwhitehorn#ifndef HAVE_DAEMON
37217309Snwhitehorn
38217309Snwhitehorn#if defined(LIBC_SCCS) && !defined(lint)
39217309Snwhitehornstatic char rcsid[] = "$OpenBSD: daemon.c,v 1.2 1996/08/19 08:22:13 tholo Exp $";
40217309Snwhitehorn#endif /* LIBC_SCCS and not lint */
41217309Snwhitehorn
42217309Snwhitehornint
43217309Snwhitehorndaemon(nochdir, noclose)
44217309Snwhitehorn	int nochdir, noclose;
45217309Snwhitehorn{
46217309Snwhitehorn	int fd;
47217309Snwhitehorn
48217309Snwhitehorn	switch (fork()) {
49251843Sbapt	case -1:
50217309Snwhitehorn		return (-1);
51217309Snwhitehorn	case 0:
52217309Snwhitehorn#ifdef HAVE_CYGWIN
53217309Snwhitehorn		register_9x_service();
54217309Snwhitehorn#endif
55217309Snwhitehorn		break;
56217309Snwhitehorn	default:
57217309Snwhitehorn#ifdef HAVE_CYGWIN
58217309Snwhitehorn		/*
59217309Snwhitehorn		 * This sleep avoids a race condition which kills the
60217309Snwhitehorn		 * child process if parent is started by a NT/W2K service.
61217309Snwhitehorn		 */
62217309Snwhitehorn		sleep(1);
63217309Snwhitehorn#endif
64217309Snwhitehorn		_exit(0);
65217309Snwhitehorn	}
66217309Snwhitehorn
67217309Snwhitehorn	if (setsid() == -1)
68217309Snwhitehorn		return (-1);
69217309Snwhitehorn
70217309Snwhitehorn	if (!nochdir)
71217309Snwhitehorn		(void)chdir("/");
72217309Snwhitehorn
73217309Snwhitehorn	if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
74217309Snwhitehorn		(void)dup2(fd, STDIN_FILENO);
75217309Snwhitehorn		(void)dup2(fd, STDOUT_FILENO);
76217309Snwhitehorn		(void)dup2(fd, STDERR_FILENO);
77217309Snwhitehorn		if (fd > 2)
78217309Snwhitehorn			(void)close (fd);
79251843Sbapt	}
80251843Sbapt	return (0);
81251843Sbapt}
82251843Sbapt
83251843Sbapt#endif /* !HAVE_DAEMON */
84217309Snwhitehorn
85217309Snwhitehorn