master xplshn/aruu / cmd / pseudo / halt.c
 1/* See LICENSE file for copyright and license details. */
 2
 3#include <sys/syscall.h>
 4
 5#include <stdio.h>
 6#include <stdlib.h>
 7#include <unistd.h>
 8
 9#include "reboot.h"
10#include "util.h"
11
12static void
13usage(void)
14{
15  eprintf("usage: %s [-pr]\n", argv0);
16}
17
18// ?man halt: halt or reboot
19// ?man halt, poweroff, or reboot the machine
20int
21main(int argc, char *argv[])
22{
23  int pflag = 0, rflag = 0;
24  int cmd = LINUX_REBOOT_CMD_HALT;
25
26  ARGBEGIN
27  {
28    // ?man -p: preserve file attributes
29    case 'p':
30      pflag = 1;
31      break;
32    // ?man -r: operate recursively
33    case 'r':
34      rflag = 1;
35      break;
36    default:
37      usage();
38  }
39  ARGEND;
40
41  if (argc > 0)
42    usage();
43
44  sync();
45
46  if (pflag && rflag)
47    usage();
48
49  if (pflag)
50    cmd = LINUX_REBOOT_CMD_POWER_OFF;
51  if (rflag)
52    cmd = LINUX_REBOOT_CMD_RESTART;
53
54  if (syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, NULL) < 0)
55    eprintf("reboot:");
56  return 0;
57}