blob: 8c0c574b653444c3c973a7aa99839b465c17f325 [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
Colin Crossf83d0b92010-04-21 12:04:20 -070036int ueventd_main(int argc, char **argv)
37{
Nick Kralevich6ebf12f2012-03-26 09:09:11 -070038 /*
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 Swetland8d48c8e2011-03-24 15:45:30 -070050 signal(SIGCHLD, SIG_IGN);
51
Elliott Hughesf86b5a62016-06-24 15:12:21 -070052 InitKernelLogging(argv);
Colin Crossf83d0b92010-04-21 12:04:20 -070053
Elliott Hughesf86b5a62016-06-24 15:12:21 -070054 LOG(INFO) << "ueventd started!";
Elliott Hughesda40c002015-03-27 23:20:44 -070055
56 selinux_callback cb;
57 cb.func_log = selinux_klog_callback;
Stephen Smalley439224e2014-06-24 13:45:43 -040058 selinux_set_callback(SELINUX_CB_LOG, cb);
59
Tom Cherryfe062052017-04-24 16:59:05 -070060 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 Patilbf298e62017-02-03 07:18:36 -080068
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 Cherryccf23532017-03-28 16:40:41 -070076 std::string hardware = android::base::GetProperty("ro.hardware", "");
Tom Cherryfe062052017-04-24 16:59:05 -070077 parser.ParseConfig("/ueventd." + hardware + ".rc");
Colin Cross44b65d02010-04-20 14:32:50 -070078
Colin Crossf83d0b92010-04-21 12:04:20 -070079 device_init();
80
Elliott Hughesda40c002015-03-27 23:20:44 -070081 pollfd ufd;
Colin Crossf83d0b92010-04-21 12:04:20 -070082 ufd.events = POLLIN;
83 ufd.fd = get_device_fd();
84
Elliott Hughesda40c002015-03-27 23:20:44 -070085 while (true) {
Colin Crossf83d0b92010-04-21 12:04:20 -070086 ufd.revents = 0;
Elliott Hughesda40c002015-03-27 23:20:44 -070087 int nr = poll(&ufd, 1, -1);
88 if (nr <= 0) {
Colin Crossf83d0b92010-04-21 12:04:20 -070089 continue;
Elliott Hughesda40c002015-03-27 23:20:44 -070090 }
91 if (ufd.revents & POLLIN) {
92 handle_device_fd();
93 }
Colin Crossf83d0b92010-04-21 12:04:20 -070094 }
Elliott Hughes21457792015-02-04 10:19:50 -080095
96 return 0;
Colin Crossf83d0b92010-04-21 12:04:20 -070097}