diff options
Diffstat (limited to 'service.c')
-rw-r--r-- | service.c | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/service.c b/service.c index 12f465f..718397d 100644 --- a/service.c +++ b/service.c @@ -16,10 +16,12 @@ #include <assert.h> #include <err.h> +#include <errno.h> #include <fcntl.h> #include <limits.h> #include <paths.h> #include <signal.h> +#include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> @@ -201,6 +203,33 @@ void serviceRestart(struct Service *service) { } } +static void serviceLog(struct Service *service, int pri, const char *log) { + syslog( + pri, "%s[%ju]: %s", + service->name, (intmax_t)service->pid, log + ); +} + +void serviceRead(struct Service *service) { + const char *out; + while (NULL != (out = lineRead(&service->outLine, service->outPipe[0]))) { + serviceLog(service, LOG_INFO, out); + } + if (errno != EAGAIN) syslog(LOG_ERR, "read: %m"); + const char *err; + while (NULL != (err = lineRead(&service->errLine, service->errPipe[0]))) { + serviceLog(service, LOG_NOTICE, err); + } + if (errno != EAGAIN) syslog(LOG_ERR, "read: %m"); +} + +static void serviceFlush(struct Service *service) { + const char *out = lineFlush(&service->outLine); + const char *err = lineFlush(&service->errLine); + if (out) serviceLog(service, LOG_INFO, out); + if (err) serviceLog(service, LOG_NOTICE, err); +} + void serviceReap(pid_t pid, int status) { struct Service *service = NULL; for (size_t i = 0; i < services.len; ++i) { @@ -213,8 +242,7 @@ void serviceReap(pid_t pid, int status) { syslog(LOG_WARNING, "reaping unknown child %jd", (intmax_t)pid); return; } - - // TODO: Flush line buffers. + serviceFlush(service); service->state = Stop; if (WIFEXITED(status)) { |