chiark / gitweb /
shutdown: fix arguments to /run/initramfs/shutdown
[elogind.git] / src / core / shutdown.c
index 1e88b057903e11630a9fe390e602111748915f72..48ed7fa1c3a0eb25d2e6d718137815125fdf910a 100644 (file)
@@ -75,7 +75,9 @@ static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 1);
         assert(argv);
 
-        while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0)
+        /* "-" prevents getopt from permuting argv[] and moving the verb away
+         * from argv[1]. Our interface to initrd promises it'll be there. */
+        while ((c = getopt_long(argc, argv, "-", options, NULL)) >= 0)
                 switch (c) {
 
                 case ARG_LOG_LEVEL:
@@ -113,6 +115,13 @@ static int parse_argv(int argc, char *argv[]) {
 
                         break;
 
+                case '\001':
+                        if (!arg_verb)
+                                arg_verb = optarg;
+                        else
+                                log_error("Excess arguments, ignoring");
+                        break;
+
                 case '?':
                         return -EINVAL;
 
@@ -120,15 +129,11 @@ static int parse_argv(int argc, char *argv[]) {
                         assert_not_reached("Unhandled option code.");
                 }
 
-        if (optind >= argc) {
+        if (!arg_verb) {
                 log_error("Verb argument missing.");
                 return -EINVAL;
         }
 
-        arg_verb = argv[optind];
-
-        if (optind + 1 < argc)
-                log_error("Excess arguments, ignoring");
         return 0;
 }
 
@@ -165,7 +170,7 @@ int main(int argc, char *argv[]) {
                 goto error;
 
         /* journald will die if not gone yet. The log target defaults
-         * to console, but may have been changed by commandline options. */
+         * to console, but may have been changed by command line options. */
 
         log_close_console(); /* force reopen of /dev/console */
         log_open();
@@ -207,7 +212,7 @@ int main(int argc, char *argv[]) {
 
         in_container = detect_container(NULL) > 0;
 
-        need_umount = true;
+        need_umount = !in_container;
         need_swapoff = !in_container;
         need_loop_detach = !in_container;
         need_dm_detach = !in_container;
@@ -313,7 +318,7 @@ int main(int argc, char *argv[]) {
             access("/run/initramfs/shutdown", X_OK) == 0) {
                 r = switch_root_initramfs();
                 if (r >= 0) {
-                        arguments[0] = (char*) "/shutdown";
+                        argv[0] = (char*) "/shutdown";
 
                         setsid();
                         make_console_stdio();
@@ -321,7 +326,7 @@ int main(int argc, char *argv[]) {
                         log_info("Successfully changed into root pivot.\n"
                                  "Returning to initrd...");
 
-                        execv("/shutdown", arguments);
+                        execv("/shutdown", argv);
                         log_error("Failed to execute shutdown binary: %m");
                 } else
                         log_error("Failed to switch root to \"/run/initramfs\": %s", strerror(-r));