Benoit Goby | c6d7e20 | 2013-03-22 16:23:48 -0700 | [diff] [blame] | 1 | /* |
| 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 20 | #include <cutils/android_reboot.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 21 | #include <unistd.h> |
| 22 | |
Benoit Goby | c6d7e20 | 2013-03-22 16:23:48 -0700 | [diff] [blame] | 23 | int main(int argc, char *argv[]) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 24 | { |
| 25 | int ret; |
| 26 | int nosync = 0; |
| 27 | int poweroff = 0; |
Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 28 | int flags = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 29 | |
| 30 | opterr = 0; |
| 31 | do { |
| 32 | int c; |
| 33 | |
| 34 | c = getopt(argc, argv, "np"); |
Benoit Goby | c6d7e20 | 2013-03-22 16:23:48 -0700 | [diff] [blame] | 35 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 | if (c == EOF) { |
| 37 | break; |
| 38 | } |
Benoit Goby | c6d7e20 | 2013-03-22 16:23:48 -0700 | [diff] [blame] | 39 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 40 | 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 Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 58 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 61 | |
| 62 | if(poweroff) |
Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 63 | ret = android_reboot(ANDROID_RB_POWEROFF, flags, 0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 64 | else if(argc > optind) |
Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 65 | ret = android_reboot(ANDROID_RB_RESTART2, flags, argv[optind]); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 66 | else |
Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 67 | ret = android_reboot(ANDROID_RB_RESTART, flags, 0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 68 | if(ret < 0) { |
| 69 | perror("reboot"); |
| 70 | exit(EXIT_FAILURE); |
| 71 | } |
| 72 | fprintf(stderr, "reboot returned\n"); |
| 73 | return 0; |
| 74 | } |