The Pedigree Project 0.1
reboot/main.c
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include <stdio.h>
21#include <string.h>
22
23#include <sys/reboot.h>
24
25static int usage(const char* program) {
26 fprintf(stderr, "Usage: %s [-r|-h|-P] [now]\n", program);
27 return 1;
28}
29
30int main(int argc, char** argv) {
31 const char* program = strrchr(argv[0], '/');
32 program = program ? program + 1 : argv[0];
33 int command = !strcmp(program, "reboot") ? RB_AUTOBOOT
34 : !strcmp(program, "halt") ? RB_HALT_SYSTEM
35 : RB_POWER_OFF;
36 int selected = 0;
37 for (int i = 1; i < argc; ++i) {
38 if (!strcmp(argv[i], "now") && i == argc - 1)
39 continue;
40 if (selected++)
41 return usage(program);
42 if (!strcmp(argv[i], "-r"))
43 command = RB_AUTOBOOT;
44 else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "-P"))
45 command = RB_POWER_OFF;
46 else
47 return usage(program);
48 }
49 // The kernel checks permission and flushes storage before terminal teardown.
50 if (reboot(command) < 0)
51 perror(program);
52 return 1;
53}