blob: df7912f96d174895aa6adcb5ddd016f288fee00a [file] [log] [blame]
Keun-young Park8d01f632017-03-13 11:54:47 -07001/*
2 * Copyright (C) 2017 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 */
Tom Cherry3f5eaae52017-04-06 16:30:22 -070016
17#include "reboot.h"
18
Keun-young Park8d01f632017-03-13 11:54:47 -070019#include <dirent.h>
20#include <fcntl.h>
Keun-young Park2ba5c812017-03-29 12:54:40 -070021#include <linux/fs.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070022#include <mntent.h>
Keun-young Park2ba5c812017-03-29 12:54:40 -070023#include <selinux/selinux.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070024#include <sys/cdefs.h>
Keun-young Park2ba5c812017-03-29 12:54:40 -070025#include <sys/ioctl.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070026#include <sys/mount.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070027#include <sys/reboot.h>
28#include <sys/stat.h>
29#include <sys/syscall.h>
30#include <sys/types.h>
31#include <sys/wait.h>
32
33#include <memory>
Keun-young Park7830d592017-03-27 16:07:02 -070034#include <set>
Keun-young Park8d01f632017-03-13 11:54:47 -070035#include <thread>
36#include <vector>
37
38#include <android-base/file.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070039#include <android-base/logging.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070040#include <android-base/macros.h>
Tom Cherryccf23532017-03-28 16:40:41 -070041#include <android-base/properties.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070042#include <android-base/stringprintf.h>
43#include <android-base/strings.h>
Keun-young Park2ba5c812017-03-29 12:54:40 -070044#include <android-base/unique_fd.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070045#include <bootloader_message/bootloader_message.h>
46#include <cutils/android_reboot.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070047#include <fs_mgr.h>
48#include <logwrap/logwrap.h>
Todd Poynorfc827be2017-04-13 15:17:24 -070049#include <private/android_filesystem_config.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070050
Keun-young Park7830d592017-03-27 16:07:02 -070051#include "property_service.h"
Keun-young Park8d01f632017-03-13 11:54:47 -070052#include "service.h"
Keun-young Park8d01f632017-03-13 11:54:47 -070053
54using android::base::StringPrintf;
55
Tom Cherry81f5d3e2017-06-22 12:53:17 -070056namespace android {
57namespace init {
58
Keun-young Park8d01f632017-03-13 11:54:47 -070059// represents umount status during reboot / shutdown.
60enum UmountStat {
61 /* umount succeeded. */
62 UMOUNT_STAT_SUCCESS = 0,
63 /* umount was not run. */
64 UMOUNT_STAT_SKIPPED = 1,
65 /* umount failed with timeout. */
66 UMOUNT_STAT_TIMEOUT = 2,
67 /* could not run due to error */
68 UMOUNT_STAT_ERROR = 3,
69 /* not used by init but reserved for other part to use this to represent the
70 the state where umount status before reboot is not found / available. */
71 UMOUNT_STAT_NOT_AVAILABLE = 4,
72};
73
74// Utility for struct mntent
75class MountEntry {
76 public:
Keun-young Park2ba5c812017-03-29 12:54:40 -070077 explicit MountEntry(const mntent& entry)
Keun-young Park8d01f632017-03-13 11:54:47 -070078 : mnt_fsname_(entry.mnt_fsname),
79 mnt_dir_(entry.mnt_dir),
80 mnt_type_(entry.mnt_type),
Keun-young Park2ba5c812017-03-29 12:54:40 -070081 mnt_opts_(entry.mnt_opts) {}
Keun-young Park8d01f632017-03-13 11:54:47 -070082
Keun-young Park2ba5c812017-03-29 12:54:40 -070083 bool Umount() {
84 int r = umount2(mnt_dir_.c_str(), 0);
85 if (r == 0) {
86 LOG(INFO) << "umounted " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_;
87 return true;
88 } else {
89 PLOG(WARNING) << "cannot umount " << mnt_fsname_ << ":" << mnt_dir_ << " opts "
90 << mnt_opts_;
91 return false;
92 }
93 }
Keun-young Park8d01f632017-03-13 11:54:47 -070094
Keun-young Park2ba5c812017-03-29 12:54:40 -070095 void DoFsck() {
96 int st;
97 if (IsF2Fs()) {
98 const char* f2fs_argv[] = {
99 "/system/bin/fsck.f2fs", "-f", mnt_fsname_.c_str(),
100 };
101 android_fork_execvp_ext(arraysize(f2fs_argv), (char**)f2fs_argv, &st, true, LOG_KLOG,
102 true, nullptr, nullptr, 0);
103 } else if (IsExt4()) {
104 const char* ext4_argv[] = {
105 "/system/bin/e2fsck", "-f", "-y", mnt_fsname_.c_str(),
106 };
107 android_fork_execvp_ext(arraysize(ext4_argv), (char**)ext4_argv, &st, true, LOG_KLOG,
108 true, nullptr, nullptr, 0);
109 }
110 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700111
112 static bool IsBlockDevice(const struct mntent& mntent) {
113 return android::base::StartsWith(mntent.mnt_fsname, "/dev/block");
114 }
115
116 static bool IsEmulatedDevice(const struct mntent& mntent) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700117 return android::base::StartsWith(mntent.mnt_fsname, "/data/");
Keun-young Park8d01f632017-03-13 11:54:47 -0700118 }
119
120 private:
Keun-young Park2ba5c812017-03-29 12:54:40 -0700121 bool IsF2Fs() const { return mnt_type_ == "f2fs"; }
122
123 bool IsExt4() const { return mnt_type_ == "ext4"; }
124
Keun-young Park8d01f632017-03-13 11:54:47 -0700125 std::string mnt_fsname_;
126 std::string mnt_dir_;
127 std::string mnt_type_;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700128 std::string mnt_opts_;
Keun-young Park8d01f632017-03-13 11:54:47 -0700129};
130
131// Turn off backlight while we are performing power down cleanup activities.
132static void TurnOffBacklight() {
133 static constexpr char OFF[] = "0";
134
135 android::base::WriteStringToFile(OFF, "/sys/class/leds/lcd-backlight/brightness");
136
137 static const char backlightDir[] = "/sys/class/backlight";
138 std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(backlightDir), closedir);
139 if (!dir) {
140 return;
141 }
142
143 struct dirent* dp;
144 while ((dp = readdir(dir.get())) != nullptr) {
145 if (((dp->d_type != DT_DIR) && (dp->d_type != DT_LNK)) || (dp->d_name[0] == '.')) {
146 continue;
147 }
148
149 std::string fileName = StringPrintf("%s/%s/brightness", backlightDir, dp->d_name);
150 android::base::WriteStringToFile(OFF, fileName);
151 }
152}
153
Keun-young Park8d01f632017-03-13 11:54:47 -0700154static void ShutdownVold() {
155 const char* vdc_argv[] = {"/system/bin/vdc", "volume", "shutdown"};
156 int status;
157 android_fork_execvp_ext(arraysize(vdc_argv), (char**)vdc_argv, &status, true, LOG_KLOG, true,
158 nullptr, nullptr, 0);
159}
160
161static void LogShutdownTime(UmountStat stat, Timer* t) {
162 LOG(WARNING) << "powerctl_shutdown_time_ms:" << std::to_string(t->duration_ms()) << ":" << stat;
163}
164
165static void __attribute__((noreturn))
166RebootSystem(unsigned int cmd, const std::string& rebootTarget) {
Keun-young Park3cd8c6f2017-03-23 15:33:16 -0700167 LOG(INFO) << "Reboot ending, jumping to kernel";
Keun-young Park8d01f632017-03-13 11:54:47 -0700168 switch (cmd) {
169 case ANDROID_RB_POWEROFF:
170 reboot(RB_POWER_OFF);
171 break;
172
173 case ANDROID_RB_RESTART2:
174 syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
175 LINUX_REBOOT_CMD_RESTART2, rebootTarget.c_str());
176 break;
177
178 case ANDROID_RB_THERMOFF:
179 reboot(RB_POWER_OFF);
180 break;
181 }
182 // In normal case, reboot should not return.
183 PLOG(FATAL) << "reboot call returned";
184 abort();
185}
186
187/* Find all read+write block devices and emulated devices in /proc/mounts
188 * and add them to correpsponding list.
189 */
190static bool FindPartitionsToUmount(std::vector<MountEntry>* blockDevPartitions,
Keun-young Park2ba5c812017-03-29 12:54:40 -0700191 std::vector<MountEntry>* emulatedPartitions, bool dump) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700192 std::unique_ptr<std::FILE, int (*)(std::FILE*)> fp(setmntent("/proc/mounts", "r"), endmntent);
193 if (fp == nullptr) {
194 PLOG(ERROR) << "Failed to open /proc/mounts";
195 return false;
196 }
197 mntent* mentry;
198 while ((mentry = getmntent(fp.get())) != nullptr) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700199 if (dump) {
200 LOG(INFO) << "mount entry " << mentry->mnt_fsname << ":" << mentry->mnt_dir << " opts "
201 << mentry->mnt_opts << " type " << mentry->mnt_type;
202 } else if (MountEntry::IsBlockDevice(*mentry) && hasmntopt(mentry, "rw")) {
203 blockDevPartitions->emplace(blockDevPartitions->begin(), *mentry);
Keun-young Park8d01f632017-03-13 11:54:47 -0700204 } else if (MountEntry::IsEmulatedDevice(*mentry)) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700205 emulatedPartitions->emplace(emulatedPartitions->begin(), *mentry);
Keun-young Park8d01f632017-03-13 11:54:47 -0700206 }
207 }
208 return true;
209}
210
Keun-young Park1663e972017-04-21 17:29:26 -0700211static void DumpUmountDebuggingInfo(bool dump_all) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700212 int status;
213 if (!security_getenforce()) {
214 LOG(INFO) << "Run lsof";
215 const char* lsof_argv[] = {"/system/bin/lsof"};
216 android_fork_execvp_ext(arraysize(lsof_argv), (char**)lsof_argv, &status, true, LOG_KLOG,
217 true, nullptr, nullptr, 0);
Keun-young Park8d01f632017-03-13 11:54:47 -0700218 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700219 FindPartitionsToUmount(nullptr, nullptr, true);
Keun-young Park1663e972017-04-21 17:29:26 -0700220 if (dump_all) {
221 // dump current tasks, this log can be lengthy, so only dump with dump_all
222 android::base::WriteStringToFile("t", "/proc/sysrq-trigger");
223 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700224}
225
226static UmountStat UmountPartitions(int timeoutMs) {
227 Timer t;
228 UmountStat stat = UMOUNT_STAT_TIMEOUT;
229 int retry = 0;
230 /* data partition needs all pending writes to be completed and all emulated partitions
231 * umounted.If the current waiting is not good enough, give
232 * up and leave it to e2fsck after reboot to fix it.
233 */
234 while (true) {
235 std::vector<MountEntry> block_devices;
236 std::vector<MountEntry> emulated_devices;
237 if (!FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
238 return UMOUNT_STAT_ERROR;
239 }
240 if (block_devices.size() == 0) {
241 stat = UMOUNT_STAT_SUCCESS;
242 break;
243 }
244 if ((timeoutMs < t.duration_ms()) && retry > 0) { // try umount at least once
245 stat = UMOUNT_STAT_TIMEOUT;
246 break;
247 }
248 if (emulated_devices.size() > 0 &&
249 std::all_of(emulated_devices.begin(), emulated_devices.end(),
250 [](auto& entry) { return entry.Umount(); })) {
251 sync();
252 }
253 for (auto& entry : block_devices) {
254 entry.Umount();
255 }
256 retry++;
257 std::this_thread::sleep_for(100ms);
258 }
259 return stat;
Keun-young Park8d01f632017-03-13 11:54:47 -0700260}
261
Keun-young Park3ee0df92017-03-27 11:21:09 -0700262static void KillAllProcesses() { android::base::WriteStringToFile("i", "/proc/sysrq-trigger"); }
263
Keun-young Park8d01f632017-03-13 11:54:47 -0700264/* Try umounting all emulated file systems R/W block device cfile systems.
265 * This will just try umount and give it up if it fails.
266 * For fs like ext4, this is ok as file system will be marked as unclean shutdown
267 * and necessary check can be done at the next reboot.
268 * For safer shutdown, caller needs to make sure that
269 * all processes / emulated partition for the target fs are all cleaned-up.
270 *
271 * return true when umount was successful. false when timed out.
272 */
Keun-young Park3ee0df92017-03-27 11:21:09 -0700273static UmountStat TryUmountAndFsck(bool runFsck, int timeoutMs) {
274 Timer t;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700275 std::vector<MountEntry> block_devices;
276 std::vector<MountEntry> emulated_devices;
Keun-young Park8d01f632017-03-13 11:54:47 -0700277
278 TurnOffBacklight(); // this part can take time. save power.
279
Keun-young Park2ba5c812017-03-29 12:54:40 -0700280 if (runFsck && !FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700281 return UMOUNT_STAT_ERROR;
282 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700283
284 UmountStat stat = UmountPartitions(timeoutMs - t.duration_ms());
285 if (stat != UMOUNT_STAT_SUCCESS) {
286 LOG(INFO) << "umount timeout, last resort, kill all and try";
Keun-young Park1663e972017-04-21 17:29:26 -0700287 if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(false);
Keun-young Park3ee0df92017-03-27 11:21:09 -0700288 KillAllProcesses();
Keun-young Park2ba5c812017-03-29 12:54:40 -0700289 // even if it succeeds, still it is timeout and do not run fsck with all processes killed
290 UmountPartitions(0);
Keun-young Park1663e972017-04-21 17:29:26 -0700291 if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(true);
Keun-young Park8d01f632017-03-13 11:54:47 -0700292 }
293
Keun-young Park2ba5c812017-03-29 12:54:40 -0700294 if (stat == UMOUNT_STAT_SUCCESS && runFsck) {
295 // fsck part is excluded from timeout check. It only runs for user initiated shutdown
296 // and should not affect reboot time.
297 for (auto& entry : block_devices) {
298 entry.DoFsck();
299 }
300 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700301 return stat;
302}
303
Keun-young Park8d01f632017-03-13 11:54:47 -0700304static void __attribute__((noreturn)) DoThermalOff() {
305 LOG(WARNING) << "Thermal system shutdown";
Keun-young Park2ba5c812017-03-29 12:54:40 -0700306 sync();
Keun-young Park8d01f632017-03-13 11:54:47 -0700307 RebootSystem(ANDROID_RB_THERMOFF, "");
308 abort();
309}
310
311void DoReboot(unsigned int cmd, const std::string& reason, const std::string& rebootTarget,
312 bool runFsck) {
313 Timer t;
Keun-young Park3cd8c6f2017-03-23 15:33:16 -0700314 LOG(INFO) << "Reboot start, reason: " << reason << ", rebootTarget: " << rebootTarget;
Keun-young Park8d01f632017-03-13 11:54:47 -0700315
Todd Poynorfc827be2017-04-13 15:17:24 -0700316 android::base::WriteStringToFile(StringPrintf("%s\n", reason.c_str()), LAST_REBOOT_REASON_FILE,
317 S_IRUSR | S_IWUSR, AID_SYSTEM, AID_SYSTEM);
Keun-young Park8d01f632017-03-13 11:54:47 -0700318
319 if (cmd == ANDROID_RB_THERMOFF) { // do not wait if it is thermal
320 DoThermalOff();
321 abort();
322 }
Keun-young Parkaa08ea42017-03-23 13:27:28 -0700323
Keun-young Park7feab682017-04-26 12:36:14 -0700324 constexpr unsigned int shutdownTimeoutDefault = 6;
Keun-young Parkc4ffa5c2017-03-28 09:41:36 -0700325 unsigned int shutdownTimeout = shutdownTimeoutDefault;
326 if (SHUTDOWN_ZERO_TIMEOUT) { // eng build
327 shutdownTimeout = 0;
328 } else {
329 shutdownTimeout =
330 android::base::GetUintProperty("ro.build.shutdown_timeout", shutdownTimeoutDefault);
331 }
Tom Cherryccf23532017-03-28 16:40:41 -0700332 LOG(INFO) << "Shutdown timeout: " << shutdownTimeout;
Keun-young Parkaa08ea42017-03-23 13:27:28 -0700333
Keun-young Park7830d592017-03-27 16:07:02 -0700334 // keep debugging tools until non critical ones are all gone.
335 const std::set<std::string> kill_after_apps{"tombstoned", "logd", "adbd"};
336 // watchdogd is a vendor specific component but should be alive to complete shutdown safely.
Keun-young Park7264bee2017-05-18 13:28:32 -0700337 const std::set<std::string> to_starts{"watchdogd", "vold", "ueventd"};
Keun-young Park7830d592017-03-27 16:07:02 -0700338 ServiceManager::GetInstance().ForEachService([&kill_after_apps, &to_starts](Service* s) {
339 if (kill_after_apps.count(s->name())) {
340 s->SetShutdownCritical();
341 } else if (to_starts.count(s->name())) {
342 s->Start();
343 s->SetShutdownCritical();
Keun-young Park8d01f632017-03-13 11:54:47 -0700344 }
Keun-young Park7830d592017-03-27 16:07:02 -0700345 });
346
347 Service* bootAnim = ServiceManager::GetInstance().FindServiceByName("bootanim");
348 Service* surfaceFlinger = ServiceManager::GetInstance().FindServiceByName("surfaceflinger");
349 if (bootAnim != nullptr && surfaceFlinger != nullptr && surfaceFlinger->IsRunning()) {
Keun-young Park7830d592017-03-27 16:07:02 -0700350 ServiceManager::GetInstance().ForEachServiceInClass("animation", [](Service* s) {
Keun-young Park7830d592017-03-27 16:07:02 -0700351 s->SetShutdownCritical(); // will not check animation class separately
352 });
Keun-young Park8d01f632017-03-13 11:54:47 -0700353 }
Keun-young Park7830d592017-03-27 16:07:02 -0700354
Keun-young Park8d01f632017-03-13 11:54:47 -0700355 // optional shutdown step
356 // 1. terminate all services except shutdown critical ones. wait for delay to finish
Keun-young Park3ee0df92017-03-27 11:21:09 -0700357 if (shutdownTimeout > 0) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700358 LOG(INFO) << "terminating init services";
Keun-young Park8d01f632017-03-13 11:54:47 -0700359
360 // Ask all services to terminate except shutdown critical ones.
361 ServiceManager::GetInstance().ForEachService([](Service* s) {
362 if (!s->IsShutdownCritical()) s->Terminate();
363 });
364
365 int service_count = 0;
Keun-young Park3ee0df92017-03-27 11:21:09 -0700366 // Up to half as long as shutdownTimeout or 3 seconds, whichever is lower.
367 unsigned int terminationWaitTimeout = std::min<unsigned int>((shutdownTimeout + 1) / 2, 3);
368 while (t.duration_s() < terminationWaitTimeout) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700369 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
370
371 service_count = 0;
372 ServiceManager::GetInstance().ForEachService([&service_count](Service* s) {
373 // Count the number of services running except shutdown critical.
374 // Exclude the console as it will ignore the SIGTERM signal
375 // and not exit.
376 // Note: SVC_CONSOLE actually means "requires console" but
377 // it is only used by the shell.
378 if (!s->IsShutdownCritical() && s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
379 service_count++;
380 }
381 });
382
383 if (service_count == 0) {
384 // All terminable services terminated. We can exit early.
385 break;
386 }
387
388 // Wait a bit before recounting the number or running services.
389 std::this_thread::sleep_for(50ms);
390 }
391 LOG(INFO) << "Terminating running services took " << t
392 << " with remaining services:" << service_count;
393 }
394
395 // minimum safety steps before restarting
396 // 2. kill all services except ones that are necessary for the shutdown sequence.
Keun-young Park2ba5c812017-03-29 12:54:40 -0700397 ServiceManager::GetInstance().ForEachService([](Service* s) {
398 if (!s->IsShutdownCritical()) s->Stop();
Keun-young Park8d01f632017-03-13 11:54:47 -0700399 });
400 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
401
402 // 3. send volume shutdown to vold
403 Service* voldService = ServiceManager::GetInstance().FindServiceByName("vold");
404 if (voldService != nullptr && voldService->IsRunning()) {
405 ShutdownVold();
Keun-young Park2ba5c812017-03-29 12:54:40 -0700406 voldService->Stop();
Keun-young Park8d01f632017-03-13 11:54:47 -0700407 } else {
408 LOG(INFO) << "vold not running, skipping vold shutdown";
409 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700410 // logcat stopped here
411 ServiceManager::GetInstance().ForEachService([&kill_after_apps](Service* s) {
412 if (kill_after_apps.count(s->name())) s->Stop();
413 });
Keun-young Park8d01f632017-03-13 11:54:47 -0700414 // 4. sync, try umount, and optionally run fsck for user shutdown
Keun-young Park2ba5c812017-03-29 12:54:40 -0700415 sync();
Keun-young Park3ee0df92017-03-27 11:21:09 -0700416 UmountStat stat = TryUmountAndFsck(runFsck, shutdownTimeout * 1000 - t.duration_ms());
Keun-young Park2ba5c812017-03-29 12:54:40 -0700417 // Follow what linux shutdown is doing: one more sync with little bit delay
418 sync();
419 std::this_thread::sleep_for(100ms);
Keun-young Park8d01f632017-03-13 11:54:47 -0700420 LogShutdownTime(stat, &t);
421 // Reboot regardless of umount status. If umount fails, fsck after reboot will fix it.
422 RebootSystem(cmd, rebootTarget);
423 abort();
424}
Tom Cherry98ad32a2017-04-17 16:34:20 -0700425
426bool HandlePowerctlMessage(const std::string& command) {
427 unsigned int cmd = 0;
428 std::vector<std::string> cmd_params = android::base::Split(command, ",");
Tom Cherry98ad32a2017-04-17 16:34:20 -0700429 std::string reboot_target = "";
430 bool run_fsck = false;
431 bool command_invalid = false;
432
433 if (cmd_params.size() > 3) {
434 command_invalid = true;
435 } else if (cmd_params[0] == "shutdown") {
436 cmd = ANDROID_RB_POWEROFF;
437 if (cmd_params.size() == 2 && cmd_params[1] == "userrequested") {
438 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
439 // Run fsck once the file system is remounted in read-only mode.
440 run_fsck = true;
Tom Cherry98ad32a2017-04-17 16:34:20 -0700441 }
442 } else if (cmd_params[0] == "reboot") {
443 cmd = ANDROID_RB_RESTART2;
444 if (cmd_params.size() >= 2) {
445 reboot_target = cmd_params[1];
446 // When rebooting to the bootloader notify the bootloader writing
447 // also the BCB.
448 if (reboot_target == "bootloader") {
449 std::string err;
450 if (!write_reboot_bootloader(&err)) {
451 LOG(ERROR) << "reboot-bootloader: Error writing "
452 "bootloader_message: "
453 << err;
454 }
455 }
456 // If there is an additional bootloader parameter, pass it along
457 if (cmd_params.size() == 3) {
458 reboot_target += "," + cmd_params[2];
459 }
460 }
461 } else if (command == "thermal-shutdown") { // no additional parameter allowed
462 cmd = ANDROID_RB_THERMOFF;
463 } else {
464 command_invalid = true;
465 }
466 if (command_invalid) {
467 LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
468 return false;
469 }
470
Tom Cherry47336ce2017-04-20 16:38:54 -0700471 DoReboot(cmd, command, reboot_target, run_fsck);
Tom Cherry98ad32a2017-04-17 16:34:20 -0700472 return true;
473}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700474
475} // namespace init
476} // namespace android