158f0484fSRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
4*16545cf5SMariusz Zaborski * Copyright (c) 1990, 1993 The Regents of the University of California.
5*16545cf5SMariusz Zaborski * Copyright (c) 2017 Mariusz Zaborski <oshogbo@FreeBSD.org>
6*16545cf5SMariusz Zaborski * All rights reserved.
758f0484fSRodney W. Grimes *
858f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without
958f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions
1058f0484fSRodney W. Grimes * are met:
1158f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
1258f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer.
1358f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
1458f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
1558f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution.
16fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
1758f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software
1858f0484fSRodney W. Grimes * without specific prior written permission.
1958f0484fSRodney W. Grimes *
2058f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2158f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2258f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2358f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2458f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2558f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2658f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2758f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2858f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2958f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3058f0484fSRodney W. Grimes * SUCH DAMAGE.
3158f0484fSRodney W. Grimes */
3258f0484fSRodney W. Grimes
33d201fe46SDaniel Eischen #include "namespace.h"
348b2b22e8SGuy Helmer #include <errno.h>
3558f0484fSRodney W. Grimes #include <fcntl.h>
3658f0484fSRodney W. Grimes #include <paths.h>
3761310091SStefan Farfeleder #include <stdlib.h>
388b2b22e8SGuy Helmer #include <signal.h>
3958f0484fSRodney W. Grimes #include <unistd.h>
40d201fe46SDaniel Eischen #include "un-namespace.h"
41bd6060a1SKonstantin Belousov #include "libc_private.h"
4258f0484fSRodney W. Grimes
4358f0484fSRodney W. Grimes int
daemonfd(int chdirfd,int nullfd)44*16545cf5SMariusz Zaborski daemonfd(int chdirfd, int nullfd)
4558f0484fSRodney W. Grimes {
468b2b22e8SGuy Helmer struct sigaction osa, sa;
478b2b22e8SGuy Helmer pid_t newgrp;
488b2b22e8SGuy Helmer int oerrno;
498b2b22e8SGuy Helmer int osa_ok;
508b2b22e8SGuy Helmer
518b2b22e8SGuy Helmer /* A SIGHUP may be thrown when the parent exits below. */
528b2b22e8SGuy Helmer sigemptyset(&sa.sa_mask);
538b2b22e8SGuy Helmer sa.sa_handler = SIG_IGN;
548b2b22e8SGuy Helmer sa.sa_flags = 0;
55bd6060a1SKonstantin Belousov osa_ok = __libc_sigaction(SIGHUP, &sa, &osa);
5658f0484fSRodney W. Grimes
5758f0484fSRodney W. Grimes switch (fork()) {
5858f0484fSRodney W. Grimes case -1:
5958f0484fSRodney W. Grimes return (-1);
6058f0484fSRodney W. Grimes case 0:
6158f0484fSRodney W. Grimes break;
6258f0484fSRodney W. Grimes default:
63156a1661SPoul-Henning Kamp /*
64156a1661SPoul-Henning Kamp * A fine point: _exit(0), not exit(0), to avoid triggering
65156a1661SPoul-Henning Kamp * atexit(3) processing
66156a1661SPoul-Henning Kamp */
6758f0484fSRodney W. Grimes _exit(0);
6858f0484fSRodney W. Grimes }
6958f0484fSRodney W. Grimes
708b2b22e8SGuy Helmer newgrp = setsid();
718b2b22e8SGuy Helmer oerrno = errno;
728b2b22e8SGuy Helmer if (osa_ok != -1)
73bd6060a1SKonstantin Belousov __libc_sigaction(SIGHUP, &osa, NULL);
748b2b22e8SGuy Helmer
758b2b22e8SGuy Helmer if (newgrp == -1) {
768b2b22e8SGuy Helmer errno = oerrno;
7758f0484fSRodney W. Grimes return (-1);
788b2b22e8SGuy Helmer }
7958f0484fSRodney W. Grimes
80*16545cf5SMariusz Zaborski if (chdirfd != -1)
81*16545cf5SMariusz Zaborski (void)fchdir(chdirfd);
8258f0484fSRodney W. Grimes
83*16545cf5SMariusz Zaborski if (nullfd != -1) {
84*16545cf5SMariusz Zaborski (void)_dup2(nullfd, STDIN_FILENO);
85*16545cf5SMariusz Zaborski (void)_dup2(nullfd, STDOUT_FILENO);
86*16545cf5SMariusz Zaborski (void)_dup2(nullfd, STDERR_FILENO);
8758f0484fSRodney W. Grimes }
8858f0484fSRodney W. Grimes return (0);
8958f0484fSRodney W. Grimes }
90*16545cf5SMariusz Zaborski
91*16545cf5SMariusz Zaborski int
daemon(int nochdir,int noclose)92*16545cf5SMariusz Zaborski daemon(int nochdir, int noclose)
93*16545cf5SMariusz Zaborski {
94*16545cf5SMariusz Zaborski int chdirfd, nullfd, ret;
95*16545cf5SMariusz Zaborski
96*16545cf5SMariusz Zaborski if (!noclose)
97*16545cf5SMariusz Zaborski nullfd = _open(_PATH_DEVNULL, O_RDWR, 0);
98*16545cf5SMariusz Zaborski else
99*16545cf5SMariusz Zaborski nullfd = -1;
100*16545cf5SMariusz Zaborski
101*16545cf5SMariusz Zaborski if (!nochdir)
102*16545cf5SMariusz Zaborski chdirfd = _open("/", O_EXEC);
103*16545cf5SMariusz Zaborski else
104*16545cf5SMariusz Zaborski chdirfd = -1;
105*16545cf5SMariusz Zaborski
106*16545cf5SMariusz Zaborski ret = daemonfd(chdirfd, nullfd);
107*16545cf5SMariusz Zaborski
108*16545cf5SMariusz Zaborski if (chdirfd != -1)
109*16545cf5SMariusz Zaborski _close(chdirfd);
110*16545cf5SMariusz Zaborski
111*16545cf5SMariusz Zaborski if (nullfd > 2)
112*16545cf5SMariusz Zaborski _close(nullfd);
113*16545cf5SMariusz Zaborski
114*16545cf5SMariusz Zaborski return (ret);
115*16545cf5SMariusz Zaborski }
116