Debian Codegraph @

libinetutils/daemon.c

raw
1 /*
2 Copyright (C) 1996-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /*
20 * Copyright (c) 1990, 1993
21 * The Regents of the University of California. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 #include <config.h>
49 #include <fcntl.h>
50 #include <unistd.h>
51 #include <signal.h>
52 #include <error.h>
53 #include <stdlib.h>
54 #include <sys/types.h>
55 #include <sys/wait.h>
56 #include <attribute.h>
57
58 /*
59 According to Unix-FAQ maintained by Andrew Gierth:
60
61 1.fork() so the parent can exit, this returns control to the command
62 line or shell invoking your program. This step is required so that the
63 new process is guaranteed not to be a process group leader. The next
64 step, setsid(), fails if you're a process group leader.
65
66 2.setsid() to become a process group and session group leader. Since a
67 controlling terminal is associated with a session, and this new session
68 has not yet acquired a controlling terminal our process now has no
69 controlling terminal, which is a Good Thing for daemons.
70
71 3.fork() again so the parent, (the session group leader), can exit. This
72 means that we, as a non-session group leader, can never regain a
73 controlling terminal.
74
75 4.chdir("/") to ensure that our process doesn't keep any directory in use.
76 Failure to do this could make it so that an administrator couldn't unmount
77 a file system, because it was our current directory.
78 [Equivalently, we could change to any directory containing files important
79 to the daemon's operation.]
80
81 5.umask(0) so that we have complete control over the permissions of
82 anything we write. We don't know what umask we may have inherited.
83 [This step is optional]
84
85 6.close() fds 0, 1, and 2. This releases the standard in, out, and error
86 we inherited from our parent process. We have no way of knowing where
87 these fds might have been redirected to. Note that many daemons use
88 sysconf() to determine the limit _SC_OPEN_MAX. _SC_OPEN_MAX tells you the
89 maximum open files/process. Then in a loop, the daemon can close all
90 possible file descriptors. You have to decide if you need to do this or not.
91 If you think that there might be file-descriptors open you should close
92 them, since there's a limit on number of concurrent file descriptors.
93
94 7.Establish new open descriptors for stdin, stdout and stderr. Even if
95 you don't plan to use them, it is still a good idea to have them open.
96 The precise handling of these is a matter of taste; if you have a logfile,
97 for example, you might wish to open it as stdout or stderr, and open
98 `/dev/null' as stdin; alternatively, you could open `/dev/console' as
99 stderr and/or stdout, and `/dev/null' as stdin, or any other combination
100 that makes sense for your particular daemon. */
101
102 #define MAXFD 64
103
104 void
105 waitdaemon_timeout (int signo MAYBE_UNUSED)
106 {
107 int left;
108
109 left = alarm (0);
110 signal (SIGALRM, SIG_DFL);
111 if (left == 0)
112 error (EXIT_FAILURE, 0, "timed out waiting for child");
113 }
114
115 /* waitdaemon is like daemon, but optionally the parent pause up
116 until maxwait before exiting. Return -1, on error, otherwise
117 waitdaemon will return the pid of the parent. */
118
119 int
120 waitdaemon (int nochdir, int noclose, int maxwait)
121 {
122 int fd;
123 pid_t childpid;
124 pid_t ppid;
125
126 ppid = getpid ();
127
128 switch (childpid = fork ())
129 {
130 case -1: /* Something went wrong. */
131 return (-1);
132
133 case 0: /* In the child. */
134 break;
135
136 default: /* In the parent. */
137 if (maxwait > 0)
138 {
139 signal (SIGALRM, waitdaemon_timeout);
140 alarm (maxwait);
141 pause ();
142 }
143 _exit (EXIT_SUCCESS);
144 }
145
146 if (setsid () == -1)
147 return -1;
148
149 /* SIGHUP must be ignored, because when the session leader terminates,
150 then SIGHUP is sent to all process belonging to the same session,
151 i.e., also to the second child.
152 */
153 signal (SIGHUP, SIG_IGN);
154
155 switch (fork ())
156 {
157 case 0:
158 break;
159
160 case -1:
161 return -1;
162
163 default:
164 _exit (EXIT_SUCCESS);
165 }
166
167 if (!nochdir && chdir ("/") < 0)
168 return -1; /* Unlikely failure, but check it. */
169
170 if (!noclose)
171 {
172 int i;
173 long fdlimit = -1;
174
175 #if defined _SC_OPEN_MAX
176 fdlimit = sysconf (_SC_OPEN_MAX);
177 #elif defined (HAVE_GETDTABLESIZE)
178 fdlimit = getdtablesize ();
179 #endif
180
181 if (fdlimit == -1)
182 fdlimit = MAXFD;
183
184 for (i = 0; i < fdlimit; i++)
185 close (i);
186
187 fd = open (PATH_DEVNULL, O_RDWR, 0);
188 if (fd != -1)
189 {
190 dup2 (fd, STDIN_FILENO);
191 dup2 (fd, STDOUT_FILENO);
192 dup2 (fd, STDERR_FILENO);
193 if (fd > 2)
194 close (fd);
195 }
196 }
197 return ppid;
198 }
199
200 int
201 daemon (int nochdir, int noclose)
202 {
203 return (waitdaemon (nochdir, noclose, 0) == -1) ? -1 : 0;
204 }