blob: 249739b1e030262b7d0c9bb4605b2c8c0476de22 [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
Colin Cross44b65d02010-04-20 14:32:50 -070017#include <ctype.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070018#include <fcntl.h>
19#include <poll.h>
Brian Swetland8d48c8e2011-03-24 15:45:30 -070020#include <signal.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
Brian Swetland8d48c8e2011-03-24 15:45:30 -070024
Elliott Hughes4f713192015-12-04 22:00:26 -080025#include <android-base/stringprintf.h>
Colin Cross44b65d02010-04-20 14:32:50 -070026#include <private/android_filesystem_config.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070027#include <selinux/selinux.h>
Colin Crossf83d0b92010-04-21 12:04:20 -070028
29#include "ueventd.h"
30#include "log.h"
31#include "util.h"
32#include "devices.h"
Colin Cross44b65d02010-04-20 14:32:50 -070033#include "ueventd_parser.h"
Rom Lemarchand74b34f32015-02-27 17:20:29 -080034#include "property_service.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
Colin Crossf83d0b92010-04-21 12:04:20 -070052 open_devnull_stdio();
Dima Zavin8f912822011-08-31 18:26:17 -070053 klog_init();
Elliott Hughesda40c002015-03-27 23:20:44 -070054 klog_set_level(KLOG_NOTICE_LEVEL);
Colin Crossf83d0b92010-04-21 12:04:20 -070055
Elliott Hughesda40c002015-03-27 23:20:44 -070056 NOTICE("ueventd started!\n");
57
58 selinux_callback cb;
59 cb.func_log = selinux_klog_callback;
Stephen Smalley439224e2014-06-24 13:45:43 -040060 selinux_set_callback(SELINUX_CB_LOG, cb);
61
Yabin Cui74edcea2015-07-24 10:11:05 -070062 std::string hardware = property_get("ro.hardware");
Colin Cross44b65d02010-04-20 14:32:50 -070063
64 ueventd_parse_config_file("/ueventd.rc");
Yabin Cui74edcea2015-07-24 10:11:05 -070065 ueventd_parse_config_file(android::base::StringPrintf("/ueventd.%s.rc", hardware.c_str()).c_str());
Colin Cross44b65d02010-04-20 14:32:50 -070066
Colin Crossf83d0b92010-04-21 12:04:20 -070067 device_init();
68
Elliott Hughesda40c002015-03-27 23:20:44 -070069 pollfd ufd;
Colin Crossf83d0b92010-04-21 12:04:20 -070070 ufd.events = POLLIN;
71 ufd.fd = get_device_fd();
72
Elliott Hughesda40c002015-03-27 23:20:44 -070073 while (true) {
Colin Crossf83d0b92010-04-21 12:04:20 -070074 ufd.revents = 0;
Elliott Hughesda40c002015-03-27 23:20:44 -070075 int nr = poll(&ufd, 1, -1);
76 if (nr <= 0) {
Colin Crossf83d0b92010-04-21 12:04:20 -070077 continue;
Elliott Hughesda40c002015-03-27 23:20:44 -070078 }
79 if (ufd.revents & POLLIN) {
80 handle_device_fd();
81 }
Colin Crossf83d0b92010-04-21 12:04:20 -070082 }
Elliott Hughes21457792015-02-04 10:19:50 -080083
84 return 0;
Colin Crossf83d0b92010-04-21 12:04:20 -070085}
Colin Cross44b65d02010-04-20 14:32:50 -070086
87static int get_android_id(const char *id)
88{
89 unsigned int i;
90 for (i = 0; i < ARRAY_SIZE(android_ids); i++)
91 if (!strcmp(id, android_ids[i].name))
92 return android_ids[i].aid;
Veeren Mandalia4f97fd92012-08-02 15:20:49 -070093 return -1;
Colin Cross44b65d02010-04-20 14:32:50 -070094}
95
96void set_device_permission(int nargs, char **args)
97{
98 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070099 char *attr = 0;
Colin Cross44b65d02010-04-20 14:32:50 -0700100 mode_t perm;
101 uid_t uid;
102 gid_t gid;
103 int prefix = 0;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700104 int wildcard = 0;
Colin Cross44b65d02010-04-20 14:32:50 -0700105 char *endptr;
106 int ret;
107 char *tmp = 0;
108
109 if (nargs == 0)
110 return;
111
112 if (args[0][0] == '#')
113 return;
114
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700115 name = args[0];
116
117 if (!strncmp(name,"/sys/", 5) && (nargs == 5)) {
118 INFO("/sys/ rule %s %s\n",args[0],args[1]);
119 attr = args[1];
120 args++;
121 nargs--;
122 }
123
Colin Cross44b65d02010-04-20 14:32:50 -0700124 if (nargs != 4) {
125 ERROR("invalid line ueventd.rc line for '%s'\n", args[0]);
126 return;
127 }
128
Colin Cross44b65d02010-04-20 14:32:50 -0700129 /* If path starts with mtd@ lookup the mount number. */
130 if (!strncmp(name, "mtd@", 4)) {
131 int n = mtd_name_to_number(name + 4);
132 if (n >= 0)
133 asprintf(&tmp, "/dev/mtd/mtd%d", n);
134 name = tmp;
135 } else {
136 int len = strlen(name);
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700137 char *wildcard_chr = strchr(name, '*');
138 if ((name[len - 1] == '*') &&
139 (wildcard_chr == (name + len - 1))) {
Colin Cross44b65d02010-04-20 14:32:50 -0700140 prefix = 1;
141 name[len - 1] = '\0';
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700142 } else if (wildcard_chr) {
143 wildcard = 1;
Colin Cross44b65d02010-04-20 14:32:50 -0700144 }
145 }
146
147 perm = strtol(args[1], &endptr, 8);
148 if (!endptr || *endptr != '\0') {
149 ERROR("invalid mode '%s'\n", args[1]);
150 free(tmp);
151 return;
152 }
153
154 ret = get_android_id(args[2]);
155 if (ret < 0) {
156 ERROR("invalid uid '%s'\n", args[2]);
157 free(tmp);
158 return;
159 }
160 uid = ret;
161
162 ret = get_android_id(args[3]);
163 if (ret < 0) {
164 ERROR("invalid gid '%s'\n", args[3]);
165 free(tmp);
166 return;
167 }
168 gid = ret;
169
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700170 add_dev_perms(name, attr, perm, uid, gid, prefix, wildcard);
Colin Cross44b65d02010-04-20 14:32:50 -0700171 free(tmp);
172}