From: Lennart Poettering Date: Fri, 19 Feb 2021 19:36:45 +0000 (+0100) Subject: systemctl: don't search in the full argv[0] for the invocation name X-Git-Tag: v248-rc1~28 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2306d1778f2479cb16423d51d82f8f31b15146d1;p=thirdparty%2Fsystemd.git 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 --- diff --git a/src/basic/process-util.c b/src/basic/process-util.c index 0851613fc9e..304b03960a5 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 6144f142c47..bf709f6669a 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 67189fef6d3..b8ff184af02 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 f30061dac74..c1be0e0afbe 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 b645147fd82..2bd18d2c6d3 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 f92598a7bb5..bcc5f896b9a 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 47d1a2fb9c9..e55ae4bd54c 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();