1dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <errno.h>
2dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdio.h>
3dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdlib.h>
4e3aeeb4de34dbb93e832e6554f494122ba633f3bKen Sumrall#include <cutils/android_reboot.h>
5dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <unistd.h>
6dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
7dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint reboot_main(int argc, char *argv[])
8dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
9dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int ret;
10dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int nosync = 0;
11dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int poweroff = 0;
12e3aeeb4de34dbb93e832e6554f494122ba633f3bKen Sumrall    int flags = 0;
13dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
14dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    opterr = 0;
15dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    do {
16dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        int c;
17dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
18dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        c = getopt(argc, argv, "np");
19dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
20dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (c == EOF) {
21dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
22dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
23dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
24dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        switch (c) {
25dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        case 'n':
26dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            nosync = 1;
27dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
28dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        case 'p':
29dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            poweroff = 1;
30dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
31dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        case '?':
32dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            fprintf(stderr, "usage: %s [-n] [-p] [rebootcommand]\n", argv[0]);
33dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            exit(EXIT_FAILURE);
34dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
35dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } while (1);
36dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
37dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(argc > optind + 1) {
38dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fprintf(stderr, "%s: too many arguments\n", argv[0]);
39dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        exit(EXIT_FAILURE);
40dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
41dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
42e3aeeb4de34dbb93e832e6554f494122ba633f3bKen Sumrall    if(nosync)
43e3aeeb4de34dbb93e832e6554f494122ba633f3bKen Sumrall        /* also set NO_REMOUNT_RO as remount ro includes an implicit sync */
44e3aeeb4de34dbb93e832e6554f494122ba633f3bKen Sumrall        flags = ANDROID_RB_FLAG_NO_SYNC | ANDROID_RB_FLAG_NO_REMOUNT_RO;
45dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
46dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(poweroff)
47e3aeeb4de34dbb93e832e6554f494122ba633f3bKen Sumrall        ret = android_reboot(ANDROID_RB_POWEROFF, flags, 0);
48dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    else if(argc > optind)
49e3aeeb4de34dbb93e832e6554f494122ba633f3bKen Sumrall        ret = android_reboot(ANDROID_RB_RESTART2, flags, argv[optind]);
50dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    else
51e3aeeb4de34dbb93e832e6554f494122ba633f3bKen Sumrall        ret = android_reboot(ANDROID_RB_RESTART, flags, 0);
52dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(ret < 0) {
53dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        perror("reboot");
54dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        exit(EXIT_FAILURE);
55dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
56dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fprintf(stderr, "reboot returned\n");
57dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 0;
58dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
59