Deleted Added
full compact
daemon.c (255521) daemon.c (255707)
1/*-
2 * Copyright (c) 1999 Berkeley Software Design, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.

--- 15 unchanged lines hidden (view full) ---

24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * From BSDI: daemon.c,v 1.2 1996/08/15 01:11:09 jch Exp
29 */
30
31#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1999 Berkeley Software Design, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.

--- 15 unchanged lines hidden (view full) ---

24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * From BSDI: daemon.c,v 1.2 1996/08/15 01:11:09 jch Exp
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: head/usr.sbin/daemon/daemon.c 255521 2013-09-13 16:57:28Z jmg $");
32__FBSDID("$FreeBSD: head/usr.sbin/daemon/daemon.c 255707 2013-09-19 18:00:05Z trociny $");
33
34#include <sys/param.h>
35#include <sys/mman.h>
36#include <sys/wait.h>
37
38#include <err.h>
39#include <errno.h>
40#include <libutil.h>

--- 9 unchanged lines hidden (view full) ---

50static int wait_child(pid_t pid, sigset_t *mask);
51static void usage(void);
52
53int
54main(int argc, char *argv[])
55{
56 struct pidfh *ppfh, *pfh;
57 sigset_t mask, oldmask;
33
34#include <sys/param.h>
35#include <sys/mman.h>
36#include <sys/wait.h>
37
38#include <err.h>
39#include <errno.h>
40#include <libutil.h>

--- 9 unchanged lines hidden (view full) ---

50static int wait_child(pid_t pid, sigset_t *mask);
51static void usage(void);
52
53int
54main(int argc, char *argv[])
55{
56 struct pidfh *ppfh, *pfh;
57 sigset_t mask, oldmask;
58 int ch, nochdir, noclose, restart;
58 int ch, nochdir, noclose, restart, serrno;
59 const char *pidfile, *ppidfile, *user;
60 pid_t otherpid, pid;
61
62 nochdir = noclose = 1;
63 restart = 0;
59 const char *pidfile, *ppidfile, *user;
60 pid_t otherpid, pid;
61
62 nochdir = noclose = 1;
63 restart = 0;
64 ppfh = pfh = NULL;
65 ppidfile = pidfile = user = NULL;
66 while ((ch = getopt(argc, argv, "cfp:P:ru:")) != -1) {
67 switch (ch) {
68 case 'c':
69 nochdir = 0;
70 break;
71 case 'f':
72 noclose = 0;

--- 30 unchanged lines hidden (view full) ---

103 if (pfh == NULL) {
104 if (errno == EEXIST) {
105 errx(3, "process already running, pid: %d",
106 otherpid);
107 }
108 err(2, "pidfile ``%s''", pidfile);
109 }
110 }
64 ppidfile = pidfile = user = NULL;
65 while ((ch = getopt(argc, argv, "cfp:P:ru:")) != -1) {
66 switch (ch) {
67 case 'c':
68 nochdir = 0;
69 break;
70 case 'f':
71 noclose = 0;

--- 30 unchanged lines hidden (view full) ---

102 if (pfh == NULL) {
103 if (errno == EEXIST) {
104 errx(3, "process already running, pid: %d",
105 otherpid);
106 }
107 err(2, "pidfile ``%s''", pidfile);
108 }
109 }
111
112 /* do same for actual daemon process */
110 /* Do the same for actual daemon process. */
113 if (ppidfile != NULL) {
114 ppfh = pidfile_open(ppidfile, 0600, &otherpid);
115 if (ppfh == NULL) {
111 if (ppidfile != NULL) {
112 ppfh = pidfile_open(ppidfile, 0600, &otherpid);
113 if (ppfh == NULL) {
114 serrno = errno;
115 pidfile_remove(pfh);
116 errno = serrno;
116 if (errno == EEXIST) {
117 errx(3, "process already running, pid: %d",
118 otherpid);
119 }
120 err(2, "ppidfile ``%s''", ppidfile);
121 }
122 }
123
117 if (errno == EEXIST) {
118 errx(3, "process already running, pid: %d",
119 otherpid);
120 }
121 err(2, "ppidfile ``%s''", ppidfile);
122 }
123 }
124
124 if (daemon(nochdir, noclose) == -1)
125 err(1, NULL);
125 if (daemon(nochdir, noclose) == -1) {
126 warn("daemon");
127 goto exit;
128 }
129 /* Write out parent pidfile if needed. */
130 pidfile_write(ppfh);
126
127 /*
128 * If the pidfile or restart option is specified the daemon
129 * executes the command in a forked process and wait on child
130 * exit to remove the pidfile or restart the command. Normally
131 * we don't want the monitoring daemon to be terminated
132 * leaving the running process and the stale pidfile, so we
133 * catch SIGTERM and forward it to the children expecting to
134 * get SIGCHLD eventually.
135 */
136 pid = -1;
137 if (pidfile != NULL || restart) {
138 /*
139 * Restore default action for SIGTERM in case the
140 * parent process decided to ignore it.
141 */
131
132 /*
133 * If the pidfile or restart option is specified the daemon
134 * executes the command in a forked process and wait on child
135 * exit to remove the pidfile or restart the command. Normally
136 * we don't want the monitoring daemon to be terminated
137 * leaving the running process and the stale pidfile, so we
138 * catch SIGTERM and forward it to the children expecting to
139 * get SIGCHLD eventually.
140 */
141 pid = -1;
142 if (pidfile != NULL || restart) {
143 /*
144 * Restore default action for SIGTERM in case the
145 * parent process decided to ignore it.
146 */
142 if (signal(SIGTERM, SIG_DFL) == SIG_ERR)
143 err(1, "signal");
147 if (signal(SIGTERM, SIG_DFL) == SIG_ERR) {
148 warn("signal");
149 goto exit;
150 }
144 /*
145 * Because SIGCHLD is ignored by default, setup dummy handler
146 * for it, so we can mask it.
147 */
151 /*
152 * Because SIGCHLD is ignored by default, setup dummy handler
153 * for it, so we can mask it.
154 */
148 if (signal(SIGCHLD, dummy_sighandler) == SIG_ERR)
149 err(1, "signal");
155 if (signal(SIGCHLD, dummy_sighandler) == SIG_ERR) {
156 warn("signal");
157 goto exit;
158 }
150 /*
151 * Block interesting signals.
152 */
153 sigemptyset(&mask);
154 sigaddset(&mask, SIGTERM);
155 sigaddset(&mask, SIGCHLD);
159 /*
160 * Block interesting signals.
161 */
162 sigemptyset(&mask);
163 sigaddset(&mask, SIGTERM);
164 sigaddset(&mask, SIGCHLD);
156 if (sigprocmask(SIG_SETMASK, &mask, &oldmask) == -1)
157 err(1, "sigprocmask");
165 if (sigprocmask(SIG_SETMASK, &mask, &oldmask) == -1) {
166 warn("sigprocmask");
167 goto exit;
168 }
158 /*
159 * Try to protect against pageout kill. Ignore the
160 * error, madvise(2) will fail only if a process does
161 * not have superuser privileges.
162 */
163 (void)madvise(NULL, 0, MADV_PROTECT);
164restart:
165 /*
166 * Spawn a child to exec the command, so in the parent
167 * we could wait for it to exit and remove pidfile.
168 */
169 pid = fork();
170 if (pid == -1) {
169 /*
170 * Try to protect against pageout kill. Ignore the
171 * error, madvise(2) will fail only if a process does
172 * not have superuser privileges.
173 */
174 (void)madvise(NULL, 0, MADV_PROTECT);
175restart:
176 /*
177 * Spawn a child to exec the command, so in the parent
178 * we could wait for it to exit and remove pidfile.
179 */
180 pid = fork();
181 if (pid == -1) {
171 pidfile_remove(pfh);
172 err(1, "fork");
182 warn("fork");
183 goto exit;
173 }
174 }
175 if (pid <= 0) {
176 if (pid == 0) {
177 /* Restore old sigmask in the child. */
178 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) == -1)
179 err(1, "sigprocmask");
180 }

--- 6 unchanged lines hidden (view full) ---

187 execvp(argv[0], argv);
188
189 /*
190 * execvp() failed -- report the error. The child is
191 * now running, so the exit status doesn't matter.
192 */
193 err(1, "%s", argv[0]);
194 }
184 }
185 }
186 if (pid <= 0) {
187 if (pid == 0) {
188 /* Restore old sigmask in the child. */
189 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) == -1)
190 err(1, "sigprocmask");
191 }

--- 6 unchanged lines hidden (view full) ---

198 execvp(argv[0], argv);
199
200 /*
201 * execvp() failed -- report the error. The child is
202 * now running, so the exit status doesn't matter.
203 */
204 err(1, "%s", argv[0]);
205 }
195 /* write out parent pidfile if needed */
196 if (ppidfile != NULL)
197 pidfile_write(ppfh);
198
199 setproctitle("%s[%d]", argv[0], pid);
200 if (wait_child(pid, &mask) == 0 && restart) {
201 sleep(1);
202 goto restart;
203 }
206
207 setproctitle("%s[%d]", argv[0], pid);
208 if (wait_child(pid, &mask) == 0 && restart) {
209 sleep(1);
210 goto restart;
211 }
212exit:
204 pidfile_remove(pfh);
205 pidfile_remove(ppfh);
213 pidfile_remove(pfh);
214 pidfile_remove(ppfh);
206 exit(0); /* Exit status does not matter. */
215 exit(1); /* If daemon(3) succeeded exit status does not matter. */
207}
208
209static void
210dummy_sighandler(int sig __unused)
211{
212 /* Nothing to do. */
213}
214

--- 53 unchanged lines hidden ---
216}
217
218static void
219dummy_sighandler(int sig __unused)
220{
221 /* Nothing to do. */
222}
223

--- 53 unchanged lines hidden ---