1 /*
2  * Copyright (C) 2008 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 "init.h"
18 
19 #include <dirent.h>
20 #include <fcntl.h>
21 #include <paths.h>
22 #include <pthread.h>
23 #include <signal.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/eventfd.h>
27 #include <sys/mount.h>
28 #include <sys/signalfd.h>
29 #include <sys/types.h>
30 #include <sys/utsname.h>
31 #include <unistd.h>
32 
33 #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
34 #include <sys/_system_properties.h>
35 
36 #include <filesystem>
37 #include <fstream>
38 #include <functional>
39 #include <iostream>
40 #include <map>
41 #include <memory>
42 #include <mutex>
43 #include <optional>
44 #include <thread>
45 #include <vector>
46 
47 #include <android-base/chrono_utils.h>
48 #include <android-base/file.h>
49 #include <android-base/logging.h>
50 #include <android-base/parseint.h>
51 #include <android-base/properties.h>
52 #include <android-base/stringprintf.h>
53 #include <android-base/strings.h>
54 #include <android-base/thread_annotations.h>
55 #include <fs_avb/fs_avb.h>
56 #include <fs_mgr_vendor_overlay.h>
57 #include <libavb/libavb.h>
58 #include <libgsi/libgsi.h>
59 #include <libsnapshot/snapshot.h>
60 #include <logwrap/logwrap.h>
61 #include <processgroup/processgroup.h>
62 #include <processgroup/setup.h>
63 #include <selinux/android.h>
64 #include <unwindstack/AndroidUnwinder.h>
65 
66 #include "action.h"
67 #include "action_manager.h"
68 #include "action_parser.h"
69 #include "apex_init_util.h"
70 #include "epoll.h"
71 #include "first_stage_init.h"
72 #include "first_stage_mount.h"
73 #include "import_parser.h"
74 #include "keychords.h"
75 #include "lmkd_service.h"
76 #include "mount_handler.h"
77 #include "mount_namespace.h"
78 #include "property_service.h"
79 #include "proto_utils.h"
80 #include "reboot.h"
81 #include "reboot_utils.h"
82 #include "second_stage_resources.h"
83 #include "security.h"
84 #include "selabel.h"
85 #include "selinux.h"
86 #include "service.h"
87 #include "service_list.h"
88 #include "service_parser.h"
89 #include "sigchld_handler.h"
90 #include "snapuserd_transition.h"
91 #include "subcontext.h"
92 #include "system/core/init/property_service.pb.h"
93 #include "util.h"
94 
95 #ifndef RECOVERY
96 #include "com_android_apex.h"
97 #endif  // RECOVERY
98 
99 using namespace std::chrono_literals;
100 using namespace std::string_literals;
101 
102 using android::base::boot_clock;
103 using android::base::ConsumePrefix;
104 using android::base::GetProperty;
105 using android::base::ReadFileToString;
106 using android::base::SetProperty;
107 using android::base::StringPrintf;
108 using android::base::Timer;
109 using android::base::Trim;
110 using android::base::unique_fd;
111 using android::fs_mgr::AvbHandle;
112 using android::snapshot::SnapshotManager;
113 
114 namespace android {
115 namespace init {
116 
117 static int property_triggers_enabled = 0;
118 
119 static int sigterm_fd = -1;
120 static int property_fd = -1;
121 
122 struct PendingControlMessage {
123     std::string message;
124     std::string name;
125     pid_t pid;
126     int fd;
127 };
128 static std::mutex pending_control_messages_lock;
129 static std::queue<PendingControlMessage> pending_control_messages;
130 
131 // Init epolls various FDs to wait for various inputs.  It previously waited on property changes
132 // with a blocking socket that contained the information related to the change, however, it was easy
133 // to fill that socket and deadlock the system.  Now we use locks to handle the property changes
134 // directly in the property thread, however we still must wake the epoll to inform init that there
135 // is a change to process, so we use this FD.  It is non-blocking, since we do not care how many
136 // times WakeMainInitThread() is called, only that the epoll will wake.
137 static int wake_main_thread_fd = -1;
InstallInitNotifier(Epoll * epoll)138 static void InstallInitNotifier(Epoll* epoll) {
139     wake_main_thread_fd = eventfd(0, EFD_CLOEXEC);
140     if (wake_main_thread_fd == -1) {
141         PLOG(FATAL) << "Failed to create eventfd for waking init";
142     }
143     auto clear_eventfd = [] {
144         uint64_t counter;
145         TEMP_FAILURE_RETRY(read(wake_main_thread_fd, &counter, sizeof(counter)));
146     };
147 
148     if (auto result = epoll->RegisterHandler(wake_main_thread_fd, clear_eventfd); !result.ok()) {
149         LOG(FATAL) << result.error();
150     }
151 }
152 
WakeMainInitThread()153 static void WakeMainInitThread() {
154     uint64_t counter = 1;
155     TEMP_FAILURE_RETRY(write(wake_main_thread_fd, &counter, sizeof(counter)));
156 }
157 
158 static class PropWaiterState {
159   public:
StartWaiting(const char * name,const char * value)160     bool StartWaiting(const char* name, const char* value) {
161         auto lock = std::lock_guard{lock_};
162         if (waiting_for_prop_) {
163             return false;
164         }
165         if (GetProperty(name, "") != value) {
166             // Current property value is not equal to expected value
167             wait_prop_name_ = name;
168             wait_prop_value_ = value;
169             waiting_for_prop_.reset(new Timer());
170         } else {
171             LOG(INFO) << "start_waiting_for_property(\"" << name << "\", \"" << value
172                       << "\"): already set";
173         }
174         return true;
175     }
176 
ResetWaitForProp()177     void ResetWaitForProp() {
178         auto lock = std::lock_guard{lock_};
179         ResetWaitForPropLocked();
180     }
181 
CheckAndResetWait(const std::string & name,const std::string & value)182     void CheckAndResetWait(const std::string& name, const std::string& value) {
183         auto lock = std::lock_guard{lock_};
184         // We always record how long init waited for ueventd to tell us cold boot finished.
185         // If we aren't waiting on this property, it means that ueventd finished before we even
186         // started to wait.
187         if (name == kColdBootDoneProp) {
188             auto time_waited = waiting_for_prop_ ? waiting_for_prop_->duration().count() : 0;
189             std::thread([time_waited] {
190                 SetProperty("ro.boottime.init.cold_boot_wait", std::to_string(time_waited));
191             }).detach();
192         }
193 
194         if (waiting_for_prop_) {
195             if (wait_prop_name_ == name && wait_prop_value_ == value) {
196                 LOG(INFO) << "Wait for property '" << wait_prop_name_ << "=" << wait_prop_value_
197                           << "' took " << *waiting_for_prop_;
198                 ResetWaitForPropLocked();
199                 WakeMainInitThread();
200             }
201         }
202     }
203 
204     // This is not thread safe because it releases the lock when it returns, so the waiting state
205     // may change.  However, we only use this function to prevent running commands in the main
206     // thread loop when we are waiting, so we do not care about false positives; only false
207     // negatives.  StartWaiting() and this function are always called from the same thread, so false
208     // negatives are not possible and therefore we're okay.
MightBeWaiting()209     bool MightBeWaiting() {
210         auto lock = std::lock_guard{lock_};
211         return static_cast<bool>(waiting_for_prop_);
212     }
213 
214   private:
ResetWaitForPropLocked()215     void ResetWaitForPropLocked() EXCLUSIVE_LOCKS_REQUIRED(lock_) {
216         wait_prop_name_.clear();
217         wait_prop_value_.clear();
218         waiting_for_prop_.reset();
219     }
220 
221     std::mutex lock_;
GUARDED_BY(lock_)222     GUARDED_BY(lock_) std::unique_ptr<Timer> waiting_for_prop_{nullptr};
223     GUARDED_BY(lock_) std::string wait_prop_name_;
224     GUARDED_BY(lock_) std::string wait_prop_value_;
225 
226 } prop_waiter_state;
227 
start_waiting_for_property(const char * name,const char * value)228 bool start_waiting_for_property(const char* name, const char* value) {
229     return prop_waiter_state.StartWaiting(name, value);
230 }
231 
ResetWaitForProp()232 void ResetWaitForProp() {
233     prop_waiter_state.ResetWaitForProp();
234 }
235 
236 static class ShutdownState {
237   public:
TriggerShutdown(const std::string & command)238     void TriggerShutdown(const std::string& command) {
239         // We can't call HandlePowerctlMessage() directly in this function,
240         // because it modifies the contents of the action queue, which can cause the action queue
241         // to get into a bad state if this function is called from a command being executed by the
242         // action queue.  Instead we set this flag and ensure that shutdown happens before the next
243         // command is run in the main init loop.
244         auto lock = std::lock_guard{shutdown_command_lock_};
245         shutdown_command_ = command;
246         do_shutdown_ = true;
247         WakeMainInitThread();
248     }
249 
CheckShutdown()250     std::optional<std::string> CheckShutdown() __attribute__((warn_unused_result)) {
251         auto lock = std::lock_guard{shutdown_command_lock_};
252         if (do_shutdown_ && !IsShuttingDown()) {
253             do_shutdown_ = false;
254             return shutdown_command_;
255         }
256         return {};
257     }
258 
259   private:
260     std::mutex shutdown_command_lock_;
261     std::string shutdown_command_ GUARDED_BY(shutdown_command_lock_);
262     bool do_shutdown_ = false;
263 } shutdown_state;
264 
DumpState()265 void DumpState() {
266     ServiceList::GetInstance().DumpState();
267     ActionManager::GetInstance().DumpState();
268 }
269 
CreateParser(ActionManager & action_manager,ServiceList & service_list)270 Parser CreateParser(ActionManager& action_manager, ServiceList& service_list) {
271     Parser parser;
272 
273     parser.AddSectionParser("service", std::make_unique<ServiceParser>(
274                                                &service_list, GetSubcontext(), std::nullopt));
275     parser.AddSectionParser("on", std::make_unique<ActionParser>(&action_manager, GetSubcontext()));
276     parser.AddSectionParser("import", std::make_unique<ImportParser>(&parser));
277 
278     return parser;
279 }
280 
281 #ifndef RECOVERY
282 template <typename T>
283 struct LibXmlErrorHandler {
284     T handler_;
285     template <typename Handler>
LibXmlErrorHandlerandroid::init::LibXmlErrorHandler286     LibXmlErrorHandler(Handler&& handler) : handler_(std::move(handler)) {
287         xmlSetGenericErrorFunc(nullptr, &ErrorHandler);
288     }
~LibXmlErrorHandlerandroid::init::LibXmlErrorHandler289     ~LibXmlErrorHandler() { xmlSetGenericErrorFunc(nullptr, nullptr); }
ErrorHandlerandroid::init::LibXmlErrorHandler290     static void ErrorHandler(void*, const char* msg, ...) {
291         va_list args;
292         va_start(args, msg);
293         char* formatted;
294         if (vasprintf(&formatted, msg, args) >= 0) {
295             LOG(ERROR) << formatted;
296         }
297         free(formatted);
298         va_end(args);
299     }
300 };
301 
302 template <typename Handler>
303 LibXmlErrorHandler(Handler&&) -> LibXmlErrorHandler<Handler>;
304 #endif  // RECOVERY
305 
306 // Returns a Parser that accepts scripts from APEX modules. It supports `service` and `on`.
CreateApexConfigParser(ActionManager & action_manager,ServiceList & service_list)307 Parser CreateApexConfigParser(ActionManager& action_manager, ServiceList& service_list) {
308     Parser parser;
309     auto subcontext = GetSubcontext();
310 #ifndef RECOVERY
311     if (subcontext) {
312         const auto apex_info_list_file = "/apex/apex-info-list.xml";
313         auto error_handler = LibXmlErrorHandler([&](const auto& error_message) {
314             LOG(ERROR) << "Failed to read " << apex_info_list_file << ":" << error_message;
315         });
316         const auto apex_info_list = com::android::apex::readApexInfoList(apex_info_list_file);
317         if (apex_info_list.has_value()) {
318             std::vector<std::string> subcontext_apexes;
319             for (const auto& info : apex_info_list->getApexInfo()) {
320                 if (info.hasPreinstalledModulePath() &&
321                     subcontext->PathMatchesSubcontext(info.getPreinstalledModulePath())) {
322                     subcontext_apexes.push_back(info.getModuleName());
323                 }
324             }
325             subcontext->SetApexList(std::move(subcontext_apexes));
326         }
327     }
328 #endif  // RECOVERY
329     parser.AddSectionParser("service",
330                             std::make_unique<ServiceParser>(&service_list, subcontext,
331                             std::nullopt));
332     parser.AddSectionParser("on", std::make_unique<ActionParser>(&action_manager, subcontext));
333 
334     return parser;
335 }
336 
LoadBootScripts(ActionManager & action_manager,ServiceList & service_list)337 static void LoadBootScripts(ActionManager& action_manager, ServiceList& service_list) {
338     Parser parser = CreateParser(action_manager, service_list);
339 
340     std::string bootscript = GetProperty("ro.boot.init_rc", "");
341     if (bootscript.empty()) {
342         parser.ParseConfig("/system/etc/init/hw/init.rc");
343         if (!parser.ParseConfig("/system/etc/init")) {
344             late_import_paths.emplace_back("/system/etc/init");
345         }
346         // late_import is available only in Q and earlier release. As we don't
347         // have system_ext in those versions, skip late_import for system_ext.
348         parser.ParseConfig("/system_ext/etc/init");
349         if (!parser.ParseConfig("/vendor/etc/init")) {
350             late_import_paths.emplace_back("/vendor/etc/init");
351         }
352         if (!parser.ParseConfig("/odm/etc/init")) {
353             late_import_paths.emplace_back("/odm/etc/init");
354         }
355         if (!parser.ParseConfig("/product/etc/init")) {
356             late_import_paths.emplace_back("/product/etc/init");
357         }
358     } else {
359         parser.ParseConfig(bootscript);
360     }
361 }
362 
PropertyChanged(const std::string & name,const std::string & value)363 void PropertyChanged(const std::string& name, const std::string& value) {
364     // If the property is sys.powerctl, we bypass the event queue and immediately handle it.
365     // This is to ensure that init will always and immediately shutdown/reboot, regardless of
366     // if there are other pending events to process or if init is waiting on an exec service or
367     // waiting on a property.
368     // In non-thermal-shutdown case, 'shutdown' trigger will be fired to let device specific
369     // commands to be executed.
370     if (name == "sys.powerctl") {
371         trigger_shutdown(value);
372     }
373 
374     if (property_triggers_enabled) {
375         ActionManager::GetInstance().QueuePropertyChange(name, value);
376         WakeMainInitThread();
377     }
378 
379     prop_waiter_state.CheckAndResetWait(name, value);
380 }
381 
HandleProcessActions()382 static std::optional<boot_clock::time_point> HandleProcessActions() {
383     std::optional<boot_clock::time_point> next_process_action_time;
384     for (const auto& s : ServiceList::GetInstance()) {
385         if ((s->flags() & SVC_RUNNING) && s->timeout_period()) {
386             auto timeout_time = s->time_started() + *s->timeout_period();
387             if (boot_clock::now() > timeout_time) {
388                 s->Timeout();
389             } else {
390                 if (!next_process_action_time || timeout_time < *next_process_action_time) {
391                     next_process_action_time = timeout_time;
392                 }
393             }
394         }
395 
396         if (!(s->flags() & SVC_RESTARTING)) continue;
397 
398         auto restart_time = s->time_started() + s->restart_period();
399         if (boot_clock::now() > restart_time) {
400             if (auto result = s->Start(); !result.ok()) {
401                 LOG(ERROR) << "Could not restart process '" << s->name() << "': " << result.error();
402             }
403         } else {
404             if (!next_process_action_time || restart_time < *next_process_action_time) {
405                 next_process_action_time = restart_time;
406             }
407         }
408     }
409     return next_process_action_time;
410 }
411 
DoControlStart(Service * service)412 static Result<void> DoControlStart(Service* service) {
413     return service->Start();
414 }
415 
DoControlStop(Service * service)416 static Result<void> DoControlStop(Service* service) {
417     service->Stop();
418     return {};
419 }
420 
DoControlRestart(Service * service)421 static Result<void> DoControlRestart(Service* service) {
422     service->Restart();
423     return {};
424 }
425 
StopServicesFromApex(const std::string & apex_name)426 int StopServicesFromApex(const std::string& apex_name) {
427     auto services = ServiceList::GetInstance().FindServicesByApexName(apex_name);
428     if (services.empty()) {
429         LOG(INFO) << "No service found for APEX: " << apex_name;
430         return 0;
431     }
432     std::set<std::string> service_names;
433     for (const auto& service : services) {
434         service_names.emplace(service->name());
435     }
436     constexpr std::chrono::milliseconds kServiceStopTimeout = 10s;
437     int still_running = StopServicesAndLogViolations(service_names, kServiceStopTimeout,
438                         true /*SIGTERM*/);
439     // Send SIGKILL to ones that didn't terminate cleanly.
440     if (still_running > 0) {
441         still_running = StopServicesAndLogViolations(service_names, 0ms, false /*SIGKILL*/);
442     }
443     return still_running;
444 }
445 
RemoveServiceAndActionFromApex(const std::string & apex_name)446 void RemoveServiceAndActionFromApex(const std::string& apex_name) {
447     // Remove services and actions that match apex name
448     ActionManager::GetInstance().RemoveActionIf([&](const std::unique_ptr<Action>& action) -> bool {
449         if (GetApexNameFromFileName(action->filename()) == apex_name) {
450             return true;
451         }
452         return false;
453     });
454     ServiceList::GetInstance().RemoveServiceIf([&](const std::unique_ptr<Service>& s) -> bool {
455         if (GetApexNameFromFileName(s->filename()) == apex_name) {
456             return true;
457         }
458         return false;
459     });
460 }
461 
DoUnloadApex(const std::string & apex_name)462 static Result<void> DoUnloadApex(const std::string& apex_name) {
463     if (StopServicesFromApex(apex_name) > 0) {
464         return Error() << "Unable to stop all service from " << apex_name;
465     }
466     RemoveServiceAndActionFromApex(apex_name);
467     return {};
468 }
469 
UpdateApexLinkerConfig(const std::string & apex_name)470 static Result<void> UpdateApexLinkerConfig(const std::string& apex_name) {
471     // Do not invoke linkerconfig when there's no bin/ in the apex.
472     const std::string bin_path = "/apex/" + apex_name + "/bin";
473     if (access(bin_path.c_str(), R_OK) != 0) {
474         return {};
475     }
476     const char* linkerconfig_binary = "/apex/com.android.runtime/bin/linkerconfig";
477     const char* linkerconfig_target = "/linkerconfig";
478     const char* arguments[] = {linkerconfig_binary, "--target", linkerconfig_target, "--apex",
479                                apex_name.c_str(),   "--strict"};
480 
481     if (logwrap_fork_execvp(arraysize(arguments), arguments, nullptr, false, LOG_KLOG, false,
482                             nullptr) != 0) {
483         return ErrnoError() << "failed to execute linkerconfig";
484     }
485     LOG(INFO) << "Generated linker configuration for " << apex_name;
486     return {};
487 }
488 
DoLoadApex(const std::string & apex_name)489 static Result<void> DoLoadApex(const std::string& apex_name) {
490     if (auto result = ParseRcScriptsFromApex(apex_name); !result.ok()) {
491         return result.error();
492     }
493 
494     if (auto result = UpdateApexLinkerConfig(apex_name); !result.ok()) {
495         return result.error();
496     }
497 
498     return {};
499 }
500 
501 enum class ControlTarget {
502     SERVICE,    // function gets called for the named service
503     INTERFACE,  // action gets called for every service that holds this interface
504 };
505 
506 using ControlMessageFunction = std::function<Result<void>(Service*)>;
507 
GetControlMessageMap()508 static const std::map<std::string, ControlMessageFunction, std::less<>>& GetControlMessageMap() {
509     // clang-format off
510     static const std::map<std::string, ControlMessageFunction, std::less<>> control_message_functions = {
511         {"sigstop_on",        [](auto* service) { service->set_sigstop(true); return Result<void>{}; }},
512         {"sigstop_off",       [](auto* service) { service->set_sigstop(false); return Result<void>{}; }},
513         {"oneshot_on",        [](auto* service) { service->set_oneshot(true); return Result<void>{}; }},
514         {"oneshot_off",       [](auto* service) { service->set_oneshot(false); return Result<void>{}; }},
515         {"start",             DoControlStart},
516         {"stop",              DoControlStop},
517         {"restart",           DoControlRestart},
518     };
519     // clang-format on
520 
521     return control_message_functions;
522 }
523 
HandleApexControlMessage(std::string_view action,const std::string & name,std::string_view message)524 static Result<void> HandleApexControlMessage(std::string_view action, const std::string& name,
525                                              std::string_view message) {
526     if (action == "load") {
527         return DoLoadApex(name);
528     } else if (action == "unload") {
529         return DoUnloadApex(name);
530     } else {
531         return Error() << "Unknown control msg '" << message << "'";
532     }
533 }
534 
HandleControlMessage(std::string_view message,const std::string & name,pid_t from_pid)535 static bool HandleControlMessage(std::string_view message, const std::string& name,
536                                  pid_t from_pid) {
537     std::string cmdline_path = StringPrintf("proc/%d/cmdline", from_pid);
538     std::string process_cmdline;
539     if (ReadFileToString(cmdline_path, &process_cmdline)) {
540         std::replace(process_cmdline.begin(), process_cmdline.end(), '\0', ' ');
541         process_cmdline = Trim(process_cmdline);
542     } else {
543         process_cmdline = "unknown process";
544     }
545 
546     auto action = message;
547     if (ConsumePrefix(&action, "apex_")) {
548         if (auto result = HandleApexControlMessage(action, name, message); !result.ok()) {
549             LOG(ERROR) << "Control message: Could not ctl." << message << " for '" << name
550                        << "' from pid: " << from_pid << " (" << process_cmdline
551                        << "): " << result.error();
552             return false;
553         }
554         LOG(INFO) << "Control message: Processed ctl." << message << " for '" << name
555                   << "' from pid: " << from_pid << " (" << process_cmdline << ")";
556         return true;
557     }
558 
559     Service* service = nullptr;
560     if (ConsumePrefix(&action, "interface_")) {
561         service = ServiceList::GetInstance().FindInterface(name);
562     } else {
563         service = ServiceList::GetInstance().FindService(name);
564     }
565 
566     if (service == nullptr) {
567         LOG(ERROR) << "Control message: Could not find '" << name << "' for ctl." << message
568                    << " from pid: " << from_pid << " (" << process_cmdline << ")";
569         return false;
570     }
571 
572     const auto& map = GetControlMessageMap();
573     const auto it = map.find(action);
574     if (it == map.end()) {
575         LOG(ERROR) << "Unknown control msg '" << message << "'";
576         return false;
577     }
578     const auto& function = it->second;
579 
580     if (auto result = function(service); !result.ok()) {
581         LOG(ERROR) << "Control message: Could not ctl." << message << " for '" << name
582                    << "' from pid: " << from_pid << " (" << process_cmdline
583                    << "): " << result.error();
584         return false;
585     }
586 
587     LOG(INFO) << "Control message: Processed ctl." << message << " for '" << name
588               << "' from pid: " << from_pid << " (" << process_cmdline << ")";
589     return true;
590 }
591 
QueueControlMessage(const std::string & message,const std::string & name,pid_t pid,int fd)592 bool QueueControlMessage(const std::string& message, const std::string& name, pid_t pid, int fd) {
593     auto lock = std::lock_guard{pending_control_messages_lock};
594     if (pending_control_messages.size() > 100) {
595         LOG(ERROR) << "Too many pending control messages, dropped '" << message << "' for '" << name
596                    << "' from pid: " << pid;
597         return false;
598     }
599     pending_control_messages.push({message, name, pid, fd});
600     WakeMainInitThread();
601     return true;
602 }
603 
HandleControlMessages()604 static void HandleControlMessages() {
605     auto lock = std::unique_lock{pending_control_messages_lock};
606     // Init historically would only execute handle one property message, including control messages
607     // in each iteration of its main loop.  We retain this behavior here to prevent starvation of
608     // other actions in the main loop.
609     if (!pending_control_messages.empty()) {
610         auto control_message = pending_control_messages.front();
611         pending_control_messages.pop();
612         lock.unlock();
613 
614         bool success = HandleControlMessage(control_message.message, control_message.name,
615                                             control_message.pid);
616 
617         uint32_t response = success ? PROP_SUCCESS : PROP_ERROR_HANDLE_CONTROL_MESSAGE;
618         if (control_message.fd != -1) {
619             TEMP_FAILURE_RETRY(send(control_message.fd, &response, sizeof(response), 0));
620             close(control_message.fd);
621         }
622         lock.lock();
623     }
624     // If we still have items to process, make sure we wake back up to do so.
625     if (!pending_control_messages.empty()) {
626         WakeMainInitThread();
627     }
628 }
629 
wait_for_coldboot_done_action(const BuiltinArguments & args)630 static Result<void> wait_for_coldboot_done_action(const BuiltinArguments& args) {
631     if (!prop_waiter_state.StartWaiting(kColdBootDoneProp, "true")) {
632         LOG(FATAL) << "Could not wait for '" << kColdBootDoneProp << "'";
633     }
634 
635     return {};
636 }
637 
SetupCgroupsAction(const BuiltinArguments &)638 static Result<void> SetupCgroupsAction(const BuiltinArguments&) {
639     if (!CgroupsAvailable()) {
640         LOG(INFO) << "Cgroups support in kernel is not enabled";
641         return {};
642     }
643     // Have to create <CGROUPS_RC_DIR> using make_dir function
644     // for appropriate sepolicy to be set for it
645     make_dir(android::base::Dirname(CGROUPS_RC_PATH), 0711);
646     if (!CgroupSetup()) {
647         return ErrnoError() << "Failed to setup cgroups";
648     }
649 
650     return {};
651 }
652 
export_oem_lock_status()653 static void export_oem_lock_status() {
654     if (!android::base::GetBoolProperty("ro.oem_unlock_supported", false)) {
655         return;
656     }
657     SetProperty(
658             "ro.boot.flash.locked",
659             android::base::GetProperty("ro.boot.verifiedbootstate", "") == "orange" ? "0" : "1");
660 }
661 
property_enable_triggers_action(const BuiltinArguments & args)662 static Result<void> property_enable_triggers_action(const BuiltinArguments& args) {
663     /* Enable property triggers. */
664     property_triggers_enabled = 1;
665     return {};
666 }
667 
queue_property_triggers_action(const BuiltinArguments & args)668 static Result<void> queue_property_triggers_action(const BuiltinArguments& args) {
669     ActionManager::GetInstance().QueueBuiltinAction(property_enable_triggers_action, "enable_property_trigger");
670     ActionManager::GetInstance().QueueAllPropertyActions();
671     return {};
672 }
673 
674 // Set the UDC controller for the ConfigFS USB Gadgets.
675 // Read the UDC controller in use from "/sys/class/udc".
676 // In case of multiple UDC controllers select the first one.
SetUsbController()677 static void SetUsbController() {
678     static auto controller_set = false;
679     if (controller_set) return;
680     std::unique_ptr<DIR, decltype(&closedir)>dir(opendir("/sys/class/udc"), closedir);
681     if (!dir) return;
682 
683     dirent* dp;
684     while ((dp = readdir(dir.get())) != nullptr) {
685         if (dp->d_name[0] == '.') continue;
686 
687         SetProperty("sys.usb.controller", dp->d_name);
688         controller_set = true;
689         break;
690     }
691 }
692 
693 /// Set ro.kernel.version property to contain the major.minor pair as returned
694 /// by uname(2).
SetKernelVersion()695 static void SetKernelVersion() {
696     struct utsname uts;
697     unsigned int major, minor;
698 
699     if ((uname(&uts) != 0) || (sscanf(uts.release, "%u.%u", &major, &minor) != 2)) {
700         LOG(ERROR) << "Could not parse the kernel version from uname";
701         return;
702     }
703     SetProperty("ro.kernel.version", android::base::StringPrintf("%u.%u", major, minor));
704 }
705 
HandleSigtermSignal(const signalfd_siginfo & siginfo)706 static void HandleSigtermSignal(const signalfd_siginfo& siginfo) {
707     if (siginfo.ssi_pid != 0) {
708         // Drop any userspace SIGTERM requests.
709         LOG(DEBUG) << "Ignoring SIGTERM from pid " << siginfo.ssi_pid;
710         return;
711     }
712 
713     HandlePowerctlMessage("shutdown,container");
714 }
715 
HandleSignalFd(int signal)716 static void HandleSignalFd(int signal) {
717     signalfd_siginfo siginfo;
718     const int signal_fd = signal == SIGCHLD ? Service::GetSigchldFd() : sigterm_fd;
719     ssize_t bytes_read = TEMP_FAILURE_RETRY(read(signal_fd, &siginfo, sizeof(siginfo)));
720     if (bytes_read != sizeof(siginfo)) {
721         PLOG(ERROR) << "Failed to read siginfo from signal_fd";
722         return;
723     }
724 
725     switch (siginfo.ssi_signo) {
726         case SIGCHLD:
727             ReapAnyOutstandingChildren();
728             break;
729         case SIGTERM:
730             HandleSigtermSignal(siginfo);
731             break;
732         default:
733             LOG(ERROR) << "signal_fd: received unexpected signal " << siginfo.ssi_signo;
734             break;
735     }
736 }
737 
UnblockSignals()738 static void UnblockSignals() {
739     const struct sigaction act { .sa_handler = SIG_DFL };
740     sigaction(SIGCHLD, &act, nullptr);
741 
742     sigset_t mask;
743     sigemptyset(&mask);
744     sigaddset(&mask, SIGCHLD);
745     sigaddset(&mask, SIGTERM);
746 
747     if (sigprocmask(SIG_UNBLOCK, &mask, nullptr) == -1) {
748         PLOG(FATAL) << "failed to unblock signals for PID " << getpid();
749     }
750 }
751 
RegisterSignalFd(Epoll * epoll,int signal,int fd)752 static Result<void> RegisterSignalFd(Epoll* epoll, int signal, int fd) {
753     return epoll->RegisterHandler(
754             fd, [signal]() { HandleSignalFd(signal); }, EPOLLIN | EPOLLPRI);
755 }
756 
CreateAndRegisterSignalFd(Epoll * epoll,int signal)757 static Result<int> CreateAndRegisterSignalFd(Epoll* epoll, int signal) {
758     sigset_t mask;
759     sigemptyset(&mask);
760     sigaddset(&mask, signal);
761     if (sigprocmask(SIG_BLOCK, &mask, nullptr) == -1) {
762         return ErrnoError() << "failed to block signal " << signal;
763     }
764 
765     unique_fd signal_fd(signalfd(-1, &mask, SFD_CLOEXEC));
766     if (signal_fd.get() < 0) {
767         return ErrnoError() << "failed to create signalfd for signal " << signal;
768     }
769     OR_RETURN(RegisterSignalFd(epoll, signal, signal_fd.get()));
770 
771     return signal_fd.release();
772 }
773 
InstallSignalFdHandler(Epoll * epoll)774 static void InstallSignalFdHandler(Epoll* epoll) {
775     // Applying SA_NOCLDSTOP to a defaulted SIGCHLD handler prevents the signalfd from receiving
776     // SIGCHLD when a child process stops or continues (b/77867680#comment9).
777     const struct sigaction act { .sa_flags = SA_NOCLDSTOP, .sa_handler = SIG_DFL };
778     sigaction(SIGCHLD, &act, nullptr);
779 
780     // Register a handler to unblock signals in the child processes.
781     const int result = pthread_atfork(nullptr, nullptr, &UnblockSignals);
782     if (result != 0) {
783         LOG(FATAL) << "Failed to register a fork handler: " << strerror(result);
784     }
785 
786     Result<void> cs_result = RegisterSignalFd(epoll, SIGCHLD, Service::GetSigchldFd());
787     if (!cs_result.ok()) {
788         PLOG(FATAL) << cs_result.error();
789     }
790 
791     if (!IsRebootCapable()) {
792         Result<int> cs_result = CreateAndRegisterSignalFd(epoll, SIGTERM);
793         if (!cs_result.ok()) {
794             PLOG(FATAL) << cs_result.error();
795         }
796         sigterm_fd = cs_result.value();
797     }
798 }
799 
HandleKeychord(const std::vector<int> & keycodes)800 void HandleKeychord(const std::vector<int>& keycodes) {
801     // Only handle keychords if adb is enabled.
802     std::string adb_enabled = android::base::GetProperty("init.svc.adbd", "");
803     if (adb_enabled != "running") {
804         LOG(WARNING) << "Not starting service for keychord " << android::base::Join(keycodes, ' ')
805                      << " because ADB is disabled";
806         return;
807     }
808 
809     auto found = false;
810     for (const auto& service : ServiceList::GetInstance()) {
811         auto svc = service.get();
812         if (svc->keycodes() == keycodes) {
813             found = true;
814             LOG(INFO) << "Starting service '" << svc->name() << "' from keychord "
815                       << android::base::Join(keycodes, ' ');
816             if (auto result = svc->Start(); !result.ok()) {
817                 LOG(ERROR) << "Could not start service '" << svc->name() << "' from keychord "
818                            << android::base::Join(keycodes, ' ') << ": " << result.error();
819             }
820         }
821     }
822     if (!found) {
823         LOG(ERROR) << "Service for keychord " << android::base::Join(keycodes, ' ') << " not found";
824     }
825 }
826 
UmountDebugRamdisk()827 static void UmountDebugRamdisk() {
828     if (umount("/debug_ramdisk") != 0) {
829         PLOG(ERROR) << "Failed to umount /debug_ramdisk";
830     }
831 }
832 
UmountSecondStageRes()833 static void UmountSecondStageRes() {
834     if (umount(kSecondStageRes) != 0) {
835         PLOG(ERROR) << "Failed to umount " << kSecondStageRes;
836     }
837 }
838 
MountExtraFilesystems()839 static void MountExtraFilesystems() {
840 #define CHECKCALL(x) \
841     if ((x) != 0) PLOG(FATAL) << #x " failed.";
842 
843     // /apex is used to mount APEXes
844     CHECKCALL(mount("tmpfs", "/apex", "tmpfs", MS_NOEXEC | MS_NOSUID | MS_NODEV,
845                     "mode=0755,uid=0,gid=0"));
846 
847     if (NeedsTwoMountNamespaces()) {
848         // /bootstrap-apex is used to mount "bootstrap" APEXes.
849         CHECKCALL(mount("tmpfs", "/bootstrap-apex", "tmpfs", MS_NOEXEC | MS_NOSUID | MS_NODEV,
850                         "mode=0755,uid=0,gid=0"));
851     }
852 
853     // /linkerconfig is used to keep generated linker configuration
854     CHECKCALL(mount("tmpfs", "/linkerconfig", "tmpfs", MS_NOEXEC | MS_NOSUID | MS_NODEV,
855                     "mode=0755,uid=0,gid=0"));
856 #undef CHECKCALL
857 }
858 
RecordStageBoottimes(const boot_clock::time_point & second_stage_start_time)859 static void RecordStageBoottimes(const boot_clock::time_point& second_stage_start_time) {
860     int64_t first_stage_start_time_ns = -1;
861     if (auto first_stage_start_time_str = getenv(kEnvFirstStageStartedAt);
862         first_stage_start_time_str) {
863         SetProperty("ro.boottime.init", first_stage_start_time_str);
864         android::base::ParseInt(first_stage_start_time_str, &first_stage_start_time_ns);
865     }
866     unsetenv(kEnvFirstStageStartedAt);
867 
868     int64_t selinux_start_time_ns = -1;
869     if (auto selinux_start_time_str = getenv(kEnvSelinuxStartedAt); selinux_start_time_str) {
870         android::base::ParseInt(selinux_start_time_str, &selinux_start_time_ns);
871     }
872     unsetenv(kEnvSelinuxStartedAt);
873 
874     if (selinux_start_time_ns == -1) return;
875     if (first_stage_start_time_ns == -1) return;
876 
877     SetProperty("ro.boottime.init.first_stage",
878                 std::to_string(selinux_start_time_ns - first_stage_start_time_ns));
879     SetProperty("ro.boottime.init.selinux",
880                 std::to_string(second_stage_start_time.time_since_epoch().count() -
881                                selinux_start_time_ns));
882     if (auto init_module_time_str = getenv(kEnvInitModuleDurationMs); init_module_time_str) {
883         SetProperty("ro.boottime.init.modules", init_module_time_str);
884         unsetenv(kEnvInitModuleDurationMs);
885     }
886 }
887 
SendLoadPersistentPropertiesMessage()888 void SendLoadPersistentPropertiesMessage() {
889     auto init_message = InitMessage{};
890     init_message.set_load_persistent_properties(true);
891     if (auto result = SendMessage(property_fd, init_message); !result.ok()) {
892         LOG(ERROR) << "Failed to send load persistent properties message: " << result.error();
893     }
894 }
895 
ConnectEarlyStageSnapuserdAction(const BuiltinArguments & args)896 static Result<void> ConnectEarlyStageSnapuserdAction(const BuiltinArguments& args) {
897     auto pid = GetSnapuserdFirstStagePid();
898     if (!pid) {
899         return {};
900     }
901 
902     auto info = GetSnapuserdFirstStageInfo();
903     if (auto iter = std::find(info.begin(), info.end(), "socket"s); iter == info.end()) {
904         // snapuserd does not support socket handoff, so exit early.
905         return {};
906     }
907 
908     // Socket handoff is supported.
909     auto svc = ServiceList::GetInstance().FindService("snapuserd");
910     if (!svc) {
911         LOG(FATAL) << "Failed to find snapuserd service entry";
912     }
913 
914     svc->SetShutdownCritical();
915     svc->SetStartedInFirstStage(*pid);
916 
917     svc = ServiceList::GetInstance().FindService("snapuserd_proxy");
918     if (!svc) {
919         LOG(FATAL) << "Failed find snapuserd_proxy service entry, merge will never initiate";
920     }
921     if (!svc->MarkSocketPersistent("snapuserd")) {
922         LOG(FATAL) << "Could not find snapuserd socket in snapuserd_proxy service entry";
923     }
924     if (auto result = svc->Start(); !result.ok()) {
925         LOG(FATAL) << "Could not start snapuserd_proxy: " << result.error();
926     }
927     return {};
928 }
929 
SecondStageMain(int argc,char ** argv)930 int SecondStageMain(int argc, char** argv) {
931     if (REBOOT_BOOTLOADER_ON_PANIC) {
932         InstallRebootSignalHandlers();
933     }
934 
935     // No threads should be spin up until signalfd
936     // is registered. If the threads are indeed required,
937     // each of these threads _should_ make sure SIGCHLD signal
938     // is blocked. See b/223076262
939     boot_clock::time_point start_time = boot_clock::now();
940 
941     trigger_shutdown = [](const std::string& command) { shutdown_state.TriggerShutdown(command); };
942 
943     SetStdioToDevNull(argv);
944     InitKernelLogging(argv);
945     LOG(INFO) << "init second stage started!";
946 
947     SelinuxSetupKernelLogging();
948 
949     // Update $PATH in the case the second stage init is newer than first stage init, where it is
950     // first set.
951     if (setenv("PATH", _PATH_DEFPATH, 1) != 0) {
952         PLOG(FATAL) << "Could not set $PATH to '" << _PATH_DEFPATH << "' in second stage";
953     }
954 
955     // Init should not crash because of a dependence on any other process, therefore we ignore
956     // SIGPIPE and handle EPIPE at the call site directly.  Note that setting a signal to SIG_IGN
957     // is inherited across exec, but custom signal handlers are not.  Since we do not want to
958     // ignore SIGPIPE for child processes, we set a no-op function for the signal handler instead.
959     {
960         struct sigaction action = {.sa_flags = SA_RESTART};
961         action.sa_handler = [](int) {};
962         sigaction(SIGPIPE, &action, nullptr);
963     }
964 
965     // Set init and its forked children's oom_adj.
966     if (auto result =
967                 WriteFile("/proc/1/oom_score_adj", StringPrintf("%d", DEFAULT_OOM_SCORE_ADJUST));
968         !result.ok()) {
969         LOG(ERROR) << "Unable to write " << DEFAULT_OOM_SCORE_ADJUST
970                    << " to /proc/1/oom_score_adj: " << result.error();
971     }
972 
973     // Indicate that booting is in progress to background fw loaders, etc.
974     close(open("/dev/.booting", O_WRONLY | O_CREAT | O_CLOEXEC, 0000));
975 
976     // See if need to load debug props to allow adb root, when the device is unlocked.
977     const char* force_debuggable_env = getenv("INIT_FORCE_DEBUGGABLE");
978     bool load_debug_prop = false;
979     if (force_debuggable_env && AvbHandle::IsDeviceUnlocked()) {
980         load_debug_prop = "true"s == force_debuggable_env;
981     }
982     unsetenv("INIT_FORCE_DEBUGGABLE");
983 
984     // Umount the debug ramdisk so property service doesn't read .prop files from there, when it
985     // is not meant to.
986     if (!load_debug_prop) {
987         UmountDebugRamdisk();
988     }
989 
990     PropertyInit();
991 
992     // Umount second stage resources after property service has read the .prop files.
993     UmountSecondStageRes();
994 
995     // Umount the debug ramdisk after property service has read the .prop files when it means to.
996     if (load_debug_prop) {
997         UmountDebugRamdisk();
998     }
999 
1000     // Mount extra filesystems required during second stage init
1001     MountExtraFilesystems();
1002 
1003     // Now set up SELinux for second stage.
1004     SelabelInitialize();
1005     SelinuxRestoreContext();
1006 
1007     Epoll epoll;
1008     if (auto result = epoll.Open(); !result.ok()) {
1009         PLOG(FATAL) << result.error();
1010     }
1011 
1012     // We always reap children before responding to the other pending functions. This is to
1013     // prevent a race where other daemons see that a service has exited and ask init to
1014     // start it again via ctl.start before init has reaped it.
1015     epoll.SetFirstCallback(ReapAnyOutstandingChildren);
1016 
1017     InstallSignalFdHandler(&epoll);
1018     InstallInitNotifier(&epoll);
1019     StartPropertyService(&property_fd);
1020 
1021     // Make the time that init stages started available for bootstat to log.
1022     RecordStageBoottimes(start_time);
1023 
1024     // Set libavb version for Framework-only OTA match in Treble build.
1025     if (const char* avb_version = getenv("INIT_AVB_VERSION"); avb_version != nullptr) {
1026         SetProperty("ro.boot.avb_version", avb_version);
1027     }
1028     unsetenv("INIT_AVB_VERSION");
1029 
1030     fs_mgr_vendor_overlay_mount_all();
1031     export_oem_lock_status();
1032     MountHandler mount_handler(&epoll);
1033     SetUsbController();
1034     SetKernelVersion();
1035 
1036     const BuiltinFunctionMap& function_map = GetBuiltinFunctionMap();
1037     Action::set_function_map(&function_map);
1038 
1039     if (!SetupMountNamespaces()) {
1040         PLOG(FATAL) << "SetupMountNamespaces failed";
1041     }
1042 
1043     InitializeSubcontext();
1044 
1045     ActionManager& am = ActionManager::GetInstance();
1046     ServiceList& sm = ServiceList::GetInstance();
1047 
1048     LoadBootScripts(am, sm);
1049 
1050     // Turning this on and letting the INFO logging be discarded adds 0.2s to
1051     // Nexus 9 boot time, so it's disabled by default.
1052     if (false) DumpState();
1053 
1054     // Make the GSI status available before scripts start running.
1055     auto is_running = android::gsi::IsGsiRunning() ? "1" : "0";
1056     SetProperty(gsi::kGsiBootedProp, is_running);
1057     auto is_installed = android::gsi::IsGsiInstalled() ? "1" : "0";
1058     SetProperty(gsi::kGsiInstalledProp, is_installed);
1059     if (android::gsi::IsGsiRunning()) {
1060         std::string dsu_slot;
1061         if (android::gsi::GetActiveDsu(&dsu_slot)) {
1062             SetProperty(gsi::kDsuSlotProp, dsu_slot);
1063         }
1064     }
1065 
1066     am.QueueBuiltinAction(SetupCgroupsAction, "SetupCgroups");
1067     am.QueueBuiltinAction(SetKptrRestrictAction, "SetKptrRestrict");
1068     am.QueueBuiltinAction(TestPerfEventSelinuxAction, "TestPerfEventSelinux");
1069     am.QueueEventTrigger("early-init");
1070     am.QueueBuiltinAction(ConnectEarlyStageSnapuserdAction, "ConnectEarlyStageSnapuserd");
1071 
1072     // Queue an action that waits for coldboot done so we know ueventd has set up all of /dev...
1073     am.QueueBuiltinAction(wait_for_coldboot_done_action, "wait_for_coldboot_done");
1074     // ... so that we can start queuing up actions that require stuff from /dev.
1075     am.QueueBuiltinAction(SetMmapRndBitsAction, "SetMmapRndBits");
1076     Keychords keychords;
1077     am.QueueBuiltinAction(
1078             [&epoll, &keychords](const BuiltinArguments& args) -> Result<void> {
1079                 for (const auto& svc : ServiceList::GetInstance()) {
1080                     keychords.Register(svc->keycodes());
1081                 }
1082                 keychords.Start(&epoll, HandleKeychord);
1083                 return {};
1084             },
1085             "KeychordInit");
1086 
1087     // Trigger all the boot actions to get us started.
1088     am.QueueEventTrigger("init");
1089 
1090     // Don't mount filesystems or start core system services in charger mode.
1091     std::string bootmode = GetProperty("ro.bootmode", "");
1092     if (bootmode == "charger") {
1093         am.QueueEventTrigger("charger");
1094     } else {
1095         am.QueueEventTrigger("late-init");
1096     }
1097 
1098     // Run all property triggers based on current state of the properties.
1099     am.QueueBuiltinAction(queue_property_triggers_action, "queue_property_triggers");
1100 
1101     // Restore prio before main loop
1102     setpriority(PRIO_PROCESS, 0, 0);
1103     while (true) {
1104         // By default, sleep until something happens. Do not convert far_future into
1105         // std::chrono::milliseconds because that would trigger an overflow. The unit of boot_clock
1106         // is 1ns.
1107         const boot_clock::time_point far_future = boot_clock::time_point::max();
1108         boot_clock::time_point next_action_time = far_future;
1109 
1110         auto shutdown_command = shutdown_state.CheckShutdown();
1111         if (shutdown_command) {
1112             LOG(INFO) << "Got shutdown_command '" << *shutdown_command
1113                       << "' Calling HandlePowerctlMessage()";
1114             HandlePowerctlMessage(*shutdown_command);
1115         }
1116 
1117         if (!(prop_waiter_state.MightBeWaiting() || Service::is_exec_service_running())) {
1118             am.ExecuteOneCommand();
1119             // If there's more work to do, wake up again immediately.
1120             if (am.HasMoreCommands()) {
1121                 next_action_time = boot_clock::now();
1122             }
1123         }
1124         // Since the above code examined pending actions, no new actions must be
1125         // queued by the code between this line and the Epoll::Wait() call below
1126         // without calling WakeMainInitThread().
1127         if (!IsShuttingDown()) {
1128             auto next_process_action_time = HandleProcessActions();
1129 
1130             // If there's a process that needs restarting, wake up in time for that.
1131             if (next_process_action_time) {
1132                 next_action_time = std::min(next_action_time, *next_process_action_time);
1133             }
1134         }
1135 
1136         std::optional<std::chrono::milliseconds> epoll_timeout;
1137         if (next_action_time != far_future) {
1138             epoll_timeout = std::chrono::ceil<std::chrono::milliseconds>(
1139                     std::max(next_action_time - boot_clock::now(), 0ns));
1140         }
1141         auto epoll_result = epoll.Wait(epoll_timeout);
1142         if (!epoll_result.ok()) {
1143             LOG(ERROR) << epoll_result.error();
1144         }
1145         if (!IsShuttingDown()) {
1146             HandleControlMessages();
1147             SetUsbController();
1148         }
1149     }
1150 
1151     return 0;
1152 }
1153 
1154 }  // namespace init
1155 }  // namespace android
1156