blob: 963cc4df9bf66c7349cc68d2c59a7ec5ba3b5957 [file] [log] [blame]
Colin Crossf83d0b92010-04-21 12:04:20 -07001/*
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 Cherry3f5eaae52017-04-06 16:30:22 -070017#include "ueventd.h"
18
Colin Cross44b65d02010-04-20 14:32:50 -070019#include <ctype.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070020#include <fcntl.h>
21#include <poll.h>
Brian Swetland8d48c8e2011-03-24 15:45:30 -070022#include <signal.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Brian Swetland8d48c8e2011-03-24 15:45:30 -070026
Tom Cherry3f5eaae52017-04-06 16:30:22 -070027#include <android-base/logging.h>
Tom Cherryccf23532017-03-28 16:40:41 -070028#include <android-base/properties.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080029#include <android-base/stringprintf.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070030#include <selinux/selinux.h>
Colin Crossf83d0b92010-04-21 12:04:20 -070031
Colin Crossf83d0b92010-04-21 12:04:20 -070032#include "devices.h"
Tom Cherry3f5eaae52017-04-06 16:30:22 -070033#include "log.h"
Tom Cherry3f5eaae52017-04-06 16:30:22 -070034#include "util.h"
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -070035
Tom Cherryfe062052017-04-24 16:59:05 -070036template <bool sysfs>
37static bool ParseSingleLine(std::vector<std::string>&& line, std::string* err);
38
Colin Crossf83d0b92010-04-21 12:04:20 -070039int ueventd_main(int argc, char **argv)
40{
Nick Kralevich6ebf12f2012-03-26 09:09:11 -070041 /*
42 * init sets the umask to 077 for forked processes. We need to
43 * create files with exact permissions, without modification by
44 * the umask.
45 */
46 umask(000);
47
48 /* Prevent fire-and-forget children from becoming zombies.
49 * If we should need to wait() for some children in the future
50 * (as opposed to none right now), double-forking here instead
51 * of ignoring SIGCHLD may be the better solution.
52 */
Brian Swetland8d48c8e2011-03-24 15:45:30 -070053 signal(SIGCHLD, SIG_IGN);
54
Elliott Hughesf86b5a62016-06-24 15:12:21 -070055 InitKernelLogging(argv);
Colin Crossf83d0b92010-04-21 12:04:20 -070056
Elliott Hughesf86b5a62016-06-24 15:12:21 -070057 LOG(INFO) << "ueventd started!";
Elliott Hughesda40c002015-03-27 23:20:44 -070058
59 selinux_callback cb;
60 cb.func_log = selinux_klog_callback;
Stephen Smalley439224e2014-06-24 13:45:43 -040061 selinux_set_callback(SELINUX_CB_LOG, cb);
62
Tom Cherryfe062052017-04-24 16:59:05 -070063 Parser& parser = Parser::GetInstance();
64 parser.AddSectionParser("subsystem", std::make_unique<SubsystemParser>());
65 using namespace std::placeholders;
66 parser.AddSingleLineParser("/sys/", std::bind(ParsePermissionsLine, _1, _2, true));
67 parser.AddSingleLineParser("/dev/", std::bind(ParsePermissionsLine, _1, _2, false));
68 parser.ParseConfig("/ueventd.rc");
69 parser.ParseConfig("/vendor/ueventd.rc");
70 parser.ParseConfig("/odm/ueventd.rc");
Sandeep Patilbf298e62017-02-03 07:18:36 -080071
72 /*
73 * keep the current product name base configuration so
74 * we remain backwards compatible and allow it to override
75 * everything
76 * TODO: cleanup platform ueventd.rc to remove vendor specific
77 * device node entries (b/34968103)
78 */
Tom Cherryccf23532017-03-28 16:40:41 -070079 std::string hardware = android::base::GetProperty("ro.hardware", "");
Tom Cherryfe062052017-04-24 16:59:05 -070080 parser.ParseConfig("/ueventd." + hardware + ".rc");
Colin Cross44b65d02010-04-20 14:32:50 -070081
Colin Crossf83d0b92010-04-21 12:04:20 -070082 device_init();
83
Elliott Hughesda40c002015-03-27 23:20:44 -070084 pollfd ufd;
Colin Crossf83d0b92010-04-21 12:04:20 -070085 ufd.events = POLLIN;
86 ufd.fd = get_device_fd();
87
Elliott Hughesda40c002015-03-27 23:20:44 -070088 while (true) {
Colin Crossf83d0b92010-04-21 12:04:20 -070089 ufd.revents = 0;
Elliott Hughesda40c002015-03-27 23:20:44 -070090 int nr = poll(&ufd, 1, -1);
91 if (nr <= 0) {
Colin Crossf83d0b92010-04-21 12:04:20 -070092 continue;
Elliott Hughesda40c002015-03-27 23:20:44 -070093 }
94 if (ufd.revents & POLLIN) {
95 handle_device_fd();
96 }
Colin Crossf83d0b92010-04-21 12:04:20 -070097 }
Elliott Hughes21457792015-02-04 10:19:50 -080098
99 return 0;
Colin Crossf83d0b92010-04-21 12:04:20 -0700100}