1*7f2fe78bSCy Schubert /* -*- mode: c; c-file-style: "bsd"; indent-tabs-mode: t -*- */
2*7f2fe78bSCy Schubert /*
3*7f2fe78bSCy Schubert * Copyright (c) 1990 The Regents of the University of California.
4*7f2fe78bSCy Schubert * All rights reserved.
5*7f2fe78bSCy Schubert *
6*7f2fe78bSCy Schubert * Redistribution and use in source and binary forms, with or without
7*7f2fe78bSCy Schubert * modification, are permitted provided that the following conditions
8*7f2fe78bSCy Schubert * are met:
9*7f2fe78bSCy Schubert * 1. Redistributions of source code must retain the above copyright
10*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer.
11*7f2fe78bSCy Schubert * 2. Redistributions in binary form must reproduce the above copyright
12*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer in the
13*7f2fe78bSCy Schubert * documentation and/or other materials provided with the distribution.
14*7f2fe78bSCy Schubert * 3. All advertising materials mentioning features or use of this software
15*7f2fe78bSCy Schubert * must display the following acknowledgement:
16*7f2fe78bSCy Schubert * This product includes software developed by the University of
17*7f2fe78bSCy Schubert * California, Berkeley and its contributors.
18*7f2fe78bSCy Schubert * 4. Neither the name of the University nor the names of its contributors
19*7f2fe78bSCy Schubert * may be used to endorse or promote products derived from this software
20*7f2fe78bSCy Schubert * without specific prior written permission.
21*7f2fe78bSCy Schubert *
22*7f2fe78bSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*7f2fe78bSCy Schubert * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*7f2fe78bSCy Schubert * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*7f2fe78bSCy Schubert * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*7f2fe78bSCy Schubert * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*7f2fe78bSCy Schubert * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*7f2fe78bSCy Schubert * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*7f2fe78bSCy Schubert * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*7f2fe78bSCy Schubert * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*7f2fe78bSCy Schubert * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*7f2fe78bSCy Schubert * SUCH DAMAGE.
33*7f2fe78bSCy Schubert */
34*7f2fe78bSCy Schubert
35*7f2fe78bSCy Schubert #include "k5-int.h"
36*7f2fe78bSCy Schubert #include <fcntl.h>
37*7f2fe78bSCy Schubert #include <sys/types.h>
38*7f2fe78bSCy Schubert #include <sys/file.h>
39*7f2fe78bSCy Schubert #include <unistd.h>
40*7f2fe78bSCy Schubert #ifdef HAVE_PATHS_H
41*7f2fe78bSCy Schubert #include <paths.h>
42*7f2fe78bSCy Schubert #endif
43*7f2fe78bSCy Schubert
44*7f2fe78bSCy Schubert #ifndef _PATH_DEVNULL
45*7f2fe78bSCy Schubert #define _PATH_DEVNULL "/dev/null"
46*7f2fe78bSCy Schubert #endif
47*7f2fe78bSCy Schubert
48*7f2fe78bSCy Schubert int
daemon(nochdir,noclose)49*7f2fe78bSCy Schubert daemon(nochdir, noclose)
50*7f2fe78bSCy Schubert int nochdir, noclose;
51*7f2fe78bSCy Schubert {
52*7f2fe78bSCy Schubert int cpid;
53*7f2fe78bSCy Schubert
54*7f2fe78bSCy Schubert if ((cpid = fork()) == -1)
55*7f2fe78bSCy Schubert return (-1);
56*7f2fe78bSCy Schubert if (cpid)
57*7f2fe78bSCy Schubert exit(0);
58*7f2fe78bSCy Schubert #ifdef HAVE_SETSID
59*7f2fe78bSCy Schubert (void) setsid();
60*7f2fe78bSCy Schubert #else
61*7f2fe78bSCy Schubert #ifndef TIOCNOTTY
62*7f2fe78bSCy Schubert setpgrp();
63*7f2fe78bSCy Schubert #else
64*7f2fe78bSCy Schubert {
65*7f2fe78bSCy Schubert int n;
66*7f2fe78bSCy Schubert
67*7f2fe78bSCy Schubert /*
68*7f2fe78bSCy Schubert * The open below may hang on pseudo ttys if the person
69*7f2fe78bSCy Schubert * who starts named logs out before this point. Thus,
70*7f2fe78bSCy Schubert * the need for the timer.
71*7f2fe78bSCy Schubert */
72*7f2fe78bSCy Schubert alarm(120);
73*7f2fe78bSCy Schubert n = open("/dev/tty", O_RDWR);
74*7f2fe78bSCy Schubert alarm(0);
75*7f2fe78bSCy Schubert if (n > 0) {
76*7f2fe78bSCy Schubert (void) ioctl(n, TIOCNOTTY, (char *)NULL);
77*7f2fe78bSCy Schubert (void) close(n);
78*7f2fe78bSCy Schubert }
79*7f2fe78bSCy Schubert }
80*7f2fe78bSCy Schubert #endif
81*7f2fe78bSCy Schubert #endif
82*7f2fe78bSCy Schubert if (!nochdir)
83*7f2fe78bSCy Schubert (void) chdir("/");
84*7f2fe78bSCy Schubert if (!noclose) {
85*7f2fe78bSCy Schubert int devnull = open(_PATH_DEVNULL, O_RDWR, 0);
86*7f2fe78bSCy Schubert
87*7f2fe78bSCy Schubert if (devnull != -1) {
88*7f2fe78bSCy Schubert (void) dup2(devnull, 0);
89*7f2fe78bSCy Schubert (void) dup2(devnull, 1);
90*7f2fe78bSCy Schubert (void) dup2(devnull, 2);
91*7f2fe78bSCy Schubert if (devnull > 2)
92*7f2fe78bSCy Schubert (void) close(devnull);
93*7f2fe78bSCy Schubert }
94*7f2fe78bSCy Schubert }
95*7f2fe78bSCy Schubert return (0);
96*7f2fe78bSCy Schubert }
97