blob: e4c535481aabf2fd957d43655f9d7c1301a4b29d [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.
204 if (mount_dir != "/system" && mount_dir != "/vendor" && mount_dir != "/oem") {
205 blockDevPartitions->emplace(blockDevPartitions->begin(), *mentry);
206 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700207 } else if (MountEntry::IsEmulatedDevice(*mentry)) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700208 emulatedPartitions->emplace(emulatedPartitions->begin(), *mentry);
Keun-young Park8d01f632017-03-13 11:54:47 -0700209 }
210 }
211 return true;
212}
213
Keun-young Park1663e972017-04-21 17:29:26 -0700214static void DumpUmountDebuggingInfo(bool dump_all) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700215 int status;
216 if (!security_getenforce()) {
217 LOG(INFO) << "Run lsof";
218 const char* lsof_argv[] = {"/system/bin/lsof"};
219 android_fork_execvp_ext(arraysize(lsof_argv), (char**)lsof_argv, &status, true, LOG_KLOG,
220 true, nullptr, nullptr, 0);
Keun-young Park8d01f632017-03-13 11:54:47 -0700221 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700222 FindPartitionsToUmount(nullptr, nullptr, true);
Keun-young Park1663e972017-04-21 17:29:26 -0700223 if (dump_all) {
224 // dump current tasks, this log can be lengthy, so only dump with dump_all
225 android::base::WriteStringToFile("t", "/proc/sysrq-trigger");
226 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700227}
228
229static UmountStat UmountPartitions(int timeoutMs) {
230 Timer t;
231 UmountStat stat = UMOUNT_STAT_TIMEOUT;
232 int retry = 0;
233 /* data partition needs all pending writes to be completed and all emulated partitions
234 * umounted.If the current waiting is not good enough, give
235 * up and leave it to e2fsck after reboot to fix it.
236 */
237 while (true) {
238 std::vector<MountEntry> block_devices;
239 std::vector<MountEntry> emulated_devices;
240 if (!FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
241 return UMOUNT_STAT_ERROR;
242 }
243 if (block_devices.size() == 0) {
244 stat = UMOUNT_STAT_SUCCESS;
245 break;
246 }
247 if ((timeoutMs < t.duration_ms()) && retry > 0) { // try umount at least once
248 stat = UMOUNT_STAT_TIMEOUT;
249 break;
250 }
251 if (emulated_devices.size() > 0 &&
252 std::all_of(emulated_devices.begin(), emulated_devices.end(),
253 [](auto& entry) { return entry.Umount(); })) {
254 sync();
255 }
256 for (auto& entry : block_devices) {
257 entry.Umount();
258 }
259 retry++;
260 std::this_thread::sleep_for(100ms);
261 }
262 return stat;
Keun-young Park8d01f632017-03-13 11:54:47 -0700263}
264
Keun-young Park3ee0df92017-03-27 11:21:09 -0700265static void KillAllProcesses() { android::base::WriteStringToFile("i", "/proc/sysrq-trigger"); }
266
Keun-young Park8d01f632017-03-13 11:54:47 -0700267/* Try umounting all emulated file systems R/W block device cfile systems.
268 * This will just try umount and give it up if it fails.
269 * For fs like ext4, this is ok as file system will be marked as unclean shutdown
270 * and necessary check can be done at the next reboot.
271 * For safer shutdown, caller needs to make sure that
272 * all processes / emulated partition for the target fs are all cleaned-up.
273 *
274 * return true when umount was successful. false when timed out.
275 */
Keun-young Park3ee0df92017-03-27 11:21:09 -0700276static UmountStat TryUmountAndFsck(bool runFsck, int timeoutMs) {
277 Timer t;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700278 std::vector<MountEntry> block_devices;
279 std::vector<MountEntry> emulated_devices;
Keun-young Park8d01f632017-03-13 11:54:47 -0700280
281 TurnOffBacklight(); // this part can take time. save power.
282
Keun-young Park2ba5c812017-03-29 12:54:40 -0700283 if (runFsck && !FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700284 return UMOUNT_STAT_ERROR;
285 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700286
287 UmountStat stat = UmountPartitions(timeoutMs - t.duration_ms());
288 if (stat != UMOUNT_STAT_SUCCESS) {
289 LOG(INFO) << "umount timeout, last resort, kill all and try";
Keun-young Park366c51e2017-07-18 18:52:25 -0700290 if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(true);
Keun-young Park3ee0df92017-03-27 11:21:09 -0700291 KillAllProcesses();
Keun-young Park2ba5c812017-03-29 12:54:40 -0700292 // 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 -0700293 UmountStat st = UmountPartitions(0);
294 if ((st != UMOUNT_STAT_SUCCESS) && DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(false);
Keun-young Park8d01f632017-03-13 11:54:47 -0700295 }
296
Keun-young Park2ba5c812017-03-29 12:54:40 -0700297 if (stat == UMOUNT_STAT_SUCCESS && runFsck) {
298 // fsck part is excluded from timeout check. It only runs for user initiated shutdown
299 // and should not affect reboot time.
300 for (auto& entry : block_devices) {
301 entry.DoFsck();
302 }
303 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700304 return stat;
305}
306
Keun-young Park8d01f632017-03-13 11:54:47 -0700307static void __attribute__((noreturn)) DoThermalOff() {
308 LOG(WARNING) << "Thermal system shutdown";
Keun-young Park2ba5c812017-03-29 12:54:40 -0700309 sync();
Keun-young Park8d01f632017-03-13 11:54:47 -0700310 RebootSystem(ANDROID_RB_THERMOFF, "");
311 abort();
312}
313
314void DoReboot(unsigned int cmd, const std::string& reason, const std::string& rebootTarget,
315 bool runFsck) {
316 Timer t;
Keun-young Park3cd8c6f2017-03-23 15:33:16 -0700317 LOG(INFO) << "Reboot start, reason: " << reason << ", rebootTarget: " << rebootTarget;
Keun-young Park8d01f632017-03-13 11:54:47 -0700318
Todd Poynorfc827be2017-04-13 15:17:24 -0700319 android::base::WriteStringToFile(StringPrintf("%s\n", reason.c_str()), LAST_REBOOT_REASON_FILE,
320 S_IRUSR | S_IWUSR, AID_SYSTEM, AID_SYSTEM);
Keun-young Park8d01f632017-03-13 11:54:47 -0700321
322 if (cmd == ANDROID_RB_THERMOFF) { // do not wait if it is thermal
323 DoThermalOff();
324 abort();
325 }
Keun-young Parkaa08ea42017-03-23 13:27:28 -0700326
Keun-young Park7feab682017-04-26 12:36:14 -0700327 constexpr unsigned int shutdownTimeoutDefault = 6;
Keun-young Parkc4ffa5c2017-03-28 09:41:36 -0700328 unsigned int shutdownTimeout = shutdownTimeoutDefault;
329 if (SHUTDOWN_ZERO_TIMEOUT) { // eng build
330 shutdownTimeout = 0;
331 } else {
332 shutdownTimeout =
333 android::base::GetUintProperty("ro.build.shutdown_timeout", shutdownTimeoutDefault);
334 }
Tom Cherryccf23532017-03-28 16:40:41 -0700335 LOG(INFO) << "Shutdown timeout: " << shutdownTimeout;
Keun-young Parkaa08ea42017-03-23 13:27:28 -0700336
Keun-young Park7830d592017-03-27 16:07:02 -0700337 // keep debugging tools until non critical ones are all gone.
338 const std::set<std::string> kill_after_apps{"tombstoned", "logd", "adbd"};
339 // watchdogd is a vendor specific component but should be alive to complete shutdown safely.
Keun-young Parkd266d372017-07-05 11:38:44 -0700340 const std::set<std::string> to_starts{"watchdogd"};
Keun-young Park7830d592017-03-27 16:07:02 -0700341 ServiceManager::GetInstance().ForEachService([&kill_after_apps, &to_starts](Service* s) {
342 if (kill_after_apps.count(s->name())) {
343 s->SetShutdownCritical();
344 } else if (to_starts.count(s->name())) {
345 s->Start();
346 s->SetShutdownCritical();
Keun-young Parkd266d372017-07-05 11:38:44 -0700347 } else if (s->IsShutdownCritical()) {
348 s->Start(); // start shutdown critical service if not started
Keun-young Park8d01f632017-03-13 11:54:47 -0700349 }
Keun-young Park7830d592017-03-27 16:07:02 -0700350 });
351
352 Service* bootAnim = ServiceManager::GetInstance().FindServiceByName("bootanim");
353 Service* surfaceFlinger = ServiceManager::GetInstance().FindServiceByName("surfaceflinger");
354 if (bootAnim != nullptr && surfaceFlinger != nullptr && surfaceFlinger->IsRunning()) {
Keun-young Park7830d592017-03-27 16:07:02 -0700355 ServiceManager::GetInstance().ForEachServiceInClass("animation", [](Service* s) {
Keun-young Park7830d592017-03-27 16:07:02 -0700356 s->SetShutdownCritical(); // will not check animation class separately
357 });
Keun-young Park8d01f632017-03-13 11:54:47 -0700358 }
Keun-young Park7830d592017-03-27 16:07:02 -0700359
Keun-young Park8d01f632017-03-13 11:54:47 -0700360 // optional shutdown step
361 // 1. terminate all services except shutdown critical ones. wait for delay to finish
Keun-young Park3ee0df92017-03-27 11:21:09 -0700362 if (shutdownTimeout > 0) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700363 LOG(INFO) << "terminating init services";
Keun-young Park8d01f632017-03-13 11:54:47 -0700364
365 // Ask all services to terminate except shutdown critical ones.
366 ServiceManager::GetInstance().ForEachService([](Service* s) {
367 if (!s->IsShutdownCritical()) s->Terminate();
368 });
369
370 int service_count = 0;
Keun-young Park3ee0df92017-03-27 11:21:09 -0700371 // Up to half as long as shutdownTimeout or 3 seconds, whichever is lower.
372 unsigned int terminationWaitTimeout = std::min<unsigned int>((shutdownTimeout + 1) / 2, 3);
373 while (t.duration_s() < terminationWaitTimeout) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700374 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
375
376 service_count = 0;
377 ServiceManager::GetInstance().ForEachService([&service_count](Service* s) {
378 // Count the number of services running except shutdown critical.
379 // Exclude the console as it will ignore the SIGTERM signal
380 // and not exit.
381 // Note: SVC_CONSOLE actually means "requires console" but
382 // it is only used by the shell.
383 if (!s->IsShutdownCritical() && s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
384 service_count++;
385 }
386 });
387
388 if (service_count == 0) {
389 // All terminable services terminated. We can exit early.
390 break;
391 }
392
393 // Wait a bit before recounting the number or running services.
394 std::this_thread::sleep_for(50ms);
395 }
396 LOG(INFO) << "Terminating running services took " << t
397 << " with remaining services:" << service_count;
398 }
399
400 // minimum safety steps before restarting
401 // 2. kill all services except ones that are necessary for the shutdown sequence.
Keun-young Park2ba5c812017-03-29 12:54:40 -0700402 ServiceManager::GetInstance().ForEachService([](Service* s) {
403 if (!s->IsShutdownCritical()) s->Stop();
Keun-young Park8d01f632017-03-13 11:54:47 -0700404 });
405 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
406
407 // 3. send volume shutdown to vold
408 Service* voldService = ServiceManager::GetInstance().FindServiceByName("vold");
409 if (voldService != nullptr && voldService->IsRunning()) {
410 ShutdownVold();
Keun-young Park2ba5c812017-03-29 12:54:40 -0700411 voldService->Stop();
Keun-young Park8d01f632017-03-13 11:54:47 -0700412 } else {
413 LOG(INFO) << "vold not running, skipping vold shutdown";
414 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700415 // logcat stopped here
416 ServiceManager::GetInstance().ForEachService([&kill_after_apps](Service* s) {
417 if (kill_after_apps.count(s->name())) s->Stop();
418 });
Keun-young Park8d01f632017-03-13 11:54:47 -0700419 // 4. sync, try umount, and optionally run fsck for user shutdown
Keun-young Park2ba5c812017-03-29 12:54:40 -0700420 sync();
Keun-young Park3ee0df92017-03-27 11:21:09 -0700421 UmountStat stat = TryUmountAndFsck(runFsck, shutdownTimeout * 1000 - t.duration_ms());
Keun-young Park2ba5c812017-03-29 12:54:40 -0700422 // Follow what linux shutdown is doing: one more sync with little bit delay
423 sync();
424 std::this_thread::sleep_for(100ms);
Keun-young Park8d01f632017-03-13 11:54:47 -0700425 LogShutdownTime(stat, &t);
426 // Reboot regardless of umount status. If umount fails, fsck after reboot will fix it.
427 RebootSystem(cmd, rebootTarget);
428 abort();
429}
Tom Cherry98ad32a2017-04-17 16:34:20 -0700430
431bool HandlePowerctlMessage(const std::string& command) {
432 unsigned int cmd = 0;
433 std::vector<std::string> cmd_params = android::base::Split(command, ",");
Tom Cherry98ad32a2017-04-17 16:34:20 -0700434 std::string reboot_target = "";
435 bool run_fsck = false;
436 bool command_invalid = false;
437
438 if (cmd_params.size() > 3) {
439 command_invalid = true;
440 } else if (cmd_params[0] == "shutdown") {
441 cmd = ANDROID_RB_POWEROFF;
442 if (cmd_params.size() == 2 && cmd_params[1] == "userrequested") {
443 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
444 // Run fsck once the file system is remounted in read-only mode.
445 run_fsck = true;
Tom Cherry98ad32a2017-04-17 16:34:20 -0700446 }
447 } else if (cmd_params[0] == "reboot") {
448 cmd = ANDROID_RB_RESTART2;
449 if (cmd_params.size() >= 2) {
450 reboot_target = cmd_params[1];
451 // When rebooting to the bootloader notify the bootloader writing
452 // also the BCB.
453 if (reboot_target == "bootloader") {
454 std::string err;
455 if (!write_reboot_bootloader(&err)) {
456 LOG(ERROR) << "reboot-bootloader: Error writing "
457 "bootloader_message: "
458 << err;
459 }
460 }
461 // If there is an additional bootloader parameter, pass it along
462 if (cmd_params.size() == 3) {
463 reboot_target += "," + cmd_params[2];
464 }
465 }
466 } else if (command == "thermal-shutdown") { // no additional parameter allowed
467 cmd = ANDROID_RB_THERMOFF;
Wei Wang5d4d6f72017-06-27 22:08:45 -0700468 // Do not queue "shutdown" trigger since we want to shutdown immediately
469 DoReboot(cmd, command, reboot_target, run_fsck);
470 return true;
Tom Cherry98ad32a2017-04-17 16:34:20 -0700471 } else {
472 command_invalid = true;
473 }
474 if (command_invalid) {
475 LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
476 return false;
477 }
478
Wei Wang5d4d6f72017-06-27 22:08:45 -0700479 LOG(INFO) << "Clear action queue and start shutdown trigger";
480 ActionManager::GetInstance().ClearQueue();
481 // Queue shutdown trigger first
482 ActionManager::GetInstance().QueueEventTrigger("shutdown");
483 // Queue built-in shutdown_done
484 auto shutdown_handler = [cmd, command, reboot_target,
485 run_fsck](const std::vector<std::string>&) {
486 DoReboot(cmd, command, reboot_target, run_fsck);
487 return 0;
488 };
489 ActionManager::GetInstance().QueueBuiltinAction(shutdown_handler, "shutdown_done");
490
491 // Skip wait for prop if it is in progress
492 ResetWaitForProp();
493
494 // Skip wait for exec if it is in progress
495 if (ServiceManager::GetInstance().IsWaitingForExec()) {
496 ServiceManager::GetInstance().ClearExecWait();
497 }
498
Tom Cherry98ad32a2017-04-17 16:34:20 -0700499 return true;
500}