Handle log redirect to syslog when logit is disabled

`--disable-logit` at configure time means redirecting stdout from a
service to a file is completely disabled.  Fallback to `logger` can
not be guaranteed to provide log file rotation, so all logs will be
sent to syslog instead.  In case logger is also missing, a simple
redirect using fork() + syslog() is used as fallback.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson committed 2020-03-30 11:53:43 +02:00
1 parent 89c7171223
commit bf6e599584
2 files changed
+57 -16

No files matched your search

+3
View File
@@ -191,6 +191,9 @@ AS_IF([test "x$enable_inetd" != "xno"], [
enable_inetd="yes" enable_inetd="yes"
AC_DEFINE(INETD_ENABLED, 1, [Enable built-in inetd])]) AC_DEFINE(INETD_ENABLED, 1, [Enable built-in inetd])])
AS_IF([test "x$enable_logit" != "xno"], [
AC_DEFINE(LOGIT_ENABLED, 1, [Enable logit tool, used for log redirection by Finit])])
AS_IF([test "x$enable_logrotate" != "xno"], [ AS_IF([test "x$enable_logrotate" != "xno"], [
AC_DEFINE(LOGROTATE_ENABLED, 1, [Enable built-in rotation of /var/log/wtmp et al.])]) AC_DEFINE(LOGROTATE_ENABLED, 1, [Enable built-in rotation of /var/log/wtmp et al.])])
+54 -16
View File
@@ -133,30 +133,33 @@ static int fredirect(const char *file)
return -1; return -1;
} }
#ifndef LOGIT_ENABLED
/* /*
* Handle redirection of process output, if enabled * Fallback in case we don't even have logger on the system.
* XXX: we should parse 'prio' here to get facility.level
*/ */
static int redirect(svc_t *svc) static void fallback_logger(char *ident, char *prio)
{ {
/* Redirect inetd socket to stdin for connection */ int facility = LOG_DAEMON;
#ifdef INETD_ENABLED int level = LOG_NOTICE;
if (svc_is_inetd_conn(svc)) { char buf[256];
dup2(svc->stdin_fd, STDIN_FILENO);
close(svc->stdin_fd); openlog(ident, LOG_NOWAIT | LOG_PID, facility);
dup2(STDIN_FILENO, STDOUT_FILENO); while ((fgets(buf, sizeof(buf), stdin)))
dup2(STDIN_FILENO, STDERR_FILENO); syslog(level, "%s", buf);
} else
closelog();
}
#endif #endif
if (svc->log.enabled) { /*
* Redirect output to syslog using the command line logit tool
*/
static int lredirect(svc_t *svc)
{
pid_t pid; pid_t pid;
int fd; int fd;
if (svc->log.null)
return fredirect("/dev/null");
if (svc->log.console)
return fredirect(CONSOLE);
/* /*
* 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
@@ -191,6 +194,7 @@ static int redirect(svc_t *svc)
sig_unblock(); sig_unblock();
if (svc->log.file[0] == '/') { if (svc->log.file[0] == '/') {
#ifdef LOGIT_ENABLED
char sz[20], num[3]; char sz[20], num[3];
snprintf(sz, sizeof(sz), "%d", logfile_size_max); snprintf(sz, sizeof(sz), "%d", logfile_size_max);
@@ -198,6 +202,9 @@ static int redirect(svc_t *svc)
execlp("logit", "logit", "-f", svc->log.file, "-n", sz, "-r", num, NULL); execlp("logit", "logit", "-f", svc->log.file, "-n", sz, "-r", num, NULL);
_exit(0); _exit(0);
#else
logit(LOG_INFO, "logit disabled, logging %s to syslog instead", svc->name);
#endif
} }
if (svc->log.ident[0]) if (svc->log.ident[0])
@@ -205,11 +212,42 @@ static int redirect(svc_t *svc)
if (svc->log.prio[0]) if (svc->log.prio[0])
prio = svc->log.prio; prio = svc->log.prio;
#ifdef LOGIT_ENABLED
execlp("logit", "logit", "-t", tag, "-p", prio, NULL); execlp("logit", "logit", "-t", tag, "-p", prio, NULL);
#else
if (whichp("logger"))
execlp("logger", "logger", "-t", tag, "-p", prio, NULL);
fallback_logger(tag, prio);
#endif
_exit(0); _exit(0);
} }
return close(fd); return close(fd);
}
/*
* Handle redirection of process output, if enabled
*/
static int redirect(svc_t *svc)
{
/* Redirect inetd socket to stdin for connection */
#ifdef INETD_ENABLED
if (svc_is_inetd_conn(svc)) {
dup2(svc->stdin_fd, STDIN_FILENO);
close(svc->stdin_fd);
dup2(STDIN_FILENO, STDOUT_FILENO);
dup2(STDIN_FILENO, STDERR_FILENO);
} else
#endif
if (svc->log.enabled) {
if (svc->log.null)
return fredirect("/dev/null");
if (svc->log.console)
return fredirect(CONSOLE);
return lredirect(svc);
} else if (log_is_debug()) } else if (log_is_debug())
return fredirect(CONSOLE); return fredirect(CONSOLE);
#ifdef REDIRECT_OUTPUT #ifdef REDIRECT_OUTPUT