| 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> | 
| Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 20 | #include <cutils/properties.h> | 
| Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 21 | #include <cutils/android_reboot.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 22 | #include <unistd.h> | 
|  | 23 |  | 
| Mark Salyzyn | 7b0f41c | 2017-09-26 10:19:41 -0700 | [diff] [blame] | 24 | int main(int argc, char* argv[]) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 | int ret; | 
| Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 26 | size_t prop_len; | 
|  | 27 | char property_val[PROPERTY_VALUE_MAX]; | 
| Mark Salyzyn | 7b0f41c | 2017-09-26 10:19:41 -0700 | [diff] [blame] | 28 | static const char reboot[] = "reboot"; | 
|  | 29 | const char* cmd = reboot; | 
|  | 30 | char* optarg = ""; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 31 |  | 
|  | 32 | opterr = 0; | 
|  | 33 | do { | 
|  | 34 | int c; | 
|  | 35 |  | 
| Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 36 | c = getopt(argc, argv, "p"); | 
| Benoit Goby | c6d7e20 | 2013-03-22 16:23:48 -0700 | [diff] [blame] | 37 |  | 
| David Gumberg | 1c3cb91 | 2014-03-02 09:59:19 -0800 | [diff] [blame] | 38 | if (c == -1) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 39 | break; | 
|  | 40 | } | 
| Benoit Goby | c6d7e20 | 2013-03-22 16:23:48 -0700 | [diff] [blame] | 41 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 42 | switch (c) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | case 'p': | 
| Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 44 | cmd = "shutdown"; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 45 | break; | 
|  | 46 | case '?': | 
| Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 47 | fprintf(stderr, "usage: %s [-p] [rebootcommand]\n", argv[0]); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 48 | exit(EXIT_FAILURE); | 
|  | 49 | } | 
|  | 50 | } while (1); | 
|  | 51 |  | 
|  | 52 | if(argc > optind + 1) { | 
|  | 53 | fprintf(stderr, "%s: too many arguments\n", argv[0]); | 
|  | 54 | exit(EXIT_FAILURE); | 
|  | 55 | } | 
|  | 56 |  | 
| Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 57 | if (argc > optind) | 
|  | 58 | optarg = argv[optind]; | 
| Mark Salyzyn | 277eca1 | 2017-09-11 15:22:57 -0700 | [diff] [blame] | 59 | if (!optarg || !optarg[0]) optarg = "shell"; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 60 |  | 
| Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 61 | prop_len = snprintf(property_val, sizeof(property_val), "%s,%s", cmd, optarg); | 
|  | 62 | if (prop_len >= sizeof(property_val)) { | 
| Mark Salyzyn | 7b0f41c | 2017-09-26 10:19:41 -0700 | [diff] [blame] | 63 | fprintf(stderr, "%s command too long: %s\n", cmd, optarg); | 
| Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 64 | exit(EXIT_FAILURE); | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | ret = property_set(ANDROID_RB_PROPERTY, property_val); | 
| Mark Salyzyn | 7b0f41c | 2017-09-26 10:19:41 -0700 | [diff] [blame] | 68 | if (ret < 0) { | 
|  | 69 | perror(cmd); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 70 | exit(EXIT_FAILURE); | 
|  | 71 | } | 
| Nick Kralevich | 9170452 | 2013-10-24 08:53:48 -0700 | [diff] [blame] | 72 |  | 
|  | 73 | // Don't return early. Give the reboot command time to take effect | 
|  | 74 | // to avoid messing up scripts which do "adb shell reboot && adb wait-for-device" | 
| Mark Salyzyn | 7b0f41c | 2017-09-26 10:19:41 -0700 | [diff] [blame] | 75 | if (cmd == reboot) { | 
|  | 76 | while (1) { | 
|  | 77 | pause(); | 
|  | 78 | } | 
|  | 79 | } | 
| Nick Kralevich | 9170452 | 2013-10-24 08:53:48 -0700 | [diff] [blame] | 80 |  | 
| Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 81 | fprintf(stderr, "Done\n"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 82 | return 0; | 
|  | 83 | } |