about summary refs log tree commit diff
path: root/halt.c
diff options
context:
space:
mode:
authorJuan RP <xtraeme@gmail.com>2014-04-26 09:28:15 +0200
committerJuan RP <xtraeme@gmail.com>2014-04-26 09:29:39 +0200
commit7aecf46ec589a5bc49ae2392137bcd0e7468dd08 (patch)
tree2164dbf05eea99348d6a0bf893b9fd218bc9ee83 /halt.c
parent72c31feddc31ca0e9f5311a3133d26eef69de971 (diff)
downloadrunit-void-7aecf46ec589a5bc49ae2392137bcd0e7468dd08.tar.gz
runit-void-7aecf46ec589a5bc49ae2392137bcd0e7468dd08.tar.xz
runit-void-7aecf46ec589a5bc49ae2392137bcd0e7468dd08.zip
Add halt/pause utils from ignite.
Diffstat (limited to 'halt.c')
-rw-r--r--halt.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/halt.c b/halt.c
new file mode 100644
index 0000000..d6c52e6
--- /dev/null
+++ b/halt.c
@@ -0,0 +1,76 @@
+#include <errno.h>
+#include <unistd.h>
+#include <err.h>
+#include <string.h>
+#include <sys/reboot.h>
+
+extern char *__progname;
+
+typedef enum {NOOP, HALT, REBOOT, POWEROFF} action_t;
+
+int main(int argc, char *argv[]) {
+  int do_sync = 1;
+  int do_force = 0;
+  int opt;
+  action_t action = NOOP;
+
+  if (strcmp(__progname, "halt") == 0)
+    action = HALT;
+  else if (strcmp(__progname, "reboot") == 0)
+    action = REBOOT;
+  else if (strcmp(__progname, "poweroff") == 0)
+    action = POWEROFF;
+  else
+    warnx("no default behavior, needs to be called as halt/reboot/poweroff.");
+
+  while ((opt = getopt(argc, argv, "dfhinw")) != -1)
+    switch (opt) {
+    case 'n':
+      do_sync = 0;
+      break;
+    case 'w':
+      action = NOOP;
+      break;
+    case 'd':
+    case 'h':
+    case 'i':
+      /* silently ignored.  */
+      break;
+    case 'f':
+      do_force = 1;
+      break;
+    default:
+      errx(1, "Usage: %s [-n] [-f]", __progname);
+    }
+  
+  if (do_sync)
+    sync();
+
+  switch (action) {
+  case HALT:
+    if (do_force)
+      reboot(RB_HALT_SYSTEM);
+    else
+      execl("/bin/init", "init", "0", (char*)0);
+    err(1, "halt failed");
+    break;
+  case POWEROFF:
+    if (do_force)
+      reboot(RB_POWER_OFF);
+    else
+      execl("/bin/init", "init", "0", (char*)0);
+    err(1, "poweroff failed");
+    break;
+  case REBOOT:
+    if (do_force)
+      reboot(RB_AUTOBOOT);
+    else
+      execl("/bin/init", "init", "6", (char*)0);
+    err(1, "reboot failed");
+    break;
+  case NOOP:
+    break;
+  }
+
+  return 0;
+}