Deleted Added
sdiff udiff text old ( 244986 ) new ( 255521 )
full compact
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 244986 2013-01-03 00:49:11Z marius $");
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>

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

48static void dummy_sighandler(int);
49static void restrict_process(const char *);
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 *pfh = NULL;
57 sigset_t mask, oldmask;
58 int ch, nochdir, noclose, restart;
59 const char *pidfile, *user;
60 pid_t otherpid, pid;
61
62 nochdir = noclose = 1;
63 restart = 0;
64 pidfile = user = NULL;
65 while ((ch = getopt(argc, argv, "cfp:ru:")) != -1) {
66 switch (ch) {
67 case 'c':
68 nochdir = 0;
69 break;
70 case 'f':
71 noclose = 0;
72 break;
73 case 'p':
74 pidfile = optarg;
75 break;
76 case 'r':
77 restart = 1;
78 break;
79 case 'u':
80 user = optarg;
81 break;
82 default:
83 usage();
84 }
85 }
86 argc -= optind;
87 argv += optind;
88
89 if (argc == 0)
90 usage();
91
92 pfh = NULL;
93 /*
94 * Try to open the pidfile before calling daemon(3),
95 * to be able to report the error intelligently
96 */
97 if (pidfile != NULL) {
98 pfh = pidfile_open(pidfile, 0600, &otherpid);
99 if (pfh == NULL) {
100 if (errno == EEXIST) {
101 errx(3, "process already running, pid: %d",
102 otherpid);
103 }
104 err(2, "pidfile ``%s''", pidfile);
105 }
106 }
107
108 if (daemon(nochdir, noclose) == -1)
109 err(1, NULL);
110
111 /*
112 * If the pidfile or restart option is specified the daemon
113 * executes the command in a forked process and wait on child
114 * exit to remove the pidfile or restart the command. Normally

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

171 execvp(argv[0], argv);
172
173 /*
174 * execvp() failed -- report the error. The child is
175 * now running, so the exit status doesn't matter.
176 */
177 err(1, "%s", argv[0]);
178 }
179 setproctitle("%s[%d]", argv[0], pid);
180 if (wait_child(pid, &mask) == 0 && restart) {
181 sleep(1);
182 goto restart;
183 }
184 pidfile_remove(pfh);
185 exit(0); /* Exit status does not matter. */
186}
187
188static void
189dummy_sighandler(int sig __unused)
190{
191 /* Nothing to do. */
192}

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

235 }
236 }
237}
238
239static void
240usage(void)
241{
242 (void)fprintf(stderr,
243 "usage: daemon [-cfr] [-p pidfile] [-u user] command "
244 "arguments ...\n");
245 exit(1);
246}