1
0
mirror of https://github.com/systemd/systemd.git synced 2024-10-31 07:51:21 +03:00

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: d41a9e4fc1
This commit is contained in:
Lennart Poettering 2021-02-19 20:36:45 +01:00 committed by Zbigniew Jędrzejewski-Szmek
parent 27edd36eaa
commit 2306d1778f
7 changed files with 31 additions and 15 deletions

View 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",

View 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);

View 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);

View 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)

View 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);

View 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);
}

View 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();