From 10577f3e486c7eba87f3fe7f9ad77d28b6ce4176 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 25 Mar 2022 06:53:59 +0100 Subject: [PATCH] Allow matching of svc_t and pid file, prefer PID over file lookup When a SysV init script starts a daemon, Finit knows nothing of the PID it should monitor. The PID is written, by start-stop-daemon or the daemon itself, to the PID file. Finit monitors for new PID files and can match the PID in such files with an svc_t. For the regular use-case, we prefer first looking up the matching svc_t based on the PID -- assuming we start and monitor the service. As a fallback we resort to mathching the svc_t's declared PID file with the new file we just discovered. Signed-off-by: Joachim Wiberg --- src/svc.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/svc.c b/src/svc.c index a27ee54f..441ac34e 100644 --- a/src/svc.c +++ b/src/svc.c @@ -473,23 +473,23 @@ svc_t *svc_find_by_pidfile(char *fn) pid_t pid; pid = pid_file_read(fn); - if (pid <= 0) { - for (svc = svc_iterator(&iter, 1); svc; svc = svc_iterator(&iter, 0)) { - int v = 0; - - if (svc->pidfile[0] == '!') - v = 1; - if (strcmp(&svc->pidfile[v], fn)) - continue; - + if (pid > 1) { + svc = svc_find_by_pid(pid); + if (svc) return svc; - } - - return NULL; } + /* + * No PID in file, or a sysv service. So it has probably forked + * off a chiled that we don't know about. See if we can match + * the PID file to a service instead. + */ for (svc = svc_iterator(&iter, 1); svc; svc = svc_iterator(&iter, 0)) { - if (svc->pid != pid) + int v = 0; + + if (svc->pidfile[0] == '!') + v = 1; + if (strcmp(&svc->pidfile[v], fn)) continue; return svc;