chiark / gitweb /
shutdown: fix arguments to /run/initramfs/shutdown
authorMichal Schmidt <mschmidt@redhat.com>
Thu, 6 Nov 2014 21:24:13 +0000 (22:24 +0100)
committerMichal Schmidt <mschmidt@redhat.com>
Thu, 6 Nov 2014 21:33:08 +0000 (22:33 +0100)
Our initrd interface specifies that the verb is in argv[1].
This is where systemd passes it to systemd-shutdown, but getopt
permutes argv[]. This confuses dracut's shutdown script:
  Shutdown called with argument '--log-level'. Rebooting!

getopt can be convinced to not permute argv[] by having '-' as the first
character of optstring. Let's use it. This requires changing the way
non-option arguments (in our case, the verb) are processed.

This fixes a bug where the system would reboot instead of powering off.

src/core/shutdown.c

index dd11ae3d6fa9f749431df1238fb04386a93d615f..48ed7fa1c3a0eb25d2e6d718137815125fdf910a 100644 (file)
@@ -75,7 +75,9 @@ static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 1);
         assert(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:
                 switch (c) {
 
                 case ARG_LOG_LEVEL:
@@ -113,6 +115,13 @@ static int parse_argv(int argc, char *argv[]) {
 
                         break;
 
 
                         break;
 
+                case '\001':
+                        if (!arg_verb)
+                                arg_verb = optarg;
+                        else
+                                log_error("Excess arguments, ignoring");
+                        break;
+
                 case '?':
                         return -EINVAL;
 
                 case '?':
                         return -EINVAL;
 
@@ -120,15 +129,11 @@ static int parse_argv(int argc, char *argv[]) {
                         assert_not_reached("Unhandled option code.");
                 }
 
                         assert_not_reached("Unhandled option code.");
                 }
 
-        if (optind >= argc) {
+        if (!arg_verb) {
                 log_error("Verb argument missing.");
                 return -EINVAL;
         }
 
                 log_error("Verb argument missing.");
                 return -EINVAL;
         }
 
-        arg_verb = argv[optind];
-
-        if (optind + 1 < argc)
-                log_error("Excess arguments, ignoring");
         return 0;
 }
 
         return 0;
 }