blob: f8546de2d185696976e285707041625a47233187 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001#include <errno.h>
2#include <stdio.h>
3#include <stdlib.h>
Ken Sumralle3aeeb42011-03-07 23:29:42 -08004#include <cutils/android_reboot.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08005#include <unistd.h>
6
7int reboot_main(int argc, char *argv[])
8{
9 int ret;
10 int nosync = 0;
11 int poweroff = 0;
Ken Sumralle3aeeb42011-03-07 23:29:42 -080012 int flags = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080013
14 opterr = 0;
15 do {
16 int c;
17
18 c = getopt(argc, argv, "np");
19
20 if (c == EOF) {
21 break;
22 }
23
24 switch (c) {
25 case 'n':
26 nosync = 1;
27 break;
28 case 'p':
29 poweroff = 1;
30 break;
31 case '?':
32 fprintf(stderr, "usage: %s [-n] [-p] [rebootcommand]\n", argv[0]);
33 exit(EXIT_FAILURE);
34 }
35 } while (1);
36
37 if(argc > optind + 1) {
38 fprintf(stderr, "%s: too many arguments\n", argv[0]);
39 exit(EXIT_FAILURE);
40 }
41
Ken Sumralle3aeeb42011-03-07 23:29:42 -080042 if(nosync)
43 /* also set NO_REMOUNT_RO as remount ro includes an implicit sync */
44 flags = ANDROID_RB_FLAG_NO_SYNC | ANDROID_RB_FLAG_NO_REMOUNT_RO;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045
46 if(poweroff)
Ken Sumralle3aeeb42011-03-07 23:29:42 -080047 ret = android_reboot(ANDROID_RB_POWEROFF, flags, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048 else if(argc > optind)
Ken Sumralle3aeeb42011-03-07 23:29:42 -080049 ret = android_reboot(ANDROID_RB_RESTART2, flags, argv[optind]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050 else
Ken Sumralle3aeeb42011-03-07 23:29:42 -080051 ret = android_reboot(ANDROID_RB_RESTART, flags, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052 if(ret < 0) {
53 perror("reboot");
54 exit(EXIT_FAILURE);
55 }
56 fprintf(stderr, "reboot returned\n");
57 return 0;
58}