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