blob: d9a422764d36cf186e6c60de825da106bf2a0ea2 [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>
Nick Kralevichca8e66a2013-04-18 12:20:02 -070020#include <cutils/properties.h>
Ken Sumralle3aeeb42011-03-07 23:29:42 -080021#include <cutils/android_reboot.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022#include <unistd.h>
23
Benoit Gobyc6d7e202013-03-22 16:23:48 -070024int main(int argc, char *argv[])
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025{
26 int ret;
Nick Kralevichca8e66a2013-04-18 12:20:02 -070027 size_t prop_len;
28 char property_val[PROPERTY_VALUE_MAX];
29 const char *cmd = "reboot";
30 char *optarg = "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031
32 opterr = 0;
33 do {
34 int c;
35
Nick Kralevichca8e66a2013-04-18 12:20:02 -070036 c = getopt(argc, argv, "p");
Benoit Gobyc6d7e202013-03-22 16:23:48 -070037
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038 if (c == EOF) {
39 break;
40 }
Benoit Gobyc6d7e202013-03-22 16:23:48 -070041
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042 switch (c) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043 case 'p':
Nick Kralevichca8e66a2013-04-18 12:20:02 -070044 cmd = "shutdown";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045 break;
46 case '?':
Nick Kralevichca8e66a2013-04-18 12:20:02 -070047 fprintf(stderr, "usage: %s [-p] [rebootcommand]\n", argv[0]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048 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 Kralevichca8e66a2013-04-18 12:20:02 -070057 if (argc > optind)
58 optarg = argv[optind];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059
Nick Kralevichca8e66a2013-04-18 12:20:02 -070060 prop_len = snprintf(property_val, sizeof(property_val), "%s,%s", cmd, optarg);
61 if (prop_len >= sizeof(property_val)) {
62 fprintf(stderr, "reboot command too long: %s\n", optarg);
63 exit(EXIT_FAILURE);
64 }
65
66 ret = property_set(ANDROID_RB_PROPERTY, property_val);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067 if(ret < 0) {
68 perror("reboot");
69 exit(EXIT_FAILURE);
70 }
Nick Kralevich91704522013-10-24 08:53:48 -070071
72 // Don't return early. Give the reboot command time to take effect
73 // to avoid messing up scripts which do "adb shell reboot && adb wait-for-device"
74 while(1) { pause(); }
75
Nick Kralevichca8e66a2013-04-18 12:20:02 -070076 fprintf(stderr, "Done\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077 return 0;
78}