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