blob: 4ad0cb969dd3c7cc38790e281791bff16d7f706e [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
17#include <poll.h>
Colin Cross44b65d02010-04-20 14:32:50 -070018#include <fcntl.h>
19#include <string.h>
20#include <stdlib.h>
21#include <stdio.h>
22#include <ctype.h>
Brian Swetland8d48c8e2011-03-24 15:45:30 -070023#include <signal.h>
24
Colin Cross44b65d02010-04-20 14:32:50 -070025#include <private/android_filesystem_config.h>
Colin Crossf83d0b92010-04-21 12:04:20 -070026
27#include "ueventd.h"
28#include "log.h"
29#include "util.h"
30#include "devices.h"
Colin Cross44b65d02010-04-20 14:32:50 -070031#include "ueventd_parser.h"
32
33static char hardware[32];
34static unsigned revision = 0;
Colin Crossf83d0b92010-04-21 12:04:20 -070035
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -070036static void import_kernel_nv(char *name, int in_qemu)
37{
38 if (*name != '\0') {
39 char *value = strchr(name, '=');
40 if (value != NULL) {
41 *value++ = 0;
42 if (!strcmp(name,"androidboot.hardware"))
43 {
44 strlcpy(hardware, value, sizeof(hardware));
45 }
46 }
47 }
48}
49
Colin Crossf83d0b92010-04-21 12:04:20 -070050int ueventd_main(int argc, char **argv)
51{
52 struct pollfd ufd;
53 int nr;
Colin Cross44b65d02010-04-20 14:32:50 -070054 char tmp[32];
Colin Crossf83d0b92010-04-21 12:04:20 -070055
Nick Kralevich6ebf12f2012-03-26 09:09:11 -070056 /*
57 * init sets the umask to 077 for forked processes. We need to
58 * create files with exact permissions, without modification by
59 * the umask.
60 */
61 umask(000);
62
63 /* Prevent fire-and-forget children from becoming zombies.
64 * If we should need to wait() for some children in the future
65 * (as opposed to none right now), double-forking here instead
66 * of ignoring SIGCHLD may be the better solution.
67 */
Brian Swetland8d48c8e2011-03-24 15:45:30 -070068 signal(SIGCHLD, SIG_IGN);
69
Colin Crossf83d0b92010-04-21 12:04:20 -070070 open_devnull_stdio();
Dima Zavin8f912822011-08-31 18:26:17 -070071 klog_init();
Alex Ray18ccc1b2014-03-06 15:07:42 -080072#if LOG_UEVENTS
73 /* Ensure we're at a logging level that will show the events */
Alex Rayeb6ffc72014-03-19 15:08:22 -070074 if (klog_get_level() < KLOG_INFO_LEVEL) {
75 klog_set_level(KLOG_INFO_LEVEL);
Alex Ray18ccc1b2014-03-06 15:07:42 -080076 }
77#endif
Colin Crossf83d0b92010-04-21 12:04:20 -070078
79 INFO("starting ueventd\n");
80
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -070081 /* Respect hardware passed in through the kernel cmd line. Here we will look
82 * for androidboot.hardware param in kernel cmdline, and save its value in
83 * hardware[]. */
84 import_kernel_cmdline(0, import_kernel_nv);
85
Colin Cross44b65d02010-04-20 14:32:50 -070086 get_hardware_name(hardware, &revision);
87
88 ueventd_parse_config_file("/ueventd.rc");
89
90 snprintf(tmp, sizeof(tmp), "/ueventd.%s.rc", hardware);
91 ueventd_parse_config_file(tmp);
92
Colin Crossf83d0b92010-04-21 12:04:20 -070093 device_init();
94
95 ufd.events = POLLIN;
96 ufd.fd = get_device_fd();
97
98 while(1) {
99 ufd.revents = 0;
100 nr = poll(&ufd, 1, -1);
101 if (nr <= 0)
102 continue;
Amir Goldstein1d4e86c2013-11-10 15:36:58 +0200103 if (ufd.revents & POLLIN)
Colin Crossf83d0b92010-04-21 12:04:20 -0700104 handle_device_fd();
105 }
106}
Colin Cross44b65d02010-04-20 14:32:50 -0700107
108static int get_android_id(const char *id)
109{
110 unsigned int i;
111 for (i = 0; i < ARRAY_SIZE(android_ids); i++)
112 if (!strcmp(id, android_ids[i].name))
113 return android_ids[i].aid;
Veeren Mandalia4f97fd92012-08-02 15:20:49 -0700114 return -1;
Colin Cross44b65d02010-04-20 14:32:50 -0700115}
116
117void set_device_permission(int nargs, char **args)
118{
119 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700120 char *attr = 0;
Colin Cross44b65d02010-04-20 14:32:50 -0700121 mode_t perm;
122 uid_t uid;
123 gid_t gid;
124 int prefix = 0;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700125 int wildcard = 0;
Colin Cross44b65d02010-04-20 14:32:50 -0700126 char *endptr;
127 int ret;
128 char *tmp = 0;
129
130 if (nargs == 0)
131 return;
132
133 if (args[0][0] == '#')
134 return;
135
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700136 name = args[0];
137
138 if (!strncmp(name,"/sys/", 5) && (nargs == 5)) {
139 INFO("/sys/ rule %s %s\n",args[0],args[1]);
140 attr = args[1];
141 args++;
142 nargs--;
143 }
144
Colin Cross44b65d02010-04-20 14:32:50 -0700145 if (nargs != 4) {
146 ERROR("invalid line ueventd.rc line for '%s'\n", args[0]);
147 return;
148 }
149
Colin Cross44b65d02010-04-20 14:32:50 -0700150 /* If path starts with mtd@ lookup the mount number. */
151 if (!strncmp(name, "mtd@", 4)) {
152 int n = mtd_name_to_number(name + 4);
153 if (n >= 0)
154 asprintf(&tmp, "/dev/mtd/mtd%d", n);
155 name = tmp;
156 } else {
157 int len = strlen(name);
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700158 char *wildcard_chr = strchr(name, '*');
159 if ((name[len - 1] == '*') &&
160 (wildcard_chr == (name + len - 1))) {
Colin Cross44b65d02010-04-20 14:32:50 -0700161 prefix = 1;
162 name[len - 1] = '\0';
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700163 } else if (wildcard_chr) {
164 wildcard = 1;
Colin Cross44b65d02010-04-20 14:32:50 -0700165 }
166 }
167
168 perm = strtol(args[1], &endptr, 8);
169 if (!endptr || *endptr != '\0') {
170 ERROR("invalid mode '%s'\n", args[1]);
171 free(tmp);
172 return;
173 }
174
175 ret = get_android_id(args[2]);
176 if (ret < 0) {
177 ERROR("invalid uid '%s'\n", args[2]);
178 free(tmp);
179 return;
180 }
181 uid = ret;
182
183 ret = get_android_id(args[3]);
184 if (ret < 0) {
185 ERROR("invalid gid '%s'\n", args[3]);
186 free(tmp);
187 return;
188 }
189 gid = ret;
190
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700191 add_dev_perms(name, attr, perm, uid, gid, prefix, wildcard);
Colin Cross44b65d02010-04-20 14:32:50 -0700192 free(tmp);
193}