From fa0c2f7050bcf2dc47ba4fb5e1a4b9b4d40b609c Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Thu, 23 Apr 2020 08:57:12 +0200 Subject: [PATCH] Custom name:foo will now create condition svc/foo w/o path When registering a service there is the option to set a custom PID file, and even tell Finit to maintain that PID file in case the daemon doesn't Until now there hasn't been a way to create custom conditions though ... This patch leverages the new 'name:foo' argument to tas/run/services to trigger condition 'svc/foo' (notice lack of path!) when a custom name is set and either the default, or the custom, PID file is created. Signed-off-by: Joachim Nilsson --- src/cond-w.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/cond-w.c b/src/cond-w.c index d7d1b35a..d7f13288 100644 --- a/src/cond-w.c +++ b/src/cond-w.c @@ -59,15 +59,21 @@ char *mkcond(svc_t *svc, char *buf, size_t len) { char path[256]; char *pidfile; - char *ptr; + char *ptr, *nm; strlcpy(path, svc->cmd, sizeof(path)); ptr = rindex(path, '/'); if (ptr) - *ptr = 0; + *ptr++ = 0; else path[0] = 0; + /* Figure out default name used when registering service */ + if (ptr) + nm = ptr; + else + nm = svc->cmd; + pidfile = pid_file(svc); ptr = strstr(pidfile, "run"); if (ptr) @@ -75,8 +81,14 @@ char *mkcond(svc_t *svc, char *buf, size_t len) else ptr = rindex(pidfile, '/'); - snprintf(buf, len, "svc%s%s%s", path[0] != 0 && path[0] != '/' ? "/" : "", path, ptr); - _d("Composed condition from cmd %s (path %s) and pidfile %s => %s", svc->cmd, path, ptr, buf); + /* Custom name:foo declaration found => svc/foo instead of /svc/bin/path/pidfile-.pid */ + if (strcmp(nm, svc->name)) { + snprintf(buf, len, "svc/%s", svc->name); + _d("Composed condition from svc->name %s => %s", svc->name, buf); + } else { + snprintf(buf, len, "svc%s%s%s", path[0] != 0 && path[0] != '/' ? "/" : "", path, ptr); + _d("Composed condition from cmd %s (path %s) and pidfile %s => %s", svc->cmd, path, ptr, buf); + } /* Case: /var/run/dbus/pid */ ptr = strstr(buf, "/pid");