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 | |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 19 | #include <ctype.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 20 | #include <fcntl.h> |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 21 | #include <signal.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 25 | #include <sys/wait.h> |
| 26 | |
| 27 | #include <set> |
| 28 | #include <thread> |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 29 | |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 30 | #include <android-base/chrono_utils.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 31 | #include <android-base/logging.h> |
Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 32 | #include <android-base/properties.h> |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 33 | #include <selinux/android.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 34 | #include <selinux/selinux.h> |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 35 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 36 | #include "devices.h" |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 37 | #include "firmware_handler.h" |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 38 | #include "log.h" |
Tom Cherry | c3692b3 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 39 | #include "selinux.h" |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 40 | #include "uevent_listener.h" |
| 41 | #include "ueventd_parser.h" |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 42 | #include "util.h" |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 43 | |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 44 | // At a high level, ueventd listens for uevent messages generated by the kernel through a netlink |
| 45 | // socket. When ueventd receives such a message it handles it by taking appropriate actions, |
| 46 | // which can typically be creating a device node in /dev, setting file permissions, setting selinux |
| 47 | // labels, etc. |
| 48 | // Ueventd also handles loading of firmware that the kernel requests, and creates symlinks for block |
| 49 | // and character devices. |
| 50 | |
| 51 | // When ueventd starts, it regenerates uevents for all currently registered devices by traversing |
| 52 | // /sys and writing 'add' to each 'uevent' file that it finds. This causes the kernel to generate |
| 53 | // and resend uevent messages for all of the currently registered devices. This is done, because |
| 54 | // ueventd would not have been running when these devices were registered and therefore was unable |
| 55 | // to receive their uevent messages and handle them appropriately. This process is known as |
| 56 | // 'cold boot'. |
| 57 | |
| 58 | // 'init' currently waits synchronously on the cold boot process of ueventd before it continues |
| 59 | // its boot process. For this reason, cold boot should be as quick as possible. One way to achieve |
| 60 | // a speed up here is to parallelize the handling of ueventd messages, which consume the bulk of the |
| 61 | // time during cold boot. |
| 62 | |
| 63 | // Handling of uevent messages has two unique properties: |
| 64 | // 1) It can be done in isolation; it doesn't need to read or write any status once it is started. |
| 65 | // 2) It uses setegid() and setfscreatecon() so either care (aka locking) must be taken to ensure |
| 66 | // that no file system operations are done while the uevent process has an abnormal egid or |
| 67 | // fscreatecon or this handling must happen in a separate process. |
| 68 | // Given the above two properties, it is best to fork() subprocesses to handle the uevents. This |
| 69 | // reduces the overhead and complexity that would be required in a solution with threads and locks. |
| 70 | // In testing, a racy multithreaded solution has the same performance as the fork() solution, so |
| 71 | // there is no reason to deal with the complexity of the former. |
| 72 | |
| 73 | // One other important caveat during the boot process is the handling of SELinux restorecon. |
| 74 | // Since many devices have child devices, calling selinux_android_restorecon() recursively for each |
| 75 | // device when its uevent is handled, results in multiple restorecon operations being done on a |
| 76 | // given file. It is more efficient to simply do restorecon recursively on /sys during cold boot, |
| 77 | // than to do restorecon on each device as its uevent is handled. This only applies to cold boot; |
| 78 | // once that has completed, restorecon is done for each device as its uevent is handled. |
| 79 | |
| 80 | // With all of the above considered, the cold boot process has the below steps: |
| 81 | // 1) ueventd regenerates uevents by doing the /sys traversal and listens to the netlink socket for |
| 82 | // the generated uevents. It writes these uevents into a queue represented by a vector. |
| 83 | // |
| 84 | // 2) ueventd forks 'n' separate uevent handler subprocesses and has each of them to handle the |
| 85 | // uevents in the queue based on a starting offset (their process number) and a stride (the total |
| 86 | // number of processes). Note that no IPC happens at this point and only const functions from |
| 87 | // DeviceHandler should be called from this context. |
| 88 | // |
| 89 | // 3) In parallel to the subprocesses handling the uevents, the main thread of ueventd calls |
| 90 | // selinux_android_restorecon() recursively on /sys/class, /sys/block, and /sys/devices. |
| 91 | // |
| 92 | // 4) Once the restorecon operation finishes, the main thread calls waitpid() to wait for all |
| 93 | // subprocess handlers to complete and exit. Once this happens, it marks coldboot as having |
| 94 | // completed. |
| 95 | // |
| 96 | // At this point, ueventd is single threaded, poll()'s and then handles any future uevents. |
| 97 | |
| 98 | // Lastly, it should be noted that uevents that occur during the coldboot process are handled |
| 99 | // without issue after the coldboot process completes. This is because the uevent listener is |
| 100 | // paused while the uevent handler and restorecon actions take place. Once coldboot completes, |
| 101 | // the uevent listener resumes in polling mode and will handle the uevents that occurred during |
| 102 | // coldboot. |
| 103 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 104 | namespace android { |
| 105 | namespace init { |
| 106 | |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 107 | class ColdBoot { |
| 108 | public: |
| 109 | ColdBoot(UeventListener& uevent_listener, DeviceHandler& device_handler) |
| 110 | : uevent_listener_(uevent_listener), |
| 111 | device_handler_(device_handler), |
| 112 | num_handler_subprocesses_(std::thread::hardware_concurrency() ?: 4) {} |
| 113 | |
| 114 | void Run(); |
| 115 | |
| 116 | private: |
| 117 | void UeventHandlerMain(unsigned int process_num, unsigned int total_processes); |
| 118 | void RegenerateUevents(); |
| 119 | void ForkSubProcesses(); |
| 120 | void DoRestoreCon(); |
| 121 | void WaitForSubProcesses(); |
| 122 | |
| 123 | UeventListener& uevent_listener_; |
| 124 | DeviceHandler& device_handler_; |
| 125 | |
| 126 | unsigned int num_handler_subprocesses_; |
| 127 | std::vector<Uevent> uevent_queue_; |
| 128 | |
| 129 | std::set<pid_t> subprocess_pids_; |
| 130 | }; |
| 131 | |
| 132 | void ColdBoot::UeventHandlerMain(unsigned int process_num, unsigned int total_processes) { |
| 133 | for (unsigned int i = process_num; i < uevent_queue_.size(); i += total_processes) { |
| 134 | auto& uevent = uevent_queue_[i]; |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 135 | device_handler_.HandleDeviceEvent(uevent); |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 136 | } |
| 137 | _exit(EXIT_SUCCESS); |
| 138 | } |
| 139 | |
| 140 | void ColdBoot::RegenerateUevents() { |
| 141 | uevent_listener_.RegenerateUevents([this](const Uevent& uevent) { |
| 142 | HandleFirmwareEvent(uevent); |
| 143 | |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 144 | uevent_queue_.emplace_back(std::move(uevent)); |
Sandeep Patil | 4cbedee | 2017-06-21 13:02:57 -0700 | [diff] [blame] | 145 | return ListenerAction::kContinue; |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 146 | }); |
| 147 | } |
| 148 | |
| 149 | void ColdBoot::ForkSubProcesses() { |
| 150 | for (unsigned int i = 0; i < num_handler_subprocesses_; ++i) { |
| 151 | auto pid = fork(); |
| 152 | if (pid < 0) { |
| 153 | PLOG(FATAL) << "fork() failed!"; |
| 154 | } |
| 155 | |
| 156 | if (pid == 0) { |
| 157 | UeventHandlerMain(i, num_handler_subprocesses_); |
| 158 | } |
| 159 | |
| 160 | subprocess_pids_.emplace(pid); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | void ColdBoot::DoRestoreCon() { |
Tom Cherry | d2fd54e | 2017-06-07 14:32:30 -0700 | [diff] [blame] | 165 | selinux_android_restorecon("/sys", SELINUX_ANDROID_RESTORECON_RECURSE); |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 166 | device_handler_.set_skip_restorecon(false); |
| 167 | } |
| 168 | |
| 169 | void ColdBoot::WaitForSubProcesses() { |
| 170 | // Treat subprocesses that crash or get stuck the same as if ueventd itself has crashed or gets |
| 171 | // stuck. |
| 172 | // |
| 173 | // When a subprocess crashes, we fatally abort from ueventd. init will restart ueventd when |
| 174 | // init reaps it, and the cold boot process will start again. If this continues to fail, then |
| 175 | // since ueventd is marked as a critical service, init will reboot to recovery. |
| 176 | // |
| 177 | // When a subprocess gets stuck, keep ueventd spinning waiting for it. init has a timeout for |
| 178 | // cold boot and will reboot to the bootloader if ueventd does not complete in time. |
| 179 | while (!subprocess_pids_.empty()) { |
| 180 | int status; |
| 181 | pid_t pid = TEMP_FAILURE_RETRY(waitpid(-1, &status, 0)); |
| 182 | if (pid == -1) { |
| 183 | PLOG(ERROR) << "waitpid() failed"; |
| 184 | continue; |
| 185 | } |
| 186 | |
| 187 | auto it = std::find(subprocess_pids_.begin(), subprocess_pids_.end(), pid); |
| 188 | if (it == subprocess_pids_.end()) continue; |
| 189 | |
| 190 | if (WIFEXITED(status)) { |
| 191 | if (WEXITSTATUS(status) == EXIT_SUCCESS) { |
| 192 | subprocess_pids_.erase(it); |
| 193 | } else { |
| 194 | LOG(FATAL) << "subprocess exited with status " << WEXITSTATUS(status); |
| 195 | } |
| 196 | } else if (WIFSIGNALED(status)) { |
| 197 | LOG(FATAL) << "subprocess killed by signal " << WTERMSIG(status); |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | void ColdBoot::Run() { |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 203 | android::base::Timer cold_boot_timer; |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 204 | |
| 205 | RegenerateUevents(); |
| 206 | |
| 207 | ForkSubProcesses(); |
| 208 | |
| 209 | DoRestoreCon(); |
| 210 | |
| 211 | WaitForSubProcesses(); |
| 212 | |
| 213 | close(open(COLDBOOT_DONE, O_WRONLY | O_CREAT | O_CLOEXEC, 0000)); |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 214 | LOG(INFO) << "Coldboot took " << cold_boot_timer.duration().count() / 1000.0f << " seconds"; |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 217 | DeviceHandler CreateDeviceHandler() { |
| 218 | Parser parser; |
| 219 | |
| 220 | std::vector<Subsystem> subsystems; |
| 221 | parser.AddSectionParser("subsystem", std::make_unique<SubsystemParser>(&subsystems)); |
| 222 | |
| 223 | using namespace std::placeholders; |
| 224 | std::vector<SysfsPermissions> sysfs_permissions; |
| 225 | std::vector<Permissions> dev_permissions; |
Tom Cherry | b592dd8 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 226 | parser.AddSingleLineParser("/sys/", |
| 227 | std::bind(ParsePermissionsLine, _1, &sysfs_permissions, nullptr)); |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 228 | parser.AddSingleLineParser("/dev/", |
Tom Cherry | b592dd8 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 229 | std::bind(ParsePermissionsLine, _1, nullptr, &dev_permissions)); |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 230 | |
| 231 | parser.ParseConfig("/ueventd.rc"); |
| 232 | parser.ParseConfig("/vendor/ueventd.rc"); |
| 233 | parser.ParseConfig("/odm/ueventd.rc"); |
| 234 | |
| 235 | /* |
| 236 | * keep the current product name base configuration so |
| 237 | * we remain backwards compatible and allow it to override |
| 238 | * everything |
| 239 | * TODO: cleanup platform ueventd.rc to remove vendor specific |
| 240 | * device node entries (b/34968103) |
| 241 | */ |
| 242 | std::string hardware = android::base::GetProperty("ro.hardware", ""); |
| 243 | parser.ParseConfig("/ueventd." + hardware + ".rc"); |
| 244 | |
| 245 | return DeviceHandler(std::move(dev_permissions), std::move(sysfs_permissions), |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 246 | std::move(subsystems), true); |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 249 | int ueventd_main(int argc, char** argv) { |
Nick Kralevich | 6ebf12f | 2012-03-26 09:09:11 -0700 | [diff] [blame] | 250 | /* |
| 251 | * init sets the umask to 077 for forked processes. We need to |
| 252 | * create files with exact permissions, without modification by |
| 253 | * the umask. |
| 254 | */ |
| 255 | umask(000); |
| 256 | |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 257 | InitKernelLogging(argv); |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 258 | |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 259 | LOG(INFO) << "ueventd started!"; |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 260 | |
Tom Cherry | c3692b3 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 261 | SelinuxSetupKernelLogging(); |
| 262 | SelabelInitialize(); |
Stephen Smalley | 439224e | 2014-06-24 13:45:43 -0400 | [diff] [blame] | 263 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 264 | DeviceHandler device_handler = CreateDeviceHandler(); |
| 265 | UeventListener uevent_listener; |
Sandeep Patil | bf298e6 | 2017-02-03 07:18:36 -0800 | [diff] [blame] | 266 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 267 | if (access(COLDBOOT_DONE, F_OK) != 0) { |
Tom Cherry | c583305 | 2017-05-16 15:35:41 -0700 | [diff] [blame] | 268 | ColdBoot cold_boot(uevent_listener, device_handler); |
| 269 | cold_boot.Run(); |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 270 | } |
Elliott Hughes | 2145779 | 2015-02-04 10:19:50 -0800 | [diff] [blame] | 271 | |
Tom Cherry | 0f296e0 | 2017-06-30 12:58:39 -0700 | [diff] [blame] | 272 | // We use waitpid() in ColdBoot, so we can't ignore SIGCHLD until now. |
| 273 | signal(SIGCHLD, SIG_IGN); |
| 274 | // Reap and pending children that exited between the last call to waitpid() and setting SIG_IGN |
| 275 | // for SIGCHLD above. |
| 276 | while (waitpid(-1, nullptr, WNOHANG) > 0) { |
| 277 | } |
| 278 | |
Sandeep Patil | 4cbedee | 2017-06-21 13:02:57 -0700 | [diff] [blame] | 279 | uevent_listener.Poll([&device_handler](const Uevent& uevent) { |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 280 | HandleFirmwareEvent(uevent); |
| 281 | device_handler.HandleDeviceEvent(uevent); |
Sandeep Patil | 4cbedee | 2017-06-21 13:02:57 -0700 | [diff] [blame] | 282 | return ListenerAction::kContinue; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 283 | }); |
| 284 | |
Elliott Hughes | 2145779 | 2015-02-04 10:19:50 -0800 | [diff] [blame] | 285 | return 0; |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 286 | } |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 287 | |
| 288 | } // namespace init |
| 289 | } // namespace android |