blob: 172fc77c9b361eb6f39edd582c6f4a9e0073f5b1 [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
56// represents umount status during reboot / shutdown.
57enum UmountStat {
58 /* umount succeeded. */
59 UMOUNT_STAT_SUCCESS = 0,
60 /* umount was not run. */
61 UMOUNT_STAT_SKIPPED = 1,
62 /* umount failed with timeout. */
63 UMOUNT_STAT_TIMEOUT = 2,
64 /* could not run due to error */
65 UMOUNT_STAT_ERROR = 3,
66 /* not used by init but reserved for other part to use this to represent the
67 the state where umount status before reboot is not found / available. */
68 UMOUNT_STAT_NOT_AVAILABLE = 4,
69};
70
71// Utility for struct mntent
72class MountEntry {
73 public:
Keun-young Park2ba5c812017-03-29 12:54:40 -070074 explicit MountEntry(const mntent& entry)
Keun-young Park8d01f632017-03-13 11:54:47 -070075 : mnt_fsname_(entry.mnt_fsname),
76 mnt_dir_(entry.mnt_dir),
77 mnt_type_(entry.mnt_type),
Keun-young Park2ba5c812017-03-29 12:54:40 -070078 mnt_opts_(entry.mnt_opts) {}
Keun-young Park8d01f632017-03-13 11:54:47 -070079
Keun-young Park2ba5c812017-03-29 12:54:40 -070080 bool Umount() {
81 int r = umount2(mnt_dir_.c_str(), 0);
82 if (r == 0) {
83 LOG(INFO) << "umounted " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_;
84 return true;
85 } else {
86 PLOG(WARNING) << "cannot umount " << mnt_fsname_ << ":" << mnt_dir_ << " opts "
87 << mnt_opts_;
88 return false;
89 }
90 }
Keun-young Park8d01f632017-03-13 11:54:47 -070091
Keun-young Park2ba5c812017-03-29 12:54:40 -070092 void DoFsck() {
93 int st;
94 if (IsF2Fs()) {
95 const char* f2fs_argv[] = {
96 "/system/bin/fsck.f2fs", "-f", mnt_fsname_.c_str(),
97 };
98 android_fork_execvp_ext(arraysize(f2fs_argv), (char**)f2fs_argv, &st, true, LOG_KLOG,
99 true, nullptr, nullptr, 0);
100 } else if (IsExt4()) {
101 const char* ext4_argv[] = {
102 "/system/bin/e2fsck", "-f", "-y", mnt_fsname_.c_str(),
103 };
104 android_fork_execvp_ext(arraysize(ext4_argv), (char**)ext4_argv, &st, true, LOG_KLOG,
105 true, nullptr, nullptr, 0);
106 }
107 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700108
109 static bool IsBlockDevice(const struct mntent& mntent) {
110 return android::base::StartsWith(mntent.mnt_fsname, "/dev/block");
111 }
112
113 static bool IsEmulatedDevice(const struct mntent& mntent) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700114 return android::base::StartsWith(mntent.mnt_fsname, "/data/");
Keun-young Park8d01f632017-03-13 11:54:47 -0700115 }
116
117 private:
Keun-young Park2ba5c812017-03-29 12:54:40 -0700118 bool IsF2Fs() const { return mnt_type_ == "f2fs"; }
119
120 bool IsExt4() const { return mnt_type_ == "ext4"; }
121
Keun-young Park8d01f632017-03-13 11:54:47 -0700122 std::string mnt_fsname_;
123 std::string mnt_dir_;
124 std::string mnt_type_;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700125 std::string mnt_opts_;
Keun-young Park8d01f632017-03-13 11:54:47 -0700126};
127
128// Turn off backlight while we are performing power down cleanup activities.
129static void TurnOffBacklight() {
130 static constexpr char OFF[] = "0";
131
132 android::base::WriteStringToFile(OFF, "/sys/class/leds/lcd-backlight/brightness");
133
134 static const char backlightDir[] = "/sys/class/backlight";
135 std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(backlightDir), closedir);
136 if (!dir) {
137 return;
138 }
139
140 struct dirent* dp;
141 while ((dp = readdir(dir.get())) != nullptr) {
142 if (((dp->d_type != DT_DIR) && (dp->d_type != DT_LNK)) || (dp->d_name[0] == '.')) {
143 continue;
144 }
145
146 std::string fileName = StringPrintf("%s/%s/brightness", backlightDir, dp->d_name);
147 android::base::WriteStringToFile(OFF, fileName);
148 }
149}
150
Keun-young Park8d01f632017-03-13 11:54:47 -0700151static void ShutdownVold() {
152 const char* vdc_argv[] = {"/system/bin/vdc", "volume", "shutdown"};
153 int status;
154 android_fork_execvp_ext(arraysize(vdc_argv), (char**)vdc_argv, &status, true, LOG_KLOG, true,
155 nullptr, nullptr, 0);
156}
157
158static void LogShutdownTime(UmountStat stat, Timer* t) {
159 LOG(WARNING) << "powerctl_shutdown_time_ms:" << std::to_string(t->duration_ms()) << ":" << stat;
160}
161
162static void __attribute__((noreturn))
163RebootSystem(unsigned int cmd, const std::string& rebootTarget) {
Keun-young Park3cd8c6f2017-03-23 15:33:16 -0700164 LOG(INFO) << "Reboot ending, jumping to kernel";
Keun-young Park8d01f632017-03-13 11:54:47 -0700165 switch (cmd) {
166 case ANDROID_RB_POWEROFF:
167 reboot(RB_POWER_OFF);
168 break;
169
170 case ANDROID_RB_RESTART2:
171 syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
172 LINUX_REBOOT_CMD_RESTART2, rebootTarget.c_str());
173 break;
174
175 case ANDROID_RB_THERMOFF:
176 reboot(RB_POWER_OFF);
177 break;
178 }
179 // In normal case, reboot should not return.
180 PLOG(FATAL) << "reboot call returned";
181 abort();
182}
183
184/* Find all read+write block devices and emulated devices in /proc/mounts
185 * and add them to correpsponding list.
186 */
187static bool FindPartitionsToUmount(std::vector<MountEntry>* blockDevPartitions,
Keun-young Park2ba5c812017-03-29 12:54:40 -0700188 std::vector<MountEntry>* emulatedPartitions, bool dump) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700189 std::unique_ptr<std::FILE, int (*)(std::FILE*)> fp(setmntent("/proc/mounts", "r"), endmntent);
190 if (fp == nullptr) {
191 PLOG(ERROR) << "Failed to open /proc/mounts";
192 return false;
193 }
194 mntent* mentry;
195 while ((mentry = getmntent(fp.get())) != nullptr) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700196 if (dump) {
197 LOG(INFO) << "mount entry " << mentry->mnt_fsname << ":" << mentry->mnt_dir << " opts "
198 << mentry->mnt_opts << " type " << mentry->mnt_type;
199 } else if (MountEntry::IsBlockDevice(*mentry) && hasmntopt(mentry, "rw")) {
200 blockDevPartitions->emplace(blockDevPartitions->begin(), *mentry);
Keun-young Park8d01f632017-03-13 11:54:47 -0700201 } else if (MountEntry::IsEmulatedDevice(*mentry)) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700202 emulatedPartitions->emplace(emulatedPartitions->begin(), *mentry);
Keun-young Park8d01f632017-03-13 11:54:47 -0700203 }
204 }
205 return true;
206}
207
Keun-young Park1663e972017-04-21 17:29:26 -0700208static void DumpUmountDebuggingInfo(bool dump_all) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700209 int status;
210 if (!security_getenforce()) {
211 LOG(INFO) << "Run lsof";
212 const char* lsof_argv[] = {"/system/bin/lsof"};
213 android_fork_execvp_ext(arraysize(lsof_argv), (char**)lsof_argv, &status, true, LOG_KLOG,
214 true, nullptr, nullptr, 0);
Keun-young Park8d01f632017-03-13 11:54:47 -0700215 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700216 FindPartitionsToUmount(nullptr, nullptr, true);
Keun-young Park1663e972017-04-21 17:29:26 -0700217 if (dump_all) {
218 // dump current tasks, this log can be lengthy, so only dump with dump_all
219 android::base::WriteStringToFile("t", "/proc/sysrq-trigger");
220 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700221}
222
223static UmountStat UmountPartitions(int timeoutMs) {
224 Timer t;
225 UmountStat stat = UMOUNT_STAT_TIMEOUT;
226 int retry = 0;
227 /* data partition needs all pending writes to be completed and all emulated partitions
228 * umounted.If the current waiting is not good enough, give
229 * up and leave it to e2fsck after reboot to fix it.
230 */
231 while (true) {
232 std::vector<MountEntry> block_devices;
233 std::vector<MountEntry> emulated_devices;
234 if (!FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
235 return UMOUNT_STAT_ERROR;
236 }
237 if (block_devices.size() == 0) {
238 stat = UMOUNT_STAT_SUCCESS;
239 break;
240 }
241 if ((timeoutMs < t.duration_ms()) && retry > 0) { // try umount at least once
242 stat = UMOUNT_STAT_TIMEOUT;
243 break;
244 }
245 if (emulated_devices.size() > 0 &&
246 std::all_of(emulated_devices.begin(), emulated_devices.end(),
247 [](auto& entry) { return entry.Umount(); })) {
248 sync();
249 }
250 for (auto& entry : block_devices) {
251 entry.Umount();
252 }
253 retry++;
254 std::this_thread::sleep_for(100ms);
255 }
256 return stat;
Keun-young Park8d01f632017-03-13 11:54:47 -0700257}
258
Keun-young Park3ee0df92017-03-27 11:21:09 -0700259static void KillAllProcesses() { android::base::WriteStringToFile("i", "/proc/sysrq-trigger"); }
260
Keun-young Park8d01f632017-03-13 11:54:47 -0700261/* Try umounting all emulated file systems R/W block device cfile systems.
262 * This will just try umount and give it up if it fails.
263 * For fs like ext4, this is ok as file system will be marked as unclean shutdown
264 * and necessary check can be done at the next reboot.
265 * For safer shutdown, caller needs to make sure that
266 * all processes / emulated partition for the target fs are all cleaned-up.
267 *
268 * return true when umount was successful. false when timed out.
269 */
Keun-young Park3ee0df92017-03-27 11:21:09 -0700270static UmountStat TryUmountAndFsck(bool runFsck, int timeoutMs) {
271 Timer t;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700272 std::vector<MountEntry> block_devices;
273 std::vector<MountEntry> emulated_devices;
Keun-young Park8d01f632017-03-13 11:54:47 -0700274
275 TurnOffBacklight(); // this part can take time. save power.
276
Keun-young Park2ba5c812017-03-29 12:54:40 -0700277 if (runFsck && !FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700278 return UMOUNT_STAT_ERROR;
279 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700280
281 UmountStat stat = UmountPartitions(timeoutMs - t.duration_ms());
282 if (stat != UMOUNT_STAT_SUCCESS) {
283 LOG(INFO) << "umount timeout, last resort, kill all and try";
Keun-young Park1663e972017-04-21 17:29:26 -0700284 if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(false);
Keun-young Park3ee0df92017-03-27 11:21:09 -0700285 KillAllProcesses();
Keun-young Park2ba5c812017-03-29 12:54:40 -0700286 // even if it succeeds, still it is timeout and do not run fsck with all processes killed
287 UmountPartitions(0);
Keun-young Park1663e972017-04-21 17:29:26 -0700288 if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(true);
Keun-young Park8d01f632017-03-13 11:54:47 -0700289 }
290
Keun-young Park2ba5c812017-03-29 12:54:40 -0700291 if (stat == UMOUNT_STAT_SUCCESS && runFsck) {
292 // fsck part is excluded from timeout check. It only runs for user initiated shutdown
293 // and should not affect reboot time.
294 for (auto& entry : block_devices) {
295 entry.DoFsck();
296 }
297 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700298 return stat;
299}
300
Keun-young Park8d01f632017-03-13 11:54:47 -0700301static void __attribute__((noreturn)) DoThermalOff() {
302 LOG(WARNING) << "Thermal system shutdown";
Keun-young Park2ba5c812017-03-29 12:54:40 -0700303 sync();
Keun-young Park8d01f632017-03-13 11:54:47 -0700304 RebootSystem(ANDROID_RB_THERMOFF, "");
305 abort();
306}
307
308void DoReboot(unsigned int cmd, const std::string& reason, const std::string& rebootTarget,
309 bool runFsck) {
310 Timer t;
Keun-young Park3cd8c6f2017-03-23 15:33:16 -0700311 LOG(INFO) << "Reboot start, reason: " << reason << ", rebootTarget: " << rebootTarget;
Keun-young Park8d01f632017-03-13 11:54:47 -0700312
Todd Poynorfc827be2017-04-13 15:17:24 -0700313 android::base::WriteStringToFile(StringPrintf("%s\n", reason.c_str()), LAST_REBOOT_REASON_FILE,
314 S_IRUSR | S_IWUSR, AID_SYSTEM, AID_SYSTEM);
Keun-young Park8d01f632017-03-13 11:54:47 -0700315
316 if (cmd == ANDROID_RB_THERMOFF) { // do not wait if it is thermal
317 DoThermalOff();
318 abort();
319 }
Keun-young Parkaa08ea42017-03-23 13:27:28 -0700320
Keun-young Park7feab682017-04-26 12:36:14 -0700321 constexpr unsigned int shutdownTimeoutDefault = 6;
Keun-young Parkc4ffa5c2017-03-28 09:41:36 -0700322 unsigned int shutdownTimeout = shutdownTimeoutDefault;
323 if (SHUTDOWN_ZERO_TIMEOUT) { // eng build
324 shutdownTimeout = 0;
325 } else {
326 shutdownTimeout =
327 android::base::GetUintProperty("ro.build.shutdown_timeout", shutdownTimeoutDefault);
328 }
Tom Cherryccf23532017-03-28 16:40:41 -0700329 LOG(INFO) << "Shutdown timeout: " << shutdownTimeout;
Keun-young Parkaa08ea42017-03-23 13:27:28 -0700330
Keun-young Park7830d592017-03-27 16:07:02 -0700331 // keep debugging tools until non critical ones are all gone.
332 const std::set<std::string> kill_after_apps{"tombstoned", "logd", "adbd"};
333 // watchdogd is a vendor specific component but should be alive to complete shutdown safely.
334 const std::set<std::string> to_starts{"watchdogd", "vold"};
335 ServiceManager::GetInstance().ForEachService([&kill_after_apps, &to_starts](Service* s) {
336 if (kill_after_apps.count(s->name())) {
337 s->SetShutdownCritical();
338 } else if (to_starts.count(s->name())) {
339 s->Start();
340 s->SetShutdownCritical();
Keun-young Park8d01f632017-03-13 11:54:47 -0700341 }
Keun-young Park7830d592017-03-27 16:07:02 -0700342 });
343
344 Service* bootAnim = ServiceManager::GetInstance().FindServiceByName("bootanim");
345 Service* surfaceFlinger = ServiceManager::GetInstance().FindServiceByName("surfaceflinger");
346 if (bootAnim != nullptr && surfaceFlinger != nullptr && surfaceFlinger->IsRunning()) {
347 property_set("service.bootanim.exit", "0");
348 // Could be in the middle of animation. Stop and start so that it can pick
349 // up the right mode.
350 bootAnim->Stop();
351 // start all animation classes if stopped.
352 ServiceManager::GetInstance().ForEachServiceInClass("animation", [](Service* s) {
353 s->Start();
354 s->SetShutdownCritical(); // will not check animation class separately
355 });
356 bootAnim->Start();
357 surfaceFlinger->SetShutdownCritical();
358 bootAnim->SetShutdownCritical();
Keun-young Park8d01f632017-03-13 11:54:47 -0700359 }
Keun-young Park7830d592017-03-27 16:07:02 -0700360
Keun-young Park8d01f632017-03-13 11:54:47 -0700361 // optional shutdown step
362 // 1. terminate all services except shutdown critical ones. wait for delay to finish
Keun-young Park3ee0df92017-03-27 11:21:09 -0700363 if (shutdownTimeout > 0) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700364 LOG(INFO) << "terminating init services";
Keun-young Park8d01f632017-03-13 11:54:47 -0700365
366 // Ask all services to terminate except shutdown critical ones.
367 ServiceManager::GetInstance().ForEachService([](Service* s) {
368 if (!s->IsShutdownCritical()) s->Terminate();
369 });
370
371 int service_count = 0;
Keun-young Park3ee0df92017-03-27 11:21:09 -0700372 // Up to half as long as shutdownTimeout or 3 seconds, whichever is lower.
373 unsigned int terminationWaitTimeout = std::min<unsigned int>((shutdownTimeout + 1) / 2, 3);
374 while (t.duration_s() < terminationWaitTimeout) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700375 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
376
377 service_count = 0;
378 ServiceManager::GetInstance().ForEachService([&service_count](Service* s) {
379 // Count the number of services running except shutdown critical.
380 // Exclude the console as it will ignore the SIGTERM signal
381 // and not exit.
382 // Note: SVC_CONSOLE actually means "requires console" but
383 // it is only used by the shell.
384 if (!s->IsShutdownCritical() && s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
385 service_count++;
386 }
387 });
388
389 if (service_count == 0) {
390 // All terminable services terminated. We can exit early.
391 break;
392 }
393
394 // Wait a bit before recounting the number or running services.
395 std::this_thread::sleep_for(50ms);
396 }
397 LOG(INFO) << "Terminating running services took " << t
398 << " with remaining services:" << service_count;
399 }
400
401 // minimum safety steps before restarting
402 // 2. kill all services except ones that are necessary for the shutdown sequence.
Keun-young Park2ba5c812017-03-29 12:54:40 -0700403 ServiceManager::GetInstance().ForEachService([](Service* s) {
404 if (!s->IsShutdownCritical()) s->Stop();
Keun-young Park8d01f632017-03-13 11:54:47 -0700405 });
406 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
407
408 // 3. send volume shutdown to vold
409 Service* voldService = ServiceManager::GetInstance().FindServiceByName("vold");
410 if (voldService != nullptr && voldService->IsRunning()) {
411 ShutdownVold();
Keun-young Park2ba5c812017-03-29 12:54:40 -0700412 voldService->Stop();
Keun-young Park8d01f632017-03-13 11:54:47 -0700413 } else {
414 LOG(INFO) << "vold not running, skipping vold shutdown";
415 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700416 // logcat stopped here
417 ServiceManager::GetInstance().ForEachService([&kill_after_apps](Service* s) {
418 if (kill_after_apps.count(s->name())) s->Stop();
419 });
Keun-young Park8d01f632017-03-13 11:54:47 -0700420 // 4. sync, try umount, and optionally run fsck for user shutdown
Keun-young Park2ba5c812017-03-29 12:54:40 -0700421 sync();
Keun-young Park3ee0df92017-03-27 11:21:09 -0700422 UmountStat stat = TryUmountAndFsck(runFsck, shutdownTimeout * 1000 - t.duration_ms());
Keun-young Park2ba5c812017-03-29 12:54:40 -0700423 // Follow what linux shutdown is doing: one more sync with little bit delay
424 sync();
425 std::this_thread::sleep_for(100ms);
Keun-young Park8d01f632017-03-13 11:54:47 -0700426 LogShutdownTime(stat, &t);
427 // Reboot regardless of umount status. If umount fails, fsck after reboot will fix it.
428 RebootSystem(cmd, rebootTarget);
429 abort();
430}
Tom Cherry98ad32a2017-04-17 16:34:20 -0700431
432bool HandlePowerctlMessage(const std::string& command) {
433 unsigned int cmd = 0;
434 std::vector<std::string> cmd_params = android::base::Split(command, ",");
435 std::string reason_string = cmd_params[0];
436 std::string reboot_target = "";
437 bool run_fsck = false;
438 bool command_invalid = false;
439
440 if (cmd_params.size() > 3) {
441 command_invalid = true;
442 } else if (cmd_params[0] == "shutdown") {
443 cmd = ANDROID_RB_POWEROFF;
444 if (cmd_params.size() == 2 && cmd_params[1] == "userrequested") {
445 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
446 // Run fsck once the file system is remounted in read-only mode.
447 run_fsck = true;
448 reason_string = cmd_params[1];
449 }
450 } else if (cmd_params[0] == "reboot") {
451 cmd = ANDROID_RB_RESTART2;
452 if (cmd_params.size() >= 2) {
453 reboot_target = cmd_params[1];
454 // When rebooting to the bootloader notify the bootloader writing
455 // also the BCB.
456 if (reboot_target == "bootloader") {
457 std::string err;
458 if (!write_reboot_bootloader(&err)) {
459 LOG(ERROR) << "reboot-bootloader: Error writing "
460 "bootloader_message: "
461 << err;
462 }
463 }
464 // If there is an additional bootloader parameter, pass it along
465 if (cmd_params.size() == 3) {
466 reboot_target += "," + cmd_params[2];
467 }
468 }
469 } else if (command == "thermal-shutdown") { // no additional parameter allowed
470 cmd = ANDROID_RB_THERMOFF;
471 } else {
472 command_invalid = true;
473 }
474 if (command_invalid) {
475 LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
476 return false;
477 }
478
479 DoReboot(cmd, reason_string, reboot_target, run_fsck);
480 return true;
481}