blob: 04aa9aefdc8c0f9c733f85d0f119f7a908f1ec0b [file] [log] [blame]
preludedrew38058dc2011-01-29 23:30:44 -07001#include <errno.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <sys/reboot.h>
5#include <unistd.h>
6#include <cutils/properties.h>
7
8int reboot_main(int argc, char *argv[])
9{
10 int ret;
11 int nosync = 0;
12 int poweroff = 0;
13 int force = 0;
14
15 opterr = 0;
16 do {
17 int c;
18
19 c = getopt(argc, argv, "npf");
20
21 if (c == EOF) {
22 break;
23 }
24
25 switch (c) {
26 case 'n':
27 nosync = 1;
28 break;
29 case 'p':
30 poweroff = 1;
31 break;
32 case 'f':
33 force = 1;
34 break;
35 case '?':
36 fprintf(stderr, "usage: %s [-n] [-p] [rebootcommand]\n", argv[0]);
37 exit(EXIT_FAILURE);
38 }
39 } while (1);
40
41 if(argc > optind + 1) {
42 fprintf(stderr, "%s: too many arguments\n", argv[0]);
43 exit(EXIT_FAILURE);
44 }
45
46 if(!nosync)
47 sync();
48
49 if(force || argc > optind) {
50 if(poweroff)
51 ret = __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_POWER_OFF, NULL);
52 else if(argc > optind)
53 ret = __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, argv[optind]);
54 else
55 ret = reboot(RB_AUTOBOOT);
56 } else {
57 if(poweroff) {
58 property_set("ctl.start", "poweroff");
59 ret = 0;
60 } else {
61 property_set("ctl.start", "reboot");
62 ret = 0;
63 }
64 }
65
66 if(ret < 0) {
67 perror("reboot");
68 exit(EXIT_FAILURE);
69 }
70 fprintf(stderr, "reboot returned\n");
71 return 0;
72}