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