Blame misc/daemon.c

Packit 6c4009
/*-
Packit 6c4009
 * Copyright (c) 1990, 1993
Packit 6c4009
 *	The Regents of the University of California.  All rights reserved.
Packit 6c4009
 *
Packit 6c4009
 * Redistribution and use in source and binary forms, with or without
Packit 6c4009
 * modification, are permitted provided that the following conditions
Packit 6c4009
 * are met:
Packit 6c4009
 * 1. Redistributions of source code must retain the above copyright
Packit 6c4009
 *    notice, this list of conditions and the following disclaimer.
Packit 6c4009
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 6c4009
 *    notice, this list of conditions and the following disclaimer in the
Packit 6c4009
 *    documentation and/or other materials provided with the distribution.
Packit 6c4009
 * 4. Neither the name of the University nor the names of its contributors
Packit 6c4009
 *    may be used to endorse or promote products derived from this software
Packit 6c4009
 *    without specific prior written permission.
Packit 6c4009
 *
Packit 6c4009
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit 6c4009
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 6c4009
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 6c4009
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit 6c4009
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 6c4009
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit 6c4009
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 6c4009
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit 6c4009
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit 6c4009
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 6c4009
 * SUCH DAMAGE.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#if defined(LIBC_SCCS) && !defined(lint)
Packit 6c4009
static char sccsid[] = "@(#)daemon.c	8.1 (Berkeley) 6/4/93";
Packit 6c4009
#endif /* LIBC_SCCS and not lint */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <paths.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
Packit 6c4009
#include <device-nrs.h>
Packit 6c4009
#include <not-cancel.h>
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
daemon (int nochdir, int noclose)
Packit 6c4009
{
Packit 6c4009
	int fd;
Packit 6c4009
Packit 6c4009
	switch (__fork()) {
Packit 6c4009
	case -1:
Packit 6c4009
		return (-1);
Packit 6c4009
	case 0:
Packit 6c4009
		break;
Packit 6c4009
	default:
Packit 6c4009
		_exit(0);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
	if (__setsid() == -1)
Packit 6c4009
		return (-1);
Packit 6c4009
Packit 6c4009
	if (!nochdir)
Packit 6c4009
		(void)__chdir("/");
Packit 6c4009
Packit 6c4009
	if (!noclose) {
Packit 6c4009
		struct stat64 st;
Packit 6c4009
Packit 6c4009
		if ((fd = __open_nocancel(_PATH_DEVNULL, O_RDWR, 0)) != -1
Packit 6c4009
		    && (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0)
Packit 6c4009
			== 0)) {
Packit 6c4009
			if (__builtin_expect (S_ISCHR (st.st_mode), 1) != 0
Packit 6c4009
#if defined DEV_NULL_MAJOR && defined DEV_NULL_MINOR
Packit 6c4009
			    && (st.st_rdev
Packit 6c4009
				== makedev (DEV_NULL_MAJOR, DEV_NULL_MINOR))
Packit 6c4009
#endif
Packit 6c4009
			    ) {
Packit 6c4009
				(void)__dup2(fd, STDIN_FILENO);
Packit 6c4009
				(void)__dup2(fd, STDOUT_FILENO);
Packit 6c4009
				(void)__dup2(fd, STDERR_FILENO);
Packit 6c4009
				if (fd > 2)
Packit 6c4009
					(void)__close (fd);
Packit 6c4009
			} else {
Packit 6c4009
				/* We must set an errno value since no
Packit 6c4009
				   function call actually failed.  */
Packit 6c4009
				__close_nocancel_nostatus (fd);
Packit 6c4009
				__set_errno (ENODEV);
Packit 6c4009
				return -1;
Packit 6c4009
			}
Packit 6c4009
		} else {
Packit 6c4009
			__close_nocancel_nostatus (fd);
Packit 6c4009
			return -1;
Packit 6c4009
		}
Packit 6c4009
	}
Packit 6c4009
	return (0);
Packit 6c4009
}