diff options
author | Lennart Poettering <lennart@poettering.net> | 2021-02-19 20:36:45 +0100 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2021-02-20 14:45:27 +0100 |
commit | 2306d1778f2479cb16423d51d82f8f31b15146d1 (patch) | |
tree | e8a2f3b310155f1c5a25378a26c58ab1198e4e28 /src | |
parent | network: fix typo (diff) | |
download | systemd-2306d1778f2479cb16423d51d82f8f31b15146d1.tar.xz systemd-2306d1778f2479cb16423d51d82f8f31b15146d1.zip |
systemctl: don't search in the full argv[0] for the invocation name
argv[0] might be prefixed by a path, and we shouldn't get confused by
that. Hence provide a simple helper call that abstracts the checking
away, which we can use everywhere, and expose the same behaviour, even
if argv[0] is not set.
(While we are at it, port all other multi-call binaries over to the new
helper, too)
Follow-up for: d41a9e4fc1e1bcdefc8d358da2744a97aac5820a
Diffstat (limited to 'src')
-rw-r--r-- | src/basic/process-util.c | 10 | ||||
-rw-r--r-- | src/basic/process-util.h | 2 | ||||
-rw-r--r-- | src/core/main.c | 2 | ||||
-rw-r--r-- | src/mount/mount-tool.c | 5 | ||||
-rw-r--r-- | src/resolve/resolvectl.c | 5 | ||||
-rw-r--r-- | src/systemctl/systemctl.c | 15 | ||||
-rw-r--r-- | src/udev/udevadm.c | 7 |
7 files changed, 31 insertions, 15 deletions
diff --git a/src/basic/process-util.c b/src/basic/process-util.c index 0851613fc9..304b03960a 100644 --- a/src/basic/process-util.c +++ b/src/basic/process-util.c @@ -1620,6 +1620,16 @@ int setpriority_closest(int priority) { return 0; } +bool invoked_as(char *argv[], const char *token) { + if (!argv || isempty(argv[0])) + return false; + + if (isempty(token)) + return false; + + return strstr(last_path_component(argv[0]), token); +} + static const char *const ioprio_class_table[] = { [IOPRIO_CLASS_NONE] = "none", [IOPRIO_CLASS_RT] = "realtime", diff --git a/src/basic/process-util.h b/src/basic/process-util.h index 6144f142c4..bf709f6669 100644 --- a/src/basic/process-util.h +++ b/src/basic/process-util.h @@ -199,3 +199,5 @@ assert_cc(TASKS_MAX <= (unsigned long) PID_T_MAX); int pidfd_get_pid(int fd, pid_t *ret); int setpriority_closest(int priority); + +bool invoked_as(char *argv[], const char *token); diff --git a/src/core/main.c b/src/core/main.c index 67189fef6d..b8ff184af0 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -1415,7 +1415,7 @@ static void redirect_telinit(int argc, char *argv[]) { if (getpid_cached() == 1) return; - if (!strstr(program_invocation_short_name, "init")) + if (!invoked_as(argv, "init")) return; execv(SYSTEMCTL_BINARY_PATH, argv); diff --git a/src/mount/mount-tool.c b/src/mount/mount-tool.c index f30061dac7..c1be0e0afb 100644 --- a/src/mount/mount-tool.c +++ b/src/mount/mount-tool.c @@ -27,6 +27,7 @@ #include "parse-util.h" #include "path-util.h" #include "pretty-print.h" +#include "process-util.h" #include "sort-util.h" #include "spawn-polkit-agent.h" #include "stat-util.h" @@ -182,8 +183,8 @@ static int parse_argv(int argc, char *argv[]) { assert(argc >= 0); assert(argv); - if (strstr(program_invocation_short_name, "systemd-umount")) - arg_action = ACTION_UMOUNT; + if (invoked_as(argv, "systemd-umount")) + arg_action = ACTION_UMOUNT; while ((c = getopt_long(argc, argv, "hqH:M:t:o:p:AuGl", options, NULL)) >= 0) diff --git a/src/resolve/resolvectl.c b/src/resolve/resolvectl.c index b645147fd8..2bd18d2c6d 100644 --- a/src/resolve/resolvectl.c +++ b/src/resolve/resolvectl.c @@ -26,6 +26,7 @@ #include "parse-argument.h" #include "parse-util.h" #include "pretty-print.h" +#include "process-util.h" #include "resolvconf-compat.h" #include "resolvectl.h" #include "resolved-def.h" @@ -3394,9 +3395,9 @@ static int run(int argc, char **argv) { setlocale(LC_ALL, ""); log_setup(); - if (streq(program_invocation_short_name, "resolvconf")) + if (invoked_as(argv, "resolvconf")) r = resolvconf_parse_argv(argc, argv); - else if (streq(program_invocation_short_name, "systemd-resolve")) + else if (invoked_as(argv, "systemd-resolve")) r = compat_parse_argv(argc, argv); else r = native_parse_argv(argc, argv); diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index f92598a7bb..bcc5f896b9 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -14,6 +14,7 @@ #include "parse-argument.h" #include "path-util.h" #include "pretty-print.h" +#include "process-util.h" #include "rlimit-util.h" #include "sigbus.h" #include "signal-util.h" @@ -954,26 +955,26 @@ int systemctl_dispatch_parse_argv(int argc, char *argv[]) { assert(argc >= 0); assert(argv); - if (strstr_ptr(argv[0], "halt")) { + if (invoked_as(argv, "halt")) { arg_action = ACTION_HALT; return halt_parse_argv(argc, argv); - } else if (strstr_ptr(argv[0], "poweroff")) { + } else if (invoked_as(argv, "poweroff")) { arg_action = ACTION_POWEROFF; return halt_parse_argv(argc, argv); - } else if (strstr_ptr(argv[0], "reboot")) { + } else if (invoked_as(argv, "reboot")) { if (kexec_loaded()) arg_action = ACTION_KEXEC; else arg_action = ACTION_REBOOT; return halt_parse_argv(argc, argv); - } else if (strstr_ptr(argv[0], "shutdown")) { + } else if (invoked_as(argv, "shutdown")) { arg_action = ACTION_POWEROFF; return shutdown_parse_argv(argc, argv); - } else if (strstr_ptr(argv[0], "init")) { + } else if (invoked_as(argv, "init")) { /* Matches invocations as "init" as well as "telinit", which are synonymous when run * as PID != 1 on SysV. @@ -984,7 +985,7 @@ int systemctl_dispatch_parse_argv(int argc, char *argv[]) { * for us if you invoke "init" you get "systemd", but it will execve() "systemctl" * immediately with argv[] unmodified if PID is != 1. If you invoke "telinit" you directly * get "systemctl". In both cases we shall do the same thing, which is why we do - * strstr_ptr(argv[0], "init") here, as a quick way to match both. + * invoked_as(argv, "init") here, as a quick way to match both. * * Also see redirect_telinit() in src/core/main.c. */ @@ -998,7 +999,7 @@ int systemctl_dispatch_parse_argv(int argc, char *argv[]) { return 1; } - } else if (strstr_ptr(argv[0], "runlevel")) { + } else if (invoked_as(argv, "runlevel")) { arg_action = ACTION_RUNLEVEL; return runlevel_parse_argv(argc, argv); } diff --git a/src/udev/udevadm.c b/src/udev/udevadm.c index 47d1a2fb9c..e55ae4bd54 100644 --- a/src/udev/udevadm.c +++ b/src/udev/udevadm.c @@ -8,13 +8,14 @@ #include "alloc-util.h" #include "main-func.h" #include "pretty-print.h" +#include "process-util.h" #include "selinux-util.h" #include "string-util.h" +#include "udev-util.h" #include "udevadm.h" #include "udevd.h" -#include "udev-util.h" -#include "verbs.h" #include "util.h" +#include "verbs.h" static int help(void) { static const char *const short_descriptions[][2] = { @@ -111,7 +112,7 @@ static int udevadm_main(int argc, char *argv[]) { static int run(int argc, char *argv[]) { int r; - if (strstr(program_invocation_short_name, "udevd")) + if (invoked_as(argv, "udevd")) return run_udevd(argc, argv); udev_parse_config(); |