View | Details | Raw Unified | Return to bug 207248
Collapse All | Expand All

(-)usr.sbin/daemon/daemon.8 (-2 / +33 lines)
Lines 26-32 Link Here
26
.\"
26
.\"
27
.\" $FreeBSD$
27
.\" $FreeBSD$
28
.\"
28
.\"
29
.Dd September 13, 2013
29
.Dd February 14, 2016
30
.Dt DAEMON 8
30
.Dt DAEMON 8
31
.Os
31
.Os
32
.Sh NAME
32
.Sh NAME
Lines 34-40 Link Here
34
.Nd run detached from the controlling terminal
34
.Nd run detached from the controlling terminal
35
.Sh SYNOPSIS
35
.Sh SYNOPSIS
36
.Nm
36
.Nm
37
.Op Fl cfr
37
.Op Fl cr
38
.Op Fl f | Fl e Ar stderr_path Fl o Ar stdout_path Op Fl a
38
.Op Fl p Ar child_pidfile
39
.Op Fl p Ar child_pidfile
39
.Op Fl P Ar supervisor_pidfile
40
.Op Fl P Ar supervisor_pidfile
40
.Op Fl u Ar user
41
.Op Fl u Ar user
Lines 54-59 Link Here
54
.It Fl f
55
.It Fl f
55
Redirect standard input, standard output and standard error to
56
Redirect standard input, standard output and standard error to
56
.Pa /dev/null .
57
.Pa /dev/null .
58
.It Fl e
59
Redirect standard error to
60
.Pa stderr_path.
61
.br
62
Use only with
63
.Fl o,
64
optionally
65
.Fl a
66
and not with
67
.Fl f.
68
.It Fl o
69
Redirect standard output to
70
.Pa stdout_path.
71
.br
72
Use only with
73
.Fl e,
74
optionally
75
.Fl a
76
and not with
77
.Fl f.
78
.It Fl a
79
Append to stdout / stderr redirection destination instead of overwriting it.
57
.It Fl p Ar child_pidfile
80
.It Fl p Ar child_pidfile
58
Write the ID of the created process into the
81
Write the ID of the created process into the
59
.Ar child_pidfile
82
.Ar child_pidfile
Lines 131-136 Link Here
131
stop the service, causing
154
stop the service, causing
132
.Nm
155
.Nm
133
to restart the child.
156
to restart the child.
157
.Pp
158
Redirecting stdout and stderr may be useful if the application
159
does not provide alternative means to log output to a file.
160
.br
161
/dev/null is a valid redirection target.
162
.br
163
Note that the log file is owned by the user starting 
164
.Xr daemon(8).
134
.Sh EXIT STATUS
165
.Sh EXIT STATUS
135
The
166
The
136
.Nm
167
.Nm
(-)usr.sbin/daemon/daemon.c (-6 / +68 lines)
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
}

Return to bug 207248