Blame misc/daemon.c

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