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