From 6cc44a87c9ec43d4259cffc93ac3f923b8b55aab Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Sun, 19 Jun 2022 15:39:01 +0200 Subject: [PATCH] Fix #275: 'initctl status foo' should list all instances This is a fix to a regression introduced in e51587c. The idea is to have 'initctl status foo' show all instances of foo, provided there are more than one. E.g. root@anarchy:~# initctl -t status foo 0 foo:1 stopped [--234-----] hej knekt 0 foo:2 stopped [--234-----] hej snigel 0 foo:3 stopped [--234-----] hej kalas And not foobar: 0 foo:1 stopped [--234-----] hej knekt 0 foo:2 stopped [--234-----] hej snigel 0 foo:3 stopped [--234-----] hej kalas 0 foobar stopped [--234-----] hej foobar If you type an exact match you can drill down like so: root@anarchy:~# initctl status foo:1 Status : stopped (code=exited, status=0/SUCCESS, manual=yes) Identity : foo:1 Description : hej knekt ... Signed-off-by: Joachim Wiberg --- src/initctl.c | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/initctl.c b/src/initctl.c index 863bbe18..f830278b 100644 --- a/src/initctl.c +++ b/src/initctl.c @@ -774,6 +774,28 @@ static void show_cgroup_tree(char *group, char *pfx) cgroup_tree(path, pfx, 0, 0); } +/* + * arg: 'foo' should match foo:1 foo:2, etc. but not foobar + * arg: 'foo:1' should only match foo:1 + * arg: 'foo:' is allowed to fail, unsupported syntax atm + * arg: 'foo:*' is allowed to fail, unsupported syntax atm + */ +static int svc_compare(svc_t *svc, char *arg) +{ + char ident[MAX_IDENT_LEN]; + char *ptr; + + svc_ident(svc, ident, sizeof(ident)); + ptr = strchr(ident, ':'); + if (ptr && !strchr(arg, ':')) + *ptr = 0; + + if (!strcmp(ident, arg)) + return 1; + + return 0; +} + static int show_status(char *arg) { char ident[MAX_IDENT_LEN]; @@ -787,17 +809,11 @@ static int show_status(char *arg) long now = jiffies(); char uptm[42] = "N/A"; char *pidfn = NULL; - int exact = 0; - for (svc = client_svc_iterator(1); svc; svc = client_svc_iterator(0)) { - svc_ident(svc, ident, sizeof(ident)); - if (string_compare(ident, arg)) - num++; - if (string_case_compare(ident, arg)) - exact++; - } + for (svc = client_svc_iterator(1); svc; svc = client_svc_iterator(0)) + num += svc_compare(svc, arg); - if (num > 1 && !exact) + if (num > 1) break; svc = client_svc_find(arg); @@ -878,7 +894,7 @@ static int show_status(char *arg) char *lvls; svc_ident(svc, ident, sizeof(ident)); - if (num && !string_compare(ident, arg)) + if (num && !svc_compare(svc, arg)) continue; printf("%-*d ", pw, svc->pid);