Welcome to little lamb

Code » anopa » commit f5a8ccd

Add aa-reboot

author Olivier Brunel
2015-02-21 13:52:16 UTC
committer Olivier Brunel
2015-04-04 12:47:35 UTC
parent cdbcdbfc6d3c8666a06801c70719f2eca2c11e09

Add aa-reboot

doc/aa-reboot.pod +42 -0
package/modes +1 -0
package/targets.mak +2 -0
src/utils/aa-reboot.c +87 -0
src/utils/deps-exe/aa-reboot +2 -0

diff --git a/doc/aa-reboot.pod b/doc/aa-reboot.pod
new file mode 100644
index 0000000..9812e99
--- /dev/null
+++ b/doc/aa-reboot.pod
@@ -0,0 +1,42 @@
+=head1 NAME
+
+aa-reboot - Reboots, powers off or halts the machine
+
+=head1 SYNOPSIS
+
+B<aa-reboot> [B<-r> | B<-p> | B<-H>]
+
+=head1 OPTIONS
+
+=over
+
+=item B<-H, --halt>
+
+Halts the machine.
+
+=item B<-h, --help>
+
+Show help screen and exit.
+
+=item B<-p, --poweroff>
+
+Powers off the machine.
+
+=item B<-r, --reboot>
+
+Reboots the machine. This is the default if no other option is specified.
+
+=item B<-V, --version>
+
+Show version information and exit.
+
+=back
+
+=head1 DESCRIPTION
+
+B<aa-reboot>(1) is a simple wrapper around a B<reboot>(2) call.
+
+You should never trigger it manually/directly. Instead, use B<aa-shutdown>(1),
+that will send the appropriate commands to B<s6-svscan> (PID 1) in order to
+properly shut down the system, before (probably) ending with a call to
+B<aa-reboot>(1) at the end of stage 4.
diff --git a/package/modes b/package/modes
index 988df5c..e0181ef 100644
--- a/package/modes
+++ b/package/modes
@@ -4,6 +4,7 @@ aa-enable               0755
 aa-kill                 0755
 aa-mount                0755
 aa-pivot                0755
+aa-reboot               0755
 aa-start                0755
 aa-stop                 0755
 aa-sync                 0755
diff --git a/package/targets.mak b/package/targets.mak
index a5dccb3..4596999 100644
--- a/package/targets.mak
+++ b/package/targets.mak
@@ -9,6 +9,7 @@ aa-echo \
 aa-kill \
 aa-mount \
 aa-pivot \
+aa-reboot \
 aa-sync \
 aa-terminate \
 aa-test \
@@ -22,6 +23,7 @@ aa-enable.1 \
 aa-kill.1 \
 aa-mount.1 \
 aa-pivot.1 \
+aa-reboot.1 \
 aa-start.1 \
 aa-stop.1 \
 aa-sync.1 \
diff --git a/src/utils/aa-reboot.c b/src/utils/aa-reboot.c
new file mode 100644
index 0000000..96faef8
--- /dev/null
+++ b/src/utils/aa-reboot.c
@@ -0,0 +1,87 @@
+
+#include <getopt.h>
+#include <unistd.h>
+#include <sys/reboot.h>
+#include <skalibs/strerr2.h>
+#include <anopa/common.h>
+
+const char *PROG;
+
+static void
+dieusage (int rc)
+{
+    aa_die_usage (rc, "[OPTION]",
+            " -r, --reboot                  Reboot the machine; This is the default\n"
+            " -H, --halt                    Halt the machine\n"
+            " -p, --poweroff                Power off the machine\n"
+            " -h, --help                    Show this help screen and exit\n"
+            " -V, --version                 Show version information and exit\n"
+            );
+}
+
+int
+main (int argc, char * const argv[])
+{
+    PROG = "aa-reboot";
+    struct
+    {
+        int cmd;
+        const char *desc;
+    } cmd[3] = {
+        { .cmd = RB_HALT_SYSTEM, .desc = "halt" },
+        { .cmd = RB_POWER_OFF,   .desc = "power off" },
+        { .cmd = RB_AUTOBOOT,    .desc = "reboot" }
+    };
+    int i = 2;
+
+    for (;;)
+    {
+        struct option longopts[] = {
+            { "halt",               no_argument,        NULL,   'H' },
+            { "help",               no_argument,        NULL,   'h' },
+            { "poweroff",           no_argument,        NULL,   'p' },
+            { "reboot",             no_argument,        NULL,   'r' },
+            { "version",            no_argument,        NULL,   'V' },
+            { NULL, 0, 0, 0 }
+        };
+        int c;
+
+        c = getopt_long (argc, argv, "HhprV", longopts, NULL);
+        if (c == -1)
+            break;
+        switch (c)
+        {
+            case 'H':
+                i = 0;
+                break;
+
+            case 'h':
+                dieusage (0);
+
+            case 'p':
+                i = 1;
+                break;
+
+            case 'r':
+                i = 2;
+                break;
+
+            case 'V':
+                aa_die_version ();
+
+            default:
+                dieusage (1);
+        }
+    }
+    argc -= optind;
+    argv += optind;
+
+    if (argc != 0)
+        dieusage (1);
+
+    if (reboot (cmd[i].cmd) < 0)
+        strerr_diefu2sys (2, cmd[i].desc, " the machine");
+
+    /* unlikely :p */
+    return 0;
+}
diff --git a/src/utils/deps-exe/aa-reboot b/src/utils/deps-exe/aa-reboot
new file mode 100644
index 0000000..30987b4
--- /dev/null
+++ b/src/utils/deps-exe/aa-reboot
@@ -0,0 +1,2 @@
+${LIBANOPA}
+-lskarnet