xref: /aosp_15_r20/system/core/init/reboot.cpp (revision 00c7fec1bb09f3284aad6a6f96d2f63dfc3650ad)
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 <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 <libsnapshot/snapshot.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 
PersistRebootReason(const char * reason,bool write_to_property)89 static void PersistRebootReason(const char* reason, bool write_to_property) {
90     if (write_to_property) {
91         SetProperty(LAST_REBOOT_REASON_PROPERTY, reason);
92     }
93     auto fd = unique_fd(TEMP_FAILURE_RETRY(open(
94             LAST_REBOOT_REASON_FILE, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY, 0666)));
95     if (!fd.ok()) {
96         PLOG(ERROR) << "Could not open '" << LAST_REBOOT_REASON_FILE
97                     << "' to persist reboot reason";
98         return;
99     }
100     WriteStringToFd(reason, fd);
101     fsync(fd.get());
102 }
103 
104 // represents umount status during reboot / shutdown.
105 enum UmountStat {
106     /* umount succeeded. */
107     UMOUNT_STAT_SUCCESS = 0,
108     /* umount was not run. */
109     UMOUNT_STAT_SKIPPED = 1,
110     /* umount failed with timeout. */
111     UMOUNT_STAT_TIMEOUT = 2,
112     /* could not run due to error */
113     UMOUNT_STAT_ERROR = 3,
114     /* not used by init but reserved for other part to use this to represent the
115        the state where umount status before reboot is not found / available. */
116     UMOUNT_STAT_NOT_AVAILABLE = 4,
117 };
118 
119 // Utility for struct mntent
120 class MountEntry {
121   public:
MountEntry(const mntent & entry)122     explicit MountEntry(const mntent& entry)
123         : mnt_fsname_(entry.mnt_fsname),
124           mnt_dir_(entry.mnt_dir),
125           mnt_type_(entry.mnt_type),
126           mnt_opts_(entry.mnt_opts) {}
127 
Umount(bool force)128     bool Umount(bool force) {
129         LOG(INFO) << "Unmounting " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_;
130         int r = umount2(mnt_dir_.c_str(), force ? MNT_FORCE : 0);
131         if (r == 0) {
132             LOG(INFO) << "Umounted " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_;
133             return true;
134         } else {
135             PLOG(WARNING) << "Cannot umount " << mnt_fsname_ << ":" << mnt_dir_ << " opts "
136                           << mnt_opts_;
137             return false;
138         }
139     }
140 
DoFsck()141     void DoFsck() {
142         int st;
143         if (IsF2Fs()) {
144             const char* f2fs_argv[] = {
145                     "/system/bin/fsck.f2fs",
146                     "-a",
147                     mnt_fsname_.c_str(),
148             };
149             logwrap_fork_execvp(arraysize(f2fs_argv), f2fs_argv, &st, false, LOG_KLOG, true,
150                                 nullptr);
151         } else if (IsExt4()) {
152             const char* ext4_argv[] = {
153                     "/system/bin/e2fsck",
154                     "-y",
155                     mnt_fsname_.c_str(),
156             };
157             logwrap_fork_execvp(arraysize(ext4_argv), ext4_argv, &st, false, LOG_KLOG, true,
158                                 nullptr);
159         }
160     }
161 
IsBlockDevice(const struct mntent & mntent)162     static bool IsBlockDevice(const struct mntent& mntent) {
163         return android::base::StartsWith(mntent.mnt_fsname, "/dev/block");
164     }
165 
IsEmulatedDevice(const struct mntent & mntent)166     static bool IsEmulatedDevice(const struct mntent& mntent) {
167         return android::base::StartsWith(mntent.mnt_fsname, "/data/");
168     }
169 
170   private:
IsF2Fs() const171     bool IsF2Fs() const { return mnt_type_ == "f2fs"; }
172 
IsExt4() const173     bool IsExt4() const { return mnt_type_ == "ext4"; }
174 
175     std::string mnt_fsname_;
176     std::string mnt_dir_;
177     std::string mnt_type_;
178     std::string mnt_opts_;
179 };
180 
181 // Turn off backlight while we are performing power down cleanup activities.
TurnOffBacklight()182 static void TurnOffBacklight() {
183     Service* service = ServiceList::GetInstance().FindService("blank_screen");
184     if (service == nullptr) {
185         LOG(WARNING) << "cannot find blank_screen in TurnOffBacklight";
186         return;
187     }
188     if (auto result = service->Start(); !result.ok()) {
189         LOG(WARNING) << "Could not start blank_screen service: " << result.error();
190     }
191 }
192 
CallVdc(const std::string & system,const std::string & cmd)193 static Result<void> CallVdc(const std::string& system, const std::string& cmd) {
194     LOG(INFO) << "Calling /system/bin/vdc " << system << " " << cmd;
195     const char* vdc_argv[] = {"/system/bin/vdc", system.c_str(), cmd.c_str()};
196     int status;
197     if (logwrap_fork_execvp(arraysize(vdc_argv), vdc_argv, &status, false, LOG_KLOG, true,
198                             nullptr) != 0) {
199         return ErrnoError() << "Failed to call '/system/bin/vdc " << system << " " << cmd << "'";
200     }
201     if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
202         return {};
203     }
204     return Error() << "'/system/bin/vdc " << system << " " << cmd << "' failed : " << status;
205 }
206 
LogShutdownTime(UmountStat stat,Timer * t)207 static void LogShutdownTime(UmountStat stat, Timer* t) {
208     LOG(WARNING) << "powerctl_shutdown_time_ms:" << std::to_string(t->duration().count()) << ":"
209                  << stat;
210 }
211 
IsDataMounted(const std::string & fstype)212 static bool IsDataMounted(const std::string& fstype) {
213     std::unique_ptr<std::FILE, int (*)(std::FILE*)> fp(setmntent("/proc/mounts", "re"), endmntent);
214     if (fp == nullptr) {
215         PLOG(ERROR) << "Failed to open /proc/mounts";
216         return false;
217     }
218     mntent* mentry;
219     while ((mentry = getmntent(fp.get())) != nullptr) {
220         if (mentry->mnt_dir == "/data"s) {
221             return fstype == "*" || mentry->mnt_type == fstype;
222         }
223     }
224     return false;
225 }
226 
227 // Find all read+write block devices and emulated devices in /proc/mounts and add them to
228 // the correpsponding list.
FindPartitionsToUmount(std::vector<MountEntry> * block_dev_partitions,std::vector<MountEntry> * emulated_partitions,bool dump)229 static bool FindPartitionsToUmount(std::vector<MountEntry>* block_dev_partitions,
230                                    std::vector<MountEntry>* emulated_partitions, bool dump) {
231     std::unique_ptr<std::FILE, int (*)(std::FILE*)> fp(setmntent("/proc/mounts", "re"), endmntent);
232     if (fp == nullptr) {
233         PLOG(ERROR) << "Failed to open /proc/mounts";
234         return false;
235     }
236     mntent* mentry;
237     while ((mentry = getmntent(fp.get())) != nullptr) {
238         if (dump) {
239             LOG(INFO) << "mount entry " << mentry->mnt_fsname << ":" << mentry->mnt_dir << " opts "
240                       << mentry->mnt_opts << " type " << mentry->mnt_type;
241         } else if (MountEntry::IsBlockDevice(*mentry) && hasmntopt(mentry, "rw")) {
242             std::string mount_dir(mentry->mnt_dir);
243             // These are R/O partitions changed to R/W after adb remount.
244             // Do not umount them as shutdown critical services may rely on them.
245             if (mount_dir != "/" && mount_dir != "/system" && mount_dir != "/vendor" &&
246                 mount_dir != "/oem") {
247                 block_dev_partitions->emplace(block_dev_partitions->begin(), *mentry);
248             }
249         } else if (MountEntry::IsEmulatedDevice(*mentry)) {
250             emulated_partitions->emplace(emulated_partitions->begin(), *mentry);
251         }
252     }
253     return true;
254 }
255 
DumpUmountDebuggingInfo()256 static void DumpUmountDebuggingInfo() {
257     int status;
258     if (!security_getenforce()) {
259         LOG(INFO) << "Run lsof";
260         const char* lsof_argv[] = {"/system/bin/lsof"};
261         logwrap_fork_execvp(arraysize(lsof_argv), lsof_argv, &status, false, LOG_KLOG, true,
262                             nullptr);
263     }
264     FindPartitionsToUmount(nullptr, nullptr, true);
265     // dump current CPU stack traces and uninterruptible tasks
266     WriteStringToFile("l", PROC_SYSRQ);
267     WriteStringToFile("w", PROC_SYSRQ);
268 }
269 
UmountPartitions(std::chrono::milliseconds timeout)270 static UmountStat UmountPartitions(std::chrono::milliseconds timeout) {
271     Timer t;
272     /* data partition needs all pending writes to be completed and all emulated partitions
273      * umounted.If the current waiting is not good enough, give
274      * up and leave it to e2fsck after reboot to fix it.
275      */
276     while (true) {
277         std::vector<MountEntry> block_devices;
278         std::vector<MountEntry> emulated_devices;
279         if (!FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
280             return UMOUNT_STAT_ERROR;
281         }
282         if (block_devices.size() == 0) {
283             return UMOUNT_STAT_SUCCESS;
284         }
285         bool unmount_done = true;
286         if (emulated_devices.size() > 0) {
287             for (auto& entry : emulated_devices) {
288                 if (!entry.Umount(false)) unmount_done = false;
289             }
290             if (unmount_done) {
291                 sync();
292             }
293         }
294         for (auto& entry : block_devices) {
295             if (!entry.Umount(timeout == 0ms)) unmount_done = false;
296         }
297         if (unmount_done) {
298             return UMOUNT_STAT_SUCCESS;
299         }
300         if ((timeout < t.duration())) {  // try umount at least once
301             return UMOUNT_STAT_TIMEOUT;
302         }
303         std::this_thread::sleep_for(100ms);
304     }
305 }
306 
KillAllProcesses()307 static void KillAllProcesses() {
308     WriteStringToFile("i", PROC_SYSRQ);
309 }
310 
311 // 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)312 void RebootMonitorThread(unsigned int cmd, const std::string& reboot_target,
313                          sem_t* reboot_semaphore, std::chrono::milliseconds shutdown_timeout,
314                          bool* reboot_monitor_run) {
315     unsigned int remaining_shutdown_time = 0;
316 
317     // 300 seconds more than the timeout passed to the thread as there is a final Umount pass
318     // after the timeout is reached.
319     constexpr unsigned int shutdown_watchdog_timeout_default = 300;
320     auto shutdown_watchdog_timeout = android::base::GetUintProperty(
321             "ro.build.shutdown.watchdog.timeout", shutdown_watchdog_timeout_default);
322     remaining_shutdown_time = shutdown_watchdog_timeout + shutdown_timeout.count() / 1000;
323 
324     while (*reboot_monitor_run == true) {
325         if (TEMP_FAILURE_RETRY(sem_wait(reboot_semaphore)) == -1) {
326             LOG(ERROR) << "sem_wait failed and exit RebootMonitorThread()";
327             return;
328         }
329 
330         timespec shutdown_timeout_timespec;
331         if (clock_gettime(CLOCK_MONOTONIC, &shutdown_timeout_timespec) == -1) {
332             LOG(ERROR) << "clock_gettime() fail! exit RebootMonitorThread()";
333             return;
334         }
335 
336         // If there are some remaining shutdown time left from previous round, we use
337         // remaining time here.
338         shutdown_timeout_timespec.tv_sec += remaining_shutdown_time;
339 
340         LOG(INFO) << "shutdown_timeout_timespec.tv_sec: " << shutdown_timeout_timespec.tv_sec;
341 
342         int sem_return = 0;
343         while ((sem_return = sem_timedwait_monotonic_np(reboot_semaphore,
344                                                         &shutdown_timeout_timespec)) == -1 &&
345                errno == EINTR) {
346         }
347 
348         if (sem_return == -1) {
349             LOG(ERROR) << "Reboot thread timed out";
350 
351             if (android::base::GetBoolProperty("ro.debuggable", false) == true) {
352                 if (false) {
353                     // SEPolicy will block debuggerd from running and this is intentional.
354                     // But these lines are left to be enabled during debugging.
355                     LOG(INFO) << "Try to dump init process call trace:";
356                     const char* vdc_argv[] = {"/system/bin/debuggerd", "-b", "1"};
357                     int status;
358                     logwrap_fork_execvp(arraysize(vdc_argv), vdc_argv, &status, false, LOG_KLOG,
359                                         true, nullptr);
360                 }
361                 LOG(INFO) << "Show stack for all active CPU:";
362                 WriteStringToFile("l", PROC_SYSRQ);
363 
364                 LOG(INFO) << "Show tasks that are in disk sleep(uninterruptable sleep), which are "
365                              "like "
366                              "blocked in mutex or hardware register access:";
367                 WriteStringToFile("w", PROC_SYSRQ);
368             }
369 
370             // In shutdown case,notify kernel to sync and umount fs to read-only before shutdown.
371             if (cmd == ANDROID_RB_POWEROFF || cmd == ANDROID_RB_THERMOFF) {
372                 WriteStringToFile("s", PROC_SYSRQ);
373 
374                 WriteStringToFile("u", PROC_SYSRQ);
375 
376                 RebootSystem(cmd, reboot_target);
377             }
378 
379             LOG(ERROR) << "Trigger crash at last!";
380             WriteStringToFile("c", PROC_SYSRQ);
381         } else {
382             timespec current_time_timespec;
383 
384             if (clock_gettime(CLOCK_MONOTONIC, &current_time_timespec) == -1) {
385                 LOG(ERROR) << "clock_gettime() fail! exit RebootMonitorThread()";
386                 return;
387             }
388 
389             remaining_shutdown_time =
390                     shutdown_timeout_timespec.tv_sec - current_time_timespec.tv_sec;
391 
392             LOG(INFO) << "remaining_shutdown_time: " << remaining_shutdown_time;
393         }
394     }
395 }
396 
397 /* Try umounting all emulated file systems R/W block device cfile systems.
398  * This will just try umount and give it up if it fails.
399  * For fs like ext4, this is ok as file system will be marked as unclean shutdown
400  * and necessary check can be done at the next reboot.
401  * For safer shutdown, caller needs to make sure that
402  * all processes / emulated partition for the target fs are all cleaned-up.
403  *
404  * return true when umount was successful. false when timed out.
405  */
TryUmountAndFsck(unsigned int cmd,bool run_fsck,std::chrono::milliseconds timeout,sem_t * reboot_semaphore)406 static UmountStat TryUmountAndFsck(unsigned int cmd, bool run_fsck,
407                                    std::chrono::milliseconds timeout, sem_t* reboot_semaphore) {
408     Timer t;
409     std::vector<MountEntry> block_devices;
410     std::vector<MountEntry> emulated_devices;
411 
412     if (run_fsck && !FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
413         return UMOUNT_STAT_ERROR;
414     }
415     auto sm = snapshot::SnapshotManager::New();
416     bool ota_update_in_progress = false;
417     if (sm->IsUserspaceSnapshotUpdateInProgress()) {
418         LOG(INFO) << "OTA update in progress";
419         ota_update_in_progress = true;
420     }
421     UmountStat stat = UmountPartitions(timeout - t.duration());
422     if (stat != UMOUNT_STAT_SUCCESS) {
423         LOG(INFO) << "umount timeout, last resort, kill all and try";
424         if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo();
425         // Since umount timedout, we will try to kill all processes
426         // and do one more attempt to umount the partitions.
427         //
428         // However, if OTA update is in progress, we don't want
429         // to kill the snapuserd daemon as the daemon will
430         // be serving I/O requests. Killing the daemon will
431         // end up with I/O failures. If the update is in progress,
432         // we will just return the umount failure status immediately.
433         // This is ok, given the fact that killing the processes
434         // and doing an umount is just a last effort. We are
435         // still not doing fsck when all processes are killed.
436         //
437         if (ota_update_in_progress) {
438             return stat;
439         }
440         KillAllProcesses();
441         // even if it succeeds, still it is timeout and do not run fsck with all processes killed
442         UmountStat st = UmountPartitions(0ms);
443         if ((st != UMOUNT_STAT_SUCCESS) && DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo();
444     }
445 
446     if (stat == UMOUNT_STAT_SUCCESS && run_fsck) {
447         LOG(INFO) << "Pause reboot monitor thread before fsck";
448         sem_post(reboot_semaphore);
449 
450         // fsck part is excluded from timeout check. It only runs for user initiated shutdown
451         // and should not affect reboot time.
452         for (auto& entry : block_devices) {
453             entry.DoFsck();
454         }
455 
456         LOG(INFO) << "Resume reboot monitor thread after fsck";
457         sem_post(reboot_semaphore);
458     }
459     return stat;
460 }
461 
462 // zram is able to use backing device on top of a loopback device.
463 // In order to unmount /data successfully, we have to kill the loopback device first
464 #define ZRAM_DEVICE       "/dev/block/zram0"
465 #define ZRAM_RESET        "/sys/block/zram0/reset"
466 #define ZRAM_BACK_DEV     "/sys/block/zram0/backing_dev"
467 #define ZRAM_INITSTATE    "/sys/block/zram0/initstate"
KillZramBackingDevice()468 static Result<void> KillZramBackingDevice() {
469     std::string zram_initstate;
470     if (!android::base::ReadFileToString(ZRAM_INITSTATE, &zram_initstate)) {
471         return ErrnoError() << "Failed to read " << ZRAM_INITSTATE;
472     }
473 
474     zram_initstate.erase(zram_initstate.length() - 1);
475     if (zram_initstate == "0") {
476         LOG(INFO) << "Zram has not been swapped on";
477         return {};
478     }
479 
480     if (access(ZRAM_BACK_DEV, F_OK) != 0 && errno == ENOENT) {
481         LOG(INFO) << "No zram backing device configured";
482         return {};
483     }
484     std::string backing_dev;
485     if (!android::base::ReadFileToString(ZRAM_BACK_DEV, &backing_dev)) {
486         return ErrnoError() << "Failed to read " << ZRAM_BACK_DEV;
487     }
488 
489     // cut the last "\n"
490     backing_dev.erase(backing_dev.length() - 1);
491 
492     if (android::base::StartsWith(backing_dev, "none")) {
493         LOG(INFO) << "No zram backing device configured";
494         return {};
495     }
496 
497     // shutdown zram handle
498     Timer swap_timer;
499     LOG(INFO) << "swapoff() start...";
500     if (swapoff(ZRAM_DEVICE) == -1) {
501         return ErrnoError() << "zram_backing_dev: swapoff (" << backing_dev << ")"
502                             << " failed";
503     }
504     LOG(INFO) << "swapoff() took " << swap_timer;
505 
506     if (!WriteStringToFile("1", ZRAM_RESET)) {
507         return Error() << "zram_backing_dev: reset (" << backing_dev << ")"
508                        << " failed";
509     }
510 
511     if (!android::base::StartsWith(backing_dev, "/dev/block/loop")) {
512         LOG(INFO) << backing_dev << " is not a loop device. Exiting early";
513         return {};
514     }
515 
516     // clear loopback device
517     unique_fd loop(TEMP_FAILURE_RETRY(open(backing_dev.c_str(), O_RDWR | O_CLOEXEC)));
518     if (loop.get() < 0) {
519         return ErrnoError() << "zram_backing_dev: open(" << backing_dev << ")"
520                             << " failed";
521     }
522 
523     if (ioctl(loop.get(), LOOP_CLR_FD, 0) < 0) {
524         return ErrnoError() << "zram_backing_dev: loop_clear (" << backing_dev << ")"
525                             << " failed";
526     }
527     LOG(INFO) << "zram_backing_dev: `" << backing_dev << "` is cleared successfully.";
528     return {};
529 }
530 
531 // Stops given services, waits for them to be stopped for |timeout| ms.
532 // If terminate is true, then SIGTERM is sent to services, otherwise SIGKILL is sent.
533 // Note that services are stopped in order given by |ServiceList::services_in_shutdown_order|
534 // function.
StopServices(const std::set<std::string> & services,std::chrono::milliseconds timeout,bool terminate)535 static void StopServices(const std::set<std::string>& services, std::chrono::milliseconds timeout,
536                          bool terminate) {
537     LOG(INFO) << "Stopping " << services.size() << " services by sending "
538               << (terminate ? "SIGTERM" : "SIGKILL");
539     std::vector<pid_t> pids;
540     pids.reserve(services.size());
541     for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
542         if (services.count(s->name()) == 0) {
543             continue;
544         }
545         if (s->pid() > 0) {
546             pids.push_back(s->pid());
547         }
548         if (terminate) {
549             s->Terminate();
550         } else {
551             s->Stop();
552         }
553     }
554     if (timeout > 0ms) {
555         WaitToBeReaped(Service::GetSigchldFd(), pids, timeout);
556     } else {
557         // Even if we don't to wait for services to stop, we still optimistically reap zombies.
558         ReapAnyOutstandingChildren();
559     }
560 }
561 
562 // Like StopServices, but also logs all the services that failed to stop after the provided timeout.
563 // Returns number of violators.
StopServicesAndLogViolations(const std::set<std::string> & services,std::chrono::milliseconds timeout,bool terminate)564 int StopServicesAndLogViolations(const std::set<std::string>& services,
565                                  std::chrono::milliseconds timeout, bool terminate) {
566     StopServices(services, timeout, terminate);
567     int still_running = 0;
568     for (const auto& s : ServiceList::GetInstance()) {
569         if (s->IsRunning() && services.count(s->name())) {
570             LOG(ERROR) << "[service-misbehaving] : service '" << s->name() << "' is still running "
571                        << timeout.count() << "ms after receiving "
572                        << (terminate ? "SIGTERM" : "SIGKILL");
573             still_running++;
574         }
575     }
576     return still_running;
577 }
578 
UnmountAllApexes()579 static Result<void> UnmountAllApexes() {
580     // don't need to unmount because apexd doesn't use /data in Microdroid
581     if (IsMicrodroid()) {
582         return {};
583     }
584 
585     const char* args[] = {"/system/bin/apexd", "--unmount-all"};
586     int status;
587     if (logwrap_fork_execvp(arraysize(args), args, &status, false, LOG_KLOG, true, nullptr) != 0) {
588         return ErrnoError() << "Failed to call '/system/bin/apexd --unmount-all'";
589     }
590     if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
591         return {};
592     }
593     return Error() << "'/system/bin/apexd --unmount-all' failed : " << status;
594 }
595 
596 //* Reboot / shutdown the system.
597 // cmd ANDROID_RB_* as defined in android_reboot.h
598 // reason Reason string like "reboot", "shutdown,userrequested"
599 // reboot_target Reboot target string like "bootloader". Otherwise, it should be an empty string.
600 // run_fsck Whether to run fsck after umount is done.
601 //
DoReboot(unsigned int cmd,const std::string & reason,const std::string & reboot_target,bool run_fsck)602 static void DoReboot(unsigned int cmd, const std::string& reason, const std::string& reboot_target,
603                      bool run_fsck) {
604     Timer t;
605     LOG(INFO) << "Reboot start, reason: " << reason << ", reboot_target: " << reboot_target;
606 
607     bool is_thermal_shutdown = cmd == ANDROID_RB_THERMOFF;
608 
609     auto shutdown_timeout = 0ms;
610     if (!SHUTDOWN_ZERO_TIMEOUT) {
611         constexpr unsigned int shutdown_timeout_default = 6;
612         constexpr unsigned int max_thermal_shutdown_timeout = 3;
613         auto shutdown_timeout_final = android::base::GetUintProperty("ro.build.shutdown_timeout",
614                                                                      shutdown_timeout_default);
615         if (is_thermal_shutdown && shutdown_timeout_final > max_thermal_shutdown_timeout) {
616             shutdown_timeout_final = max_thermal_shutdown_timeout;
617         }
618         shutdown_timeout = std::chrono::seconds(shutdown_timeout_final);
619     }
620     LOG(INFO) << "Shutdown timeout: " << shutdown_timeout.count() << " ms";
621 
622     sem_t reboot_semaphore;
623     if (sem_init(&reboot_semaphore, false, 0) == -1) {
624         // These should never fail, but if they do, skip the graceful reboot and reboot immediately.
625         LOG(ERROR) << "sem_init() fail and RebootSystem() return!";
626         RebootSystem(cmd, reboot_target, reason);
627     }
628 
629     // Start a thread to monitor init shutdown process
630     LOG(INFO) << "Create reboot monitor thread.";
631     bool reboot_monitor_run = true;
632     std::thread reboot_monitor_thread(&RebootMonitorThread, cmd, reboot_target, &reboot_semaphore,
633                                       shutdown_timeout, &reboot_monitor_run);
634     reboot_monitor_thread.detach();
635 
636     // Start reboot monitor thread
637     sem_post(&reboot_semaphore);
638 
639     // Ensure last reboot reason is reduced to canonical
640     // alias reported in bootloader or system boot reason.
641     size_t skip = 0;
642     std::vector<std::string> reasons = Split(reason, ",");
643     if (reasons.size() >= 2 && reasons[0] == "reboot" &&
644         (reasons[1] == "recovery" || reasons[1] == "bootloader" || reasons[1] == "cold" ||
645          reasons[1] == "hard" || reasons[1] == "warm")) {
646         skip = strlen("reboot,");
647     }
648     PersistRebootReason(reason.c_str() + skip, true);
649 
650     // If /data isn't mounted then we can skip the extra reboot steps below, since we don't need to
651     // worry about unmounting it.
652     if (!IsDataMounted("*")) {
653         sync();
654         RebootSystem(cmd, reboot_target, reason);
655         abort();
656     }
657 
658     bool do_shutdown_animation = GetBoolProperty("ro.init.shutdown_animation", false);
659     // watchdogd is a vendor specific component but should be alive to complete shutdown safely.
660     const std::set<std::string> to_starts{"watchdogd"};
661     std::set<std::string> stop_first;
662     for (const auto& s : ServiceList::GetInstance()) {
663         if (kDebuggingServices.count(s->name())) {
664             // keep debugging tools until non critical ones are all gone.
665             s->SetShutdownCritical();
666         } else if (to_starts.count(s->name())) {
667             if (auto result = s->Start(); !result.ok()) {
668                 LOG(ERROR) << "Could not start shutdown 'to_start' service '" << s->name()
669                            << "': " << result.error();
670             }
671             s->SetShutdownCritical();
672         } else if (do_shutdown_animation && s->classnames().count("animation") > 0) {
673             // Need these for shutdown animations.
674         } else if (s->IsShutdownCritical()) {
675             // Start shutdown critical service if not started.
676             if (auto result = s->Start(); !result.ok()) {
677                 LOG(ERROR) << "Could not start shutdown critical service '" << s->name()
678                            << "': " << result.error();
679             }
680         } else {
681             stop_first.insert(s->name());
682         }
683     }
684 
685     // remaining operations (specifically fsck) may take a substantial duration
686     if (!do_shutdown_animation && (cmd == ANDROID_RB_POWEROFF || is_thermal_shutdown)) {
687         TurnOffBacklight();
688     }
689 
690     Service* boot_anim = ServiceList::GetInstance().FindService("bootanim");
691     Service* surface_flinger = ServiceList::GetInstance().FindService("surfaceflinger");
692     if (boot_anim != nullptr && surface_flinger != nullptr && surface_flinger->IsRunning()) {
693 
694         if (do_shutdown_animation) {
695             SetProperty("service.bootanim.exit", "0");
696             SetProperty("service.bootanim.progress", "0");
697             // Could be in the middle of animation. Stop and start so that it can pick
698             // up the right mode.
699             boot_anim->Stop();
700         }
701 
702         for (const auto& service : ServiceList::GetInstance()) {
703             if (service->classnames().count("animation") == 0) {
704                 continue;
705             }
706 
707             // start all animation classes if stopped.
708             if (do_shutdown_animation) {
709                 service->Start();
710             }
711             service->SetShutdownCritical();  // will not check animation class separately
712         }
713 
714         if (do_shutdown_animation) {
715             boot_anim->Start();
716             surface_flinger->SetShutdownCritical();
717             boot_anim->SetShutdownCritical();
718         }
719     }
720 
721     // optional shutdown step
722     // 1. terminate all services except shutdown critical ones. wait for delay to finish
723     if (shutdown_timeout > 0ms) {
724         StopServicesAndLogViolations(stop_first, shutdown_timeout / 2, true /* SIGTERM */);
725     }
726     // Send SIGKILL to ones that didn't terminate cleanly.
727     StopServicesAndLogViolations(stop_first, 0ms, false /* SIGKILL */);
728     SubcontextTerminate();
729     // Reap subcontext pids.
730     ReapAnyOutstandingChildren();
731 
732     // 3. send volume abort_fuse and volume shutdown to vold
733     Service* vold_service = ServiceList::GetInstance().FindService("vold");
734     if (vold_service != nullptr && vold_service->IsRunning()) {
735         // Manually abort FUSE connections, since the FUSE daemon is already dead
736         // at this point, and unmounting it might hang.
737         CallVdc("volume", "abort_fuse");
738         CallVdc("volume", "shutdown");
739         vold_service->Stop();
740     } else {
741         LOG(INFO) << "vold not running, skipping vold shutdown";
742     }
743     // logcat stopped here
744     StopServices(kDebuggingServices, 0ms, false /* SIGKILL */);
745     // 4. sync, try umount, and optionally run fsck for user shutdown
746     {
747         Timer sync_timer;
748         LOG(INFO) << "sync() before umount...";
749         sync();
750         LOG(INFO) << "sync() before umount took" << sync_timer;
751     }
752     // 5. drop caches and disable zram backing device, if exist
753     KillZramBackingDevice();
754 
755     LOG(INFO) << "Ready to unmount apexes. So far shutdown sequence took " << t;
756     // 6. unmount active apexes, otherwise they might prevent clean unmount of /data.
757     if (auto ret = UnmountAllApexes(); !ret.ok()) {
758         LOG(ERROR) << ret.error();
759     }
760     UmountStat stat =
761             TryUmountAndFsck(cmd, run_fsck, shutdown_timeout - t.duration(), &reboot_semaphore);
762     // Follow what linux shutdown is doing: one more sync with little bit delay
763     {
764         Timer sync_timer;
765         LOG(INFO) << "sync() after umount...";
766         sync();
767         LOG(INFO) << "sync() after umount took" << sync_timer;
768     }
769     if (!is_thermal_shutdown) std::this_thread::sleep_for(100ms);
770     LogShutdownTime(stat, &t);
771 
772     // Send signal to terminate reboot monitor thread.
773     reboot_monitor_run = false;
774     sem_post(&reboot_semaphore);
775 
776     // Reboot regardless of umount status. If umount fails, fsck after reboot will fix it.
777     if (IsDataMounted("f2fs")) {
778         uint32_t flag = F2FS_GOING_DOWN_FULLSYNC;
779         unique_fd fd(TEMP_FAILURE_RETRY(open("/data", O_RDONLY)));
780         int ret = ioctl(fd.get(), F2FS_IOC_SHUTDOWN, &flag);
781         if (ret) {
782             PLOG(ERROR) << "Shutdown /data: ";
783         } else {
784             LOG(INFO) << "Shutdown /data";
785         }
786     }
787     RebootSystem(cmd, reboot_target, reason);
788     abort();
789 }
790 
EnterShutdown()791 static void EnterShutdown() {
792     LOG(INFO) << "Entering shutdown mode";
793     shutting_down = true;
794     // Skip wait for prop if it is in progress
795     ResetWaitForProp();
796     // Clear EXEC flag if there is one pending
797     for (const auto& s : ServiceList::GetInstance()) {
798         s->UnSetExec();
799     }
800 }
801 
802 /**
803  * Check if "command" field is set in bootloader message.
804  *
805  * If "command" field is broken (contains non-printable characters prior to
806  * terminating zero), it will be zeroed.
807  *
808  * @param[in,out] boot Bootloader message (BCB) structure
809  * @return true if "command" field is already set, and false if it's empty
810  */
CommandIsPresent(bootloader_message * boot)811 static bool CommandIsPresent(bootloader_message* boot) {
812     if (boot->command[0] == '\0')
813         return false;
814 
815     for (size_t i = 0; i < arraysize(boot->command); ++i) {
816         if (boot->command[i] == '\0')
817             return true;
818         if (!isprint(boot->command[i]))
819             break;
820     }
821 
822     memset(boot->command, 0, sizeof(boot->command));
823     return false;
824 }
825 
HandlePowerctlMessage(const std::string & command)826 void HandlePowerctlMessage(const std::string& command) {
827     unsigned int cmd = 0;
828     std::vector<std::string> cmd_params = Split(command, ",");
829     std::string reboot_target = "";
830     bool run_fsck = false;
831     bool command_invalid = false;
832 
833     if (cmd_params[0] == "shutdown") {
834         cmd = ANDROID_RB_POWEROFF;
835         if (cmd_params.size() >= 2) {
836             if (cmd_params[1] == "userrequested") {
837                 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
838                 // Run fsck once the file system is remounted in read-only mode.
839                 run_fsck = true;
840             } else if (cmd_params[1] == "thermal") {
841                 // Turn off sources of heat immediately.
842                 TurnOffBacklight();
843                 // run_fsck is false to avoid delay
844                 cmd = ANDROID_RB_THERMOFF;
845             }
846         }
847     } else if (cmd_params[0] == "reboot") {
848         cmd = ANDROID_RB_RESTART2;
849         if (cmd_params.size() >= 2) {
850             reboot_target = cmd_params[1];
851             if (reboot_target == "userspace") {
852                 LOG(ERROR) << "Userspace reboot is deprecated.";
853                 return;
854             }
855             // adb reboot fastboot should boot into bootloader for devices not
856             // supporting logical partitions.
857             if (reboot_target == "fastboot" &&
858                 !android::base::GetBoolProperty("ro.boot.dynamic_partitions", false)) {
859                 reboot_target = "bootloader";
860             }
861             // When rebooting to the bootloader notify the bootloader writing
862             // also the BCB.
863             if (reboot_target == "bootloader") {
864                 std::string err;
865                 if (!write_reboot_bootloader(&err)) {
866                     LOG(ERROR) << "reboot-bootloader: Error writing "
867                                   "bootloader_message: "
868                                << err;
869                 }
870             } else if (reboot_target == "recovery") {
871                 bootloader_message boot = {};
872                 if (std::string err; !read_bootloader_message(&boot, &err)) {
873                     LOG(ERROR) << "Failed to read bootloader message: " << err;
874                 }
875                 // Update the boot command field if it's empty, and preserve
876                 // the other arguments in the bootloader message.
877                 if (!CommandIsPresent(&boot)) {
878                     strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
879                     if (std::string err; !write_bootloader_message(boot, &err)) {
880                         LOG(ERROR) << "Failed to set bootloader message: " << err;
881                         return;
882                     }
883                 }
884             } else if (std::find(cmd_params.begin(), cmd_params.end(), "quiescent")
885                     != cmd_params.end()) { // Quiescent can be either subreason or details.
886                 bootloader_message boot = {};
887                 if (std::string err; !read_bootloader_message(&boot, &err)) {
888                     LOG(ERROR) << "Failed to read bootloader message: " << err;
889                 }
890                 // Update the boot command field if it's empty, and preserve
891                 // the other arguments in the bootloader message.
892                 if (!CommandIsPresent(&boot)) {
893                     strlcpy(boot.command, "boot-quiescent", sizeof(boot.command));
894                     if (std::string err; !write_bootloader_message(boot, &err)) {
895                         LOG(ERROR) << "Failed to set bootloader message: " << err;
896                         return;
897                     }
898                 }
899             } else if (reboot_target == "sideload" || reboot_target == "sideload-auto-reboot" ||
900                        reboot_target == "fastboot") {
901                 std::string arg = reboot_target == "sideload-auto-reboot" ? "sideload_auto_reboot"
902                                                                           : reboot_target;
903                 const std::vector<std::string> options = {
904                         "--" + arg,
905                 };
906                 std::string err;
907                 if (!write_bootloader_message(options, &err)) {
908                     LOG(ERROR) << "Failed to set bootloader message: " << err;
909                     return;
910                 }
911                 reboot_target = "recovery";
912             }
913 
914             // If there are additional parameter, pass them along
915             for (size_t i = 2; (cmd_params.size() > i) && cmd_params[i].size(); ++i) {
916                 reboot_target += "," + cmd_params[i];
917             }
918         }
919     } else {
920         command_invalid = true;
921     }
922     if (command_invalid) {
923         LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
924         return;
925     }
926 
927     // We do not want to process any messages (queue'ing triggers, shutdown messages, control
928     // messages, etc) from properties during reboot.
929     StopSendingMessages();
930 
931     LOG(INFO) << "Clear action queue and start shutdown trigger";
932     ActionManager::GetInstance().ClearQueue();
933     // Queue shutdown trigger first
934     ActionManager::GetInstance().QueueEventTrigger("shutdown");
935     // Queue built-in shutdown_done
936     auto shutdown_handler = [cmd, command, reboot_target, run_fsck](const BuiltinArguments&) {
937         DoReboot(cmd, command, reboot_target, run_fsck);
938         return Result<void>{};
939     };
940     ActionManager::GetInstance().QueueBuiltinAction(shutdown_handler, "shutdown_done");
941 
942     EnterShutdown();
943 }
944 
IsShuttingDown()945 bool IsShuttingDown() {
946     return shutting_down;
947 }
948 
949 }  // namespace init
950 }  // namespace android
951