blob: bd21a3e9523126571fbc2686a98fd7016865f03a [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>
Brian Swetland8d48c8e2011-03-24 15:45:30 -070021#include <signal.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Brian Swetland8d48c8e2011-03-24 15:45:30 -070025
Tom Cherry3f5eaae52017-04-06 16:30:22 -070026#include <android-base/logging.h>
Tom Cherryccf23532017-03-28 16:40:41 -070027#include <android-base/properties.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080028#include <android-base/stringprintf.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070029#include <selinux/selinux.h>
Colin Crossf83d0b92010-04-21 12:04:20 -070030
Colin Crossf83d0b92010-04-21 12:04:20 -070031#include "devices.h"
Tom Cherryed506f72017-05-25 15:58:59 -070032#include "firmware_handler.h"
Tom Cherry3f5eaae52017-04-06 16:30:22 -070033#include "log.h"
Tom Cherryed506f72017-05-25 15:58:59 -070034#include "uevent_listener.h"
35#include "ueventd_parser.h"
Tom Cherry3f5eaae52017-04-06 16:30:22 -070036#include "util.h"
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -070037
Tom Cherryed506f72017-05-25 15:58:59 -070038DeviceHandler 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 Crossf83d0b92010-04-21 12:04:20 -070070int ueventd_main(int argc, char **argv)
71{
Nick Kralevich6ebf12f2012-03-26 09:09:11 -070072 /*
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 Swetland8d48c8e2011-03-24 15:45:30 -070084 signal(SIGCHLD, SIG_IGN);
85
Elliott Hughesf86b5a62016-06-24 15:12:21 -070086 InitKernelLogging(argv);
Colin Crossf83d0b92010-04-21 12:04:20 -070087
Elliott Hughesf86b5a62016-06-24 15:12:21 -070088 LOG(INFO) << "ueventd started!";
Elliott Hughesda40c002015-03-27 23:20:44 -070089
90 selinux_callback cb;
91 cb.func_log = selinux_klog_callback;
Stephen Smalley439224e2014-06-24 13:45:43 -040092 selinux_set_callback(SELINUX_CB_LOG, cb);
93
Tom Cherryed506f72017-05-25 15:58:59 -070094 DeviceHandler device_handler = CreateDeviceHandler();
95 UeventListener uevent_listener;
Sandeep Patilbf298e62017-02-03 07:18:36 -080096
Tom Cherryed506f72017-05-25 15:58:59 -070097 if (access(COLDBOOT_DONE, F_OK) != 0) {
98 Timer t;
Colin Cross44b65d02010-04-20 14:32:50 -070099
Tom Cherryed506f72017-05-25 15:58:59 -0700100 uevent_listener.RegenerateUevents([&device_handler](const Uevent& uevent) {
101 HandleFirmwareEvent(uevent);
102 device_handler.HandleDeviceEvent(uevent);
103 return RegenerationAction::kContinue;
104 });
Colin Crossf83d0b92010-04-21 12:04:20 -0700105
Tom Cherryed506f72017-05-25 15:58:59 -0700106 close(open(COLDBOOT_DONE, O_WRONLY | O_CREAT | O_CLOEXEC, 0000));
107 LOG(INFO) << "Coldboot took " << t;
Colin Crossf83d0b92010-04-21 12:04:20 -0700108 }
Elliott Hughes21457792015-02-04 10:19:50 -0800109
Tom Cherryed506f72017-05-25 15:58:59 -0700110 uevent_listener.DoPolling([&device_handler](const Uevent& uevent) {
111 HandleFirmwareEvent(uevent);
112 device_handler.HandleDeviceEvent(uevent);
113 });
114
Elliott Hughes21457792015-02-04 10:19:50 -0800115 return 0;
Colin Crossf83d0b92010-04-21 12:04:20 -0700116}