]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
manager: perform objective->shutdown_verb mapping locally
authorVito Caputo <vcaputo@pengaru.com>
Thu, 29 Dec 2022 01:53:22 +0000 (17:53 -0800)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 2 Jan 2023 15:00:22 +0000 (16:00 +0100)
This is a small cleanup removing the need for the spurious
*ret_shutdown_verb argument on invoke_main_loop() while moving
the MANAGER_OBJECTIVE::shutdown_verb string mapping local to
where it actually gets added to the shutdown argv in
become_shutdown().

This also eliminates the need for the several clearings of
*ret_shutdown_argv, and the streq() branches of shutdown_verb in
favor of plain equality tests against the objective value.

Nothing functionally has been changed.

src/core/main.c

index f779410fda5b4370478d4d4791c2c26c1c09c759..899d211d1af7a49cc50457daf953f7367a381773 100644 (file)
@@ -1494,9 +1494,15 @@ static void redirect_telinit(int argc, char *argv[]) {
 #endif
 }
 
-static int become_shutdown(
-                const char *shutdown_verb,
-                int retval) {
+static int become_shutdown(int objective, int retval) {
+
+        static const char* const table[_MANAGER_OBJECTIVE_MAX] = {
+                [MANAGER_EXIT]     = "exit",
+                [MANAGER_REBOOT]   = "reboot",
+                [MANAGER_POWEROFF] = "poweroff",
+                [MANAGER_HALT]     = "halt",
+                [MANAGER_KEXEC]    = "kexec",
+        };
 
         char log_level[DECIMAL_STR_MAX(int) + 1],
                 exit_code[DECIMAL_STR_MAX(uint8_t) + 1],
@@ -1504,7 +1510,7 @@ static int become_shutdown(
 
         const char* command_line[13] = {
                 SYSTEMD_SHUTDOWN_BINARY_PATH,
-                shutdown_verb,
+                table[objective],
                 "--timeout", timeout,
                 "--log-level", log_level,
                 "--log-target",
@@ -1515,7 +1521,8 @@ static int become_shutdown(
         size_t pos = 7;
         int r;
 
-        assert(shutdown_verb);
+        assert(objective >= 0 && objective < _MANAGER_OBJECTIVE_MAX);
+        assert(table[objective]);
         assert(!command_line[pos]);
         env_block = strv_copy(environ);
 
@@ -1549,7 +1556,7 @@ static int become_shutdown(
         if (log_get_show_time())
                 command_line[pos++] = "--log-time";
 
-        if (streq(shutdown_verb, "exit")) {
+        if (objective == MANAGER_EXIT) {
                 command_line[pos++] = "--exit-code";
                 command_line[pos++] = exit_code;
                 xsprintf(exit_code, "%d", retval);
@@ -1557,9 +1564,9 @@ static int become_shutdown(
 
         assert(pos < ELEMENTSOF(command_line));
 
-        if (streq(shutdown_verb, "reboot"))
+        if (objective == MANAGER_REBOOT)
                 watchdog_timer = arg_reboot_watchdog;
-        else if (streq(shutdown_verb, "kexec"))
+        else if (objective == MANAGER_KEXEC)
                 watchdog_timer = arg_kexec_watchdog;
 
         /* If we reboot or kexec let's set the shutdown watchdog and tell the
@@ -1925,7 +1932,6 @@ static int invoke_main_loop(
                 const struct rlimit *saved_rlimit_nofile,
                 const struct rlimit *saved_rlimit_memlock,
                 int *ret_retval,                   /* Return parameters relevant for shutting down */
-                const char **ret_shutdown_verb,    /* … */
                 FDSet **ret_fds,                   /* Return parameters for reexecuting */
                 char **ret_switch_root_dir,        /* … */
                 char **ret_switch_root_init,       /* … */
@@ -1937,7 +1943,6 @@ static int invoke_main_loop(
         assert(saved_rlimit_nofile);
         assert(saved_rlimit_memlock);
         assert(ret_retval);
-        assert(ret_shutdown_verb);
         assert(ret_fds);
         assert(ret_switch_root_dir);
         assert(ret_switch_root_init);
@@ -1995,7 +2000,6 @@ static int invoke_main_loop(
                         log_notice("Reexecuting.");
 
                         *ret_retval = EXIT_SUCCESS;
-                        *ret_shutdown_verb = NULL;
                         *ret_switch_root_dir = *ret_switch_root_init = NULL;
 
                         return objective;
@@ -2015,7 +2019,6 @@ static int invoke_main_loop(
                         log_notice("Switching root.");
 
                         *ret_retval = EXIT_SUCCESS;
-                        *ret_shutdown_verb = NULL;
 
                         /* Steal the switch root parameters */
                         *ret_switch_root_dir = TAKE_PTR(m->switch_root);
@@ -2028,7 +2031,6 @@ static int invoke_main_loop(
                                 log_debug("Exit.");
 
                                 *ret_retval = m->return_value;
-                                *ret_shutdown_verb = NULL;
                                 *ret_fds = NULL;
                                 *ret_switch_root_dir = *ret_switch_root_init = NULL;
 
@@ -2040,18 +2042,9 @@ static int invoke_main_loop(
                 case MANAGER_POWEROFF:
                 case MANAGER_HALT:
                 case MANAGER_KEXEC: {
-                        static const char* const table[_MANAGER_OBJECTIVE_MAX] = {
-                                [MANAGER_EXIT]     = "exit",
-                                [MANAGER_REBOOT]   = "reboot",
-                                [MANAGER_POWEROFF] = "poweroff",
-                                [MANAGER_HALT]     = "halt",
-                                [MANAGER_KEXEC]    = "kexec",
-                        };
-
                         log_notice("Shutting down.");
 
                         *ret_retval = m->return_value;
-                        assert_se(*ret_shutdown_verb = table[m->objective]);
                         *ret_fds = NULL;
                         *ret_switch_root_dir = *ret_switch_root_init = NULL;
 
@@ -2723,7 +2716,7 @@ int main(int argc, char *argv[]) {
         char *switch_root_dir = NULL, *switch_root_init = NULL;
         usec_t before_startup, after_startup;
         static char systemd[] = "systemd";
-        const char *shutdown_verb = NULL, *error_message = NULL;
+        const char *error_message = NULL;
         int r, retval = EXIT_FAILURE;
         Manager *m = NULL;
         FDSet *fds = NULL;
@@ -3036,7 +3029,6 @@ int main(int argc, char *argv[]) {
                              &saved_rlimit_nofile,
                              &saved_rlimit_memlock,
                              &retval,
-                             &shutdown_verb,
                              &fds,
                              &switch_root_dir,
                              &switch_root_init,
@@ -3096,8 +3088,13 @@ finish:
 
         /* Try to invoke the shutdown binary unless we already failed.
          * If we failed above, we want to freeze after finishing cleanup. */
-        if (r >= 0 && shutdown_verb) {
-                r = become_shutdown(shutdown_verb, retval);
+        if (arg_system && IN_SET(r, MANAGER_EXIT,
+                                    MANAGER_REBOOT,
+                                    MANAGER_POWEROFF,
+                                    MANAGER_HALT,
+                                    MANAGER_KEXEC)) {
+
+                r = become_shutdown(r, retval);
                 log_error_errno(r, "Failed to execute shutdown binary, %s: %m", getpid_cached() == 1 ? "freezing" : "quitting");
                 error_message = "Failed to execute shutdown binary";
         }