1234228Sluigi/*- 2262151Sluigi * Copyright (c) 1990, 1993 3234228Sluigi * The Regents of the University of California. All rights reserved. 4234228Sluigi * 5234228Sluigi * Redistribution and use in source and binary forms, with or without 6234228Sluigi * modification, are permitted provided that the following conditions 7234228Sluigi * are met: 8234228Sluigi * 1. Redistributions of source code must retain the above copyright 9234228Sluigi * notice, this list of conditions and the following disclaimer. 10234228Sluigi * 2. Redistributions in binary form must reproduce the above copyright 11262151Sluigi * notice, this list of conditions and the following disclaimer in the 12234228Sluigi * documentation and/or other materials provided with the distribution. 13234228Sluigi * 4. Neither the name of the University nor the names of its contributors 14234228Sluigi * may be used to endorse or promote products derived from this software 15234228Sluigi * without specific prior written permission. 16234228Sluigi * 17234228Sluigi * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18234228Sluigi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19234228Sluigi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20234228Sluigi * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21234228Sluigi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22234228Sluigi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23234228Sluigi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24234228Sluigi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25234228Sluigi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26262151Sluigi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27262151Sluigi * SUCH DAMAGE. 28262151Sluigi */ 29234228Sluigi 30262151Sluigi#if defined(LIBC_SCCS) && !defined(lint) 31262151Sluigistatic char sccsid[] = "@(#)daemon.c 8.1 (Berkeley) 6/4/93"; 32262151Sluigi#endif /* LIBC_SCCS and not lint */ 33234228Sluigi#include <sys/cdefs.h> 34262151Sluigi__FBSDID("$FreeBSD$"); 35262151Sluigi 36262151Sluigi#include "namespace.h" 37234228Sluigi#include <errno.h> 38262151Sluigi#include <fcntl.h> 39262151Sluigi#include <paths.h> 40262151Sluigi#include <stdlib.h> 41262151Sluigi#include <signal.h> 42262151Sluigi#include <unistd.h> 43262151Sluigi#include "un-namespace.h" 44262151Sluigi 45262151Sluigiint 46262151Sluigidaemon(nochdir, noclose) 47262151Sluigi int nochdir, noclose; 48262151Sluigi{ 49262151Sluigi struct sigaction osa, sa; 50262151Sluigi int fd; 51262151Sluigi pid_t newgrp; 52262151Sluigi int oerrno; 53262151Sluigi int osa_ok; 54262151Sluigi 55262151Sluigi /* A SIGHUP may be thrown when the parent exits below. */ 56262151Sluigi sigemptyset(&sa.sa_mask); 57270252Sluigi sa.sa_handler = SIG_IGN; 58270252Sluigi sa.sa_flags = 0; 59270252Sluigi osa_ok = _sigaction(SIGHUP, &sa, &osa); 60270252Sluigi 61270252Sluigi switch (fork()) { 62270252Sluigi case -1: 63270252Sluigi return (-1); 64270252Sluigi case 0: 65270252Sluigi break; 66270252Sluigi default: 67270252Sluigi /* 68270252Sluigi * A fine point: _exit(0), not exit(0), to avoid triggering 69270252Sluigi * atexit(3) processing 70270252Sluigi */ 71270252Sluigi _exit(0); 72270252Sluigi } 73270252Sluigi 74270252Sluigi newgrp = setsid(); 75270252Sluigi oerrno = errno; 76270252Sluigi if (osa_ok != -1) 77270252Sluigi _sigaction(SIGHUP, &osa, NULL); 78270252Sluigi 79270252Sluigi if (newgrp == -1) { 80270252Sluigi errno = oerrno; 81270252Sluigi return (-1); 82270252Sluigi } 83270252Sluigi 84270252Sluigi if (!nochdir) 85270252Sluigi (void)chdir("/"); 86270252Sluigi 87270252Sluigi if (!noclose && (fd = _open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { 88270252Sluigi (void)_dup2(fd, STDIN_FILENO); 89270252Sluigi (void)_dup2(fd, STDOUT_FILENO); 90270252Sluigi (void)_dup2(fd, STDERR_FILENO); 91270252Sluigi if (fd > 2) 92270252Sluigi (void)_close(fd); 93270252Sluigi } 94270252Sluigi return (0); 95270252Sluigi} 96270252Sluigi