]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
rtcwake: use poweroff if shutdown is not found
authorJustin Chen <justinpopo6@gmail.com>
Thu, 1 Nov 2018 18:10:38 +0000 (11:10 -0700)
committerKarel Zak <kzak@redhat.com>
Tue, 6 Nov 2018 11:34:02 +0000 (12:34 +0100)
Some systems do not have the shutdown command. Use poweroff as an
alternative.

Signed-off-by: Justin Chen <justinpopo6@gmail.com>
include/pathnames.h
sys-utils/rtcwake.c

index 3d5052e6f740be04e3aad4af79bdf25cea7ca056..ed8ea330ddf10a751af529c739bc75ecd2f374a4 100644 (file)
@@ -53,6 +53,7 @@
 # define _PATH_LOGIN           "/bin/login"
 #endif
 #define _PATH_SHUTDOWN         "/sbin/shutdown"
+#define _PATH_POWEROFF         "/sbin/poweroff"
 
 #define _PATH_TERMCOLORS_DIRNAME "terminal-colors.d"
 #define _PATH_TERMCOLORS_DIR   "/etc/" _PATH_TERMCOLORS_DIRNAME
index b63c646277e411b1f0c0e9e33ab4240d02d22179..029f00f9b5e6e2d4844602f09b0fb38bb00d1d32 100644 (file)
@@ -28,6 +28,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/ioctl.h>
+#include <sys/stat.h>
 #include <sys/time.h>
 #include <sys/types.h>
 #include <termios.h>
@@ -582,18 +583,32 @@ int main(int argc, char **argv)
                char *arg[5];
                int i = 0;
 
-               if (ctl.verbose)
-                       printf(_("suspend mode: off; executing %s\n"),
-                                               _PATH_SHUTDOWN);
-               arg[i++] = _PATH_SHUTDOWN;
-               arg[i++] = "-h";
-               arg[i++] = "-P";
-               arg[i++] = "now";
-               arg[i]   = NULL;
-               if (!ctl.dryrun) {
-                       execv(arg[0], arg);
-                       warn(_("failed to execute %s"), _PATH_SHUTDOWN);
-                       rc = EXIT_FAILURE;
+               if (!access(_PATH_SHUTDOWN, X_OK)) {
+                       arg[i++] = _PATH_SHUTDOWN;
+                       arg[i++] = "-h";
+                       arg[i++] = "-P";
+                       arg[i++] = "now";
+                       arg[i]   = NULL;
+               } else if (!access(_PATH_POWEROFF, X_OK)) {
+                       arg[i++] = _PATH_POWEROFF;
+                       arg[i]   = NULL;
+               } else {
+                       arg[i]   = NULL;
+               }
+
+               if (arg[0]) {
+                       if (ctl.verbose)
+                               printf(_("suspend mode: off; executing %s\n"),
+                                               arg[0]);
+                       if (!ctl.dryrun) {
+                               execv(arg[0], arg);
+                               warn(_("failed to execute %s"), arg[0]);
+                               rc = EX_EXEC_ENOENT;
+                       }
+               } else {
+                       /* Failed to find shutdown command */
+                       warn(_("failed to find shutdown command"));
+                       rc = EX_EXEC_ENOENT;
                }
                break;
        }