Jiangyi | 4983f4b | 2013-11-03 05:37:40 -0500 | [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 | |
Tanguy Pruvot | 8aeb371 | 2011-06-30 08:59:26 +0200 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
Jiangyi | 4983f4b | 2013-11-03 05:37:40 -0500 | [diff] [blame] | 20 | #include <cutils/properties.h> |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 21 | #include <cutils/android_reboot.h> |
Tanguy Pruvot | 8aeb371 | 2011-06-30 08:59:26 +0200 | [diff] [blame] | 22 | #include <unistd.h> |
| 23 | |
| 24 | int reboot_main(int argc, char *argv[]) |
| 25 | { |
| 26 | int ret; |
Jiangyi | 4983f4b | 2013-11-03 05:37:40 -0500 | [diff] [blame] | 27 | size_t prop_len; |
| 28 | char property_val[PROPERTY_VALUE_MAX]; |
| 29 | const char *cmd = "reboot"; |
| 30 | char *optarg = ""; |
Tanguy Pruvot | 8aeb371 | 2011-06-30 08:59:26 +0200 | [diff] [blame] | 31 | |
| 32 | opterr = 0; |
| 33 | do { |
| 34 | int c; |
| 35 | |
Jiangyi | 4983f4b | 2013-11-03 05:37:40 -0500 | [diff] [blame] | 36 | c = getopt(argc, argv, "p"); |
| 37 | |
Tanguy Pruvot | 8aeb371 | 2011-06-30 08:59:26 +0200 | [diff] [blame] | 38 | if (c == EOF) { |
| 39 | break; |
| 40 | } |
Jiangyi | 4983f4b | 2013-11-03 05:37:40 -0500 | [diff] [blame] | 41 | |
Tanguy Pruvot | 8aeb371 | 2011-06-30 08:59:26 +0200 | [diff] [blame] | 42 | switch (c) { |
Tanguy Pruvot | 8aeb371 | 2011-06-30 08:59:26 +0200 | [diff] [blame] | 43 | case 'p': |
Jiangyi | 4983f4b | 2013-11-03 05:37:40 -0500 | [diff] [blame] | 44 | cmd = "shutdown"; |
Tanguy Pruvot | 8aeb371 | 2011-06-30 08:59:26 +0200 | [diff] [blame] | 45 | break; |
| 46 | case '?': |
Jiangyi | 4983f4b | 2013-11-03 05:37:40 -0500 | [diff] [blame] | 47 | fprintf(stderr, "usage: %s [-p] [rebootcommand]\n", argv[0]); |
Tanguy Pruvot | 8aeb371 | 2011-06-30 08:59:26 +0200 | [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 | |
Jiangyi | 4983f4b | 2013-11-03 05:37:40 -0500 | [diff] [blame] | 57 | if (argc > optind) |
| 58 | optarg = argv[optind]; |
| 59 | |
| 60 | 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); |
Tanguy Pruvot | 8aeb371 | 2011-06-30 08:59:26 +0200 | [diff] [blame] | 67 | if(ret < 0) { |
| 68 | perror("reboot"); |
| 69 | exit(EXIT_FAILURE); |
| 70 | } |
Jiangyi | 4983f4b | 2013-11-03 05:37:40 -0500 | [diff] [blame] | 71 | fprintf(stderr, "Done\n"); |
Tanguy Pruvot | 8aeb371 | 2011-06-30 08:59:26 +0200 | [diff] [blame] | 72 | return 0; |
| 73 | } |