1 /*
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 */
16
17 #include "reboot.h"
18
19 #include <dirent.h>
20 #include <fcntl.h>
21 #include <linux/f2fs.h>
22 #include <linux/fs.h>
23 #include <linux/loop.h>
24 #include <mntent.h>
25 #include <semaphore.h>
26 #include <stdlib.h>
27 #include <sys/cdefs.h>
28 #include <sys/ioctl.h>
29 #include <sys/mount.h>
30 #include <sys/stat.h>
31 #include <sys/swap.h>
32 #include <sys/syscall.h>
33 #include <sys/types.h>
34 #include <sys/wait.h>
35
36 #include <chrono>
37 #include <memory>
38 #include <set>
39 #include <thread>
40 #include <vector>
41
42 #include <InitProperties.sysprop.h>
43 #include <android-base/chrono_utils.h>
44 #include <android-base/file.h>
45 #include <android-base/logging.h>
46 #include <android-base/macros.h>
47 #include <android-base/properties.h>
48 #include <android-base/scopeguard.h>
49 #include <android-base/strings.h>
50 #include <android-base/unique_fd.h>
51 #include <bootloader_message/bootloader_message.h>
52 #include <cutils/android_reboot.h>
53 #include <fs_mgr.h>
54 #include <logwrap/logwrap.h>
55 #include <private/android_filesystem_config.h>
56 #include <selinux/selinux.h>
57
58 #include "action.h"
59 #include "action_manager.h"
60 #include "builtin_arguments.h"
61 #include "init.h"
62 #include "mount_namespace.h"
63 #include "property_service.h"
64 #include "reboot_utils.h"
65 #include "service.h"
66 #include "service_list.h"
67 #include "sigchld_handler.h"
68 #include "util.h"
69
70 using namespace std::literals;
71
72 using android::base::boot_clock;
73 using android::base::GetBoolProperty;
74 using android::base::GetUintProperty;
75 using android::base::SetProperty;
76 using android::base::Split;
77 using android::base::Timer;
78 using android::base::unique_fd;
79 using android::base::WaitForProperty;
80 using android::base::WriteStringToFile;
81
82 namespace android {
83 namespace init {
84
85 static bool shutting_down = false;
86
87 static const std::set<std::string> kDebuggingServices{"tombstoned", "logd", "adbd", "console"};
88
GetPostDataDebuggingServices()89 static std::set<std::string> GetPostDataDebuggingServices() {
90 std::set<std::string> ret;
91 for (const auto& s : ServiceList::GetInstance()) {
92 if (kDebuggingServices.count(s->name()) && s->is_post_data()) {
93 ret.insert(s->name());
94 }
95 }
96 return ret;
97 }
98
PersistRebootReason(const char * reason,bool write_to_property)99 static void PersistRebootReason(const char* reason, bool write_to_property) {
100 if (write_to_property) {
101 SetProperty(LAST_REBOOT_REASON_PROPERTY, reason);
102 }
103 auto fd = unique_fd(TEMP_FAILURE_RETRY(open(
104 LAST_REBOOT_REASON_FILE, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY, 0666)));
105 if (!fd.ok()) {
106 PLOG(ERROR) << "Could not open '" << LAST_REBOOT_REASON_FILE
107 << "' to persist reboot reason";
108 return;
109 }
110 WriteStringToFd(reason, fd);
111 fsync(fd.get());
112 }
113
114 // represents umount status during reboot / shutdown.
115 enum UmountStat {
116 /* umount succeeded. */
117 UMOUNT_STAT_SUCCESS = 0,
118 /* umount was not run. */
119 UMOUNT_STAT_SKIPPED = 1,
120 /* umount failed with timeout. */
121 UMOUNT_STAT_TIMEOUT = 2,
122 /* could not run due to error */
123 UMOUNT_STAT_ERROR = 3,
124 /* not used by init but reserved for other part to use this to represent the
125 the state where umount status before reboot is not found / available. */
126 UMOUNT_STAT_NOT_AVAILABLE = 4,
127 };
128
129 // Utility for struct mntent
130 class MountEntry {
131 public:
MountEntry(const mntent & entry)132 explicit MountEntry(const mntent& entry)
133 : mnt_fsname_(entry.mnt_fsname),
134 mnt_dir_(entry.mnt_dir),
135 mnt_type_(entry.mnt_type),
136 mnt_opts_(entry.mnt_opts) {}
137
Umount(bool force)138 bool Umount(bool force) {
139 LOG(INFO) << "Unmounting " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_;
140 int r = umount2(mnt_dir_.c_str(), force ? MNT_FORCE : 0);
141 if (r == 0) {
142 LOG(INFO) << "Umounted " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_;
143 return true;
144 } else {
145 PLOG(WARNING) << "Cannot umount " << mnt_fsname_ << ":" << mnt_dir_ << " opts "
146 << mnt_opts_;
147 return false;
148 }
149 }
150
DoFsck()151 void DoFsck() {
152 int st;
153 if (IsF2Fs()) {
154 const char* f2fs_argv[] = {
155 "/system/bin/fsck.f2fs",
156 "-a",
157 mnt_fsname_.c_str(),
158 };
159 logwrap_fork_execvp(arraysize(f2fs_argv), f2fs_argv, &st, false, LOG_KLOG, true,
160 nullptr);
161 } else if (IsExt4()) {
162 const char* ext4_argv[] = {
163 "/system/bin/e2fsck",
164 "-y",
165 mnt_fsname_.c_str(),
166 };
167 logwrap_fork_execvp(arraysize(ext4_argv), ext4_argv, &st, false, LOG_KLOG, true,
168 nullptr);
169 }
170 }
171
IsBlockDevice(const struct mntent & mntent)172 static bool IsBlockDevice(const struct mntent& mntent) {
173 return android::base::StartsWith(mntent.mnt_fsname, "/dev/block");
174 }
175
IsEmulatedDevice(const struct mntent & mntent)176 static bool IsEmulatedDevice(const struct mntent& mntent) {
177 return android::base::StartsWith(mntent.mnt_fsname, "/data/");
178 }
179
180 private:
IsF2Fs() const181 bool IsF2Fs() const { return mnt_type_ == "f2fs"; }
182
IsExt4() const183 bool IsExt4() const { return mnt_type_ == "ext4"; }
184
185 std::string mnt_fsname_;
186 std::string mnt_dir_;
187 std::string mnt_type_;
188 std::string mnt_opts_;
189 };
190
191 // Turn off backlight while we are performing power down cleanup activities.
TurnOffBacklight()192 static void TurnOffBacklight() {
193 Service* service = ServiceList::GetInstance().FindService("blank_screen");
194 if (service == nullptr) {
195 LOG(WARNING) << "cannot find blank_screen in TurnOffBacklight";
196 return;
197 }
198 if (auto result = service->Start(); !result.ok()) {
199 LOG(WARNING) << "Could not start blank_screen service: " << result.error();
200 }
201 }
202
CallVdc(const std::string & system,const std::string & cmd)203 static Result<void> CallVdc(const std::string& system, const std::string& cmd) {
204 LOG(INFO) << "Calling /system/bin/vdc " << system << " " << cmd;
205 const char* vdc_argv[] = {"/system/bin/vdc", system.c_str(), cmd.c_str()};
206 int status;
207 if (logwrap_fork_execvp(arraysize(vdc_argv), vdc_argv, &status, false, LOG_KLOG, true,
208 nullptr) != 0) {
209 return ErrnoError() << "Failed to call '/system/bin/vdc " << system << " " << cmd << "'";
210 }
211 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
212 return {};
213 }
214 return Error() << "'/system/bin/vdc " << system << " " << cmd << "' failed : " << status;
215 }
216
LogShutdownTime(UmountStat stat,Timer * t)217 static void LogShutdownTime(UmountStat stat, Timer* t) {
218 LOG(WARNING) << "powerctl_shutdown_time_ms:" << std::to_string(t->duration().count()) << ":"
219 << stat;
220 }
221
IsDataMounted(const std::string & fstype)222 static bool IsDataMounted(const std::string& fstype) {
223 std::unique_ptr<std::FILE, int (*)(std::FILE*)> fp(setmntent("/proc/mounts", "re"), endmntent);
224 if (fp == nullptr) {
225 PLOG(ERROR) << "Failed to open /proc/mounts";
226 return false;
227 }
228 mntent* mentry;
229 while ((mentry = getmntent(fp.get())) != nullptr) {
230 if (mentry->mnt_dir == "/data"s) {
231 return fstype == "*" || mentry->mnt_type == fstype;
232 }
233 }
234 return false;
235 }
236
237 // Find all read+write block devices and emulated devices in /proc/mounts and add them to
238 // the correpsponding list.
FindPartitionsToUmount(std::vector<MountEntry> * block_dev_partitions,std::vector<MountEntry> * emulated_partitions,bool dump)239 static bool FindPartitionsToUmount(std::vector<MountEntry>* block_dev_partitions,
240 std::vector<MountEntry>* emulated_partitions, bool dump) {
241 std::unique_ptr<std::FILE, int (*)(std::FILE*)> fp(setmntent("/proc/mounts", "re"), endmntent);
242 if (fp == nullptr) {
243 PLOG(ERROR) << "Failed to open /proc/mounts";
244 return false;
245 }
246 mntent* mentry;
247 while ((mentry = getmntent(fp.get())) != nullptr) {
248 if (dump) {
249 LOG(INFO) << "mount entry " << mentry->mnt_fsname << ":" << mentry->mnt_dir << " opts "
250 << mentry->mnt_opts << " type " << mentry->mnt_type;
251 } else if (MountEntry::IsBlockDevice(*mentry) && hasmntopt(mentry, "rw")) {
252 std::string mount_dir(mentry->mnt_dir);
253 // These are R/O partitions changed to R/W after adb remount.
254 // Do not umount them as shutdown critical services may rely on them.
255 if (mount_dir != "/" && mount_dir != "/system" && mount_dir != "/vendor" &&
256 mount_dir != "/oem") {
257 block_dev_partitions->emplace(block_dev_partitions->begin(), *mentry);
258 }
259 } else if (MountEntry::IsEmulatedDevice(*mentry)) {
260 emulated_partitions->emplace(emulated_partitions->begin(), *mentry);
261 }
262 }
263 return true;
264 }
265
DumpUmountDebuggingInfo()266 static void DumpUmountDebuggingInfo() {
267 int status;
268 if (!security_getenforce()) {
269 LOG(INFO) << "Run lsof";
270 const char* lsof_argv[] = {"/system/bin/lsof"};
271 logwrap_fork_execvp(arraysize(lsof_argv), lsof_argv, &status, false, LOG_KLOG, true,
272 nullptr);
273 }
274 FindPartitionsToUmount(nullptr, nullptr, true);
275 // dump current CPU stack traces and uninterruptible tasks
276 WriteStringToFile("l", PROC_SYSRQ);
277 WriteStringToFile("w", PROC_SYSRQ);
278 }
279
UmountPartitions(std::chrono::milliseconds timeout)280 static UmountStat UmountPartitions(std::chrono::milliseconds timeout) {
281 Timer t;
282 /* data partition needs all pending writes to be completed and all emulated partitions
283 * umounted.If the current waiting is not good enough, give
284 * up and leave it to e2fsck after reboot to fix it.
285 */
286 while (true) {
287 std::vector<MountEntry> block_devices;
288 std::vector<MountEntry> emulated_devices;
289 if (!FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
290 return UMOUNT_STAT_ERROR;
291 }
292 if (block_devices.size() == 0) {
293 return UMOUNT_STAT_SUCCESS;
294 }
295 bool unmount_done = true;
296 if (emulated_devices.size() > 0) {
297 for (auto& entry : emulated_devices) {
298 if (!entry.Umount(false)) unmount_done = false;
299 }
300 if (unmount_done) {
301 sync();
302 }
303 }
304 for (auto& entry : block_devices) {
305 if (!entry.Umount(timeout == 0ms)) unmount_done = false;
306 }
307 if (unmount_done) {
308 return UMOUNT_STAT_SUCCESS;
309 }
310 if ((timeout < t.duration())) { // try umount at least once
311 return UMOUNT_STAT_TIMEOUT;
312 }
313 std::this_thread::sleep_for(100ms);
314 }
315 }
316
KillAllProcesses()317 static void KillAllProcesses() {
318 WriteStringToFile("i", PROC_SYSRQ);
319 }
320
321 // Create reboot/shutdwon monitor thread
RebootMonitorThread(unsigned int cmd,const std::string & reboot_target,sem_t * reboot_semaphore,std::chrono::milliseconds shutdown_timeout,bool * reboot_monitor_run)322 void RebootMonitorThread(unsigned int cmd, const std::string& reboot_target,
323 sem_t* reboot_semaphore, std::chrono::milliseconds shutdown_timeout,
324 bool* reboot_monitor_run) {
325 unsigned int remaining_shutdown_time = 0;
326
327 // 300 seconds more than the timeout passed to the thread as there is a final Umount pass
328 // after the timeout is reached.
329 constexpr unsigned int shutdown_watchdog_timeout_default = 300;
330 auto shutdown_watchdog_timeout = android::base::GetUintProperty(
331 "ro.build.shutdown.watchdog.timeout", shutdown_watchdog_timeout_default);
332 remaining_shutdown_time = shutdown_watchdog_timeout + shutdown_timeout.count() / 1000;
333
334 while (*reboot_monitor_run == true) {
335 if (TEMP_FAILURE_RETRY(sem_wait(reboot_semaphore)) == -1) {
336 LOG(ERROR) << "sem_wait failed and exit RebootMonitorThread()";
337 return;
338 }
339
340 timespec shutdown_timeout_timespec;
341 if (clock_gettime(CLOCK_MONOTONIC, &shutdown_timeout_timespec) == -1) {
342 LOG(ERROR) << "clock_gettime() fail! exit RebootMonitorThread()";
343 return;
344 }
345
346 // If there are some remaining shutdown time left from previous round, we use
347 // remaining time here.
348 shutdown_timeout_timespec.tv_sec += remaining_shutdown_time;
349
350 LOG(INFO) << "shutdown_timeout_timespec.tv_sec: " << shutdown_timeout_timespec.tv_sec;
351
352 int sem_return = 0;
353 while ((sem_return = sem_timedwait_monotonic_np(reboot_semaphore,
354 &shutdown_timeout_timespec)) == -1 &&
355 errno == EINTR) {
356 }
357
358 if (sem_return == -1) {
359 LOG(ERROR) << "Reboot thread timed out";
360
361 if (android::base::GetBoolProperty("ro.debuggable", false) == true) {
362 if (false) {
363 // SEPolicy will block debuggerd from running and this is intentional.
364 // But these lines are left to be enabled during debugging.
365 LOG(INFO) << "Try to dump init process call trace:";
366 const char* vdc_argv[] = {"/system/bin/debuggerd", "-b", "1"};
367 int status;
368 logwrap_fork_execvp(arraysize(vdc_argv), vdc_argv, &status, false, LOG_KLOG,
369 true, nullptr);
370 }
371 LOG(INFO) << "Show stack for all active CPU:";
372 WriteStringToFile("l", PROC_SYSRQ);
373
374 LOG(INFO) << "Show tasks that are in disk sleep(uninterruptable sleep), which are "
375 "like "
376 "blocked in mutex or hardware register access:";
377 WriteStringToFile("w", PROC_SYSRQ);
378 }
379
380 // In shutdown case,notify kernel to sync and umount fs to read-only before shutdown.
381 if (cmd == ANDROID_RB_POWEROFF || cmd == ANDROID_RB_THERMOFF) {
382 WriteStringToFile("s", PROC_SYSRQ);
383
384 WriteStringToFile("u", PROC_SYSRQ);
385
386 RebootSystem(cmd, reboot_target);
387 }
388
389 LOG(ERROR) << "Trigger crash at last!";
390 WriteStringToFile("c", PROC_SYSRQ);
391 } else {
392 timespec current_time_timespec;
393
394 if (clock_gettime(CLOCK_MONOTONIC, ¤t_time_timespec) == -1) {
395 LOG(ERROR) << "clock_gettime() fail! exit RebootMonitorThread()";
396 return;
397 }
398
399 remaining_shutdown_time =
400 shutdown_timeout_timespec.tv_sec - current_time_timespec.tv_sec;
401
402 LOG(INFO) << "remaining_shutdown_time: " << remaining_shutdown_time;
403 }
404 }
405 }
406
407 /* Try umounting all emulated file systems R/W block device cfile systems.
408 * This will just try umount and give it up if it fails.
409 * For fs like ext4, this is ok as file system will be marked as unclean shutdown
410 * and necessary check can be done at the next reboot.
411 * For safer shutdown, caller needs to make sure that
412 * all processes / emulated partition for the target fs are all cleaned-up.
413 *
414 * return true when umount was successful. false when timed out.
415 */
TryUmountAndFsck(unsigned int cmd,bool run_fsck,std::chrono::milliseconds timeout,sem_t * reboot_semaphore)416 static UmountStat TryUmountAndFsck(unsigned int cmd, bool run_fsck,
417 std::chrono::milliseconds timeout, sem_t* reboot_semaphore) {
418 Timer t;
419 std::vector<MountEntry> block_devices;
420 std::vector<MountEntry> emulated_devices;
421
422 if (run_fsck && !FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
423 return UMOUNT_STAT_ERROR;
424 }
425
426 UmountStat stat = UmountPartitions(timeout - t.duration());
427 if (stat != UMOUNT_STAT_SUCCESS) {
428 LOG(INFO) << "umount timeout, last resort, kill all and try";
429 if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo();
430 KillAllProcesses();
431 // even if it succeeds, still it is timeout and do not run fsck with all processes killed
432 UmountStat st = UmountPartitions(0ms);
433 if ((st != UMOUNT_STAT_SUCCESS) && DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo();
434 }
435
436 if (stat == UMOUNT_STAT_SUCCESS && run_fsck) {
437 LOG(INFO) << "Pause reboot monitor thread before fsck";
438 sem_post(reboot_semaphore);
439
440 // fsck part is excluded from timeout check. It only runs for user initiated shutdown
441 // and should not affect reboot time.
442 for (auto& entry : block_devices) {
443 entry.DoFsck();
444 }
445
446 LOG(INFO) << "Resume reboot monitor thread after fsck";
447 sem_post(reboot_semaphore);
448 }
449 return stat;
450 }
451
452 // zram is able to use backing device on top of a loopback device.
453 // In order to unmount /data successfully, we have to kill the loopback device first
454 #define ZRAM_DEVICE "/dev/block/zram0"
455 #define ZRAM_RESET "/sys/block/zram0/reset"
456 #define ZRAM_BACK_DEV "/sys/block/zram0/backing_dev"
457 #define ZRAM_INITSTATE "/sys/block/zram0/initstate"
KillZramBackingDevice()458 static Result<void> KillZramBackingDevice() {
459 std::string zram_initstate;
460 if (!android::base::ReadFileToString(ZRAM_INITSTATE, &zram_initstate)) {
461 return ErrnoError() << "Failed to read " << ZRAM_INITSTATE;
462 }
463
464 zram_initstate.erase(zram_initstate.length() - 1);
465 if (zram_initstate == "0") {
466 LOG(INFO) << "Zram has not been swapped on";
467 return {};
468 }
469
470 if (access(ZRAM_BACK_DEV, F_OK) != 0 && errno == ENOENT) {
471 LOG(INFO) << "No zram backing device configured";
472 return {};
473 }
474 std::string backing_dev;
475 if (!android::base::ReadFileToString(ZRAM_BACK_DEV, &backing_dev)) {
476 return ErrnoError() << "Failed to read " << ZRAM_BACK_DEV;
477 }
478
479 // cut the last "\n"
480 backing_dev.erase(backing_dev.length() - 1);
481
482 if (android::base::StartsWith(backing_dev, "none")) {
483 LOG(INFO) << "No zram backing device configured";
484 return {};
485 }
486
487 // shutdown zram handle
488 Timer swap_timer;
489 LOG(INFO) << "swapoff() start...";
490 if (swapoff(ZRAM_DEVICE) == -1) {
491 return ErrnoError() << "zram_backing_dev: swapoff (" << backing_dev << ")"
492 << " failed";
493 }
494 LOG(INFO) << "swapoff() took " << swap_timer;;
495
496 if (!WriteStringToFile("1", ZRAM_RESET)) {
497 return Error() << "zram_backing_dev: reset (" << backing_dev << ")"
498 << " failed";
499 }
500
501 if (!android::base::StartsWith(backing_dev, "/dev/block/loop")) {
502 LOG(INFO) << backing_dev << " is not a loop device. Exiting early";
503 return {};
504 }
505
506 // clear loopback device
507 unique_fd loop(TEMP_FAILURE_RETRY(open(backing_dev.c_str(), O_RDWR | O_CLOEXEC)));
508 if (loop.get() < 0) {
509 return ErrnoError() << "zram_backing_dev: open(" << backing_dev << ")"
510 << " failed";
511 }
512
513 if (ioctl(loop.get(), LOOP_CLR_FD, 0) < 0) {
514 return ErrnoError() << "zram_backing_dev: loop_clear (" << backing_dev << ")"
515 << " failed";
516 }
517 LOG(INFO) << "zram_backing_dev: `" << backing_dev << "` is cleared successfully.";
518 return {};
519 }
520
521 // Stops given services, waits for them to be stopped for |timeout| ms.
522 // If terminate is true, then SIGTERM is sent to services, otherwise SIGKILL is sent.
523 // Note that services are stopped in order given by |ServiceList::services_in_shutdown_order|
524 // function.
StopServices(const std::set<std::string> & services,std::chrono::milliseconds timeout,bool terminate)525 static void StopServices(const std::set<std::string>& services, std::chrono::milliseconds timeout,
526 bool terminate) {
527 LOG(INFO) << "Stopping " << services.size() << " services by sending "
528 << (terminate ? "SIGTERM" : "SIGKILL");
529 std::vector<pid_t> pids;
530 pids.reserve(services.size());
531 for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
532 if (services.count(s->name()) == 0) {
533 continue;
534 }
535 if (s->pid() > 0) {
536 pids.push_back(s->pid());
537 }
538 if (terminate) {
539 s->Terminate();
540 } else {
541 s->Stop();
542 }
543 }
544 if (timeout > 0ms) {
545 WaitToBeReaped(pids, timeout);
546 } else {
547 // Even if we don't to wait for services to stop, we still optimistically reap zombies.
548 ReapAnyOutstandingChildren();
549 }
550 }
551
552 // Like StopServices, but also logs all the services that failed to stop after the provided timeout.
553 // Returns number of violators.
StopServicesAndLogViolations(const std::set<std::string> & services,std::chrono::milliseconds timeout,bool terminate)554 int StopServicesAndLogViolations(const std::set<std::string>& services,
555 std::chrono::milliseconds timeout, bool terminate) {
556 StopServices(services, timeout, terminate);
557 int still_running = 0;
558 for (const auto& s : ServiceList::GetInstance()) {
559 if (s->IsRunning() && services.count(s->name())) {
560 LOG(ERROR) << "[service-misbehaving] : service '" << s->name() << "' is still running "
561 << timeout.count() << "ms after receiving "
562 << (terminate ? "SIGTERM" : "SIGKILL");
563 still_running++;
564 }
565 }
566 return still_running;
567 }
568
UnmountAllApexes()569 static Result<void> UnmountAllApexes() {
570 const char* args[] = {"/system/bin/apexd", "--unmount-all"};
571 int status;
572 if (logwrap_fork_execvp(arraysize(args), args, &status, false, LOG_KLOG, true, nullptr) != 0) {
573 return ErrnoError() << "Failed to call '/system/bin/apexd --unmount-all'";
574 }
575 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
576 return {};
577 }
578 return Error() << "'/system/bin/apexd --unmount-all' failed : " << status;
579 }
580
581 //* Reboot / shutdown the system.
582 // cmd ANDROID_RB_* as defined in android_reboot.h
583 // reason Reason string like "reboot", "shutdown,userrequested"
584 // reboot_target Reboot target string like "bootloader". Otherwise, it should be an empty string.
585 // run_fsck Whether to run fsck after umount is done.
586 //
DoReboot(unsigned int cmd,const std::string & reason,const std::string & reboot_target,bool run_fsck)587 static void DoReboot(unsigned int cmd, const std::string& reason, const std::string& reboot_target,
588 bool run_fsck) {
589 Timer t;
590 LOG(INFO) << "Reboot start, reason: " << reason << ", reboot_target: " << reboot_target;
591
592 bool is_thermal_shutdown = cmd == ANDROID_RB_THERMOFF;
593
594 auto shutdown_timeout = 0ms;
595 if (!SHUTDOWN_ZERO_TIMEOUT) {
596 constexpr unsigned int shutdown_timeout_default = 6;
597 constexpr unsigned int max_thermal_shutdown_timeout = 3;
598 auto shutdown_timeout_final = android::base::GetUintProperty("ro.build.shutdown_timeout",
599 shutdown_timeout_default);
600 if (is_thermal_shutdown && shutdown_timeout_final > max_thermal_shutdown_timeout) {
601 shutdown_timeout_final = max_thermal_shutdown_timeout;
602 }
603 shutdown_timeout = std::chrono::seconds(shutdown_timeout_final);
604 }
605 LOG(INFO) << "Shutdown timeout: " << shutdown_timeout.count() << " ms";
606
607 sem_t reboot_semaphore;
608 if (sem_init(&reboot_semaphore, false, 0) == -1) {
609 // These should never fail, but if they do, skip the graceful reboot and reboot immediately.
610 LOG(ERROR) << "sem_init() fail and RebootSystem() return!";
611 RebootSystem(cmd, reboot_target, reason);
612 }
613
614 // Start a thread to monitor init shutdown process
615 LOG(INFO) << "Create reboot monitor thread.";
616 bool reboot_monitor_run = true;
617 std::thread reboot_monitor_thread(&RebootMonitorThread, cmd, reboot_target, &reboot_semaphore,
618 shutdown_timeout, &reboot_monitor_run);
619 reboot_monitor_thread.detach();
620
621 // Start reboot monitor thread
622 sem_post(&reboot_semaphore);
623
624 // Ensure last reboot reason is reduced to canonical
625 // alias reported in bootloader or system boot reason.
626 size_t skip = 0;
627 std::vector<std::string> reasons = Split(reason, ",");
628 if (reasons.size() >= 2 && reasons[0] == "reboot" &&
629 (reasons[1] == "recovery" || reasons[1] == "bootloader" || reasons[1] == "cold" ||
630 reasons[1] == "hard" || reasons[1] == "warm")) {
631 skip = strlen("reboot,");
632 }
633 PersistRebootReason(reason.c_str() + skip, true);
634
635 // If /data isn't mounted then we can skip the extra reboot steps below, since we don't need to
636 // worry about unmounting it.
637 if (!IsDataMounted("*")) {
638 sync();
639 RebootSystem(cmd, reboot_target, reason);
640 abort();
641 }
642
643 bool do_shutdown_animation = GetBoolProperty("ro.init.shutdown_animation", false);
644 // watchdogd is a vendor specific component but should be alive to complete shutdown safely.
645 const std::set<std::string> to_starts{"watchdogd"};
646 std::set<std::string> stop_first;
647 for (const auto& s : ServiceList::GetInstance()) {
648 if (kDebuggingServices.count(s->name())) {
649 // keep debugging tools until non critical ones are all gone.
650 s->SetShutdownCritical();
651 } else if (to_starts.count(s->name())) {
652 if (auto result = s->Start(); !result.ok()) {
653 LOG(ERROR) << "Could not start shutdown 'to_start' service '" << s->name()
654 << "': " << result.error();
655 }
656 s->SetShutdownCritical();
657 } else if (do_shutdown_animation) {
658 continue;
659 } else if (s->IsShutdownCritical()) {
660 // Start shutdown critical service if not started.
661 if (auto result = s->Start(); !result.ok()) {
662 LOG(ERROR) << "Could not start shutdown critical service '" << s->name()
663 << "': " << result.error();
664 }
665 } else {
666 stop_first.insert(s->name());
667 }
668 }
669
670 // remaining operations (specifically fsck) may take a substantial duration
671 if (!do_shutdown_animation && (cmd == ANDROID_RB_POWEROFF || is_thermal_shutdown)) {
672 TurnOffBacklight();
673 }
674
675 Service* boot_anim = ServiceList::GetInstance().FindService("bootanim");
676 Service* surface_flinger = ServiceList::GetInstance().FindService("surfaceflinger");
677 if (boot_anim != nullptr && surface_flinger != nullptr && surface_flinger->IsRunning()) {
678
679 if (do_shutdown_animation) {
680 SetProperty("service.bootanim.exit", "0");
681 SetProperty("service.bootanim.progress", "0");
682 // Could be in the middle of animation. Stop and start so that it can pick
683 // up the right mode.
684 boot_anim->Stop();
685 }
686
687 for (const auto& service : ServiceList::GetInstance()) {
688 if (service->classnames().count("animation") == 0) {
689 continue;
690 }
691
692 // start all animation classes if stopped.
693 if (do_shutdown_animation) {
694 service->Start();
695 }
696 service->SetShutdownCritical(); // will not check animation class separately
697 }
698
699 if (do_shutdown_animation) {
700 boot_anim->Start();
701 surface_flinger->SetShutdownCritical();
702 boot_anim->SetShutdownCritical();
703 }
704 }
705
706 // optional shutdown step
707 // 1. terminate all services except shutdown critical ones. wait for delay to finish
708 if (shutdown_timeout > 0ms) {
709 StopServicesAndLogViolations(stop_first, shutdown_timeout / 2, true /* SIGTERM */);
710 }
711 // Send SIGKILL to ones that didn't terminate cleanly.
712 StopServicesAndLogViolations(stop_first, 0ms, false /* SIGKILL */);
713 SubcontextTerminate();
714 // Reap subcontext pids.
715 ReapAnyOutstandingChildren();
716
717 // 3. send volume abort_fuse and volume shutdown to vold
718 Service* vold_service = ServiceList::GetInstance().FindService("vold");
719 if (vold_service != nullptr && vold_service->IsRunning()) {
720 // Manually abort FUSE connections, since the FUSE daemon is already dead
721 // at this point, and unmounting it might hang.
722 CallVdc("volume", "abort_fuse");
723 CallVdc("volume", "shutdown");
724 vold_service->Stop();
725 } else {
726 LOG(INFO) << "vold not running, skipping vold shutdown";
727 }
728 // logcat stopped here
729 StopServices(kDebuggingServices, 0ms, false /* SIGKILL */);
730 // 4. sync, try umount, and optionally run fsck for user shutdown
731 {
732 Timer sync_timer;
733 LOG(INFO) << "sync() before umount...";
734 sync();
735 LOG(INFO) << "sync() before umount took" << sync_timer;
736 }
737 // 5. drop caches and disable zram backing device, if exist
738 KillZramBackingDevice();
739
740 LOG(INFO) << "Ready to unmount apexes. So far shutdown sequence took " << t;
741 // 6. unmount active apexes, otherwise they might prevent clean unmount of /data.
742 if (auto ret = UnmountAllApexes(); !ret.ok()) {
743 LOG(ERROR) << ret.error();
744 }
745 UmountStat stat =
746 TryUmountAndFsck(cmd, run_fsck, shutdown_timeout - t.duration(), &reboot_semaphore);
747 // Follow what linux shutdown is doing: one more sync with little bit delay
748 {
749 Timer sync_timer;
750 LOG(INFO) << "sync() after umount...";
751 sync();
752 LOG(INFO) << "sync() after umount took" << sync_timer;
753 }
754 if (!is_thermal_shutdown) std::this_thread::sleep_for(100ms);
755 LogShutdownTime(stat, &t);
756
757 // Send signal to terminate reboot monitor thread.
758 reboot_monitor_run = false;
759 sem_post(&reboot_semaphore);
760
761 // Reboot regardless of umount status. If umount fails, fsck after reboot will fix it.
762 if (IsDataMounted("f2fs")) {
763 uint32_t flag = F2FS_GOING_DOWN_FULLSYNC;
764 unique_fd fd(TEMP_FAILURE_RETRY(open("/data", O_RDONLY)));
765 int ret = ioctl(fd, F2FS_IOC_SHUTDOWN, &flag);
766 if (ret) {
767 PLOG(ERROR) << "Shutdown /data: ";
768 } else {
769 LOG(INFO) << "Shutdown /data";
770 }
771 }
772 RebootSystem(cmd, reboot_target, reason);
773 abort();
774 }
775
EnterShutdown()776 static void EnterShutdown() {
777 LOG(INFO) << "Entering shutdown mode";
778 shutting_down = true;
779 // Skip wait for prop if it is in progress
780 ResetWaitForProp();
781 // Clear EXEC flag if there is one pending
782 for (const auto& s : ServiceList::GetInstance()) {
783 s->UnSetExec();
784 }
785 }
786
LeaveShutdown()787 static void LeaveShutdown() {
788 LOG(INFO) << "Leaving shutdown mode";
789 shutting_down = false;
790 StartSendingMessages();
791 }
792
GetMillisProperty(const std::string & name,std::chrono::milliseconds default_value)793 static std::chrono::milliseconds GetMillisProperty(const std::string& name,
794 std::chrono::milliseconds default_value) {
795 auto value = GetUintProperty(name, static_cast<uint64_t>(default_value.count()));
796 return std::chrono::milliseconds(std::move(value));
797 }
798
DoUserspaceReboot()799 static Result<void> DoUserspaceReboot() {
800 LOG(INFO) << "Userspace reboot initiated";
801 // An ugly way to pass a more precise reason on why fallback to hard reboot was triggered.
802 std::string sub_reason = "";
803 auto guard = android::base::make_scope_guard([&sub_reason] {
804 // Leave shutdown so that we can handle a full reboot.
805 LeaveShutdown();
806 trigger_shutdown("reboot,userspace_failed,shutdown_aborted," + sub_reason);
807 });
808 // Triggering userspace-reboot-requested will result in a bunch of setprop
809 // actions. We should make sure, that all of them are propagated before
810 // proceeding with userspace reboot. Synchronously setting sys.init.userspace_reboot.in_progress
811 // property is not perfect, but it should do the trick.
812 if (!android::sysprop::InitProperties::userspace_reboot_in_progress(true)) {
813 sub_reason = "setprop";
814 return Error() << "Failed to set sys.init.userspace_reboot.in_progress property";
815 }
816 EnterShutdown();
817 if (!SetProperty("sys.powerctl", "")) {
818 sub_reason = "resetprop";
819 return Error() << "Failed to reset sys.powerctl property";
820 }
821 std::set<std::string> stop_first;
822 // Remember the services that were enabled. We will need to manually enable them again otherwise
823 // triggers like class_start won't restart them.
824 std::set<std::string> were_enabled;
825 for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
826 if (s->is_post_data() && !kDebuggingServices.count(s->name())) {
827 stop_first.insert(s->name());
828 }
829 // TODO(ioffe): we should also filter out temporary services here.
830 if (s->is_post_data() && s->IsEnabled()) {
831 were_enabled.insert(s->name());
832 }
833 }
834 {
835 Timer sync_timer;
836 LOG(INFO) << "sync() before terminating services...";
837 sync();
838 LOG(INFO) << "sync() took " << sync_timer;
839 }
840 auto sigterm_timeout = GetMillisProperty("init.userspace_reboot.sigterm.timeoutmillis", 5s);
841 auto sigkill_timeout = GetMillisProperty("init.userspace_reboot.sigkill.timeoutmillis", 10s);
842 LOG(INFO) << "Timeout to terminate services: " << sigterm_timeout.count() << "ms "
843 << "Timeout to kill services: " << sigkill_timeout.count() << "ms";
844 std::string services_file_name = "/metadata/userspacereboot/services.txt";
845 const int flags = O_RDWR | O_CREAT | O_SYNC | O_APPEND | O_CLOEXEC;
846 StopServicesAndLogViolations(stop_first, sigterm_timeout, true /* SIGTERM */);
847 if (int r = StopServicesAndLogViolations(stop_first, sigkill_timeout, false /* SIGKILL */);
848 r > 0) {
849 auto fd = unique_fd(TEMP_FAILURE_RETRY(open(services_file_name.c_str(), flags, 0666)));
850 android::base::WriteStringToFd("Post-data services still running: \n", fd);
851 for (const auto& s : ServiceList::GetInstance()) {
852 if (s->IsRunning() && stop_first.count(s->name())) {
853 android::base::WriteStringToFd(s->name() + "\n", fd);
854 }
855 }
856 sub_reason = "sigkill";
857 return Error() << r << " post-data services are still running";
858 }
859 if (auto result = KillZramBackingDevice(); !result.ok()) {
860 sub_reason = "zram";
861 return result;
862 }
863 if (auto result = CallVdc("volume", "reset"); !result.ok()) {
864 sub_reason = "vold_reset";
865 return result;
866 }
867 const auto& debugging_services = GetPostDataDebuggingServices();
868 if (int r = StopServicesAndLogViolations(debugging_services, sigkill_timeout,
869 false /* SIGKILL */);
870 r > 0) {
871 auto fd = unique_fd(TEMP_FAILURE_RETRY(open(services_file_name.c_str(), flags, 0666)));
872 android::base::WriteStringToFd("Debugging services still running: \n", fd);
873 for (const auto& s : ServiceList::GetInstance()) {
874 if (s->IsRunning() && debugging_services.count(s->name())) {
875 android::base::WriteStringToFd(s->name() + "\n", fd);
876 }
877 }
878 sub_reason = "sigkill_debug";
879 return Error() << r << " debugging services are still running";
880 }
881 {
882 Timer sync_timer;
883 LOG(INFO) << "sync() after stopping services...";
884 sync();
885 LOG(INFO) << "sync() took " << sync_timer;
886 }
887 if (auto result = UnmountAllApexes(); !result.ok()) {
888 sub_reason = "apex";
889 return result;
890 }
891 if (!SwitchToMountNamespaceIfNeeded(NS_BOOTSTRAP).ok()) {
892 sub_reason = "ns_switch";
893 return Error() << "Failed to switch to bootstrap namespace";
894 }
895 // Remove services that were defined in an APEX.
896 ServiceList::GetInstance().RemoveServiceIf([](const std::unique_ptr<Service>& s) -> bool {
897 if (s->is_from_apex()) {
898 LOG(INFO) << "Removing service '" << s->name() << "' because it's defined in an APEX";
899 return true;
900 }
901 return false;
902 });
903 // Re-enable services
904 for (const auto& s : ServiceList::GetInstance()) {
905 if (were_enabled.count(s->name())) {
906 LOG(INFO) << "Re-enabling service '" << s->name() << "'";
907 s->Enable();
908 }
909 }
910 ServiceList::GetInstance().ResetState();
911 LeaveShutdown();
912 ActionManager::GetInstance().QueueEventTrigger("userspace-reboot-resume");
913 guard.Disable(); // Go on with userspace reboot.
914 return {};
915 }
916
UserspaceRebootWatchdogThread()917 static void UserspaceRebootWatchdogThread() {
918 auto started_timeout = GetMillisProperty("init.userspace_reboot.started.timeoutmillis", 10s);
919 if (!WaitForProperty("sys.init.userspace_reboot.in_progress", "1", started_timeout)) {
920 LOG(ERROR) << "Userspace reboot didn't start in " << started_timeout.count()
921 << "ms. Switching to full reboot";
922 // Init might be wedged, don't try to write reboot reason into a persistent property and do
923 // a dirty reboot.
924 PersistRebootReason("userspace_failed,watchdog_triggered,failed_to_start", false);
925 RebootSystem(ANDROID_RB_RESTART2, "userspace_failed,watchdog_triggered,failed_to_start");
926 }
927 LOG(INFO) << "Starting userspace reboot watchdog";
928 auto watchdog_timeout = GetMillisProperty("init.userspace_reboot.watchdog.timeoutmillis", 5min);
929 LOG(INFO) << "UserspaceRebootWatchdog timeout: " << watchdog_timeout.count() << "ms";
930 if (!WaitForProperty("sys.boot_completed", "1", watchdog_timeout)) {
931 LOG(ERROR) << "Failed to boot in " << watchdog_timeout.count()
932 << "ms. Switching to full reboot";
933 // In this case device is in a boot loop. Only way to recover is to do dirty reboot.
934 // Since init might be wedged, don't try to write reboot reason into a persistent property.
935 PersistRebootReason("userspace_failed,watchdog_triggered,failed_to_boot", false);
936 RebootSystem(ANDROID_RB_RESTART2, "userspace_failed,watchdog_triggered,failed_to_boot");
937 }
938 LOG(INFO) << "Device booted, stopping userspace reboot watchdog";
939 }
940
HandleUserspaceReboot()941 static void HandleUserspaceReboot() {
942 if (!android::sysprop::InitProperties::is_userspace_reboot_supported().value_or(false)) {
943 LOG(ERROR) << "Attempted a userspace reboot on a device that doesn't support it";
944 return;
945 }
946 // Spinnig up a separate thread will fail the setns call later in the boot sequence.
947 // Fork a new process to monitor userspace reboot while we are investigating a better solution.
948 pid_t pid = fork();
949 if (pid < 0) {
950 PLOG(ERROR) << "Failed to fork process for userspace reboot watchdog. Switching to full "
951 << "reboot";
952 trigger_shutdown("reboot,userspace_failed,watchdog_fork");
953 return;
954 }
955 if (pid == 0) {
956 // Child
957 UserspaceRebootWatchdogThread();
958 _exit(EXIT_SUCCESS);
959 }
960 LOG(INFO) << "Clearing queue and starting userspace-reboot-requested trigger";
961 auto& am = ActionManager::GetInstance();
962 am.ClearQueue();
963 am.QueueEventTrigger("userspace-reboot-requested");
964 auto handler = [](const BuiltinArguments&) { return DoUserspaceReboot(); };
965 am.QueueBuiltinAction(handler, "userspace-reboot");
966 }
967
968 /**
969 * Check if "command" field is set in bootloader message.
970 *
971 * If "command" field is broken (contains non-printable characters prior to
972 * terminating zero), it will be zeroed.
973 *
974 * @param[in,out] boot Bootloader message (BCB) structure
975 * @return true if "command" field is already set, and false if it's empty
976 */
CommandIsPresent(bootloader_message * boot)977 static bool CommandIsPresent(bootloader_message* boot) {
978 if (boot->command[0] == '\0')
979 return false;
980
981 for (size_t i = 0; i < arraysize(boot->command); ++i) {
982 if (boot->command[i] == '\0')
983 return true;
984 if (!isprint(boot->command[i]))
985 break;
986 }
987
988 memset(boot->command, 0, sizeof(boot->command));
989 return false;
990 }
991
HandlePowerctlMessage(const std::string & command)992 void HandlePowerctlMessage(const std::string& command) {
993 unsigned int cmd = 0;
994 std::vector<std::string> cmd_params = Split(command, ",");
995 std::string reboot_target = "";
996 bool run_fsck = false;
997 bool command_invalid = false;
998 bool userspace_reboot = false;
999
1000 if (cmd_params[0] == "shutdown") {
1001 cmd = ANDROID_RB_POWEROFF;
1002 if (cmd_params.size() >= 2) {
1003 if (cmd_params[1] == "userrequested") {
1004 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
1005 // Run fsck once the file system is remounted in read-only mode.
1006 run_fsck = true;
1007 } else if (cmd_params[1] == "thermal") {
1008 // Turn off sources of heat immediately.
1009 TurnOffBacklight();
1010 // run_fsck is false to avoid delay
1011 cmd = ANDROID_RB_THERMOFF;
1012 }
1013 }
1014 } else if (cmd_params[0] == "reboot") {
1015 cmd = ANDROID_RB_RESTART2;
1016 if (cmd_params.size() >= 2) {
1017 reboot_target = cmd_params[1];
1018 if (reboot_target == "userspace") {
1019 LOG(INFO) << "Userspace reboot requested";
1020 userspace_reboot = true;
1021 }
1022 // adb reboot fastboot should boot into bootloader for devices not
1023 // supporting logical partitions.
1024 if (reboot_target == "fastboot" &&
1025 !android::base::GetBoolProperty("ro.boot.dynamic_partitions", false)) {
1026 reboot_target = "bootloader";
1027 }
1028 // When rebooting to the bootloader notify the bootloader writing
1029 // also the BCB.
1030 if (reboot_target == "bootloader") {
1031 std::string err;
1032 if (!write_reboot_bootloader(&err)) {
1033 LOG(ERROR) << "reboot-bootloader: Error writing "
1034 "bootloader_message: "
1035 << err;
1036 }
1037 } else if (reboot_target == "recovery") {
1038 bootloader_message boot = {};
1039 if (std::string err; !read_bootloader_message(&boot, &err)) {
1040 LOG(ERROR) << "Failed to read bootloader message: " << err;
1041 }
1042 // Update the boot command field if it's empty, and preserve
1043 // the other arguments in the bootloader message.
1044 if (!CommandIsPresent(&boot)) {
1045 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
1046 if (std::string err; !write_bootloader_message(boot, &err)) {
1047 LOG(ERROR) << "Failed to set bootloader message: " << err;
1048 return;
1049 }
1050 }
1051 } else if (reboot_target == "quiescent") {
1052 bootloader_message boot = {};
1053 if (std::string err; !read_bootloader_message(&boot, &err)) {
1054 LOG(ERROR) << "Failed to read bootloader message: " << err;
1055 }
1056 // Update the boot command field if it's empty, and preserve
1057 // the other arguments in the bootloader message.
1058 if (!CommandIsPresent(&boot)) {
1059 strlcpy(boot.command, "boot-quiescent", sizeof(boot.command));
1060 if (std::string err; !write_bootloader_message(boot, &err)) {
1061 LOG(ERROR) << "Failed to set bootloader message: " << err;
1062 return;
1063 }
1064 }
1065 } else if (reboot_target == "sideload" || reboot_target == "sideload-auto-reboot" ||
1066 reboot_target == "fastboot") {
1067 std::string arg = reboot_target == "sideload-auto-reboot" ? "sideload_auto_reboot"
1068 : reboot_target;
1069 const std::vector<std::string> options = {
1070 "--" + arg,
1071 };
1072 std::string err;
1073 if (!write_bootloader_message(options, &err)) {
1074 LOG(ERROR) << "Failed to set bootloader message: " << err;
1075 return;
1076 }
1077 reboot_target = "recovery";
1078 }
1079
1080 // If there are additional parameter, pass them along
1081 for (size_t i = 2; (cmd_params.size() > i) && cmd_params[i].size(); ++i) {
1082 reboot_target += "," + cmd_params[i];
1083 }
1084 }
1085 } else {
1086 command_invalid = true;
1087 }
1088 if (command_invalid) {
1089 LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
1090 return;
1091 }
1092
1093 // We do not want to process any messages (queue'ing triggers, shutdown messages, control
1094 // messages, etc) from properties during reboot.
1095 StopSendingMessages();
1096
1097 if (userspace_reboot) {
1098 HandleUserspaceReboot();
1099 return;
1100 }
1101
1102 LOG(INFO) << "Clear action queue and start shutdown trigger";
1103 ActionManager::GetInstance().ClearQueue();
1104 // Queue shutdown trigger first
1105 ActionManager::GetInstance().QueueEventTrigger("shutdown");
1106 // Queue built-in shutdown_done
1107 auto shutdown_handler = [cmd, command, reboot_target, run_fsck](const BuiltinArguments&) {
1108 DoReboot(cmd, command, reboot_target, run_fsck);
1109 return Result<void>{};
1110 };
1111 ActionManager::GetInstance().QueueBuiltinAction(shutdown_handler, "shutdown_done");
1112
1113 EnterShutdown();
1114 }
1115
IsShuttingDown()1116 bool IsShuttingDown() {
1117 return shutting_down;
1118 }
1119
1120 } // namespace init
1121 } // namespace android
1122