blob: 99b4922d53945caa189e4cecd838570705bd16f2 [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
Wei Wang5d4d6f72017-06-27 22:08:45 -070051#include "init.h"
Keun-young Park7830d592017-03-27 16:07:02 -070052#include "property_service.h"
Keun-young Park8d01f632017-03-13 11:54:47 -070053#include "service.h"
Keun-young Park8d01f632017-03-13 11:54:47 -070054
55using android::base::StringPrintf;
56
57// represents umount status during reboot / shutdown.
58enum UmountStat {
59 /* umount succeeded. */
60 UMOUNT_STAT_SUCCESS = 0,
61 /* umount was not run. */
62 UMOUNT_STAT_SKIPPED = 1,
63 /* umount failed with timeout. */
64 UMOUNT_STAT_TIMEOUT = 2,
65 /* could not run due to error */
66 UMOUNT_STAT_ERROR = 3,
67 /* not used by init but reserved for other part to use this to represent the
68 the state where umount status before reboot is not found / available. */
69 UMOUNT_STAT_NOT_AVAILABLE = 4,
70};
71
72// Utility for struct mntent
73class MountEntry {
74 public:
Keun-young Park2ba5c812017-03-29 12:54:40 -070075 explicit MountEntry(const mntent& entry)
Keun-young Park8d01f632017-03-13 11:54:47 -070076 : mnt_fsname_(entry.mnt_fsname),
77 mnt_dir_(entry.mnt_dir),
78 mnt_type_(entry.mnt_type),
Keun-young Park2ba5c812017-03-29 12:54:40 -070079 mnt_opts_(entry.mnt_opts) {}
Keun-young Park8d01f632017-03-13 11:54:47 -070080
Keun-young Park2ba5c812017-03-29 12:54:40 -070081 bool Umount() {
82 int r = umount2(mnt_dir_.c_str(), 0);
83 if (r == 0) {
84 LOG(INFO) << "umounted " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_;
85 return true;
86 } else {
87 PLOG(WARNING) << "cannot umount " << mnt_fsname_ << ":" << mnt_dir_ << " opts "
88 << mnt_opts_;
89 return false;
90 }
91 }
Keun-young Park8d01f632017-03-13 11:54:47 -070092
Keun-young Park2ba5c812017-03-29 12:54:40 -070093 void DoFsck() {
94 int st;
95 if (IsF2Fs()) {
96 const char* f2fs_argv[] = {
97 "/system/bin/fsck.f2fs", "-f", mnt_fsname_.c_str(),
98 };
99 android_fork_execvp_ext(arraysize(f2fs_argv), (char**)f2fs_argv, &st, true, LOG_KLOG,
100 true, nullptr, nullptr, 0);
101 } else if (IsExt4()) {
102 const char* ext4_argv[] = {
103 "/system/bin/e2fsck", "-f", "-y", mnt_fsname_.c_str(),
104 };
105 android_fork_execvp_ext(arraysize(ext4_argv), (char**)ext4_argv, &st, true, LOG_KLOG,
106 true, nullptr, nullptr, 0);
107 }
108 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700109
110 static bool IsBlockDevice(const struct mntent& mntent) {
111 return android::base::StartsWith(mntent.mnt_fsname, "/dev/block");
112 }
113
114 static bool IsEmulatedDevice(const struct mntent& mntent) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700115 return android::base::StartsWith(mntent.mnt_fsname, "/data/");
Keun-young Park8d01f632017-03-13 11:54:47 -0700116 }
117
118 private:
Keun-young Park2ba5c812017-03-29 12:54:40 -0700119 bool IsF2Fs() const { return mnt_type_ == "f2fs"; }
120
121 bool IsExt4() const { return mnt_type_ == "ext4"; }
122
Keun-young Park8d01f632017-03-13 11:54:47 -0700123 std::string mnt_fsname_;
124 std::string mnt_dir_;
125 std::string mnt_type_;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700126 std::string mnt_opts_;
Keun-young Park8d01f632017-03-13 11:54:47 -0700127};
128
129// Turn off backlight while we are performing power down cleanup activities.
130static void TurnOffBacklight() {
131 static constexpr char OFF[] = "0";
132
133 android::base::WriteStringToFile(OFF, "/sys/class/leds/lcd-backlight/brightness");
134
135 static const char backlightDir[] = "/sys/class/backlight";
136 std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(backlightDir), closedir);
137 if (!dir) {
138 return;
139 }
140
141 struct dirent* dp;
142 while ((dp = readdir(dir.get())) != nullptr) {
143 if (((dp->d_type != DT_DIR) && (dp->d_type != DT_LNK)) || (dp->d_name[0] == '.')) {
144 continue;
145 }
146
147 std::string fileName = StringPrintf("%s/%s/brightness", backlightDir, dp->d_name);
148 android::base::WriteStringToFile(OFF, fileName);
149 }
150}
151
Keun-young Park8d01f632017-03-13 11:54:47 -0700152static void ShutdownVold() {
153 const char* vdc_argv[] = {"/system/bin/vdc", "volume", "shutdown"};
154 int status;
155 android_fork_execvp_ext(arraysize(vdc_argv), (char**)vdc_argv, &status, true, LOG_KLOG, true,
156 nullptr, nullptr, 0);
157}
158
159static void LogShutdownTime(UmountStat stat, Timer* t) {
160 LOG(WARNING) << "powerctl_shutdown_time_ms:" << std::to_string(t->duration_ms()) << ":" << stat;
161}
162
163static void __attribute__((noreturn))
164RebootSystem(unsigned int cmd, const std::string& rebootTarget) {
Keun-young Park3cd8c6f2017-03-23 15:33:16 -0700165 LOG(INFO) << "Reboot ending, jumping to kernel";
Keun-young Park8d01f632017-03-13 11:54:47 -0700166 switch (cmd) {
167 case ANDROID_RB_POWEROFF:
168 reboot(RB_POWER_OFF);
169 break;
170
171 case ANDROID_RB_RESTART2:
172 syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
173 LINUX_REBOOT_CMD_RESTART2, rebootTarget.c_str());
174 break;
175
176 case ANDROID_RB_THERMOFF:
177 reboot(RB_POWER_OFF);
178 break;
179 }
180 // In normal case, reboot should not return.
181 PLOG(FATAL) << "reboot call returned";
182 abort();
183}
184
185/* Find all read+write block devices and emulated devices in /proc/mounts
186 * and add them to correpsponding list.
187 */
188static bool FindPartitionsToUmount(std::vector<MountEntry>* blockDevPartitions,
Keun-young Park2ba5c812017-03-29 12:54:40 -0700189 std::vector<MountEntry>* emulatedPartitions, bool dump) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700190 std::unique_ptr<std::FILE, int (*)(std::FILE*)> fp(setmntent("/proc/mounts", "r"), endmntent);
191 if (fp == nullptr) {
192 PLOG(ERROR) << "Failed to open /proc/mounts";
193 return false;
194 }
195 mntent* mentry;
196 while ((mentry = getmntent(fp.get())) != nullptr) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700197 if (dump) {
198 LOG(INFO) << "mount entry " << mentry->mnt_fsname << ":" << mentry->mnt_dir << " opts "
199 << mentry->mnt_opts << " type " << mentry->mnt_type;
200 } else if (MountEntry::IsBlockDevice(*mentry) && hasmntopt(mentry, "rw")) {
201 blockDevPartitions->emplace(blockDevPartitions->begin(), *mentry);
Keun-young Park8d01f632017-03-13 11:54:47 -0700202 } else if (MountEntry::IsEmulatedDevice(*mentry)) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700203 emulatedPartitions->emplace(emulatedPartitions->begin(), *mentry);
Keun-young Park8d01f632017-03-13 11:54:47 -0700204 }
205 }
206 return true;
207}
208
Keun-young Park1663e972017-04-21 17:29:26 -0700209static void DumpUmountDebuggingInfo(bool dump_all) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700210 int status;
211 if (!security_getenforce()) {
212 LOG(INFO) << "Run lsof";
213 const char* lsof_argv[] = {"/system/bin/lsof"};
214 android_fork_execvp_ext(arraysize(lsof_argv), (char**)lsof_argv, &status, true, LOG_KLOG,
215 true, nullptr, nullptr, 0);
Keun-young Park8d01f632017-03-13 11:54:47 -0700216 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700217 FindPartitionsToUmount(nullptr, nullptr, true);
Keun-young Park1663e972017-04-21 17:29:26 -0700218 if (dump_all) {
219 // dump current tasks, this log can be lengthy, so only dump with dump_all
220 android::base::WriteStringToFile("t", "/proc/sysrq-trigger");
221 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700222}
223
224static UmountStat UmountPartitions(int timeoutMs) {
225 Timer t;
226 UmountStat stat = UMOUNT_STAT_TIMEOUT;
227 int retry = 0;
228 /* data partition needs all pending writes to be completed and all emulated partitions
229 * umounted.If the current waiting is not good enough, give
230 * up and leave it to e2fsck after reboot to fix it.
231 */
232 while (true) {
233 std::vector<MountEntry> block_devices;
234 std::vector<MountEntry> emulated_devices;
235 if (!FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
236 return UMOUNT_STAT_ERROR;
237 }
238 if (block_devices.size() == 0) {
239 stat = UMOUNT_STAT_SUCCESS;
240 break;
241 }
242 if ((timeoutMs < t.duration_ms()) && retry > 0) { // try umount at least once
243 stat = UMOUNT_STAT_TIMEOUT;
244 break;
245 }
246 if (emulated_devices.size() > 0 &&
247 std::all_of(emulated_devices.begin(), emulated_devices.end(),
248 [](auto& entry) { return entry.Umount(); })) {
249 sync();
250 }
251 for (auto& entry : block_devices) {
252 entry.Umount();
253 }
254 retry++;
255 std::this_thread::sleep_for(100ms);
256 }
257 return stat;
Keun-young Park8d01f632017-03-13 11:54:47 -0700258}
259
Keun-young Park3ee0df92017-03-27 11:21:09 -0700260static void KillAllProcesses() { android::base::WriteStringToFile("i", "/proc/sysrq-trigger"); }
261
Keun-young Park8d01f632017-03-13 11:54:47 -0700262/* Try umounting all emulated file systems R/W block device cfile systems.
263 * This will just try umount and give it up if it fails.
264 * For fs like ext4, this is ok as file system will be marked as unclean shutdown
265 * and necessary check can be done at the next reboot.
266 * For safer shutdown, caller needs to make sure that
267 * all processes / emulated partition for the target fs are all cleaned-up.
268 *
269 * return true when umount was successful. false when timed out.
270 */
Keun-young Park3ee0df92017-03-27 11:21:09 -0700271static UmountStat TryUmountAndFsck(bool runFsck, int timeoutMs) {
272 Timer t;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700273 std::vector<MountEntry> block_devices;
274 std::vector<MountEntry> emulated_devices;
Keun-young Park8d01f632017-03-13 11:54:47 -0700275
276 TurnOffBacklight(); // this part can take time. save power.
277
Keun-young Park2ba5c812017-03-29 12:54:40 -0700278 if (runFsck && !FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700279 return UMOUNT_STAT_ERROR;
280 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700281
282 UmountStat stat = UmountPartitions(timeoutMs - t.duration_ms());
283 if (stat != UMOUNT_STAT_SUCCESS) {
284 LOG(INFO) << "umount timeout, last resort, kill all and try";
Keun-young Park1663e972017-04-21 17:29:26 -0700285 if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(false);
Keun-young Park3ee0df92017-03-27 11:21:09 -0700286 KillAllProcesses();
Keun-young Park2ba5c812017-03-29 12:54:40 -0700287 // even if it succeeds, still it is timeout and do not run fsck with all processes killed
288 UmountPartitions(0);
Keun-young Park1663e972017-04-21 17:29:26 -0700289 if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(true);
Keun-young Park8d01f632017-03-13 11:54:47 -0700290 }
291
Keun-young Park2ba5c812017-03-29 12:54:40 -0700292 if (stat == UMOUNT_STAT_SUCCESS && runFsck) {
293 // fsck part is excluded from timeout check. It only runs for user initiated shutdown
294 // and should not affect reboot time.
295 for (auto& entry : block_devices) {
296 entry.DoFsck();
297 }
298 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700299 return stat;
300}
301
Keun-young Park8d01f632017-03-13 11:54:47 -0700302static void __attribute__((noreturn)) DoThermalOff() {
303 LOG(WARNING) << "Thermal system shutdown";
Keun-young Park2ba5c812017-03-29 12:54:40 -0700304 sync();
Keun-young Park8d01f632017-03-13 11:54:47 -0700305 RebootSystem(ANDROID_RB_THERMOFF, "");
306 abort();
307}
308
309void DoReboot(unsigned int cmd, const std::string& reason, const std::string& rebootTarget,
310 bool runFsck) {
311 Timer t;
Keun-young Park3cd8c6f2017-03-23 15:33:16 -0700312 LOG(INFO) << "Reboot start, reason: " << reason << ", rebootTarget: " << rebootTarget;
Keun-young Park8d01f632017-03-13 11:54:47 -0700313
Todd Poynorfc827be2017-04-13 15:17:24 -0700314 android::base::WriteStringToFile(StringPrintf("%s\n", reason.c_str()), LAST_REBOOT_REASON_FILE,
315 S_IRUSR | S_IWUSR, AID_SYSTEM, AID_SYSTEM);
Keun-young Park8d01f632017-03-13 11:54:47 -0700316
317 if (cmd == ANDROID_RB_THERMOFF) { // do not wait if it is thermal
318 DoThermalOff();
319 abort();
320 }
Keun-young Parkaa08ea42017-03-23 13:27:28 -0700321
Keun-young Park7feab682017-04-26 12:36:14 -0700322 constexpr unsigned int shutdownTimeoutDefault = 6;
Keun-young Parkc4ffa5c2017-03-28 09:41:36 -0700323 unsigned int shutdownTimeout = shutdownTimeoutDefault;
324 if (SHUTDOWN_ZERO_TIMEOUT) { // eng build
325 shutdownTimeout = 0;
326 } else {
327 shutdownTimeout =
328 android::base::GetUintProperty("ro.build.shutdown_timeout", shutdownTimeoutDefault);
329 }
Tom Cherryccf23532017-03-28 16:40:41 -0700330 LOG(INFO) << "Shutdown timeout: " << shutdownTimeout;
Keun-young Parkaa08ea42017-03-23 13:27:28 -0700331
Keun-young Park7830d592017-03-27 16:07:02 -0700332 // keep debugging tools until non critical ones are all gone.
333 const std::set<std::string> kill_after_apps{"tombstoned", "logd", "adbd"};
334 // watchdogd is a vendor specific component but should be alive to complete shutdown safely.
Keun-young Park7264bee2017-05-18 13:28:32 -0700335 const std::set<std::string> to_starts{"watchdogd", "vold", "ueventd"};
Keun-young Park7830d592017-03-27 16:07:02 -0700336 ServiceManager::GetInstance().ForEachService([&kill_after_apps, &to_starts](Service* s) {
337 if (kill_after_apps.count(s->name())) {
338 s->SetShutdownCritical();
339 } else if (to_starts.count(s->name())) {
340 s->Start();
341 s->SetShutdownCritical();
Keun-young Park8d01f632017-03-13 11:54:47 -0700342 }
Keun-young Park7830d592017-03-27 16:07:02 -0700343 });
344
345 Service* bootAnim = ServiceManager::GetInstance().FindServiceByName("bootanim");
346 Service* surfaceFlinger = ServiceManager::GetInstance().FindServiceByName("surfaceflinger");
347 if (bootAnim != nullptr && surfaceFlinger != nullptr && surfaceFlinger->IsRunning()) {
Keun-young Park7830d592017-03-27 16:07:02 -0700348 ServiceManager::GetInstance().ForEachServiceInClass("animation", [](Service* s) {
Keun-young Park7830d592017-03-27 16:07:02 -0700349 s->SetShutdownCritical(); // will not check animation class separately
350 });
Keun-young Park8d01f632017-03-13 11:54:47 -0700351 }
Keun-young Park7830d592017-03-27 16:07:02 -0700352
Keun-young Park8d01f632017-03-13 11:54:47 -0700353 // optional shutdown step
354 // 1. terminate all services except shutdown critical ones. wait for delay to finish
Keun-young Park3ee0df92017-03-27 11:21:09 -0700355 if (shutdownTimeout > 0) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700356 LOG(INFO) << "terminating init services";
Keun-young Park8d01f632017-03-13 11:54:47 -0700357
358 // Ask all services to terminate except shutdown critical ones.
359 ServiceManager::GetInstance().ForEachService([](Service* s) {
360 if (!s->IsShutdownCritical()) s->Terminate();
361 });
362
363 int service_count = 0;
Keun-young Park3ee0df92017-03-27 11:21:09 -0700364 // Up to half as long as shutdownTimeout or 3 seconds, whichever is lower.
365 unsigned int terminationWaitTimeout = std::min<unsigned int>((shutdownTimeout + 1) / 2, 3);
366 while (t.duration_s() < terminationWaitTimeout) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700367 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
368
369 service_count = 0;
370 ServiceManager::GetInstance().ForEachService([&service_count](Service* s) {
371 // Count the number of services running except shutdown critical.
372 // Exclude the console as it will ignore the SIGTERM signal
373 // and not exit.
374 // Note: SVC_CONSOLE actually means "requires console" but
375 // it is only used by the shell.
376 if (!s->IsShutdownCritical() && s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
377 service_count++;
378 }
379 });
380
381 if (service_count == 0) {
382 // All terminable services terminated. We can exit early.
383 break;
384 }
385
386 // Wait a bit before recounting the number or running services.
387 std::this_thread::sleep_for(50ms);
388 }
389 LOG(INFO) << "Terminating running services took " << t
390 << " with remaining services:" << service_count;
391 }
392
393 // minimum safety steps before restarting
394 // 2. kill all services except ones that are necessary for the shutdown sequence.
Keun-young Park2ba5c812017-03-29 12:54:40 -0700395 ServiceManager::GetInstance().ForEachService([](Service* s) {
396 if (!s->IsShutdownCritical()) s->Stop();
Keun-young Park8d01f632017-03-13 11:54:47 -0700397 });
398 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
399
400 // 3. send volume shutdown to vold
401 Service* voldService = ServiceManager::GetInstance().FindServiceByName("vold");
402 if (voldService != nullptr && voldService->IsRunning()) {
403 ShutdownVold();
Keun-young Park2ba5c812017-03-29 12:54:40 -0700404 voldService->Stop();
Keun-young Park8d01f632017-03-13 11:54:47 -0700405 } else {
406 LOG(INFO) << "vold not running, skipping vold shutdown";
407 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700408 // logcat stopped here
409 ServiceManager::GetInstance().ForEachService([&kill_after_apps](Service* s) {
410 if (kill_after_apps.count(s->name())) s->Stop();
411 });
Keun-young Park8d01f632017-03-13 11:54:47 -0700412 // 4. sync, try umount, and optionally run fsck for user shutdown
Keun-young Park2ba5c812017-03-29 12:54:40 -0700413 sync();
Keun-young Park3ee0df92017-03-27 11:21:09 -0700414 UmountStat stat = TryUmountAndFsck(runFsck, shutdownTimeout * 1000 - t.duration_ms());
Keun-young Park2ba5c812017-03-29 12:54:40 -0700415 // Follow what linux shutdown is doing: one more sync with little bit delay
416 sync();
417 std::this_thread::sleep_for(100ms);
Keun-young Park8d01f632017-03-13 11:54:47 -0700418 LogShutdownTime(stat, &t);
419 // Reboot regardless of umount status. If umount fails, fsck after reboot will fix it.
420 RebootSystem(cmd, rebootTarget);
421 abort();
422}
Tom Cherry98ad32a2017-04-17 16:34:20 -0700423
424bool HandlePowerctlMessage(const std::string& command) {
425 unsigned int cmd = 0;
426 std::vector<std::string> cmd_params = android::base::Split(command, ",");
Tom Cherry98ad32a2017-04-17 16:34:20 -0700427 std::string reboot_target = "";
428 bool run_fsck = false;
429 bool command_invalid = false;
430
431 if (cmd_params.size() > 3) {
432 command_invalid = true;
433 } else if (cmd_params[0] == "shutdown") {
434 cmd = ANDROID_RB_POWEROFF;
435 if (cmd_params.size() == 2 && cmd_params[1] == "userrequested") {
436 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
437 // Run fsck once the file system is remounted in read-only mode.
438 run_fsck = true;
Tom Cherry98ad32a2017-04-17 16:34:20 -0700439 }
440 } else if (cmd_params[0] == "reboot") {
441 cmd = ANDROID_RB_RESTART2;
442 if (cmd_params.size() >= 2) {
443 reboot_target = cmd_params[1];
444 // When rebooting to the bootloader notify the bootloader writing
445 // also the BCB.
446 if (reboot_target == "bootloader") {
447 std::string err;
448 if (!write_reboot_bootloader(&err)) {
449 LOG(ERROR) << "reboot-bootloader: Error writing "
450 "bootloader_message: "
451 << err;
452 }
453 }
454 // If there is an additional bootloader parameter, pass it along
455 if (cmd_params.size() == 3) {
456 reboot_target += "," + cmd_params[2];
457 }
458 }
459 } else if (command == "thermal-shutdown") { // no additional parameter allowed
460 cmd = ANDROID_RB_THERMOFF;
Wei Wang5d4d6f72017-06-27 22:08:45 -0700461 // Do not queue "shutdown" trigger since we want to shutdown immediately
462 DoReboot(cmd, command, reboot_target, run_fsck);
463 return true;
Tom Cherry98ad32a2017-04-17 16:34:20 -0700464 } else {
465 command_invalid = true;
466 }
467 if (command_invalid) {
468 LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
469 return false;
470 }
471
Wei Wang5d4d6f72017-06-27 22:08:45 -0700472 LOG(INFO) << "Clear action queue and start shutdown trigger";
473 ActionManager::GetInstance().ClearQueue();
474 // Queue shutdown trigger first
475 ActionManager::GetInstance().QueueEventTrigger("shutdown");
476 // Queue built-in shutdown_done
477 auto shutdown_handler = [cmd, command, reboot_target,
478 run_fsck](const std::vector<std::string>&) {
479 DoReboot(cmd, command, reboot_target, run_fsck);
480 return 0;
481 };
482 ActionManager::GetInstance().QueueBuiltinAction(shutdown_handler, "shutdown_done");
483
484 // Skip wait for prop if it is in progress
485 ResetWaitForProp();
486
487 // Skip wait for exec if it is in progress
488 if (ServiceManager::GetInstance().IsWaitingForExec()) {
489 ServiceManager::GetInstance().ClearExecWait();
490 }
491
Tom Cherry98ad32a2017-04-17 16:34:20 -0700492 return true;
493}