/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1990, 1993 The Regents of the University of California.4* Copyright (c) 2017 Mariusz Zaborski <[email protected]>5* All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15* 3. Neither the name of the University nor the names of its contributors16* may be used to endorse or promote products derived from this software17* without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND20* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE21* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE22* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE23* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL24* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS25* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT27* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY28* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF29* SUCH DAMAGE.30*/3132#include "namespace.h"33#include <errno.h>34#include <fcntl.h>35#include <paths.h>36#include <stdlib.h>37#include <signal.h>38#include <unistd.h>39#include "un-namespace.h"40#include "libc_private.h"4142int43daemonfd(int chdirfd, int nullfd)44{45struct sigaction osa, sa;46pid_t newgrp;47int oerrno;48int osa_ok;4950/* A SIGHUP may be thrown when the parent exits below. */51sigemptyset(&sa.sa_mask);52sa.sa_handler = SIG_IGN;53sa.sa_flags = 0;54osa_ok = __libc_sigaction(SIGHUP, &sa, &osa);5556switch (fork()) {57case -1:58return (-1);59case 0:60break;61default:62/*63* A fine point: _exit(0), not exit(0), to avoid triggering64* atexit(3) processing65*/66_exit(0);67}6869newgrp = setsid();70oerrno = errno;71if (osa_ok != -1)72__libc_sigaction(SIGHUP, &osa, NULL);7374if (newgrp == -1) {75errno = oerrno;76return (-1);77}7879if (chdirfd != -1)80(void)fchdir(chdirfd);8182if (nullfd != -1) {83(void)_dup2(nullfd, STDIN_FILENO);84(void)_dup2(nullfd, STDOUT_FILENO);85(void)_dup2(nullfd, STDERR_FILENO);86}87return (0);88}8990int91daemon(int nochdir, int noclose)92{93int chdirfd, nullfd, ret;9495if (!noclose)96nullfd = _open(_PATH_DEVNULL, O_RDWR, 0);97else98nullfd = -1;99100if (!nochdir)101chdirfd = _open("/", O_EXEC);102else103chdirfd = -1;104105ret = daemonfd(chdirfd, nullfd);106107if (chdirfd != -1)108_close(chdirfd);109110if (nullfd > 2)111_close(nullfd);112113return (ret);114}115116117