blob: ce41cd320fdfeb5fc49910d2da2a769b41c04a26 [file] [log] [blame]
Ken Sumralle3aeeb42011-03-07 23:29:42 -08001/*
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 Hughes8e9aeb92017-11-10 10:22:07 -080016
17#include <cutils/android_reboot.h>
18
Ken Sumralle3aeeb42011-03-07 23:29:42 -080019#include <stdio.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070020#include <stdlib.h>
Ken Sumralle3aeeb42011-03-07 23:29:42 -080021
Keun-young Park8d01f632017-03-13 11:54:47 -070022#include <cutils/properties.h>
Ken Sumralle3aeeb42011-03-07 23:29:42 -080023
Yusuke Sato0df08272015-07-08 14:57:07 -070024#define TAG "android_reboot"
Mark Salyzyn2b94cc22013-11-22 07:36:45 -080025
Elliott Hughes38d25672017-11-30 16:23:51 -080026int android_reboot(int cmd, int /*flags*/, const char* arg) {
Ken Sumralle3aeeb42011-03-07 23:29:42 -080027 int ret;
Keun-young Park8d01f632017-03-13 11:54:47 -070028 const char* restart_cmd = NULL;
29 char* prop_value;
Todd Poynoreac33da2017-01-30 17:28:55 -080030
Elliott Hughes8e9aeb92017-11-10 10:22:07 -080031 switch (static_cast<unsigned>(cmd)) {
Keun-young Park8d01f632017-03-13 11:54:47 -070032 case ANDROID_RB_RESTART: // deprecated
Ken Sumralle3aeeb42011-03-07 23:29:42 -080033 case ANDROID_RB_RESTART2:
Keun-young Park8d01f632017-03-13 11:54:47 -070034 restart_cmd = "reboot";
Ken Sumralle3aeeb42011-03-07 23:29:42 -080035 break;
Keun-young Park8d01f632017-03-13 11:54:47 -070036 case ANDROID_RB_POWEROFF:
37 restart_cmd = "shutdown";
38 break;
Todd Poynor37bba3b2017-01-30 17:27:39 -080039 case ANDROID_RB_THERMOFF:
Mark Salyzyn73e6b492017-08-14 15:56:53 -070040 restart_cmd = "shutdown,thermal";
Todd Poynor37bba3b2017-01-30 17:27:39 -080041 break;
Ken Sumralle3aeeb42011-03-07 23:29:42 -080042 }
Keun-young Park8d01f632017-03-13 11:54:47 -070043 if (!restart_cmd) return -1;
Mark Salyzyn73e6b492017-08-14 15:56:53 -070044 if (arg && arg[0]) {
Keun-young Park8d01f632017-03-13 11:54:47 -070045 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 Sumralle3aeeb42011-03-07 23:29:42 -080052 return ret;
53}