blob: c63fdaa53564b496f717a5a664c8b523cd89bc11 [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 Hughesda40c002015-03-27 23:20:44 -070025#include <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
Elliott Hughesda40c002015-03-27 23:20:44 -070062 char hardware[PROP_VALUE_MAX];
Rom Lemarchand74b34f32015-02-27 17:20:29 -080063 property_get("ro.hardware", hardware);
Colin Cross44b65d02010-04-20 14:32:50 -070064
65 ueventd_parse_config_file("/ueventd.rc");
Elliott Hughesda40c002015-03-27 23:20:44 -070066 ueventd_parse_config_file(android::base::StringPrintf("/ueventd.%s.rc", hardware).c_str());
Colin Cross44b65d02010-04-20 14:32:50 -070067
Colin Crossf83d0b92010-04-21 12:04:20 -070068 device_init();
69
Elliott Hughesda40c002015-03-27 23:20:44 -070070 pollfd ufd;
Colin Crossf83d0b92010-04-21 12:04:20 -070071 ufd.events = POLLIN;
72 ufd.fd = get_device_fd();
73
Elliott Hughesda40c002015-03-27 23:20:44 -070074 while (true) {
Colin Crossf83d0b92010-04-21 12:04:20 -070075 ufd.revents = 0;
Elliott Hughesda40c002015-03-27 23:20:44 -070076 int nr = poll(&ufd, 1, -1);
77 if (nr <= 0) {
Colin Crossf83d0b92010-04-21 12:04:20 -070078 continue;
Elliott Hughesda40c002015-03-27 23:20:44 -070079 }
80 if (ufd.revents & POLLIN) {
81 handle_device_fd();
82 }
Colin Crossf83d0b92010-04-21 12:04:20 -070083 }
Elliott Hughes21457792015-02-04 10:19:50 -080084
85 return 0;
Colin Crossf83d0b92010-04-21 12:04:20 -070086}
Colin Cross44b65d02010-04-20 14:32:50 -070087
88static int get_android_id(const char *id)
89{
90 unsigned int i;
91 for (i = 0; i < ARRAY_SIZE(android_ids); i++)
92 if (!strcmp(id, android_ids[i].name))
93 return android_ids[i].aid;
Veeren Mandalia4f97fd92012-08-02 15:20:49 -070094 return -1;
Colin Cross44b65d02010-04-20 14:32:50 -070095}
96
97void set_device_permission(int nargs, char **args)
98{
99 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700100 char *attr = 0;
Colin Cross44b65d02010-04-20 14:32:50 -0700101 mode_t perm;
102 uid_t uid;
103 gid_t gid;
104 int prefix = 0;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700105 int wildcard = 0;
Colin Cross44b65d02010-04-20 14:32:50 -0700106 char *endptr;
107 int ret;
108 char *tmp = 0;
109
110 if (nargs == 0)
111 return;
112
113 if (args[0][0] == '#')
114 return;
115
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700116 name = args[0];
117
118 if (!strncmp(name,"/sys/", 5) && (nargs == 5)) {
119 INFO("/sys/ rule %s %s\n",args[0],args[1]);
120 attr = args[1];
121 args++;
122 nargs--;
123 }
124
Colin Cross44b65d02010-04-20 14:32:50 -0700125 if (nargs != 4) {
126 ERROR("invalid line ueventd.rc line for '%s'\n", args[0]);
127 return;
128 }
129
Colin Cross44b65d02010-04-20 14:32:50 -0700130 /* If path starts with mtd@ lookup the mount number. */
131 if (!strncmp(name, "mtd@", 4)) {
132 int n = mtd_name_to_number(name + 4);
133 if (n >= 0)
134 asprintf(&tmp, "/dev/mtd/mtd%d", n);
135 name = tmp;
136 } else {
137 int len = strlen(name);
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700138 char *wildcard_chr = strchr(name, '*');
139 if ((name[len - 1] == '*') &&
140 (wildcard_chr == (name + len - 1))) {
Colin Cross44b65d02010-04-20 14:32:50 -0700141 prefix = 1;
142 name[len - 1] = '\0';
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700143 } else if (wildcard_chr) {
144 wildcard = 1;
Colin Cross44b65d02010-04-20 14:32:50 -0700145 }
146 }
147
148 perm = strtol(args[1], &endptr, 8);
149 if (!endptr || *endptr != '\0') {
150 ERROR("invalid mode '%s'\n", args[1]);
151 free(tmp);
152 return;
153 }
154
155 ret = get_android_id(args[2]);
156 if (ret < 0) {
157 ERROR("invalid uid '%s'\n", args[2]);
158 free(tmp);
159 return;
160 }
161 uid = ret;
162
163 ret = get_android_id(args[3]);
164 if (ret < 0) {
165 ERROR("invalid gid '%s'\n", args[3]);
166 free(tmp);
167 return;
168 }
169 gid = ret;
170
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700171 add_dev_perms(name, attr, perm, uid, gid, prefix, wildcard);
Colin Cross44b65d02010-04-20 14:32:50 -0700172 free(tmp);
173}