Add support for configurable redirect to /dev/null, issue #21

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson
2018-01-15 07:44:33 +01:00
parent f25390004b
commit 306bf0f191
2 changed files with 27 additions and 15 deletions
+25 -14
View File
@@ -109,6 +109,18 @@ static int service_timeout_cancel(svc_t *svc)
return err;
}
static void redirect_null(void)
{
FILE *fp;
fp = fopen("/dev/null", "w");
if (fp) {
dup2(fileno(fp), STDOUT_FILENO);
dup2(fileno(fp), STDERR_FILENO);
fclose(fp);
}
}
static int is_norespawn(void)
{
return sig_stopped() ||
@@ -216,6 +228,11 @@ static int service_start(svc_t *svc)
if (svc->log.enabled) {
int fd;
if (svc->log.null) {
redirect_null();
goto logit_done;
}
/*
* Open PTY to connect to logger. A pty isn't buffered
* like a pipe, and it eats newlines so they aren't logged
@@ -223,12 +240,12 @@ static int service_start(svc_t *svc)
fd = posix_openpt(O_RDWR);
if (fd == -1) {
svc->log.enabled = 0;
goto logger_err;
goto logit_done;
}
if (grantpt(fd) == -1 || unlockpt(fd) == -1) {
close(fd);
svc->log.enabled = 0;
goto logger_err;
goto logit_done;
}
/* SIGCHLD is still blocked for grantpt() and fork() */
@@ -276,19 +293,11 @@ static int service_start(svc_t *svc)
}
}
#ifdef REDIRECT_OUTPUT
else {
FILE *fp;
fp = fopen("/dev/null", "w");
if (fp) {
dup2(fileno(fp), STDOUT_FILENO);
dup2(fileno(fp), STDERR_FILENO);
fclose(fp);
}
}
else
redirect_null();
#endif
logger_err:
logit_done:
sig_unblock();
if (svc->inetd.cmd)
@@ -307,7 +316,7 @@ static int service_start(svc_t *svc)
}
} else
#endif
if (svc->log.enabled)
if (svc->log.enabled && !svc->log.null)
waitpid(pid, NULL, 0);
exit(status);
} else if (log_is_debug()) {
@@ -519,6 +528,8 @@ static void parse_log(svc_t *svc, char *arg)
while (tok) {
if (!strcmp(tok, "log"))
svc->log.enabled = 1;
else if (!strcmp(tok, "null") || !strcmp(tok, "/dev/null"))
svc->log.null = 1;
else if (tok[0] == '/')
strlcpy(svc->log.file, tok, sizeof(svc->log.file));
else if (!strcmp(tok, "priority") || !strcmp(tok, "prio"))
+2 -1
View File
@@ -112,7 +112,8 @@ typedef struct svc {
/* Set for services we need to redirect stdout/stderr to syslog */
struct {
int enabled;
char enabled;
char null;
char file[64];
char prio[20];
char ident[20];