Skip to content

Commit 9934e9f

Browse files
Higgs1williamh
authored andcommitted
supervise-daemon: implement output_logger and error_logger.
Allows redirecting process stdin and stdout to another process, just like is already possible with start-stop-daemon. Also added --stdout-logger and --stderr-logger to the man page.
1 parent f1e5510 commit 9934e9f

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

man/supervise-daemon.8

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,23 @@ The logfile can also be a named pipe.
158158
The same thing as
159159
.Fl 1 , -stdout
160160
but with the standard error output.
161+
.It Fl -stdout-logger Ar cmd
162+
Run cmd as a child process redirecting the standard output to the
163+
standard input of cmd when started with
164+
.Fl background .
165+
Cmd must be an absolute pathname, but relative to the path optionally given with
166+
.Fl r , -chroot .
167+
This process must be prepared to accept input on stdin and be able to
168+
log it or send it to another location.
169+
.It Fl -stderr-logger Ar cmd
170+
Run cmd as a child process and
171+
Redirect the standard error of the process to the standard input of cmd
172+
when started with
173+
.Fl background .
174+
Cmd must be an absolute pathname, but relative to the path optionally given with
175+
.Fl r , -chroot .
176+
This process must be prepared to accept input on stdin and be able to
177+
log it or send it to another location.
161178
.It Fl -capabilities Ar cap-list
162179
Start the daemon with the listed inheritable, ambient and bounding capabilities.
163180
The format is the same as in cap_iab(3).

sh/supervise-daemon.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ supervise_start()
3030
${chroot:+--chroot} $chroot \
3131
${output_log+--stdout} ${output_log} \
3232
${error_log+--stderr} $error_log \
33+
${output_logger:+--stdout-logger \"$output_logger\"} \
34+
${error_logger:+--stderr-logger \"$error_logger\"} \
3335
${pidfile:+--pidfile} $pidfile \
3436
${respawn_delay:+--respawn-delay} $respawn_delay \
3537
${respawn_max:+--respawn-max} $respawn_max \

src/supervise-daemon/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
executable('supervise-daemon',
2-
['supervise-daemon.c', misc_c, plugin_c, schedules_c, usage_c, version_h],
2+
['supervise-daemon.c', '../start-stop-daemon/pipes.c', misc_c, plugin_c, schedules_c, usage_c, version_h],
33
c_args : [cc_branding_flags, cc_pam_flags, cc_cap_flags, cc_selinux_flags],
44
link_with: [libeinfo, librc],
55
dependencies: [dl_dep, pam_dep, cap_dep, util_dep, selinux_dep],

src/supervise-daemon/supervise-daemon.c

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ static struct pam_conv conv = { NULL, NULL};
6262
#include "queue.h"
6363
#include "rc.h"
6464
#include "misc.h"
65+
#include "../start-stop-daemon/pipes.h"
6566
#include "plugin.h"
6667
#include "schedules.h"
6768
#include "_usage.h"
@@ -79,6 +80,8 @@ enum {
7980
LONGOPT_OOM_SCORE_ADJ,
8081
LONGOPT_NO_NEW_PRIVS,
8182
LONGOPT_SECBITS,
83+
LONGOPT_STDERR_LOGGER,
84+
LONGOPT_STDOUT_LOGGER,
8285
};
8386

8487
const char *applet = NULL;
@@ -110,6 +113,8 @@ const struct option longopts[] = {
110113
{ "user", 1, NULL, 'u'},
111114
{ "stdout", 1, NULL, '1'},
112115
{ "stderr", 1, NULL, '2'},
116+
{ "stdout-logger",1, NULL, LONGOPT_STDOUT_LOGGER},
117+
{ "stderr-logger",1, NULL, LONGOPT_STDERR_LOGGER},
113118
{ "reexec", 0, NULL, '3'},
114119
longopts_COMMON
115120
};
@@ -138,6 +143,8 @@ const char * const longopts_help[] = {
138143
"Change the process user",
139144
"Redirect stdout to file",
140145
"Redirect stderr to file",
146+
"Redirect stdout to process",
147+
"Redirect stderr to process",
141148
"reexec (used internally)",
142149
longopts_help_COMMON
143150
};
@@ -160,6 +167,8 @@ static int stdout_fd;
160167
static int stderr_fd;
161168
static char *redirect_stderr = NULL;
162169
static char *redirect_stdout = NULL;
170+
static char *stderr_process = NULL;
171+
static char *stdout_process = NULL;
163172
#ifdef TIOCNOTTY
164173
static int tty_fd = -1;
165174
#endif
@@ -549,6 +558,12 @@ RC_NORETURN static void child_process(char *exec, char **argv)
549558
eerrorx("%s: unable to open the logfile"
550559
" for stdout `%s': %s",
551560
applet, redirect_stdout, strerror(errno));
561+
} else if (stdout_process) {
562+
stdout_fd = rc_pipe_command(stdout_process);
563+
if (stdout_fd == -1)
564+
eerrorx("%s: unable to open the logging process"
565+
" for stdout `%s': %s",
566+
applet, stdout_process, strerror(errno));
552567
}
553568
if (redirect_stderr) {
554569
if ((stderr_fd = open(redirect_stderr,
@@ -557,12 +572,18 @@ RC_NORETURN static void child_process(char *exec, char **argv)
557572
eerrorx("%s: unable to open the logfile"
558573
" for stderr `%s': %s",
559574
applet, redirect_stderr, strerror(errno));
575+
} else if (stderr_process) {
576+
stderr_fd = rc_pipe_command(stderr_process);
577+
if (stderr_fd == -1)
578+
eerrorx("%s: unable to open the logging process"
579+
" for stderr `%s': %s",
580+
applet, stderr_process, strerror(errno));
560581
}
561582

562583
dup2(stdin_fd, STDIN_FILENO);
563-
if (redirect_stdout || rc_yesno(getenv("EINFO_QUIET")))
584+
if (redirect_stdout || stdout_process || rc_yesno(getenv("EINFO_QUIET")))
564585
dup2(stdout_fd, STDOUT_FILENO);
565-
if (redirect_stderr || rc_yesno(getenv("EINFO_QUIET")))
586+
if (redirect_stderr || stderr_process || rc_yesno(getenv("EINFO_QUIET")))
566587
dup2(stderr_fd, STDERR_FILENO);
567588

568589
cloexec_fds_from(3);
@@ -1039,6 +1060,14 @@ int main(int argc, char **argv)
10391060
reexec = true;
10401061
break;
10411062

1063+
case LONGOPT_STDOUT_LOGGER: /* --stdout-logger "command to run for stdout logging" */
1064+
stdout_process = optarg;
1065+
break;
1066+
1067+
case LONGOPT_STDERR_LOGGER: /* --stderr-logger "command to run for stderr logging" */
1068+
stderr_process = optarg;
1069+
break;
1070+
10421071
case_RC_COMMON_GETOPT
10431072
}
10441073

0 commit comments

Comments
 (0)