| Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 2011, 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 | */ | 
| Elliott Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 16 |  | 
|  | 17 | #include <cutils/android_reboot.h> | 
|  | 18 |  | 
| Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 19 | #include <stdio.h> | 
| Yusuke Sato | 0df0827 | 2015-07-08 14:57:07 -0700 | [diff] [blame] | 20 | #include <stdlib.h> | 
| Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 21 |  | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 22 | #include <cutils/properties.h> | 
| Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 23 |  | 
| Yusuke Sato | 0df0827 | 2015-07-08 14:57:07 -0700 | [diff] [blame] | 24 | #define TAG "android_reboot" | 
| Mark Salyzyn | 2b94cc2 | 2013-11-22 07:36:45 -0800 | [diff] [blame] | 25 |  | 
| Elliott Hughes | 38d2567 | 2017-11-30 16:23:51 -0800 | [diff] [blame] | 26 | int android_reboot(int cmd, int /*flags*/, const char* arg) { | 
| Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 27 | int ret; | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 28 | const char* restart_cmd = NULL; | 
|  | 29 | char* prop_value; | 
| Todd Poynor | eac33da | 2017-01-30 17:28:55 -0800 | [diff] [blame] | 30 |  | 
| Elliott Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 31 | switch (static_cast<unsigned>(cmd)) { | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 32 | case ANDROID_RB_RESTART:  // deprecated | 
| Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 33 | case ANDROID_RB_RESTART2: | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 34 | restart_cmd = "reboot"; | 
| Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 35 | break; | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 36 | case ANDROID_RB_POWEROFF: | 
|  | 37 | restart_cmd = "shutdown"; | 
|  | 38 | break; | 
| Todd Poynor | 37bba3b | 2017-01-30 17:27:39 -0800 | [diff] [blame] | 39 | case ANDROID_RB_THERMOFF: | 
| Mark Salyzyn | 73e6b49 | 2017-08-14 15:56:53 -0700 | [diff] [blame] | 40 | restart_cmd = "shutdown,thermal"; | 
| Todd Poynor | 37bba3b | 2017-01-30 17:27:39 -0800 | [diff] [blame] | 41 | break; | 
| Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 42 | } | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 43 | if (!restart_cmd) return -1; | 
| Mark Salyzyn | 73e6b49 | 2017-08-14 15:56:53 -0700 | [diff] [blame] | 44 | if (arg && arg[0]) { | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 45 | ret = asprintf(&prop_value, "%s,%s", restart_cmd, arg); | 
|  | 46 | } else { | 
|  | 47 | ret = asprintf(&prop_value, "%s", restart_cmd); | 
|  | 48 | } | 
|  | 49 | if (ret < 0) return -1; | 
|  | 50 | ret = property_set(ANDROID_RB_PROPERTY, prop_value); | 
|  | 51 | free(prop_value); | 
| Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 52 | return ret; | 
|  | 53 | } |