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 committed 2018-01-15 07:44:33 +01:00
1 parent f25390004b
commit 306bf0f191
2 files changed
+27 -15

No files matched your search

+25 -14
View File
@@ -109,6 +109,18 @@ static int service_timeout_cancel(svc_t *svc)
return err; 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) static int is_norespawn(void)
{ {
return sig_stopped() || return sig_stopped() ||
@@ -216,6 +228,11 @@ static int service_start(svc_t *svc)
if (svc->log.enabled) { if (svc->log.enabled) {
int fd; int fd;
if (svc->log.null) {
redirect_null();
goto logit_done;
}
/* /*
* Open PTY to connect to logger. A pty isn't buffered * Open PTY to connect to logger. A pty isn't buffered
* like a pipe, and it eats newlines so they aren't logged * 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); fd = posix_openpt(O_RDWR);
if (fd == -1) { if (fd == -1) {
svc->log.enabled = 0; svc->log.enabled = 0;
goto logger_err; goto logit_done;
} }
if (grantpt(fd) == -1 || unlockpt(fd) == -1) { if (grantpt(fd) == -1 || unlockpt(fd) == -1) {
close(fd); close(fd);
svc->log.enabled = 0; svc->log.enabled = 0;
goto logger_err; goto logit_done;
} }
/* SIGCHLD is still blocked for grantpt() and fork() */ /* SIGCHLD is still blocked for grantpt() and fork() */
@@ -276,19 +293,11 @@ static int service_start(svc_t *svc)
} }
} }
#ifdef REDIRECT_OUTPUT #ifdef REDIRECT_OUTPUT
else { else
FILE *fp; redirect_null();
fp = fopen("/dev/null", "w");
if (fp) {
dup2(fileno(fp), STDOUT_FILENO);
dup2(fileno(fp), STDERR_FILENO);
fclose(fp);
}
}
#endif #endif
logger_err: logit_done:
sig_unblock(); sig_unblock();
if (svc->inetd.cmd) if (svc->inetd.cmd)
@@ -307,7 +316,7 @@ static int service_start(svc_t *svc)
} }
} else } else
#endif #endif
if (svc->log.enabled) if (svc->log.enabled && !svc->log.null)
waitpid(pid, NULL, 0); waitpid(pid, NULL, 0);
exit(status); exit(status);
} else if (log_is_debug()) { } else if (log_is_debug()) {
@@ -519,6 +528,8 @@ static void parse_log(svc_t *svc, char *arg)
while (tok) { while (tok) {
if (!strcmp(tok, "log")) if (!strcmp(tok, "log"))
svc->log.enabled = 1; svc->log.enabled = 1;
else if (!strcmp(tok, "null") || !strcmp(tok, "/dev/null"))
svc->log.null = 1;
else if (tok[0] == '/') else if (tok[0] == '/')
strlcpy(svc->log.file, tok, sizeof(svc->log.file)); strlcpy(svc->log.file, tok, sizeof(svc->log.file));
else if (!strcmp(tok, "priority") || !strcmp(tok, "prio")) 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 */ /* Set for services we need to redirect stdout/stderr to syslog */
struct { struct {
int enabled; char enabled;
char null;
char file[64]; char file[64];
char prio[20]; char prio[20];
char ident[20]; char ident[20];