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"
456 #define ZRAM_INITSTATE    "/sys/block/zram0/initstate"
KillZramBackingDevice()457 static Result<void> KillZramBackingDevice() {
458     std::string zram_initstate;
459     if (!android::base::ReadFileToString(ZRAM_INITSTATE, &zram_initstate)) {
460         return ErrnoError() << "Failed to read " << ZRAM_INITSTATE;
461     }
462 
463     zram_initstate.erase(zram_initstate.length() - 1);
464     if (zram_initstate == "0") {
465         LOG(INFO) << "Zram has not been swapped on";
466         return {};
467     }
468 
469     if (access(ZRAM_BACK_DEV, F_OK) != 0 && errno == ENOENT) {
470         LOG(INFO) << "No zram backing device configured";
471         return {};
472     }
473     std::string backing_dev;
474     if (!android::base::ReadFileToString(ZRAM_BACK_DEV, &backing_dev)) {
475         return ErrnoError() << "Failed to read " << ZRAM_BACK_DEV;
476     }
477 
478     // cut the last "\n"
479     backing_dev.erase(backing_dev.length() - 1);
480 
481     // shutdown zram handle
482     Timer swap_timer;
483     LOG(INFO) << "swapoff() start...";
484     if (swapoff(ZRAM_DEVICE) == -1) {
485         return ErrnoError() << "zram_backing_dev: swapoff (" << backing_dev << ")"
486                             << " failed";
487     }
488     LOG(INFO) << "swapoff() took " << swap_timer;;
489 
490     if (!WriteStringToFile("1", ZRAM_RESET)) {
491         return Error() << "zram_backing_dev: reset (" << backing_dev << ")"
492                        << " failed";
493     }
494 
495     if (!android::base::StartsWith(backing_dev, "/dev/block/loop")) {
496         LOG(INFO) << backing_dev << " is not a loop device. Exiting early";
497         return {};
498     }
499 
500     // clear loopback device
501     unique_fd loop(TEMP_FAILURE_RETRY(open(backing_dev.c_str(), O_RDWR | O_CLOEXEC)));
502     if (loop.get() < 0) {
503         return ErrnoError() << "zram_backing_dev: open(" << backing_dev << ")"
504                             << " failed";
505     }
506 
507     if (ioctl(loop.get(), LOOP_CLR_FD, 0) < 0) {
508         return ErrnoError() << "zram_backing_dev: loop_clear (" << backing_dev << ")"
509                             << " failed";
510     }
511     LOG(INFO) << "zram_backing_dev: `" << backing_dev << "` is cleared successfully.";
512     return {};
513 }
514 
515 // Stops given services, waits for them to be stopped for |timeout| ms.
516 // If terminate is true, then SIGTERM is sent to services, otherwise SIGKILL is sent.
517 // Note that services are stopped in order given by |ServiceList::services_in_shutdown_order|
518 // function.
StopServices(const std::set<std::string> & services,std::chrono::milliseconds timeout,bool terminate)519 static void StopServices(const std::set<std::string>& services, std::chrono::milliseconds timeout,
520                          bool terminate) {
521     LOG(INFO) << "Stopping " << services.size() << " services by sending "
522               << (terminate ? "SIGTERM" : "SIGKILL");
523     std::vector<pid_t> pids;
524     pids.reserve(services.size());
525     for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
526         if (services.count(s->name()) == 0) {
527             continue;
528         }
529         if (s->pid() > 0) {
530             pids.push_back(s->pid());
531         }
532         if (terminate) {
533             s->Terminate();
534         } else {
535             s->Stop();
536         }
537     }
538     if (timeout > 0ms) {
539         WaitToBeReaped(pids, timeout);
540     } else {
541         // Even if we don't to wait for services to stop, we still optimistically reap zombies.
542         ReapAnyOutstandingChildren();
543     }
544 }
545 
546 // Like StopServices, but also logs all the services that failed to stop after the provided timeout.
547 // Returns number of violators.
StopServicesAndLogViolations(const std::set<std::string> & services,std::chrono::milliseconds timeout,bool terminate)548 int StopServicesAndLogViolations(const std::set<std::string>& services,
549                                  std::chrono::milliseconds timeout, bool terminate) {
550     StopServices(services, timeout, terminate);
551     int still_running = 0;
552     for (const auto& s : ServiceList::GetInstance()) {
553         if (s->IsRunning() && services.count(s->name())) {
554             LOG(ERROR) << "[service-misbehaving] : service '" << s->name() << "' is still running "
555                        << timeout.count() << "ms after receiving "
556                        << (terminate ? "SIGTERM" : "SIGKILL");
557             still_running++;
558         }
559     }
560     return still_running;
561 }
562 
UnmountAllApexes()563 static Result<void> UnmountAllApexes() {
564     const char* args[] = {"/system/bin/apexd", "--unmount-all"};
565     int status;
566     if (logwrap_fork_execvp(arraysize(args), args, &status, false, LOG_KLOG, true, nullptr) != 0) {
567         return ErrnoError() << "Failed to call '/system/bin/apexd --unmount-all'";
568     }
569     if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
570         return {};
571     }
572     return Error() << "'/system/bin/apexd --unmount-all' failed : " << status;
573 }
574 
575 //* Reboot / shutdown the system.
576 // cmd ANDROID_RB_* as defined in android_reboot.h
577 // reason Reason string like "reboot", "shutdown,userrequested"
578 // reboot_target Reboot target string like "bootloader". Otherwise, it should be an empty string.
579 // run_fsck Whether to run fsck after umount is done.
580 //
DoReboot(unsigned int cmd,const std::string & reason,const std::string & reboot_target,bool run_fsck)581 static void DoReboot(unsigned int cmd, const std::string& reason, const std::string& reboot_target,
582                      bool run_fsck) {
583     Timer t;
584     LOG(INFO) << "Reboot start, reason: " << reason << ", reboot_target: " << reboot_target;
585 
586     bool is_thermal_shutdown = cmd == ANDROID_RB_THERMOFF;
587 
588     auto shutdown_timeout = 0ms;
589     if (!SHUTDOWN_ZERO_TIMEOUT) {
590         constexpr unsigned int shutdown_timeout_default = 6;
591         constexpr unsigned int max_thermal_shutdown_timeout = 3;
592         auto shutdown_timeout_final = android::base::GetUintProperty("ro.build.shutdown_timeout",
593                                                                      shutdown_timeout_default);
594         if (is_thermal_shutdown && shutdown_timeout_final > max_thermal_shutdown_timeout) {
595             shutdown_timeout_final = max_thermal_shutdown_timeout;
596         }
597         shutdown_timeout = std::chrono::seconds(shutdown_timeout_final);
598     }
599     LOG(INFO) << "Shutdown timeout: " << shutdown_timeout.count() << " ms";
600 
601     sem_t reboot_semaphore;
602     if (sem_init(&reboot_semaphore, false, 0) == -1) {
603         // These should never fail, but if they do, skip the graceful reboot and reboot immediately.
604         LOG(ERROR) << "sem_init() fail and RebootSystem() return!";
605         RebootSystem(cmd, reboot_target);
606     }
607 
608     // Start a thread to monitor init shutdown process
609     LOG(INFO) << "Create reboot monitor thread.";
610     bool reboot_monitor_run = true;
611     std::thread reboot_monitor_thread(&RebootMonitorThread, cmd, reboot_target, &reboot_semaphore,
612                                       shutdown_timeout, &reboot_monitor_run);
613     reboot_monitor_thread.detach();
614 
615     // Start reboot monitor thread
616     sem_post(&reboot_semaphore);
617 
618     // Ensure last reboot reason is reduced to canonical
619     // alias reported in bootloader or system boot reason.
620     size_t skip = 0;
621     std::vector<std::string> reasons = Split(reason, ",");
622     if (reasons.size() >= 2 && reasons[0] == "reboot" &&
623         (reasons[1] == "recovery" || reasons[1] == "bootloader" || reasons[1] == "cold" ||
624          reasons[1] == "hard" || reasons[1] == "warm")) {
625         skip = strlen("reboot,");
626     }
627     PersistRebootReason(reason.c_str() + skip, true);
628 
629     // If /data isn't mounted then we can skip the extra reboot steps below, since we don't need to
630     // worry about unmounting it.
631     if (!IsDataMounted()) {
632         sync();
633         RebootSystem(cmd, reboot_target);
634         abort();
635     }
636 
637     // watchdogd is a vendor specific component but should be alive to complete shutdown safely.
638     const std::set<std::string> to_starts{"watchdogd"};
639     std::set<std::string> stop_first;
640     for (const auto& s : ServiceList::GetInstance()) {
641         if (kDebuggingServices.count(s->name())) {
642             // keep debugging tools until non critical ones are all gone.
643             s->SetShutdownCritical();
644         } else if (to_starts.count(s->name())) {
645             if (auto result = s->Start(); !result.ok()) {
646                 LOG(ERROR) << "Could not start shutdown 'to_start' service '" << s->name()
647                            << "': " << result.error();
648             }
649             s->SetShutdownCritical();
650         } else if (s->IsShutdownCritical()) {
651             // Start shutdown critical service if not started.
652             if (auto result = s->Start(); !result.ok()) {
653                 LOG(ERROR) << "Could not start shutdown critical service '" << s->name()
654                            << "': " << result.error();
655             }
656         } else {
657             stop_first.insert(s->name());
658         }
659     }
660 
661     // remaining operations (specifically fsck) may take a substantial duration
662     if (cmd == ANDROID_RB_POWEROFF || is_thermal_shutdown) {
663         TurnOffBacklight();
664     }
665 
666     Service* boot_anim = ServiceList::GetInstance().FindService("bootanim");
667     Service* surface_flinger = ServiceList::GetInstance().FindService("surfaceflinger");
668     if (boot_anim != nullptr && surface_flinger != nullptr && surface_flinger->IsRunning()) {
669         bool do_shutdown_animation = GetBoolProperty("ro.init.shutdown_animation", false);
670 
671         if (do_shutdown_animation) {
672             SetProperty("service.bootanim.exit", "0");
673             SetProperty("service.bootanim.progress", "0");
674             // Could be in the middle of animation. Stop and start so that it can pick
675             // up the right mode.
676             boot_anim->Stop();
677         }
678 
679         for (const auto& service : ServiceList::GetInstance()) {
680             if (service->classnames().count("animation") == 0) {
681                 continue;
682             }
683 
684             // start all animation classes if stopped.
685             if (do_shutdown_animation) {
686                 service->Start();
687             }
688             service->SetShutdownCritical();  // will not check animation class separately
689         }
690 
691         if (do_shutdown_animation) {
692             boot_anim->Start();
693             surface_flinger->SetShutdownCritical();
694             boot_anim->SetShutdownCritical();
695         }
696     }
697 
698     // optional shutdown step
699     // 1. terminate all services except shutdown critical ones. wait for delay to finish
700     if (shutdown_timeout > 0ms) {
701         StopServicesAndLogViolations(stop_first, shutdown_timeout / 2, true /* SIGTERM */);
702     }
703     // Send SIGKILL to ones that didn't terminate cleanly.
704     StopServicesAndLogViolations(stop_first, 0ms, false /* SIGKILL */);
705     SubcontextTerminate();
706     // Reap subcontext pids.
707     ReapAnyOutstandingChildren();
708 
709     // 3. send volume abort_fuse and volume shutdown to vold
710     Service* vold_service = ServiceList::GetInstance().FindService("vold");
711     if (vold_service != nullptr && vold_service->IsRunning()) {
712         // Manually abort FUSE connections, since the FUSE daemon is already dead
713         // at this point, and unmounting it might hang.
714         CallVdc("volume", "abort_fuse");
715         CallVdc("volume", "shutdown");
716         vold_service->Stop();
717     } else {
718         LOG(INFO) << "vold not running, skipping vold shutdown";
719     }
720     // logcat stopped here
721     StopServices(kDebuggingServices, 0ms, false /* SIGKILL */);
722     // 4. sync, try umount, and optionally run fsck for user shutdown
723     {
724         Timer sync_timer;
725         LOG(INFO) << "sync() before umount...";
726         sync();
727         LOG(INFO) << "sync() before umount took" << sync_timer;
728     }
729     // 5. drop caches and disable zram backing device, if exist
730     KillZramBackingDevice();
731 
732     LOG(INFO) << "Ready to unmount apexes. So far shutdown sequence took " << t;
733     // 6. unmount active apexes, otherwise they might prevent clean unmount of /data.
734     if (auto ret = UnmountAllApexes(); !ret.ok()) {
735         LOG(ERROR) << ret.error();
736     }
737     UmountStat stat =
738             TryUmountAndFsck(cmd, run_fsck, shutdown_timeout - t.duration(), &reboot_semaphore);
739     // Follow what linux shutdown is doing: one more sync with little bit delay
740     {
741         Timer sync_timer;
742         LOG(INFO) << "sync() after umount...";
743         sync();
744         LOG(INFO) << "sync() after umount took" << sync_timer;
745     }
746     if (!is_thermal_shutdown) std::this_thread::sleep_for(100ms);
747     LogShutdownTime(stat, &t);
748 
749     // Send signal to terminate reboot monitor thread.
750     reboot_monitor_run = false;
751     sem_post(&reboot_semaphore);
752 
753     // Reboot regardless of umount status. If umount fails, fsck after reboot will fix it.
754     RebootSystem(cmd, reboot_target);
755     abort();
756 }
757 
EnterShutdown()758 static void EnterShutdown() {
759     LOG(INFO) << "Entering shutdown mode";
760     shutting_down = true;
761     // Skip wait for prop if it is in progress
762     ResetWaitForProp();
763     // Clear EXEC flag if there is one pending
764     for (const auto& s : ServiceList::GetInstance()) {
765         s->UnSetExec();
766     }
767 }
768 
LeaveShutdown()769 static void LeaveShutdown() {
770     LOG(INFO) << "Leaving shutdown mode";
771     shutting_down = false;
772     StartSendingMessages();
773 }
774 
GetMillisProperty(const std::string & name,std::chrono::milliseconds default_value)775 static std::chrono::milliseconds GetMillisProperty(const std::string& name,
776                                                    std::chrono::milliseconds default_value) {
777     auto value = GetUintProperty(name, static_cast<uint64_t>(default_value.count()));
778     return std::chrono::milliseconds(std::move(value));
779 }
780 
DoUserspaceReboot()781 static Result<void> DoUserspaceReboot() {
782     LOG(INFO) << "Userspace reboot initiated";
783     // An ugly way to pass a more precise reason on why fallback to hard reboot was triggered.
784     std::string sub_reason = "";
785     auto guard = android::base::make_scope_guard([&sub_reason] {
786         // Leave shutdown so that we can handle a full reboot.
787         LeaveShutdown();
788         trigger_shutdown("reboot,userspace_failed,shutdown_aborted," + sub_reason);
789     });
790     // Triggering userspace-reboot-requested will result in a bunch of setprop
791     // actions. We should make sure, that all of them are propagated before
792     // proceeding with userspace reboot. Synchronously setting sys.init.userspace_reboot.in_progress
793     // property is not perfect, but it should do the trick.
794     if (!android::sysprop::InitProperties::userspace_reboot_in_progress(true)) {
795         sub_reason = "setprop";
796         return Error() << "Failed to set sys.init.userspace_reboot.in_progress property";
797     }
798     EnterShutdown();
799     if (!SetProperty("sys.powerctl", "")) {
800         sub_reason = "resetprop";
801         return Error() << "Failed to reset sys.powerctl property";
802     }
803     std::set<std::string> stop_first;
804     // Remember the services that were enabled. We will need to manually enable them again otherwise
805     // triggers like class_start won't restart them.
806     std::set<std::string> were_enabled;
807     for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
808         if (s->is_post_data() && !kDebuggingServices.count(s->name())) {
809             stop_first.insert(s->name());
810         }
811         // TODO(ioffe): we should also filter out temporary services here.
812         if (s->is_post_data() && s->IsEnabled()) {
813             were_enabled.insert(s->name());
814         }
815     }
816     {
817         Timer sync_timer;
818         LOG(INFO) << "sync() before terminating services...";
819         sync();
820         LOG(INFO) << "sync() took " << sync_timer;
821     }
822     auto sigterm_timeout = GetMillisProperty("init.userspace_reboot.sigterm.timeoutmillis", 5s);
823     auto sigkill_timeout = GetMillisProperty("init.userspace_reboot.sigkill.timeoutmillis", 10s);
824     LOG(INFO) << "Timeout to terminate services: " << sigterm_timeout.count() << "ms "
825               << "Timeout to kill services: " << sigkill_timeout.count() << "ms";
826     std::string services_file_name = "/metadata/userspacereboot/services.txt";
827     const int flags = O_RDWR | O_CREAT | O_SYNC | O_APPEND | O_CLOEXEC;
828     StopServicesAndLogViolations(stop_first, sigterm_timeout, true /* SIGTERM */);
829     if (int r = StopServicesAndLogViolations(stop_first, sigkill_timeout, false /* SIGKILL */);
830         r > 0) {
831         auto fd = unique_fd(TEMP_FAILURE_RETRY(open(services_file_name.c_str(), flags, 0666)));
832         android::base::WriteStringToFd("Post-data services still running: \n", fd);
833         for (const auto& s : ServiceList::GetInstance()) {
834             if (s->IsRunning() && stop_first.count(s->name())) {
835                 android::base::WriteStringToFd(s->name() + "\n", fd);
836             }
837         }
838         sub_reason = "sigkill";
839         return Error() << r << " post-data services are still running";
840     }
841     if (auto result = KillZramBackingDevice(); !result.ok()) {
842         sub_reason = "zram";
843         return result;
844     }
845     if (auto result = CallVdc("volume", "reset"); !result.ok()) {
846         sub_reason = "vold_reset";
847         return result;
848     }
849     const auto& debugging_services = GetPostDataDebuggingServices();
850     if (int r = StopServicesAndLogViolations(debugging_services, sigkill_timeout,
851                                              false /* SIGKILL */);
852         r > 0) {
853         auto fd = unique_fd(TEMP_FAILURE_RETRY(open(services_file_name.c_str(), flags, 0666)));
854         android::base::WriteStringToFd("Debugging services still running: \n", fd);
855         for (const auto& s : ServiceList::GetInstance()) {
856             if (s->IsRunning() && debugging_services.count(s->name())) {
857                 android::base::WriteStringToFd(s->name() + "\n", fd);
858             }
859         }
860         sub_reason = "sigkill_debug";
861         return Error() << r << " debugging services are still running";
862     }
863     {
864         Timer sync_timer;
865         LOG(INFO) << "sync() after stopping services...";
866         sync();
867         LOG(INFO) << "sync() took " << sync_timer;
868     }
869     if (auto result = UnmountAllApexes(); !result.ok()) {
870         sub_reason = "apex";
871         return result;
872     }
873     if (!SwitchToMountNamespaceIfNeeded(NS_BOOTSTRAP).ok()) {
874         sub_reason = "ns_switch";
875         return Error() << "Failed to switch to bootstrap namespace";
876     }
877     // Remove services that were defined in an APEX.
878     ServiceList::GetInstance().RemoveServiceIf([](const std::unique_ptr<Service>& s) -> bool {
879         if (s->is_from_apex()) {
880             LOG(INFO) << "Removing service '" << s->name() << "' because it's defined in an APEX";
881             return true;
882         }
883         return false;
884     });
885     // Re-enable services
886     for (const auto& s : ServiceList::GetInstance()) {
887         if (were_enabled.count(s->name())) {
888             LOG(INFO) << "Re-enabling service '" << s->name() << "'";
889             s->Enable();
890         }
891     }
892     ServiceList::GetInstance().ResetState();
893     LeaveShutdown();
894     ActionManager::GetInstance().QueueEventTrigger("userspace-reboot-resume");
895     guard.Disable();  // Go on with userspace reboot.
896     return {};
897 }
898 
UserspaceRebootWatchdogThread()899 static void UserspaceRebootWatchdogThread() {
900     auto started_timeout = GetMillisProperty("init.userspace_reboot.started.timeoutmillis", 10s);
901     if (!WaitForProperty("sys.init.userspace_reboot.in_progress", "1", started_timeout)) {
902         LOG(ERROR) << "Userspace reboot didn't start in " << started_timeout.count()
903                    << "ms. Switching to full reboot";
904         // Init might be wedged, don't try to write reboot reason into a persistent property and do
905         // a dirty reboot.
906         PersistRebootReason("userspace_failed,watchdog_triggered,failed_to_start", false);
907         RebootSystem(ANDROID_RB_RESTART2, "userspace_failed,watchdog_triggered,failed_to_start");
908     }
909     LOG(INFO) << "Starting userspace reboot watchdog";
910     auto watchdog_timeout = GetMillisProperty("init.userspace_reboot.watchdog.timeoutmillis", 5min);
911     LOG(INFO) << "UserspaceRebootWatchdog timeout: " << watchdog_timeout.count() << "ms";
912     if (!WaitForProperty("sys.boot_completed", "1", watchdog_timeout)) {
913         LOG(ERROR) << "Failed to boot in " << watchdog_timeout.count()
914                    << "ms. Switching to full reboot";
915         // In this case device is in a boot loop. Only way to recover is to do dirty reboot.
916         // Since init might be wedged, don't try to write reboot reason into a persistent property.
917         PersistRebootReason("userspace_failed,watchdog_triggered,failed_to_boot", false);
918         RebootSystem(ANDROID_RB_RESTART2, "userspace_failed,watchdog_triggered,failed_to_boot");
919     }
920     LOG(INFO) << "Device booted, stopping userspace reboot watchdog";
921 }
922 
HandleUserspaceReboot()923 static void HandleUserspaceReboot() {
924     if (!android::sysprop::InitProperties::is_userspace_reboot_supported().value_or(false)) {
925         LOG(ERROR) << "Attempted a userspace reboot on a device that doesn't support it";
926         return;
927     }
928     // Spinnig up a separate thread will fail the setns call later in the boot sequence.
929     // Fork a new process to monitor userspace reboot while we are investigating a better solution.
930     pid_t pid = fork();
931     if (pid < 0) {
932         PLOG(ERROR) << "Failed to fork process for userspace reboot watchdog. Switching to full "
933                     << "reboot";
934         trigger_shutdown("reboot,userspace_failed,watchdog_fork");
935         return;
936     }
937     if (pid == 0) {
938         // Child
939         UserspaceRebootWatchdogThread();
940         _exit(EXIT_SUCCESS);
941     }
942     LOG(INFO) << "Clearing queue and starting userspace-reboot-requested trigger";
943     auto& am = ActionManager::GetInstance();
944     am.ClearQueue();
945     am.QueueEventTrigger("userspace-reboot-requested");
946     auto handler = [](const BuiltinArguments&) { return DoUserspaceReboot(); };
947     am.QueueBuiltinAction(handler, "userspace-reboot");
948 }
949 
950 /**
951  * Check if "command" field is set in bootloader message.
952  *
953  * If "command" field is broken (contains non-printable characters prior to
954  * terminating zero), it will be zeroed.
955  *
956  * @param[in,out] boot Bootloader message (BCB) structure
957  * @return true if "command" field is already set, and false if it's empty
958  */
CommandIsPresent(bootloader_message * boot)959 static bool CommandIsPresent(bootloader_message* boot) {
960     if (boot->command[0] == '\0')
961         return false;
962 
963     for (size_t i = 0; i < arraysize(boot->command); ++i) {
964         if (boot->command[i] == '\0')
965             return true;
966         if (!isprint(boot->command[i]))
967             break;
968     }
969 
970     memset(boot->command, 0, sizeof(boot->command));
971     return false;
972 }
973 
HandlePowerctlMessage(const std::string & command)974 void HandlePowerctlMessage(const std::string& command) {
975     unsigned int cmd = 0;
976     std::vector<std::string> cmd_params = Split(command, ",");
977     std::string reboot_target = "";
978     bool run_fsck = false;
979     bool command_invalid = false;
980     bool userspace_reboot = false;
981 
982     if (cmd_params[0] == "shutdown") {
983         cmd = ANDROID_RB_POWEROFF;
984         if (cmd_params.size() >= 2) {
985             if (cmd_params[1] == "userrequested") {
986                 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
987                 // Run fsck once the file system is remounted in read-only mode.
988                 run_fsck = true;
989             } else if (cmd_params[1] == "thermal") {
990                 // Turn off sources of heat immediately.
991                 TurnOffBacklight();
992                 // run_fsck is false to avoid delay
993                 cmd = ANDROID_RB_THERMOFF;
994             }
995         }
996     } else if (cmd_params[0] == "reboot") {
997         cmd = ANDROID_RB_RESTART2;
998         if (cmd_params.size() >= 2) {
999             reboot_target = cmd_params[1];
1000             if (reboot_target == "userspace") {
1001                 LOG(INFO) << "Userspace reboot requested";
1002                 userspace_reboot = true;
1003             }
1004             // adb reboot fastboot should boot into bootloader for devices not
1005             // supporting logical partitions.
1006             if (reboot_target == "fastboot" &&
1007                 !android::base::GetBoolProperty("ro.boot.dynamic_partitions", false)) {
1008                 reboot_target = "bootloader";
1009             }
1010             // When rebooting to the bootloader notify the bootloader writing
1011             // also the BCB.
1012             if (reboot_target == "bootloader") {
1013                 std::string err;
1014                 if (!write_reboot_bootloader(&err)) {
1015                     LOG(ERROR) << "reboot-bootloader: Error writing "
1016                                   "bootloader_message: "
1017                                << err;
1018                 }
1019             } else if (reboot_target == "recovery") {
1020                 bootloader_message boot = {};
1021                 if (std::string err; !read_bootloader_message(&boot, &err)) {
1022                     LOG(ERROR) << "Failed to read bootloader message: " << err;
1023                 }
1024                 // Update the boot command field if it's empty, and preserve
1025                 // the other arguments in the bootloader message.
1026                 if (!CommandIsPresent(&boot)) {
1027                     strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
1028                     if (std::string err; !write_bootloader_message(boot, &err)) {
1029                         LOG(ERROR) << "Failed to set bootloader message: " << err;
1030                         return;
1031                     }
1032                 }
1033             } else if (reboot_target == "sideload" || reboot_target == "sideload-auto-reboot" ||
1034                        reboot_target == "fastboot") {
1035                 std::string arg = reboot_target == "sideload-auto-reboot" ? "sideload_auto_reboot"
1036                                                                           : reboot_target;
1037                 const std::vector<std::string> options = {
1038                         "--" + arg,
1039                 };
1040                 std::string err;
1041                 if (!write_bootloader_message(options, &err)) {
1042                     LOG(ERROR) << "Failed to set bootloader message: " << err;
1043                     return;
1044                 }
1045                 reboot_target = "recovery";
1046             }
1047 
1048             // If there are additional parameter, pass them along
1049             for (size_t i = 2; (cmd_params.size() > i) && cmd_params[i].size(); ++i) {
1050                 reboot_target += "," + cmd_params[i];
1051             }
1052         }
1053     } else {
1054         command_invalid = true;
1055     }
1056     if (command_invalid) {
1057         LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
1058         return;
1059     }
1060 
1061     // We do not want to process any messages (queue'ing triggers, shutdown messages, control
1062     // messages, etc) from properties during reboot.
1063     StopSendingMessages();
1064 
1065     if (userspace_reboot) {
1066         HandleUserspaceReboot();
1067         return;
1068     }
1069 
1070     LOG(INFO) << "Clear action queue and start shutdown trigger";
1071     ActionManager::GetInstance().ClearQueue();
1072     // Queue shutdown trigger first
1073     ActionManager::GetInstance().QueueEventTrigger("shutdown");
1074     // Queue built-in shutdown_done
1075     auto shutdown_handler = [cmd, command, reboot_target, run_fsck](const BuiltinArguments&) {
1076         DoReboot(cmd, command, reboot_target, run_fsck);
1077         return Result<void>{};
1078     };
1079     ActionManager::GetInstance().QueueBuiltinAction(shutdown_handler, "shutdown_done");
1080 
1081     EnterShutdown();
1082 }
1083 
IsShuttingDown()1084 bool IsShuttingDown() {
1085     return shutting_down;
1086 }
1087 
1088 }  // namespace init
1089 }  // namespace android
1090