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> |
| 21 | #include <poll.h> |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 22 | #include <signal.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 26 | |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 27 | #include <android-base/logging.h> |
Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 28 | #include <android-base/properties.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 29 | #include <android-base/stringprintf.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 30 | #include <selinux/selinux.h> |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 31 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 32 | #include "devices.h" |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 33 | #include "log.h" |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 34 | #include "util.h" |
Vladimir Chtchetkine | 2b99543 | 2011-09-28 09:55:31 -0700 | [diff] [blame] | 35 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 36 | int ueventd_main(int argc, char **argv) |
| 37 | { |
Nick Kralevich | 6ebf12f | 2012-03-26 09:09:11 -0700 | [diff] [blame] | 38 | /* |
| 39 | * init sets the umask to 077 for forked processes. We need to |
| 40 | * create files with exact permissions, without modification by |
| 41 | * the umask. |
| 42 | */ |
| 43 | umask(000); |
| 44 | |
| 45 | /* Prevent fire-and-forget children from becoming zombies. |
| 46 | * If we should need to wait() for some children in the future |
| 47 | * (as opposed to none right now), double-forking here instead |
| 48 | * of ignoring SIGCHLD may be the better solution. |
| 49 | */ |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 50 | signal(SIGCHLD, SIG_IGN); |
| 51 | |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 52 | InitKernelLogging(argv); |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 53 | |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 54 | LOG(INFO) << "ueventd started!"; |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 55 | |
| 56 | selinux_callback cb; |
| 57 | cb.func_log = selinux_klog_callback; |
Stephen Smalley | 439224e | 2014-06-24 13:45:43 -0400 | [diff] [blame] | 58 | selinux_set_callback(SELINUX_CB_LOG, cb); |
| 59 | |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 60 | Parser& parser = Parser::GetInstance(); |
| 61 | parser.AddSectionParser("subsystem", std::make_unique<SubsystemParser>()); |
| 62 | using namespace std::placeholders; |
| 63 | parser.AddSingleLineParser("/sys/", std::bind(ParsePermissionsLine, _1, _2, true)); |
| 64 | parser.AddSingleLineParser("/dev/", std::bind(ParsePermissionsLine, _1, _2, false)); |
| 65 | parser.ParseConfig("/ueventd.rc"); |
| 66 | parser.ParseConfig("/vendor/ueventd.rc"); |
| 67 | parser.ParseConfig("/odm/ueventd.rc"); |
Sandeep Patil | bf298e6 | 2017-02-03 07:18:36 -0800 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | * keep the current product name base configuration so |
| 71 | * we remain backwards compatible and allow it to override |
| 72 | * everything |
| 73 | * TODO: cleanup platform ueventd.rc to remove vendor specific |
| 74 | * device node entries (b/34968103) |
| 75 | */ |
Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 76 | std::string hardware = android::base::GetProperty("ro.hardware", ""); |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 77 | parser.ParseConfig("/ueventd." + hardware + ".rc"); |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 78 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 79 | device_init(); |
| 80 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 81 | pollfd ufd; |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 82 | ufd.events = POLLIN; |
| 83 | ufd.fd = get_device_fd(); |
| 84 | |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 85 | while (true) { |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 86 | ufd.revents = 0; |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 87 | int nr = poll(&ufd, 1, -1); |
| 88 | if (nr <= 0) { |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 89 | continue; |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 90 | } |
| 91 | if (ufd.revents & POLLIN) { |
| 92 | handle_device_fd(); |
| 93 | } |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 94 | } |
Elliott Hughes | 2145779 | 2015-02-04 10:19:50 -0800 | [diff] [blame] | 95 | |
| 96 | return 0; |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 97 | } |