]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/systemctl/systemctl.c
Merge pull request #18481 from keszybz/rpm-restart-post-trans
[thirdparty/systemd.git] / src / systemctl / systemctl.c
index 315202a64b0f2713c0f5b8fe4eeba38c24793e1a..b82e541d7a6a03d8b78b76648c6b1bcd96a40853 100644 (file)
@@ -11,6 +11,7 @@
 #include "main-func.h"
 #include "output-mode.h"
 #include "pager.h"
+#include "parse-argument.h"
 #include "path-util.h"
 #include "pretty-print.h"
 #include "rlimit-util.h"
@@ -496,14 +497,12 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                 case ARG_VERSION:
                         return version();
 
-                case 't': {
-                        const char *p;
-
+                case 't':
                         if (isempty(optarg))
                                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                        "--type= requires arguments.");
 
-                        for (p = optarg;;) {
+                        for (const char *p = optarg;;) {
                                 _cleanup_free_ char *type = NULL;
 
                                 r = extract_first_word(&p, &type, ",", 0);
@@ -537,7 +536,6 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                         }
 
                         break;
-                }
 
                 case 'P':
                         arg_value = true;
@@ -550,10 +548,8 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                                 arg_properties = new0(char*, 1);
                                 if (!arg_properties)
                                         return log_oom();
-                        } else {
-                                const char *p;
-
-                                for (p = optarg;;) {
+                        } else
+                                for (const char *p = optarg;;) {
                                         _cleanup_free_ char *prop = NULL;
 
                                         r = extract_first_word(&p, &prop, ",", 0);
@@ -565,7 +561,6 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                                         if (strv_consume(&arg_properties, TAKE_PTR(prop)) < 0)
                                                 return log_oom();
                                 }
-                        }
 
                         /* If the user asked for a particular property, show it, even if it is empty. */
                         arg_all = true;
@@ -647,7 +642,7 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                         break;
 
                 case ARG_ROOT:
-                        r = parse_path_argument_and_warn(optarg, false, &arg_root);
+                        r = parse_path_argument(optarg, false, &arg_root);
                         if (r < 0)
                                 return r;
                         break;
@@ -683,16 +678,9 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                         break;
 
                 case 's':
-                        if (streq(optarg, "help")) {
-                                DUMP_STRING_TABLE(signal, int, _NSIG);
-                                return 0;
-                        }
-
-                        arg_signal = signal_from_string(optarg);
-                        if (arg_signal < 0)
-                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
-                                                       "Failed to parse signal string %s.",
-                                                       optarg);
+                        r = parse_signal_argument(optarg, &arg_signal);
+                        if (r <= 0)
+                                return r;
                         break;
 
                 case ARG_NO_ASK_PASSWORD:
@@ -782,14 +770,12 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                         arg_boot_loader_entry = empty_to_null(optarg);
                         break;
 
-                case ARG_STATE: {
-                        const char *p;
-
+                case ARG_STATE:
                         if (isempty(optarg))
                                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                        "--state= requires arguments.");
 
-                        for (p = optarg;;) {
+                        for (const char *p = optarg;;) {
                                 _cleanup_free_ char *s = NULL;
 
                                 r = extract_first_word(&p, &s, ",", 0);
@@ -807,7 +793,6 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                                         return log_oom();
                         }
                         break;
-                }
 
                 case 'r':
                         if (geteuid() != 0)
@@ -847,13 +832,11 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                         arg_with_dependencies = true;
                         break;
 
-                case ARG_WHAT: {
-                        const char *p;
-
+                case ARG_WHAT:
                         if (isempty(optarg))
                                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--what= requires arguments.");
 
-                        for (p = optarg;;) {
+                        for (const char *p = optarg;;) {
                                 _cleanup_free_ char *k = NULL;
 
                                 r = extract_first_word(&p, &k, ",", 0);
@@ -877,7 +860,6 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                         }
 
                         break;
-                }
 
                 case ARG_REBOOT_ARG:
                         arg_reboot_argument = optarg;
@@ -959,57 +941,53 @@ int systemctl_dispatch_parse_argv(int argc, char *argv[]) {
         assert(argc >= 0);
         assert(argv);
 
-        if (program_invocation_short_name) {
-
-                if (strstr(program_invocation_short_name, "halt")) {
-                        arg_action = ACTION_HALT;
-                        return halt_parse_argv(argc, argv);
-
-                } else if (strstr(program_invocation_short_name, "poweroff")) {
-                        arg_action = ACTION_POWEROFF;
-                        return halt_parse_argv(argc, argv);
-
-                } else if (strstr(program_invocation_short_name, "reboot")) {
-                        if (kexec_loaded())
-                                arg_action = ACTION_KEXEC;
-                        else
-                                arg_action = ACTION_REBOOT;
-                        return halt_parse_argv(argc, argv);
-
-                } else if (strstr(program_invocation_short_name, "shutdown")) {
-                        arg_action = ACTION_POWEROFF;
-                        return shutdown_parse_argv(argc, argv);
-
-                } else if (strstr(program_invocation_short_name, "init")) {
-
-                        /* Matches invocations as "init" as well as "telinit", which are synonymous when run
-                         * as PID != 1 on SysV.
-                         *
-                         * On SysV "telinit" was the official command to communicate with PID 1, but "init" would
-                         * redirect itself to "telinit" if called with PID != 1. We follow the same logic here still,
-                         * though we add one level of indirection, as we implement "telinit" in "systemctl". Hence, 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(p_i_s_n, "init") here, as a
-                         * quick way to match both.
-                         *
-                         * Also see redirect_telinit() in src/core/main.c. */
-
-                        if (sd_booted() > 0) {
-                                arg_action = _ACTION_INVALID;
-                                return telinit_parse_argv(argc, argv);
-                        } else {
-                                /* Hmm, so some other init system is running, we need to forward this request
-                                 * to it. */
-
-                                arg_action = ACTION_TELINIT;
-                                return 1;
-                        }
-
-                } else if (strstr(program_invocation_short_name, "runlevel")) {
-                        arg_action = ACTION_RUNLEVEL;
-                        return runlevel_parse_argv(argc, argv);
+        if (strstr_ptr(argv[0], "halt")) {
+                arg_action = ACTION_HALT;
+                return halt_parse_argv(argc, argv);
+
+        } else if (strstr_ptr(argv[0], "poweroff")) {
+                arg_action = ACTION_POWEROFF;
+                return halt_parse_argv(argc, argv);
+
+        } else if (strstr_ptr(argv[0], "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")) {
+                arg_action = ACTION_POWEROFF;
+                return shutdown_parse_argv(argc, argv);
+
+        } else if (strstr_ptr(argv[0], "init")) {
+
+                /* Matches invocations as "init" as well as "telinit", which are synonymous when run
+                 * as PID != 1 on SysV.
+                 *
+                 * On SysV "telinit" was the official command to communicate with PID 1, but "init" would
+                 * redirect itself to "telinit" if called with PID != 1. We follow the same logic here still,
+                 * though we add one level of indirection, as we implement "telinit" in "systemctl". Hence,
+                 * 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.
+                 *
+                 * Also see redirect_telinit() in src/core/main.c. */
+
+                if (sd_booted() > 0) {
+                        arg_action = _ACTION_INVALID;
+                        return telinit_parse_argv(argc, argv);
+                } else {
+                        /* Hmm, so some other init system is running, we need to forward this request to it.
+                         */
+                        arg_action = ACTION_TELINIT;
+                        return 1;
                 }
+
+        } else if (strstr_ptr(argv[0], "runlevel")) {
+                arg_action = ACTION_RUNLEVEL;
+                return runlevel_parse_argv(argc, argv);
         }
 
         arg_action = ACTION_SYSTEMCTL;