about summary refs log tree commit diff
path: root/halt.c
blob: 8eaba6455d7680d306675341fe5b851e976a045d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#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_type;

int main(int argc, char *argv[]) {
  int do_sync = 1;
  int do_force = 0;
  int opt;
  action_type 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;
      do_sync = 0;
      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/runit-init", "init", "0", (char*)0);
    err(1, "halt failed");
    break;
  case POWEROFF:
    if (do_force)
      reboot(RB_POWER_OFF);
    else
      execl("/bin/runit-init", "init", "0", (char*)0);
    err(1, "poweroff failed");
    break;
  case REBOOT:
    if (do_force)
      reboot(RB_AUTOBOOT);
    else
      execl("/bin/runit-init", "init", "6", (char*)0);
    err(1, "reboot failed");
    break;
  case NOOP:
    break;
  }

  return 0;
}