From 34c39437f2bc2045a7017230dca7179faee5b4e6 Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Sat, 15 Aug 2020 14:46:11 -0400 Subject: Read service pipes Changed line reading functions to return const char * because modifying the strings by adding '\0' into them screws up the following call. --- service.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) (limited to 'service.c') diff --git a/service.c b/service.c index 12f465f..718397d 100644 --- a/service.c +++ b/service.c @@ -16,10 +16,12 @@ #include #include +#include #include #include #include #include +#include #include #include #include @@ -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)) { -- cgit 1.4.1