blob: cee5a72e49210915d684827d6ed4a59ce764a83d [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")) {
Keun-young Park45ae1b22017-07-17 12:20:33 -0700201 std::string mount_dir(mentry->mnt_dir);
202 // These are R/O partitions changed to R/W after adb remount.
203 // Do not umount them as shutdown critical services may rely on them.
Wei Wangb7a87642017-07-25 10:52:08 -0700204 if (mount_dir != "/" && mount_dir != "/system" && mount_dir != "/vendor" &&
205 mount_dir != "/oem") {
Keun-young Park45ae1b22017-07-17 12:20:33 -0700206 blockDevPartitions->emplace(blockDevPartitions->begin(), *mentry);
207 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700208 } else if (MountEntry::IsEmulatedDevice(*mentry)) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700209 emulatedPartitions->emplace(emulatedPartitions->begin(), *mentry);
Keun-young Park8d01f632017-03-13 11:54:47 -0700210 }
211 }
212 return true;
213}
214
Keun-young Park1663e972017-04-21 17:29:26 -0700215static void DumpUmountDebuggingInfo(bool dump_all) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700216 int status;
217 if (!security_getenforce()) {
218 LOG(INFO) << "Run lsof";
219 const char* lsof_argv[] = {"/system/bin/lsof"};
220 android_fork_execvp_ext(arraysize(lsof_argv), (char**)lsof_argv, &status, true, LOG_KLOG,
221 true, nullptr, nullptr, 0);
Keun-young Park8d01f632017-03-13 11:54:47 -0700222 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700223 FindPartitionsToUmount(nullptr, nullptr, true);
Keun-young Park1663e972017-04-21 17:29:26 -0700224 if (dump_all) {
225 // dump current tasks, this log can be lengthy, so only dump with dump_all
226 android::base::WriteStringToFile("t", "/proc/sysrq-trigger");
227 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700228}
229
230static UmountStat UmountPartitions(int timeoutMs) {
231 Timer t;
232 UmountStat stat = UMOUNT_STAT_TIMEOUT;
233 int retry = 0;
234 /* data partition needs all pending writes to be completed and all emulated partitions
235 * umounted.If the current waiting is not good enough, give
236 * up and leave it to e2fsck after reboot to fix it.
237 */
238 while (true) {
239 std::vector<MountEntry> block_devices;
240 std::vector<MountEntry> emulated_devices;
241 if (!FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
242 return UMOUNT_STAT_ERROR;
243 }
244 if (block_devices.size() == 0) {
245 stat = UMOUNT_STAT_SUCCESS;
246 break;
247 }
248 if ((timeoutMs < t.duration_ms()) && retry > 0) { // try umount at least once
249 stat = UMOUNT_STAT_TIMEOUT;
250 break;
251 }
252 if (emulated_devices.size() > 0 &&
253 std::all_of(emulated_devices.begin(), emulated_devices.end(),
254 [](auto& entry) { return entry.Umount(); })) {
255 sync();
256 }
257 for (auto& entry : block_devices) {
258 entry.Umount();
259 }
260 retry++;
261 std::this_thread::sleep_for(100ms);
262 }
263 return stat;
Keun-young Park8d01f632017-03-13 11:54:47 -0700264}
265
Keun-young Park3ee0df92017-03-27 11:21:09 -0700266static void KillAllProcesses() { android::base::WriteStringToFile("i", "/proc/sysrq-trigger"); }
267
Keun-young Park8d01f632017-03-13 11:54:47 -0700268/* Try umounting all emulated file systems R/W block device cfile systems.
269 * This will just try umount and give it up if it fails.
270 * For fs like ext4, this is ok as file system will be marked as unclean shutdown
271 * and necessary check can be done at the next reboot.
272 * For safer shutdown, caller needs to make sure that
273 * all processes / emulated partition for the target fs are all cleaned-up.
274 *
275 * return true when umount was successful. false when timed out.
276 */
Keun-young Park3ee0df92017-03-27 11:21:09 -0700277static UmountStat TryUmountAndFsck(bool runFsck, int timeoutMs) {
278 Timer t;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700279 std::vector<MountEntry> block_devices;
280 std::vector<MountEntry> emulated_devices;
Keun-young Park8d01f632017-03-13 11:54:47 -0700281
282 TurnOffBacklight(); // this part can take time. save power.
283
Keun-young Park2ba5c812017-03-29 12:54:40 -0700284 if (runFsck && !FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700285 return UMOUNT_STAT_ERROR;
286 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700287
288 UmountStat stat = UmountPartitions(timeoutMs - t.duration_ms());
289 if (stat != UMOUNT_STAT_SUCCESS) {
290 LOG(INFO) << "umount timeout, last resort, kill all and try";
Keun-young Park366c51e2017-07-18 18:52:25 -0700291 if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(true);
Keun-young Park3ee0df92017-03-27 11:21:09 -0700292 KillAllProcesses();
Keun-young Park2ba5c812017-03-29 12:54:40 -0700293 // even if it succeeds, still it is timeout and do not run fsck with all processes killed
Keun-young Park366c51e2017-07-18 18:52:25 -0700294 UmountStat st = UmountPartitions(0);
295 if ((st != UMOUNT_STAT_SUCCESS) && DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(false);
Keun-young Park8d01f632017-03-13 11:54:47 -0700296 }
297
Keun-young Park2ba5c812017-03-29 12:54:40 -0700298 if (stat == UMOUNT_STAT_SUCCESS && runFsck) {
299 // fsck part is excluded from timeout check. It only runs for user initiated shutdown
300 // and should not affect reboot time.
301 for (auto& entry : block_devices) {
302 entry.DoFsck();
303 }
304 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700305 return stat;
306}
307
Keun-young Park8d01f632017-03-13 11:54:47 -0700308void 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
Keun-young Parkc1115372017-07-18 10:58:28 -0700316 bool is_thermal_shutdown = false;
317 if (cmd == ANDROID_RB_THERMOFF) {
318 is_thermal_shutdown = true;
319 runFsck = false;
Keun-young Park8d01f632017-03-13 11:54:47 -0700320 }
Keun-young Parkaa08ea42017-03-23 13:27:28 -0700321
Keun-young Parkc1115372017-07-18 10:58:28 -0700322 unsigned int shutdown_timeout = 0; // ms
323 if (!SHUTDOWN_ZERO_TIMEOUT) {
324 if (is_thermal_shutdown) {
325 constexpr unsigned int thermal_shutdown_timeout = 1; // sec
326 shutdown_timeout = thermal_shutdown_timeout * 1000;
327 } else {
328 constexpr unsigned int shutdown_timeout_default = 6; // sec
329 auto shutdown_timeout_property = android::base::GetUintProperty(
330 "ro.build.shutdown_timeout", shutdown_timeout_default);
331 shutdown_timeout = shutdown_timeout_property * 1000;
332 }
Keun-young Parkc4ffa5c2017-03-28 09:41:36 -0700333 }
Keun-young Parkc1115372017-07-18 10:58:28 -0700334 LOG(INFO) << "Shutdown timeout: " << shutdown_timeout << " ms";
Keun-young Parkaa08ea42017-03-23 13:27:28 -0700335
Keun-young Park7830d592017-03-27 16:07:02 -0700336 // keep debugging tools until non critical ones are all gone.
337 const std::set<std::string> kill_after_apps{"tombstoned", "logd", "adbd"};
338 // watchdogd is a vendor specific component but should be alive to complete shutdown safely.
Keun-young Parkd266d372017-07-05 11:38:44 -0700339 const std::set<std::string> to_starts{"watchdogd"};
Keun-young Park7830d592017-03-27 16:07:02 -0700340 ServiceManager::GetInstance().ForEachService([&kill_after_apps, &to_starts](Service* s) {
341 if (kill_after_apps.count(s->name())) {
342 s->SetShutdownCritical();
343 } else if (to_starts.count(s->name())) {
344 s->Start();
345 s->SetShutdownCritical();
Keun-young Parkd266d372017-07-05 11:38:44 -0700346 } else if (s->IsShutdownCritical()) {
347 s->Start(); // start shutdown critical service if not started
Keun-young Park8d01f632017-03-13 11:54:47 -0700348 }
Keun-young Park7830d592017-03-27 16:07:02 -0700349 });
350
351 Service* bootAnim = ServiceManager::GetInstance().FindServiceByName("bootanim");
352 Service* surfaceFlinger = ServiceManager::GetInstance().FindServiceByName("surfaceflinger");
353 if (bootAnim != nullptr && surfaceFlinger != nullptr && surfaceFlinger->IsRunning()) {
Keun-young Park7830d592017-03-27 16:07:02 -0700354 ServiceManager::GetInstance().ForEachServiceInClass("animation", [](Service* s) {
Keun-young Park7830d592017-03-27 16:07:02 -0700355 s->SetShutdownCritical(); // will not check animation class separately
356 });
Keun-young Park8d01f632017-03-13 11:54:47 -0700357 }
Keun-young Park7830d592017-03-27 16:07:02 -0700358
Keun-young Park8d01f632017-03-13 11:54:47 -0700359 // optional shutdown step
360 // 1. terminate all services except shutdown critical ones. wait for delay to finish
Keun-young Parkc1115372017-07-18 10:58:28 -0700361 if (shutdown_timeout > 0) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700362 LOG(INFO) << "terminating init services";
Keun-young Park8d01f632017-03-13 11:54:47 -0700363
364 // Ask all services to terminate except shutdown critical ones.
365 ServiceManager::GetInstance().ForEachService([](Service* s) {
366 if (!s->IsShutdownCritical()) s->Terminate();
367 });
368
369 int service_count = 0;
Keun-young Parkc1115372017-07-18 10:58:28 -0700370 // Only wait up to half of timeout here
371 auto termination_wait_timeout = shutdown_timeout / 2;
372 while (t.duration_ms() < termination_wait_timeout) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700373 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
374
375 service_count = 0;
376 ServiceManager::GetInstance().ForEachService([&service_count](Service* s) {
377 // Count the number of services running except shutdown critical.
378 // Exclude the console as it will ignore the SIGTERM signal
379 // and not exit.
380 // Note: SVC_CONSOLE actually means "requires console" but
381 // it is only used by the shell.
382 if (!s->IsShutdownCritical() && s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
383 service_count++;
384 }
385 });
386
387 if (service_count == 0) {
388 // All terminable services terminated. We can exit early.
389 break;
390 }
391
392 // Wait a bit before recounting the number or running services.
393 std::this_thread::sleep_for(50ms);
394 }
395 LOG(INFO) << "Terminating running services took " << t
396 << " with remaining services:" << service_count;
397 }
398
399 // minimum safety steps before restarting
400 // 2. kill all services except ones that are necessary for the shutdown sequence.
Keun-young Park2ba5c812017-03-29 12:54:40 -0700401 ServiceManager::GetInstance().ForEachService([](Service* s) {
402 if (!s->IsShutdownCritical()) s->Stop();
Keun-young Park8d01f632017-03-13 11:54:47 -0700403 });
404 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
405
406 // 3. send volume shutdown to vold
407 Service* voldService = ServiceManager::GetInstance().FindServiceByName("vold");
408 if (voldService != nullptr && voldService->IsRunning()) {
409 ShutdownVold();
Keun-young Park2ba5c812017-03-29 12:54:40 -0700410 voldService->Stop();
Keun-young Park8d01f632017-03-13 11:54:47 -0700411 } else {
412 LOG(INFO) << "vold not running, skipping vold shutdown";
413 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700414 // logcat stopped here
415 ServiceManager::GetInstance().ForEachService([&kill_after_apps](Service* s) {
416 if (kill_after_apps.count(s->name())) s->Stop();
417 });
Keun-young Park8d01f632017-03-13 11:54:47 -0700418 // 4. sync, try umount, and optionally run fsck for user shutdown
Keun-young Park2ba5c812017-03-29 12:54:40 -0700419 sync();
Keun-young Parkc1115372017-07-18 10:58:28 -0700420 UmountStat stat = TryUmountAndFsck(runFsck, shutdown_timeout - t.duration_ms());
Keun-young Park2ba5c812017-03-29 12:54:40 -0700421 // Follow what linux shutdown is doing: one more sync with little bit delay
422 sync();
Keun-young Parkc1115372017-07-18 10:58:28 -0700423 if (!is_thermal_shutdown) std::this_thread::sleep_for(100ms);
Keun-young Park8d01f632017-03-13 11:54:47 -0700424 LogShutdownTime(stat, &t);
425 // Reboot regardless of umount status. If umount fails, fsck after reboot will fix it.
426 RebootSystem(cmd, rebootTarget);
427 abort();
428}
Tom Cherry98ad32a2017-04-17 16:34:20 -0700429
430bool HandlePowerctlMessage(const std::string& command) {
431 unsigned int cmd = 0;
432 std::vector<std::string> cmd_params = android::base::Split(command, ",");
Tom Cherry98ad32a2017-04-17 16:34:20 -0700433 std::string reboot_target = "";
434 bool run_fsck = false;
435 bool command_invalid = false;
436
437 if (cmd_params.size() > 3) {
438 command_invalid = true;
439 } else if (cmd_params[0] == "shutdown") {
440 cmd = ANDROID_RB_POWEROFF;
441 if (cmd_params.size() == 2 && cmd_params[1] == "userrequested") {
442 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
443 // Run fsck once the file system is remounted in read-only mode.
444 run_fsck = true;
Tom Cherry98ad32a2017-04-17 16:34:20 -0700445 }
446 } else if (cmd_params[0] == "reboot") {
447 cmd = ANDROID_RB_RESTART2;
448 if (cmd_params.size() >= 2) {
449 reboot_target = cmd_params[1];
450 // When rebooting to the bootloader notify the bootloader writing
451 // also the BCB.
452 if (reboot_target == "bootloader") {
453 std::string err;
454 if (!write_reboot_bootloader(&err)) {
455 LOG(ERROR) << "reboot-bootloader: Error writing "
456 "bootloader_message: "
457 << err;
458 }
459 }
460 // If there is an additional bootloader parameter, pass it along
461 if (cmd_params.size() == 3) {
462 reboot_target += "," + cmd_params[2];
463 }
464 }
465 } else if (command == "thermal-shutdown") { // no additional parameter allowed
Wei Wang63a1d0a2017-07-24 13:08:41 -0700466 // run_fsck is false to avoid delay
Tom Cherry98ad32a2017-04-17 16:34:20 -0700467 cmd = ANDROID_RB_THERMOFF;
468 } else {
469 command_invalid = true;
470 }
471 if (command_invalid) {
472 LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
473 return false;
474 }
475
Wei Wang5d4d6f72017-06-27 22:08:45 -0700476 LOG(INFO) << "Clear action queue and start shutdown trigger";
477 ActionManager::GetInstance().ClearQueue();
478 // Queue shutdown trigger first
479 ActionManager::GetInstance().QueueEventTrigger("shutdown");
480 // Queue built-in shutdown_done
481 auto shutdown_handler = [cmd, command, reboot_target,
482 run_fsck](const std::vector<std::string>&) {
483 DoReboot(cmd, command, reboot_target, run_fsck);
484 return 0;
485 };
486 ActionManager::GetInstance().QueueBuiltinAction(shutdown_handler, "shutdown_done");
487
488 // Skip wait for prop if it is in progress
489 ResetWaitForProp();
490
491 // Skip wait for exec if it is in progress
492 if (ServiceManager::GetInstance().IsWaitingForExec()) {
493 ServiceManager::GetInstance().ClearExecWait();
494 }
495
Tom Cherry98ad32a2017-04-17 16:34:20 -0700496 return true;
497}