blob: 45d8a8ef56fc31ead8bd73739f0ae7fd407bacfb [file] [log] [blame]
Benoit Gobyc6d7e202013-03-22 16:23:48 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080017#include <errno.h>
18#include <stdio.h>
19#include <stdlib.h>
Ken Sumralle3aeeb42011-03-07 23:29:42 -080020#include <cutils/android_reboot.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080021#include <unistd.h>
22
Benoit Gobyc6d7e202013-03-22 16:23:48 -070023int main(int argc, char *argv[])
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024{
25 int ret;
26 int nosync = 0;
27 int poweroff = 0;
Ken Sumralle3aeeb42011-03-07 23:29:42 -080028 int flags = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
30 opterr = 0;
31 do {
32 int c;
33
34 c = getopt(argc, argv, "np");
Benoit Gobyc6d7e202013-03-22 16:23:48 -070035
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036 if (c == EOF) {
37 break;
38 }
Benoit Gobyc6d7e202013-03-22 16:23:48 -070039
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040 switch (c) {
41 case 'n':
42 nosync = 1;
43 break;
44 case 'p':
45 poweroff = 1;
46 break;
47 case '?':
48 fprintf(stderr, "usage: %s [-n] [-p] [rebootcommand]\n", argv[0]);
49 exit(EXIT_FAILURE);
50 }
51 } while (1);
52
53 if(argc > optind + 1) {
54 fprintf(stderr, "%s: too many arguments\n", argv[0]);
55 exit(EXIT_FAILURE);
56 }
57
Ken Sumralle3aeeb42011-03-07 23:29:42 -080058 if(nosync)
59 /* also set NO_REMOUNT_RO as remount ro includes an implicit sync */
60 flags = ANDROID_RB_FLAG_NO_SYNC | ANDROID_RB_FLAG_NO_REMOUNT_RO;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061
62 if(poweroff)
Ken Sumralle3aeeb42011-03-07 23:29:42 -080063 ret = android_reboot(ANDROID_RB_POWEROFF, flags, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064 else if(argc > optind)
Ken Sumralle3aeeb42011-03-07 23:29:42 -080065 ret = android_reboot(ANDROID_RB_RESTART2, flags, argv[optind]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080066 else
Ken Sumralle3aeeb42011-03-07 23:29:42 -080067 ret = android_reboot(ANDROID_RB_RESTART, flags, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080068 if(ret < 0) {
69 perror("reboot");
70 exit(EXIT_FAILURE);
71 }
72 fprintf(stderr, "reboot returned\n");
73 return 0;
74}