Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 "ueventd.h" |
| 18 | |
Tom Cherry | 71dd706 | 2020-12-11 09:26:55 -0800 | [diff] [blame] | 19 | #include <android/api-level.h> |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 20 | #include <ctype.h> |
Bongkyu Kim | 5aa6197 | 2019-01-30 19:59:53 +0900 | [diff] [blame] | 21 | #include <dirent.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 22 | #include <fcntl.h> |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 23 | #include <signal.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
Bongkyu Kim | 5aa6197 | 2019-01-30 19:59:53 +0900 | [diff] [blame] | 27 | #include <sys/stat.h> |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 28 | #include <sys/wait.h> |
Bongkyu Kim | 5aa6197 | 2019-01-30 19:59:53 +0900 | [diff] [blame] | 29 | #include <unistd.h> |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 30 | |
| 31 | #include <set> |
| 32 | #include <thread> |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 33 | |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 34 | #include <android-base/chrono_utils.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 35 | #include <android-base/logging.h> |
Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 36 | #include <android-base/properties.h> |
Bowgo Tsai | 8eec38f | 2018-05-16 18:33:44 +0800 | [diff] [blame] | 37 | #include <fstab/fstab.h> |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 38 | #include <selinux/android.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 39 | #include <selinux/selinux.h> |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 40 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 41 | #include "devices.h" |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 42 | #include "firmware_handler.h" |
Andrew F. Davis | 9963847 | 2018-07-09 13:12:00 -0500 | [diff] [blame] | 43 | #include "modalias_handler.h" |
Vic Yang | 92c236e | 2019-05-28 15:58:35 -0700 | [diff] [blame] | 44 | #include "selabel.h" |
Tom Cherry | c3692b3 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 45 | #include "selinux.h" |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 46 | #include "uevent_handler.h" |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 47 | #include "uevent_listener.h" |
| 48 | #include "ueventd_parser.h" |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 49 | #include "util.h" |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 50 | |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 51 | // At a high level, ueventd listens for uevent messages generated by the kernel through a netlink |
| 52 | // socket. When ueventd receives such a message it handles it by taking appropriate actions, |
| 53 | // which can typically be creating a device node in /dev, setting file permissions, setting selinux |
| 54 | // labels, etc. |
| 55 | // Ueventd also handles loading of firmware that the kernel requests, and creates symlinks for block |
| 56 | // and character devices. |
| 57 | |
| 58 | // When ueventd starts, it regenerates uevents for all currently registered devices by traversing |
| 59 | // /sys and writing 'add' to each 'uevent' file that it finds. This causes the kernel to generate |
| 60 | // and resend uevent messages for all of the currently registered devices. This is done, because |
| 61 | // ueventd would not have been running when these devices were registered and therefore was unable |
| 62 | // to receive their uevent messages and handle them appropriately. This process is known as |
| 63 | // 'cold boot'. |
| 64 | |
| 65 | // 'init' currently waits synchronously on the cold boot process of ueventd before it continues |
| 66 | // its boot process. For this reason, cold boot should be as quick as possible. One way to achieve |
| 67 | // a speed up here is to parallelize the handling of ueventd messages, which consume the bulk of the |
| 68 | // time during cold boot. |
| 69 | |
| 70 | // Handling of uevent messages has two unique properties: |
| 71 | // 1) It can be done in isolation; it doesn't need to read or write any status once it is started. |
| 72 | // 2) It uses setegid() and setfscreatecon() so either care (aka locking) must be taken to ensure |
| 73 | // that no file system operations are done while the uevent process has an abnormal egid or |
| 74 | // fscreatecon or this handling must happen in a separate process. |
| 75 | // Given the above two properties, it is best to fork() subprocesses to handle the uevents. This |
| 76 | // reduces the overhead and complexity that would be required in a solution with threads and locks. |
| 77 | // In testing, a racy multithreaded solution has the same performance as the fork() solution, so |
| 78 | // there is no reason to deal with the complexity of the former. |
| 79 | |
| 80 | // One other important caveat during the boot process is the handling of SELinux restorecon. |
| 81 | // Since many devices have child devices, calling selinux_android_restorecon() recursively for each |
| 82 | // device when its uevent is handled, results in multiple restorecon operations being done on a |
| 83 | // given file. It is more efficient to simply do restorecon recursively on /sys during cold boot, |
| 84 | // than to do restorecon on each device as its uevent is handled. This only applies to cold boot; |
| 85 | // once that has completed, restorecon is done for each device as its uevent is handled. |
| 86 | |
| 87 | // With all of the above considered, the cold boot process has the below steps: |
| 88 | // 1) ueventd regenerates uevents by doing the /sys traversal and listens to the netlink socket for |
| 89 | // the generated uevents. It writes these uevents into a queue represented by a vector. |
| 90 | // |
| 91 | // 2) ueventd forks 'n' separate uevent handler subprocesses and has each of them to handle the |
| 92 | // uevents in the queue based on a starting offset (their process number) and a stride (the total |
| 93 | // number of processes). Note that no IPC happens at this point and only const functions from |
| 94 | // DeviceHandler should be called from this context. |
| 95 | // |
| 96 | // 3) In parallel to the subprocesses handling the uevents, the main thread of ueventd calls |
| 97 | // selinux_android_restorecon() recursively on /sys/class, /sys/block, and /sys/devices. |
| 98 | // |
| 99 | // 4) Once the restorecon operation finishes, the main thread calls waitpid() to wait for all |
| 100 | // subprocess handlers to complete and exit. Once this happens, it marks coldboot as having |
| 101 | // completed. |
| 102 | // |
| 103 | // At this point, ueventd is single threaded, poll()'s and then handles any future uevents. |
| 104 | |
| 105 | // Lastly, it should be noted that uevents that occur during the coldboot process are handled |
| 106 | // without issue after the coldboot process completes. This is because the uevent listener is |
| 107 | // paused while the uevent handler and restorecon actions take place. Once coldboot completes, |
| 108 | // the uevent listener resumes in polling mode and will handle the uevents that occurred during |
| 109 | // coldboot. |
| 110 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 111 | namespace android { |
| 112 | namespace init { |
| 113 | |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 114 | class ColdBoot { |
| 115 | public: |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 116 | ColdBoot(UeventListener& uevent_listener, |
Tom Cherry | 4233ec7 | 2019-09-06 10:52:31 -0700 | [diff] [blame] | 117 | std::vector<std::unique_ptr<UeventHandler>>& uevent_handlers, |
yuehu mi | ddffa0e | 2021-05-01 14:36:58 +0800 | [diff] [blame] | 118 | bool enable_parallel_restorecon, |
| 119 | std::vector<std::string> parallel_restorecon_queue) |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 120 | : uevent_listener_(uevent_listener), |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 121 | uevent_handlers_(uevent_handlers), |
Tom Cherry | 4233ec7 | 2019-09-06 10:52:31 -0700 | [diff] [blame] | 122 | num_handler_subprocesses_(std::thread::hardware_concurrency() ?: 4), |
yuehu mi | ddffa0e | 2021-05-01 14:36:58 +0800 | [diff] [blame] | 123 | enable_parallel_restorecon_(enable_parallel_restorecon), |
| 124 | parallel_restorecon_queue_(parallel_restorecon_queue) {} |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 125 | |
| 126 | void Run(); |
| 127 | |
| 128 | private: |
| 129 | void UeventHandlerMain(unsigned int process_num, unsigned int total_processes); |
| 130 | void RegenerateUevents(); |
| 131 | void ForkSubProcesses(); |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 132 | void WaitForSubProcesses(); |
Bongkyu Kim | 5aa6197 | 2019-01-30 19:59:53 +0900 | [diff] [blame] | 133 | void RestoreConHandler(unsigned int process_num, unsigned int total_processes); |
| 134 | void GenerateRestoreCon(const std::string& directory); |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 135 | |
| 136 | UeventListener& uevent_listener_; |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 137 | std::vector<std::unique_ptr<UeventHandler>>& uevent_handlers_; |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 138 | |
| 139 | unsigned int num_handler_subprocesses_; |
Tom Cherry | 4233ec7 | 2019-09-06 10:52:31 -0700 | [diff] [blame] | 140 | bool enable_parallel_restorecon_; |
| 141 | |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 142 | std::vector<Uevent> uevent_queue_; |
| 143 | |
| 144 | std::set<pid_t> subprocess_pids_; |
Bongkyu Kim | 5aa6197 | 2019-01-30 19:59:53 +0900 | [diff] [blame] | 145 | |
| 146 | std::vector<std::string> restorecon_queue_; |
yuehu mi | ddffa0e | 2021-05-01 14:36:58 +0800 | [diff] [blame] | 147 | |
| 148 | std::vector<std::string> parallel_restorecon_queue_; |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | void ColdBoot::UeventHandlerMain(unsigned int process_num, unsigned int total_processes) { |
| 152 | for (unsigned int i = process_num; i < uevent_queue_.size(); i += total_processes) { |
| 153 | auto& uevent = uevent_queue_[i]; |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 154 | |
| 155 | for (auto& uevent_handler : uevent_handlers_) { |
| 156 | uevent_handler->HandleUevent(uevent); |
| 157 | } |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 158 | } |
Bongkyu Kim | 5aa6197 | 2019-01-30 19:59:53 +0900 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void ColdBoot::RestoreConHandler(unsigned int process_num, unsigned int total_processes) { |
yuehu mi | ddffa0e | 2021-05-01 14:36:58 +0800 | [diff] [blame] | 162 | android::base::Timer t_process; |
| 163 | |
Bongkyu Kim | 5aa6197 | 2019-01-30 19:59:53 +0900 | [diff] [blame] | 164 | for (unsigned int i = process_num; i < restorecon_queue_.size(); i += total_processes) { |
yuehu mi | ddffa0e | 2021-05-01 14:36:58 +0800 | [diff] [blame] | 165 | android::base::Timer t; |
Bongkyu Kim | 5aa6197 | 2019-01-30 19:59:53 +0900 | [diff] [blame] | 166 | auto& dir = restorecon_queue_[i]; |
| 167 | |
| 168 | selinux_android_restorecon(dir.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE); |
yuehu mi | ddffa0e | 2021-05-01 14:36:58 +0800 | [diff] [blame] | 169 | |
| 170 | //Mark a dir restorecon operation for 50ms, |
| 171 | //Maybe you can add this dir to the ueventd.rc script to parallel processing |
| 172 | if (t.duration() > 50ms) { |
| 173 | LOG(INFO) << "took " << t.duration().count() <<"ms restorecon '" |
| 174 | << dir.c_str() << "' on process '" << process_num <<"'"; |
| 175 | } |
Bongkyu Kim | 5aa6197 | 2019-01-30 19:59:53 +0900 | [diff] [blame] | 176 | } |
yuehu mi | ddffa0e | 2021-05-01 14:36:58 +0800 | [diff] [blame] | 177 | |
| 178 | //Calculate process restorecon time |
| 179 | LOG(VERBOSE) << "took " << t_process.duration().count() << "ms on process '" |
| 180 | << process_num << "'"; |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Bongkyu Kim | 5aa6197 | 2019-01-30 19:59:53 +0900 | [diff] [blame] | 183 | void ColdBoot::GenerateRestoreCon(const std::string& directory) { |
| 184 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(directory.c_str()), &closedir); |
| 185 | |
yuehu mi | ddffa0e | 2021-05-01 14:36:58 +0800 | [diff] [blame] | 186 | if (!dir) { |
| 187 | PLOG(WARNING) << "opendir " << directory.c_str(); |
| 188 | return; |
| 189 | } |
Bongkyu Kim | 5aa6197 | 2019-01-30 19:59:53 +0900 | [diff] [blame] | 190 | |
| 191 | struct dirent* dent; |
| 192 | while ((dent = readdir(dir.get())) != NULL) { |
| 193 | if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) continue; |
| 194 | |
| 195 | struct stat st; |
| 196 | if (fstatat(dirfd(dir.get()), dent->d_name, &st, 0) == -1) continue; |
| 197 | |
| 198 | if (S_ISDIR(st.st_mode)) { |
| 199 | std::string fullpath = directory + "/" + dent->d_name; |
yuehu mi | ddffa0e | 2021-05-01 14:36:58 +0800 | [diff] [blame] | 200 | auto parallel_restorecon = |
| 201 | std::find(parallel_restorecon_queue_.begin(), |
| 202 | parallel_restorecon_queue_.end(), fullpath); |
| 203 | if (parallel_restorecon == parallel_restorecon_queue_.end()) { |
Bongkyu Kim | 5aa6197 | 2019-01-30 19:59:53 +0900 | [diff] [blame] | 204 | restorecon_queue_.emplace_back(fullpath); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 210 | void ColdBoot::RegenerateUevents() { |
| 211 | uevent_listener_.RegenerateUevents([this](const Uevent& uevent) { |
Tom Cherry | 7c1d87e | 2019-07-10 11:18:24 -0700 | [diff] [blame] | 212 | uevent_queue_.emplace_back(uevent); |
Sandeep Patil | 4cbedee | 2017-06-21 13:02:57 -0700 | [diff] [blame] | 213 | return ListenerAction::kContinue; |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 214 | }); |
| 215 | } |
| 216 | |
| 217 | void ColdBoot::ForkSubProcesses() { |
| 218 | for (unsigned int i = 0; i < num_handler_subprocesses_; ++i) { |
| 219 | auto pid = fork(); |
| 220 | if (pid < 0) { |
| 221 | PLOG(FATAL) << "fork() failed!"; |
| 222 | } |
| 223 | |
| 224 | if (pid == 0) { |
| 225 | UeventHandlerMain(i, num_handler_subprocesses_); |
Tom Cherry | 4233ec7 | 2019-09-06 10:52:31 -0700 | [diff] [blame] | 226 | if (enable_parallel_restorecon_) { |
| 227 | RestoreConHandler(i, num_handler_subprocesses_); |
| 228 | } |
| 229 | _exit(EXIT_SUCCESS); |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | subprocess_pids_.emplace(pid); |
| 233 | } |
| 234 | } |
| 235 | |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 236 | void ColdBoot::WaitForSubProcesses() { |
| 237 | // Treat subprocesses that crash or get stuck the same as if ueventd itself has crashed or gets |
| 238 | // stuck. |
| 239 | // |
| 240 | // When a subprocess crashes, we fatally abort from ueventd. init will restart ueventd when |
| 241 | // init reaps it, and the cold boot process will start again. If this continues to fail, then |
Tom Cherry | ad9e7ea | 2018-10-15 17:21:48 -0700 | [diff] [blame] | 242 | // since ueventd is marked as a critical service, init will reboot to bootloader. |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 243 | // |
| 244 | // When a subprocess gets stuck, keep ueventd spinning waiting for it. init has a timeout for |
| 245 | // cold boot and will reboot to the bootloader if ueventd does not complete in time. |
| 246 | while (!subprocess_pids_.empty()) { |
| 247 | int status; |
| 248 | pid_t pid = TEMP_FAILURE_RETRY(waitpid(-1, &status, 0)); |
| 249 | if (pid == -1) { |
| 250 | PLOG(ERROR) << "waitpid() failed"; |
| 251 | continue; |
| 252 | } |
| 253 | |
| 254 | auto it = std::find(subprocess_pids_.begin(), subprocess_pids_.end(), pid); |
| 255 | if (it == subprocess_pids_.end()) continue; |
| 256 | |
| 257 | if (WIFEXITED(status)) { |
| 258 | if (WEXITSTATUS(status) == EXIT_SUCCESS) { |
| 259 | subprocess_pids_.erase(it); |
| 260 | } else { |
| 261 | LOG(FATAL) << "subprocess exited with status " << WEXITSTATUS(status); |
| 262 | } |
| 263 | } else if (WIFSIGNALED(status)) { |
| 264 | LOG(FATAL) << "subprocess killed by signal " << WTERMSIG(status); |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | void ColdBoot::Run() { |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 270 | android::base::Timer cold_boot_timer; |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 271 | |
| 272 | RegenerateUevents(); |
| 273 | |
Tom Cherry | 4233ec7 | 2019-09-06 10:52:31 -0700 | [diff] [blame] | 274 | if (enable_parallel_restorecon_) { |
yuehu mi | ddffa0e | 2021-05-01 14:36:58 +0800 | [diff] [blame] | 275 | if (parallel_restorecon_queue_.empty()) { |
| 276 | parallel_restorecon_queue_.emplace_back("/sys"); |
| 277 | // takes long time for /sys/devices, parallelize it |
| 278 | parallel_restorecon_queue_.emplace_back("/sys/devices"); |
| 279 | LOG(INFO) << "Parallel processing directory is not set, set the default"; |
| 280 | } |
| 281 | for (const auto& dir : parallel_restorecon_queue_) { |
| 282 | selinux_android_restorecon(dir.c_str(), 0); |
| 283 | GenerateRestoreCon(dir); |
| 284 | } |
Tom Cherry | 4233ec7 | 2019-09-06 10:52:31 -0700 | [diff] [blame] | 285 | } |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 286 | |
Bongkyu Kim | 5aa6197 | 2019-01-30 19:59:53 +0900 | [diff] [blame] | 287 | ForkSubProcesses(); |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 288 | |
Tom Cherry | 4233ec7 | 2019-09-06 10:52:31 -0700 | [diff] [blame] | 289 | if (!enable_parallel_restorecon_) { |
| 290 | selinux_android_restorecon("/sys", SELINUX_ANDROID_RESTORECON_RECURSE); |
| 291 | } |
| 292 | |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 293 | WaitForSubProcesses(); |
| 294 | |
Tom Cherry | 39fafed | 2019-06-10 17:49:59 -0700 | [diff] [blame] | 295 | android::base::SetProperty(kColdBootDoneProp, "true"); |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 296 | LOG(INFO) << "Coldboot took " << cold_boot_timer.duration().count() / 1000.0f << " seconds"; |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Tom Cherry | 71dd706 | 2020-12-11 09:26:55 -0800 | [diff] [blame] | 299 | static UeventdConfiguration GetConfiguration() { |
David Anderson | 0050bef | 2021-06-02 18:21:07 -0700 | [diff] [blame] | 300 | auto hardware = android::base::GetProperty("ro.hardware", ""); |
| 301 | std::vector<std::string> legacy_paths{"/vendor/ueventd.rc", "/odm/ueventd.rc", |
| 302 | "/ueventd." + hardware + ".rc"}; |
| 303 | |
| 304 | std::vector<std::string> canonical{"/system/etc/ueventd.rc"}; |
| 305 | |
Tom Cherry | 71dd706 | 2020-12-11 09:26:55 -0800 | [diff] [blame] | 306 | if (android::base::GetIntProperty("ro.product.first_api_level", 10000) <= __ANDROID_API_S__) { |
David Anderson | 0050bef | 2021-06-02 18:21:07 -0700 | [diff] [blame] | 307 | // TODO: Remove these legacy paths once Android S is no longer supported. |
| 308 | canonical.insert(canonical.end(), legacy_paths.begin(), legacy_paths.end()); |
| 309 | } else { |
| 310 | // Warn if newer device is using legacy paths. |
| 311 | for (const auto& path : legacy_paths) { |
| 312 | if (access(path.c_str(), F_OK) == 0) { |
| 313 | LOG(FATAL_WITHOUT_ABORT) |
| 314 | << "Legacy ueventd configuration file detected and will not be parsed: " |
| 315 | << path; |
| 316 | } |
| 317 | } |
Tom Cherry | 71dd706 | 2020-12-11 09:26:55 -0800 | [diff] [blame] | 318 | } |
| 319 | |
David Anderson | 0050bef | 2021-06-02 18:21:07 -0700 | [diff] [blame] | 320 | return ParseConfig(canonical); |
Tom Cherry | 71dd706 | 2020-12-11 09:26:55 -0800 | [diff] [blame] | 321 | } |
| 322 | |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 323 | int ueventd_main(int argc, char** argv) { |
Nick Kralevich | 6ebf12f | 2012-03-26 09:09:11 -0700 | [diff] [blame] | 324 | /* |
| 325 | * init sets the umask to 077 for forked processes. We need to |
| 326 | * create files with exact permissions, without modification by |
| 327 | * the umask. |
| 328 | */ |
| 329 | umask(000); |
| 330 | |
Tom Cherry | 74069d1 | 2018-07-20 15:26:25 -0700 | [diff] [blame] | 331 | android::base::InitLogging(argv, &android::base::KernelLogger); |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 332 | |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 333 | LOG(INFO) << "ueventd started!"; |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 334 | |
Tom Cherry | c3692b3 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 335 | SelinuxSetupKernelLogging(); |
| 336 | SelabelInitialize(); |
Stephen Smalley | 439224e | 2014-06-24 13:45:43 -0400 | [diff] [blame] | 337 | |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 338 | std::vector<std::unique_ptr<UeventHandler>> uevent_handlers; |
Sandeep Patil | bf298e6 | 2017-02-03 07:18:36 -0800 | [diff] [blame] | 339 | |
Tom Cherry | 71dd706 | 2020-12-11 09:26:55 -0800 | [diff] [blame] | 340 | auto ueventd_configuration = GetConfiguration(); |
Tom Cherry | 7421fa1 | 2018-07-13 15:32:02 -0700 | [diff] [blame] | 341 | |
Tom Cherry | e291010 | 2018-12-06 13:29:30 -0800 | [diff] [blame] | 342 | uevent_handlers.emplace_back(std::make_unique<DeviceHandler>( |
| 343 | std::move(ueventd_configuration.dev_permissions), |
| 344 | std::move(ueventd_configuration.sysfs_permissions), |
Tom Cherry | a3530e6 | 2019-01-30 13:25:35 -0800 | [diff] [blame] | 345 | std::move(ueventd_configuration.subsystems), android::fs_mgr::GetBootDevices(), true)); |
Tom Cherry | e291010 | 2018-12-06 13:29:30 -0800 | [diff] [blame] | 346 | uevent_handlers.emplace_back(std::make_unique<FirmwareHandler>( |
Tom Cherry | dcb3d15 | 2019-08-07 16:02:28 -0700 | [diff] [blame] | 347 | std::move(ueventd_configuration.firmware_directories), |
| 348 | std::move(ueventd_configuration.external_firmware_handlers))); |
Tom Cherry | 7421fa1 | 2018-07-13 15:32:02 -0700 | [diff] [blame] | 349 | |
Tom Cherry | e291010 | 2018-12-06 13:29:30 -0800 | [diff] [blame] | 350 | if (ueventd_configuration.enable_modalias_handling) { |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 351 | std::vector<std::string> base_paths = {"/odm/lib/modules", "/vendor/lib/modules"}; |
| 352 | uevent_handlers.emplace_back(std::make_unique<ModaliasHandler>(base_paths)); |
Tom Cherry | 7421fa1 | 2018-07-13 15:32:02 -0700 | [diff] [blame] | 353 | } |
Tom Cherry | e291010 | 2018-12-06 13:29:30 -0800 | [diff] [blame] | 354 | UeventListener uevent_listener(ueventd_configuration.uevent_socket_rcvbuf_size); |
Tom Cherry | 7421fa1 | 2018-07-13 15:32:02 -0700 | [diff] [blame] | 355 | |
Tom Cherry | 39fafed | 2019-06-10 17:49:59 -0700 | [diff] [blame] | 356 | if (!android::base::GetBoolProperty(kColdBootDoneProp, false)) { |
Tom Cherry | 4233ec7 | 2019-09-06 10:52:31 -0700 | [diff] [blame] | 357 | ColdBoot cold_boot(uevent_listener, uevent_handlers, |
yuehu mi | ddffa0e | 2021-05-01 14:36:58 +0800 | [diff] [blame] | 358 | ueventd_configuration.enable_parallel_restorecon, |
| 359 | ueventd_configuration.parallel_restorecon_dirs); |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 360 | cold_boot.Run(); |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 361 | } |
Elliott Hughes | 2145779 | 2015-02-04 10:19:50 -0800 | [diff] [blame] | 362 | |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 363 | for (auto& uevent_handler : uevent_handlers) { |
| 364 | uevent_handler->ColdbootDone(); |
| 365 | } |
| 366 | |
Tom Cherry | 0f296e0 | 2017-06-30 12:58:39 -0700 | [diff] [blame] | 367 | // We use waitpid() in ColdBoot, so we can't ignore SIGCHLD until now. |
| 368 | signal(SIGCHLD, SIG_IGN); |
| 369 | // Reap and pending children that exited between the last call to waitpid() and setting SIG_IGN |
| 370 | // for SIGCHLD above. |
| 371 | while (waitpid(-1, nullptr, WNOHANG) > 0) { |
| 372 | } |
| 373 | |
Wei Wang | 30bbf7d | 2020-07-06 15:26:49 -0700 | [diff] [blame] | 374 | // Restore prio before main loop |
| 375 | setpriority(PRIO_PROCESS, 0, 0); |
Tom Cherry | 457e28f | 2018-08-01 13:12:20 -0700 | [diff] [blame] | 376 | uevent_listener.Poll([&uevent_handlers](const Uevent& uevent) { |
| 377 | for (auto& uevent_handler : uevent_handlers) { |
| 378 | uevent_handler->HandleUevent(uevent); |
| 379 | } |
Sandeep Patil | 4cbedee | 2017-06-21 13:02:57 -0700 | [diff] [blame] | 380 | return ListenerAction::kContinue; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 381 | }); |
| 382 | |
Elliott Hughes | 2145779 | 2015-02-04 10:19:50 -0800 | [diff] [blame] | 383 | return 0; |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 384 | } |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 385 | |
| 386 | } // namespace init |
| 387 | } // namespace android |