blob: 0bc50c019a216b0fea9350d5f336f98158b2e34b
1 | #include <errno.h> |
2 | #include <stdio.h> |
3 | #include <stdlib.h> |
4 | #include <cutils/properties.h> |
5 | #include <cutils/android_reboot.h> |
6 | #include <unistd.h> |
7 | |
8 | int reboot_main(int argc, char *argv[]) |
9 | { |
10 | int ret; |
11 | size_t prop_len; |
12 | char property_val[PROPERTY_VALUE_MAX]; |
13 | const char *cmd = "reboot"; |
14 | char *optarg = ""; |
15 | |
16 | opterr = 0; |
17 | do { |
18 | int c; |
19 | |
20 | c = getopt(argc, argv, "p"); |
21 | |
22 | if (c == EOF) { |
23 | break; |
24 | } |
25 | |
26 | switch (c) { |
27 | case 'p': |
28 | cmd = "shutdown"; |
29 | break; |
30 | case '?': |
31 | fprintf(stderr, "usage: %s [-p] [rebootcommand]\n", argv[0]); |
32 | exit(EXIT_FAILURE); |
33 | } |
34 | } while (1); |
35 | |
36 | if(argc > optind + 1) { |
37 | fprintf(stderr, "%s: too many arguments\n", argv[0]); |
38 | exit(EXIT_FAILURE); |
39 | } |
40 | |
41 | if (argc > optind) |
42 | optarg = argv[optind]; |
43 | |
44 | prop_len = snprintf(property_val, sizeof(property_val), "%s,%s", cmd, optarg); |
45 | if (prop_len >= sizeof(property_val)) { |
46 | fprintf(stderr, "reboot command too long: %s\n", optarg); |
47 | exit(EXIT_FAILURE); |
48 | } |
49 | |
50 | ret = property_set(ANDROID_RB_PROPERTY, property_val); |
51 | if(ret < 0) { |
52 | perror("reboot"); |
53 | exit(EXIT_FAILURE); |
54 | } |
55 | fprintf(stderr, "Done\n"); |
56 | return 0; |
57 | } |
58 |