From 280608f30e181c8b9b4b8e34ead87bb0532b1bd3 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 11 Mar 2021 21:56:49 +0100 Subject: [PATCH] plugins: pidfile: track PID file in svc, if svc has none declared Services that create their own PID files usually don't declare one with Finit. This patch adds support to track those PID files anywayt at runtime for the purpose of identifying match svc_t when a PID file is removed, i.e. when a service exits. - On IN_CREATE the pidfile.so plugin saves the pid file name in svc_t - On ON_DELETE the pidfile.so plugin finds svc_t based on pid file Quicker tracking and less dead code, win-win. Signed-off-by: Joachim Wiberg --- plugins/pidfile.c | 3 +++ src/svc.c | 12 +++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/plugins/pidfile.c b/plugins/pidfile.c index dfb3b7df..fbd525c0 100644 --- a/plugins/pidfile.c +++ b/plugins/pidfile.c @@ -84,6 +84,9 @@ static void pidfile_update_conds(char *dir, char *name, uint32_t mask) mkcond(svc, cond, sizeof(cond)); if (mask & (IN_CLOSE_WRITE | IN_ATTRIB | IN_MODIFY | IN_MOVED_TO)) { svc_started(svc); + if (!svc_has_pidfile(svc)) + pid_file_set(svc, fn, 1); + if (svc_is_forking(svc)) { pid_t pid; diff --git a/src/svc.c b/src/svc.c index 323243c4..e9e53ebb 100644 --- a/src/svc.c +++ b/src/svc.c @@ -416,7 +416,17 @@ svc_t *svc_find_by_pidfile(char *fn) pid = pid_file_read(fn); if (pid <= 0) { - _d("pid_file_read(%s) => %d", fn, pid); + 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; + + return svc; + } + return NULL; }