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> |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 25 | |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 26 | #include <android-base/logging.h> |
Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 27 | #include <android-base/properties.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 28 | #include <android-base/stringprintf.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 29 | #include <selinux/selinux.h> |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 30 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 31 | #include "devices.h" |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 32 | #include "firmware_handler.h" |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 33 | #include "log.h" |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 34 | #include "uevent_listener.h" |
| 35 | #include "ueventd_parser.h" |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 36 | #include "util.h" |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 37 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 38 | DeviceHandler CreateDeviceHandler() { |
| 39 | Parser parser; |
| 40 | |
| 41 | std::vector<Subsystem> subsystems; |
| 42 | parser.AddSectionParser("subsystem", std::make_unique<SubsystemParser>(&subsystems)); |
| 43 | |
| 44 | using namespace std::placeholders; |
| 45 | std::vector<SysfsPermissions> sysfs_permissions; |
| 46 | std::vector<Permissions> dev_permissions; |
| 47 | parser.AddSingleLineParser( |
| 48 | "/sys/", std::bind(ParsePermissionsLine, _1, _2, &sysfs_permissions, nullptr)); |
| 49 | parser.AddSingleLineParser("/dev/", |
| 50 | std::bind(ParsePermissionsLine, _1, _2, nullptr, &dev_permissions)); |
| 51 | |
| 52 | parser.ParseConfig("/ueventd.rc"); |
| 53 | parser.ParseConfig("/vendor/ueventd.rc"); |
| 54 | parser.ParseConfig("/odm/ueventd.rc"); |
| 55 | |
| 56 | /* |
| 57 | * keep the current product name base configuration so |
| 58 | * we remain backwards compatible and allow it to override |
| 59 | * everything |
| 60 | * TODO: cleanup platform ueventd.rc to remove vendor specific |
| 61 | * device node entries (b/34968103) |
| 62 | */ |
| 63 | std::string hardware = android::base::GetProperty("ro.hardware", ""); |
| 64 | parser.ParseConfig("/ueventd." + hardware + ".rc"); |
| 65 | |
| 66 | return DeviceHandler(std::move(dev_permissions), std::move(sysfs_permissions), |
| 67 | std::move(subsystems)); |
| 68 | } |
| 69 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 70 | int ueventd_main(int argc, char **argv) |
| 71 | { |
Nick Kralevich | 6ebf12f | 2012-03-26 09:09:11 -0700 | [diff] [blame] | 72 | /* |
| 73 | * init sets the umask to 077 for forked processes. We need to |
| 74 | * create files with exact permissions, without modification by |
| 75 | * the umask. |
| 76 | */ |
| 77 | umask(000); |
| 78 | |
| 79 | /* Prevent fire-and-forget children from becoming zombies. |
| 80 | * If we should need to wait() for some children in the future |
| 81 | * (as opposed to none right now), double-forking here instead |
| 82 | * of ignoring SIGCHLD may be the better solution. |
| 83 | */ |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 84 | signal(SIGCHLD, SIG_IGN); |
| 85 | |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 86 | InitKernelLogging(argv); |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 87 | |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 88 | LOG(INFO) << "ueventd started!"; |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 89 | |
| 90 | selinux_callback cb; |
| 91 | cb.func_log = selinux_klog_callback; |
Stephen Smalley | 439224e | 2014-06-24 13:45:43 -0400 | [diff] [blame] | 92 | selinux_set_callback(SELINUX_CB_LOG, cb); |
| 93 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 94 | DeviceHandler device_handler = CreateDeviceHandler(); |
| 95 | UeventListener uevent_listener; |
Sandeep Patil | bf298e6 | 2017-02-03 07:18:36 -0800 | [diff] [blame] | 96 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 97 | if (access(COLDBOOT_DONE, F_OK) != 0) { |
| 98 | Timer t; |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 99 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 100 | uevent_listener.RegenerateUevents([&device_handler](const Uevent& uevent) { |
| 101 | HandleFirmwareEvent(uevent); |
| 102 | device_handler.HandleDeviceEvent(uevent); |
| 103 | return RegenerationAction::kContinue; |
| 104 | }); |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 105 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 106 | close(open(COLDBOOT_DONE, O_WRONLY | O_CREAT | O_CLOEXEC, 0000)); |
| 107 | LOG(INFO) << "Coldboot took " << t; |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 108 | } |
Elliott Hughes | 2145779 | 2015-02-04 10:19:50 -0800 | [diff] [blame] | 109 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 110 | uevent_listener.DoPolling([&device_handler](const Uevent& uevent) { |
| 111 | HandleFirmwareEvent(uevent); |
| 112 | device_handler.HandleDeviceEvent(uevent); |
| 113 | }); |
| 114 | |
Elliott Hughes | 2145779 | 2015-02-04 10:19:50 -0800 | [diff] [blame] | 115 | return 0; |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 116 | } |