Lines 44-49
Link Here
|
44 |
#include <stdio.h> |
44 |
#include <stdio.h> |
45 |
#include <stdlib.h> |
45 |
#include <stdlib.h> |
46 |
#include <unistd.h> |
46 |
#include <unistd.h> |
|
|
47 |
#include <fcntl.h> |
47 |
|
48 |
|
48 |
static void dummy_sighandler(int); |
49 |
static void dummy_sighandler(int); |
49 |
static void restrict_process(const char *); |
50 |
static void restrict_process(const char *); |
Lines 55-68
Link Here
|
55 |
{ |
56 |
{ |
56 |
struct pidfh *ppfh, *pfh; |
57 |
struct pidfh *ppfh, *pfh; |
57 |
sigset_t mask, oldmask; |
58 |
sigset_t mask, oldmask; |
58 |
int ch, nochdir, noclose, restart, serrno; |
59 |
int ch, nochdir, noclose, restart, serrno, append_on_redirection; |
59 |
const char *pidfile, *ppidfile, *user; |
60 |
const char *pidfile, *ppidfile, *user, *stdout_path, *stderr_path; |
60 |
pid_t otherpid, pid; |
61 |
pid_t otherpid, pid; |
61 |
|
62 |
|
62 |
nochdir = noclose = 1; |
63 |
nochdir = noclose = 1; |
63 |
restart = 0; |
64 |
restart = append_on_redirection = 0; |
64 |
ppidfile = pidfile = user = NULL; |
65 |
ppidfile = pidfile = user = stdout_path = stderr_path = NULL; |
65 |
while ((ch = getopt(argc, argv, "cfp:P:ru:")) != -1) { |
66 |
while ((ch = getopt(argc, argv, "cfp:P:ru:o:e:a")) != -1) { |
66 |
switch (ch) { |
67 |
switch (ch) { |
67 |
case 'c': |
68 |
case 'c': |
68 |
nochdir = 0; |
69 |
nochdir = 0; |
Lines 82-87
Link Here
|
82 |
case 'u': |
83 |
case 'u': |
83 |
user = optarg; |
84 |
user = optarg; |
84 |
break; |
85 |
break; |
|
|
86 |
case 'o': |
87 |
stdout_path = optarg; |
88 |
break; |
89 |
case 'e': |
90 |
stderr_path = optarg; |
91 |
break; |
92 |
case 'a': |
93 |
append_on_redirection = 1; |
94 |
break; |
85 |
default: |
95 |
default: |
86 |
usage(); |
96 |
usage(); |
87 |
} |
97 |
} |
Lines 122-127
Link Here
|
122 |
} |
132 |
} |
123 |
} |
133 |
} |
124 |
|
134 |
|
|
|
135 |
/* Handle alternative stdout if requested.*/ |
136 |
if (stdout_path != NULL || stderr_path != NULL) { |
137 |
|
138 |
if (!(stdout_path != NULL && stderr_path != NULL)) { |
139 |
warnx("specify either both or no alternative " |
140 |
"stdout and stderr"); |
141 |
goto exit; |
142 |
} |
143 |
|
144 |
if (noclose == 0) { |
145 |
warnx("redirecting stdout & stderr to /dev/null " |
146 |
"and specifying alternative redirection paths " |
147 |
"is mutually exclusive"); |
148 |
goto exit; |
149 |
} |
150 |
|
151 |
/* daemon() will not close stdin,stdout,stderr now. |
152 |
Redirect output to appropriate descriptors instead */ |
153 |
int output_flags = O_WRONLY | O_CREAT; |
154 |
output_flags |= append_on_redirection ? O_APPEND : O_TRUNC; |
155 |
|
156 |
const int output_mode = 0600; |
157 |
|
158 |
int null_fd = open("/dev/null", O_WRONLY); |
159 |
int stdout_fd = open(stdout_path, output_flags, output_mode); |
160 |
int stderr_fd = open(stderr_path, output_flags, output_mode); |
161 |
|
162 |
if (null_fd == -1 || stdout_fd == -1 || stderr_fd == -1) { |
163 |
warn("could not open one or more stdin / stdout / " |
164 |
"stderr redirection destinations."); |
165 |
goto exit; |
166 |
} |
167 |
|
168 |
if (close(STDOUT_FILENO) == -1 || |
169 |
close(STDERR_FILENO) == -1 || |
170 |
close(STDIN_FILENO) == -1) { |
171 |
warn("could not close original stdin / stdout / " |
172 |
"stderr."); |
173 |
goto exit; |
174 |
} |
175 |
|
176 |
if (dup2(null_fd, STDIN_FILENO) == -1 || |
177 |
dup2(stdout_fd, STDOUT_FILENO) == -1 || |
178 |
dup2(stderr_fd, STDERR_FILENO) == -1) { |
179 |
warn("could not redirect stdin / stdout / stderr " |
180 |
"to sepcified destinations."); |
181 |
goto exit; |
182 |
} |
183 |
|
184 |
} |
185 |
|
125 |
if (daemon(nochdir, noclose) == -1) { |
186 |
if (daemon(nochdir, noclose) == -1) { |
126 |
warn("daemon"); |
187 |
warn("daemon"); |
127 |
goto exit; |
188 |
goto exit; |
Lines 270-276
Link Here
|
270 |
usage(void) |
331 |
usage(void) |
271 |
{ |
332 |
{ |
272 |
(void)fprintf(stderr, |
333 |
(void)fprintf(stderr, |
273 |
"usage: daemon [-cfr] [-p child_pidfile] [-P supervisor_pidfile] " |
334 |
"usage: daemon [-cr] [-f | -e stderr_path -o stdout_path [-a]] " |
|
|
335 |
"[-p child_pidfile] [-P supervisor_pidfile] " |
274 |
"[-u user]\n command arguments ...\n"); |
336 |
"[-u user]\n command arguments ...\n"); |
275 |
exit(1); |
337 |
exit(1); |
276 |
} |
338 |
} |