]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
systemctl: don't search in the full argv[0] for the invocation name
authorLennart Poettering <lennart@poettering.net>
Fri, 19 Feb 2021 19:36:45 +0000 (20:36 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 20 Feb 2021 13:45:27 +0000 (14:45 +0100)
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

src/basic/process-util.c
src/basic/process-util.h
src/core/main.c
src/mount/mount-tool.c
src/resolve/resolvectl.c
src/systemctl/systemctl.c
src/udev/udevadm.c

index 0851613fc9ef03c3c57503d7c83290645f4cefab..304b03960a527469985e9b302e40346e1ecebfbd 100644 (file)
@@ -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",
index 6144f142c47a0f132a8f9ada625c21c9c05fc555..bf709f6669a293c854a7f12586e283703e1cc8f0 100644 (file)
@@ -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);
index 67189fef6d3fc4cafee641be1907ebf3c34c6022..b8ff184af0281e0a8f7637bcffcb5eb3d7146989 100644 (file)
@@ -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);
index f30061dac74e2012f6d26179b7b1115835060444..c1be0e0afbe7cb55ca2118a83e9b103d6763bd55 100644 (file)
@@ -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)
 
index b645147fd8297393d355b367c06bd6f3283f7968..2bd18d2c6d3508822ba320ca105ae7145639b4cc 100644 (file)
@@ -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);
index f92598a7bb547d930efb8b1fe143f31e1db3eea5..bcc5f896b9acc8a901cfa5ecbcfa95adb43d4409 100644 (file)
@@ -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);
         }
index 47d1a2fb9c9be1cbfe46baaf5086c5cedc99e096..e55ae4bd54cca6064edc12f86459d84b5e2002ab 100644 (file)
@@ -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();