• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <keyutils.h>
58 #include <libavb/libavb.h>
59 #include <libgsi/libgsi.h>
60 #include <libsnapshot/snapshot.h>
61 #include <logwrap/logwrap.h>
62 #include <processgroup/processgroup.h>
63 #include <processgroup/setup.h>
64 #include <selinux/android.h>
65 #include <unwindstack/AndroidUnwinder.h>
66 
67 #include "action.h"
68 #include "action_manager.h"
69 #include "action_parser.h"
70 #include "apex_init_util.h"
71 #include "epoll.h"
72 #include "first_stage_init.h"
73 #include "first_stage_mount.h"
74 #include "import_parser.h"
75 #include "keychords.h"
76 #include "lmkd_service.h"
77 #include "mount_handler.h"
78 #include "mount_namespace.h"
79 #include "property_service.h"
80 #include "proto_utils.h"
81 #include "reboot.h"
82 #include "reboot_utils.h"
83 #include "second_stage_resources.h"
84 #include "security.h"
85 #include "selabel.h"
86 #include "selinux.h"
87 #include "service.h"
88 #include "service_list.h"
89 #include "service_parser.h"
90 #include "sigchld_handler.h"
91 #include "snapuserd_transition.h"
92 #include "subcontext.h"
93 #include "system/core/init/property_service.pb.h"
94 #include "util.h"
95 
96 #ifndef RECOVERY
97 #include "com_android_apex.h"
98 #endif  // RECOVERY
99 
100 using namespace std::chrono_literals;
101 using namespace std::string_literals;
102 
103 using android::base::boot_clock;
104 using android::base::ConsumePrefix;
105 using android::base::GetProperty;
106 using android::base::ReadFileToString;
107 using android::base::SetProperty;
108 using android::base::StringPrintf;
109 using android::base::Timer;
110 using android::base::Trim;
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 signal_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 = ParseApexConfigs(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()716 static void HandleSignalFd() {
717     signalfd_siginfo siginfo;
718     ssize_t bytes_read = TEMP_FAILURE_RETRY(read(signal_fd, &siginfo, sizeof(siginfo)));
719     if (bytes_read != sizeof(siginfo)) {
720         PLOG(ERROR) << "Failed to read siginfo from signal_fd";
721         return;
722     }
723 
724     switch (siginfo.ssi_signo) {
725         case SIGCHLD:
726             ReapAnyOutstandingChildren();
727             break;
728         case SIGTERM:
729             HandleSigtermSignal(siginfo);
730             break;
731         default:
732             LOG(ERROR) << "signal_fd: received unexpected signal " << siginfo.ssi_signo;
733             break;
734     }
735 }
736 
UnblockSignals()737 static void UnblockSignals() {
738     const struct sigaction act { .sa_handler = SIG_DFL };
739     sigaction(SIGCHLD, &act, nullptr);
740 
741     sigset_t mask;
742     sigemptyset(&mask);
743     sigaddset(&mask, SIGCHLD);
744     sigaddset(&mask, SIGTERM);
745 
746     if (sigprocmask(SIG_UNBLOCK, &mask, nullptr) == -1) {
747         PLOG(FATAL) << "failed to unblock signals for PID " << getpid();
748     }
749 }
750 
InstallSignalFdHandler(Epoll * epoll)751 static void InstallSignalFdHandler(Epoll* epoll) {
752     // Applying SA_NOCLDSTOP to a defaulted SIGCHLD handler prevents the signalfd from receiving
753     // SIGCHLD when a child process stops or continues (b/77867680#comment9).
754     const struct sigaction act { .sa_handler = SIG_DFL, .sa_flags = SA_NOCLDSTOP };
755     sigaction(SIGCHLD, &act, nullptr);
756 
757     sigset_t mask;
758     sigemptyset(&mask);
759     sigaddset(&mask, SIGCHLD);
760 
761     if (!IsRebootCapable()) {
762         // If init does not have the CAP_SYS_BOOT capability, it is running in a container.
763         // In that case, receiving SIGTERM will cause the system to shut down.
764         sigaddset(&mask, SIGTERM);
765     }
766 
767     if (sigprocmask(SIG_BLOCK, &mask, nullptr) == -1) {
768         PLOG(FATAL) << "failed to block signals";
769     }
770 
771     // Register a handler to unblock signals in the child processes.
772     const int result = pthread_atfork(nullptr, nullptr, &UnblockSignals);
773     if (result != 0) {
774         LOG(FATAL) << "Failed to register a fork handler: " << strerror(result);
775     }
776 
777     signal_fd = signalfd(-1, &mask, SFD_CLOEXEC);
778     if (signal_fd == -1) {
779         PLOG(FATAL) << "failed to create signalfd";
780     }
781 
782     constexpr int flags = EPOLLIN | EPOLLPRI;
783     if (auto result = epoll->RegisterHandler(signal_fd, HandleSignalFd, flags); !result.ok()) {
784         LOG(FATAL) << result.error();
785     }
786 }
787 
HandleKeychord(const std::vector<int> & keycodes)788 void HandleKeychord(const std::vector<int>& keycodes) {
789     // Only handle keychords if adb is enabled.
790     std::string adb_enabled = android::base::GetProperty("init.svc.adbd", "");
791     if (adb_enabled != "running") {
792         LOG(WARNING) << "Not starting service for keychord " << android::base::Join(keycodes, ' ')
793                      << " because ADB is disabled";
794         return;
795     }
796 
797     auto found = false;
798     for (const auto& service : ServiceList::GetInstance()) {
799         auto svc = service.get();
800         if (svc->keycodes() == keycodes) {
801             found = true;
802             LOG(INFO) << "Starting service '" << svc->name() << "' from keychord "
803                       << android::base::Join(keycodes, ' ');
804             if (auto result = svc->Start(); !result.ok()) {
805                 LOG(ERROR) << "Could not start service '" << svc->name() << "' from keychord "
806                            << android::base::Join(keycodes, ' ') << ": " << result.error();
807             }
808         }
809     }
810     if (!found) {
811         LOG(ERROR) << "Service for keychord " << android::base::Join(keycodes, ' ') << " not found";
812     }
813 }
814 
UmountDebugRamdisk()815 static void UmountDebugRamdisk() {
816     if (umount("/debug_ramdisk") != 0) {
817         PLOG(ERROR) << "Failed to umount /debug_ramdisk";
818     }
819 }
820 
UmountSecondStageRes()821 static void UmountSecondStageRes() {
822     if (umount(kSecondStageRes) != 0) {
823         PLOG(ERROR) << "Failed to umount " << kSecondStageRes;
824     }
825 }
826 
MountExtraFilesystems()827 static void MountExtraFilesystems() {
828 #define CHECKCALL(x) \
829     if ((x) != 0) PLOG(FATAL) << #x " failed.";
830 
831     // /apex is used to mount APEXes
832     CHECKCALL(mount("tmpfs", "/apex", "tmpfs", MS_NOEXEC | MS_NOSUID | MS_NODEV,
833                     "mode=0755,uid=0,gid=0"));
834 
835     // /linkerconfig is used to keep generated linker configuration
836     CHECKCALL(mount("tmpfs", "/linkerconfig", "tmpfs", MS_NOEXEC | MS_NOSUID | MS_NODEV,
837                     "mode=0755,uid=0,gid=0"));
838 #undef CHECKCALL
839 }
840 
RecordStageBoottimes(const boot_clock::time_point & second_stage_start_time)841 static void RecordStageBoottimes(const boot_clock::time_point& second_stage_start_time) {
842     int64_t first_stage_start_time_ns = -1;
843     if (auto first_stage_start_time_str = getenv(kEnvFirstStageStartedAt);
844         first_stage_start_time_str) {
845         SetProperty("ro.boottime.init", first_stage_start_time_str);
846         android::base::ParseInt(first_stage_start_time_str, &first_stage_start_time_ns);
847     }
848     unsetenv(kEnvFirstStageStartedAt);
849 
850     int64_t selinux_start_time_ns = -1;
851     if (auto selinux_start_time_str = getenv(kEnvSelinuxStartedAt); selinux_start_time_str) {
852         android::base::ParseInt(selinux_start_time_str, &selinux_start_time_ns);
853     }
854     unsetenv(kEnvSelinuxStartedAt);
855 
856     if (selinux_start_time_ns == -1) return;
857     if (first_stage_start_time_ns == -1) return;
858 
859     SetProperty("ro.boottime.init.first_stage",
860                 std::to_string(selinux_start_time_ns - first_stage_start_time_ns));
861     SetProperty("ro.boottime.init.selinux",
862                 std::to_string(second_stage_start_time.time_since_epoch().count() -
863                                selinux_start_time_ns));
864     if (auto init_module_time_str = getenv(kEnvInitModuleDurationMs); init_module_time_str) {
865         SetProperty("ro.boottime.init.modules", init_module_time_str);
866         unsetenv(kEnvInitModuleDurationMs);
867     }
868 }
869 
SendLoadPersistentPropertiesMessage()870 void SendLoadPersistentPropertiesMessage() {
871     auto init_message = InitMessage{};
872     init_message.set_load_persistent_properties(true);
873     if (auto result = SendMessage(property_fd, init_message); !result.ok()) {
874         LOG(ERROR) << "Failed to send load persistent properties message: " << result.error();
875     }
876 }
877 
ConnectEarlyStageSnapuserdAction(const BuiltinArguments & args)878 static Result<void> ConnectEarlyStageSnapuserdAction(const BuiltinArguments& args) {
879     auto pid = GetSnapuserdFirstStagePid();
880     if (!pid) {
881         return {};
882     }
883 
884     auto info = GetSnapuserdFirstStageInfo();
885     if (auto iter = std::find(info.begin(), info.end(), "socket"s); iter == info.end()) {
886         // snapuserd does not support socket handoff, so exit early.
887         return {};
888     }
889 
890     // Socket handoff is supported.
891     auto svc = ServiceList::GetInstance().FindService("snapuserd");
892     if (!svc) {
893         LOG(FATAL) << "Failed to find snapuserd service entry";
894     }
895 
896     svc->SetShutdownCritical();
897     svc->SetStartedInFirstStage(*pid);
898 
899     svc = ServiceList::GetInstance().FindService("snapuserd_proxy");
900     if (!svc) {
901         LOG(FATAL) << "Failed find snapuserd_proxy service entry, merge will never initiate";
902     }
903     if (!svc->MarkSocketPersistent("snapuserd")) {
904         LOG(FATAL) << "Could not find snapuserd socket in snapuserd_proxy service entry";
905     }
906     if (auto result = svc->Start(); !result.ok()) {
907         LOG(FATAL) << "Could not start snapuserd_proxy: " << result.error();
908     }
909     return {};
910 }
911 
SecondStageMain(int argc,char ** argv)912 int SecondStageMain(int argc, char** argv) {
913     if (REBOOT_BOOTLOADER_ON_PANIC) {
914         InstallRebootSignalHandlers();
915     }
916 
917     // No threads should be spin up until signalfd
918     // is registered. If the threads are indeed required,
919     // each of these threads _should_ make sure SIGCHLD signal
920     // is blocked. See b/223076262
921     boot_clock::time_point start_time = boot_clock::now();
922 
923     trigger_shutdown = [](const std::string& command) { shutdown_state.TriggerShutdown(command); };
924 
925     SetStdioToDevNull(argv);
926     InitKernelLogging(argv);
927     LOG(INFO) << "init second stage started!";
928 
929     SelinuxSetupKernelLogging();
930 
931     // Update $PATH in the case the second stage init is newer than first stage init, where it is
932     // first set.
933     if (setenv("PATH", _PATH_DEFPATH, 1) != 0) {
934         PLOG(FATAL) << "Could not set $PATH to '" << _PATH_DEFPATH << "' in second stage";
935     }
936 
937     // Init should not crash because of a dependence on any other process, therefore we ignore
938     // SIGPIPE and handle EPIPE at the call site directly.  Note that setting a signal to SIG_IGN
939     // is inherited across exec, but custom signal handlers are not.  Since we do not want to
940     // ignore SIGPIPE for child processes, we set a no-op function for the signal handler instead.
941     {
942         struct sigaction action = {.sa_flags = SA_RESTART};
943         action.sa_handler = [](int) {};
944         sigaction(SIGPIPE, &action, nullptr);
945     }
946 
947     // Set init and its forked children's oom_adj.
948     if (auto result =
949                 WriteFile("/proc/1/oom_score_adj", StringPrintf("%d", DEFAULT_OOM_SCORE_ADJUST));
950         !result.ok()) {
951         LOG(ERROR) << "Unable to write " << DEFAULT_OOM_SCORE_ADJUST
952                    << " to /proc/1/oom_score_adj: " << result.error();
953     }
954 
955     // Set up a session keyring that all processes will have access to. It
956     // will hold things like FBE encryption keys. No process should override
957     // its session keyring.
958     keyctl_get_keyring_ID(KEY_SPEC_SESSION_KEYRING, 1);
959 
960     // Indicate that booting is in progress to background fw loaders, etc.
961     close(open("/dev/.booting", O_WRONLY | O_CREAT | O_CLOEXEC, 0000));
962 
963     // See if need to load debug props to allow adb root, when the device is unlocked.
964     const char* force_debuggable_env = getenv("INIT_FORCE_DEBUGGABLE");
965     bool load_debug_prop = false;
966     if (force_debuggable_env && AvbHandle::IsDeviceUnlocked()) {
967         load_debug_prop = "true"s == force_debuggable_env;
968     }
969     unsetenv("INIT_FORCE_DEBUGGABLE");
970 
971     // Umount the debug ramdisk so property service doesn't read .prop files from there, when it
972     // is not meant to.
973     if (!load_debug_prop) {
974         UmountDebugRamdisk();
975     }
976 
977     PropertyInit();
978 
979     // Umount second stage resources after property service has read the .prop files.
980     UmountSecondStageRes();
981 
982     // Umount the debug ramdisk after property service has read the .prop files when it means to.
983     if (load_debug_prop) {
984         UmountDebugRamdisk();
985     }
986 
987     // Mount extra filesystems required during second stage init
988     MountExtraFilesystems();
989 
990     // Now set up SELinux for second stage.
991     SelabelInitialize();
992     SelinuxRestoreContext();
993 
994     Epoll epoll;
995     if (auto result = epoll.Open(); !result.ok()) {
996         PLOG(FATAL) << result.error();
997     }
998 
999     // We always reap children before responding to the other pending functions. This is to
1000     // prevent a race where other daemons see that a service has exited and ask init to
1001     // start it again via ctl.start before init has reaped it.
1002     epoll.SetFirstCallback(ReapAnyOutstandingChildren);
1003 
1004     InstallSignalFdHandler(&epoll);
1005     InstallInitNotifier(&epoll);
1006     StartPropertyService(&property_fd);
1007 
1008     // Make the time that init stages started available for bootstat to log.
1009     RecordStageBoottimes(start_time);
1010 
1011     // Set libavb version for Framework-only OTA match in Treble build.
1012     if (const char* avb_version = getenv("INIT_AVB_VERSION"); avb_version != nullptr) {
1013         SetProperty("ro.boot.avb_version", avb_version);
1014     }
1015     unsetenv("INIT_AVB_VERSION");
1016 
1017     fs_mgr_vendor_overlay_mount_all();
1018     export_oem_lock_status();
1019     MountHandler mount_handler(&epoll);
1020     SetUsbController();
1021     SetKernelVersion();
1022 
1023     const BuiltinFunctionMap& function_map = GetBuiltinFunctionMap();
1024     Action::set_function_map(&function_map);
1025 
1026     if (!SetupMountNamespaces()) {
1027         PLOG(FATAL) << "SetupMountNamespaces failed";
1028     }
1029 
1030     InitializeSubcontext();
1031 
1032     ActionManager& am = ActionManager::GetInstance();
1033     ServiceList& sm = ServiceList::GetInstance();
1034 
1035     LoadBootScripts(am, sm);
1036 
1037     // Turning this on and letting the INFO logging be discarded adds 0.2s to
1038     // Nexus 9 boot time, so it's disabled by default.
1039     if (false) DumpState();
1040 
1041     // Make the GSI status available before scripts start running.
1042     auto is_running = android::gsi::IsGsiRunning() ? "1" : "0";
1043     SetProperty(gsi::kGsiBootedProp, is_running);
1044     auto is_installed = android::gsi::IsGsiInstalled() ? "1" : "0";
1045     SetProperty(gsi::kGsiInstalledProp, is_installed);
1046     if (android::gsi::IsGsiRunning()) {
1047         std::string dsu_slot;
1048         if (android::gsi::GetActiveDsu(&dsu_slot)) {
1049             SetProperty(gsi::kDsuSlotProp, dsu_slot);
1050         }
1051     }
1052 
1053     am.QueueBuiltinAction(SetupCgroupsAction, "SetupCgroups");
1054     am.QueueBuiltinAction(SetKptrRestrictAction, "SetKptrRestrict");
1055     am.QueueBuiltinAction(TestPerfEventSelinuxAction, "TestPerfEventSelinux");
1056     am.QueueBuiltinAction(ConnectEarlyStageSnapuserdAction, "ConnectEarlyStageSnapuserd");
1057     am.QueueEventTrigger("early-init");
1058 
1059     // Queue an action that waits for coldboot done so we know ueventd has set up all of /dev...
1060     am.QueueBuiltinAction(wait_for_coldboot_done_action, "wait_for_coldboot_done");
1061     // ... so that we can start queuing up actions that require stuff from /dev.
1062     am.QueueBuiltinAction(SetMmapRndBitsAction, "SetMmapRndBits");
1063     Keychords keychords;
1064     am.QueueBuiltinAction(
1065             [&epoll, &keychords](const BuiltinArguments& args) -> Result<void> {
1066                 for (const auto& svc : ServiceList::GetInstance()) {
1067                     keychords.Register(svc->keycodes());
1068                 }
1069                 keychords.Start(&epoll, HandleKeychord);
1070                 return {};
1071             },
1072             "KeychordInit");
1073 
1074     // Trigger all the boot actions to get us started.
1075     am.QueueEventTrigger("init");
1076 
1077     // Don't mount filesystems or start core system services in charger mode.
1078     std::string bootmode = GetProperty("ro.bootmode", "");
1079     if (bootmode == "charger") {
1080         am.QueueEventTrigger("charger");
1081     } else {
1082         am.QueueEventTrigger("late-init");
1083     }
1084 
1085     // Run all property triggers based on current state of the properties.
1086     am.QueueBuiltinAction(queue_property_triggers_action, "queue_property_triggers");
1087 
1088     // Restore prio before main loop
1089     setpriority(PRIO_PROCESS, 0, 0);
1090     while (true) {
1091         // By default, sleep until something happens. Do not convert far_future into
1092         // std::chrono::milliseconds because that would trigger an overflow. The unit of boot_clock
1093         // is 1ns.
1094         const boot_clock::time_point far_future = boot_clock::time_point::max();
1095         boot_clock::time_point next_action_time = far_future;
1096 
1097         auto shutdown_command = shutdown_state.CheckShutdown();
1098         if (shutdown_command) {
1099             LOG(INFO) << "Got shutdown_command '" << *shutdown_command
1100                       << "' Calling HandlePowerctlMessage()";
1101             HandlePowerctlMessage(*shutdown_command);
1102         }
1103 
1104         if (!(prop_waiter_state.MightBeWaiting() || Service::is_exec_service_running())) {
1105             am.ExecuteOneCommand();
1106             // If there's more work to do, wake up again immediately.
1107             if (am.HasMoreCommands()) {
1108                 next_action_time = boot_clock::now();
1109             }
1110         }
1111         // Since the above code examined pending actions, no new actions must be
1112         // queued by the code between this line and the Epoll::Wait() call below
1113         // without calling WakeMainInitThread().
1114         if (!IsShuttingDown()) {
1115             auto next_process_action_time = HandleProcessActions();
1116 
1117             // If there's a process that needs restarting, wake up in time for that.
1118             if (next_process_action_time) {
1119                 next_action_time = std::min(next_action_time, *next_process_action_time);
1120             }
1121         }
1122 
1123         std::optional<std::chrono::milliseconds> epoll_timeout;
1124         if (next_action_time != far_future) {
1125             epoll_timeout = std::chrono::ceil<std::chrono::milliseconds>(
1126                     std::max(next_action_time - boot_clock::now(), 0ns));
1127         }
1128         auto epoll_result = epoll.Wait(epoll_timeout);
1129         if (!epoll_result.ok()) {
1130             LOG(ERROR) << epoll_result.error();
1131         }
1132         if (!IsShuttingDown()) {
1133             HandleControlMessages();
1134             SetUsbController();
1135         }
1136     }
1137 
1138     return 0;
1139 }
1140 
1141 }  // namespace init
1142 }  // namespace android
1143