blob: 1328d19a57408bb76ab7d716f864aad17aae5fd4 [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
36int ueventd_main(int argc, char **argv)
37{
38 struct pollfd ufd;
39 int nr;
Colin Cross44b65d02010-04-20 14:32:50 -070040 char tmp[32];
Colin Crossf83d0b92010-04-21 12:04:20 -070041
Brian Swetland8d48c8e2011-03-24 15:45:30 -070042 /* Prevent fire-and-forget children from becoming zombies.
43 * If we should need to wait() for some children in the future
44 * (as opposed to none right now), double-forking here instead
45 * of ignoring SIGCHLD may be the better solution.
46 */
47 signal(SIGCHLD, SIG_IGN);
48
Colin Crossf83d0b92010-04-21 12:04:20 -070049 open_devnull_stdio();
50 log_init();
51
52 INFO("starting ueventd\n");
53
Colin Cross44b65d02010-04-20 14:32:50 -070054 get_hardware_name(hardware, &revision);
55
56 ueventd_parse_config_file("/ueventd.rc");
57
58 snprintf(tmp, sizeof(tmp), "/ueventd.%s.rc", hardware);
59 ueventd_parse_config_file(tmp);
60
Colin Crossf83d0b92010-04-21 12:04:20 -070061 device_init();
62
63 ufd.events = POLLIN;
64 ufd.fd = get_device_fd();
65
66 while(1) {
67 ufd.revents = 0;
68 nr = poll(&ufd, 1, -1);
69 if (nr <= 0)
70 continue;
71 if (ufd.revents == POLLIN)
72 handle_device_fd();
73 }
74}
Colin Cross44b65d02010-04-20 14:32:50 -070075
76static int get_android_id(const char *id)
77{
78 unsigned int i;
79 for (i = 0; i < ARRAY_SIZE(android_ids); i++)
80 if (!strcmp(id, android_ids[i].name))
81 return android_ids[i].aid;
82 return 0;
83}
84
85void set_device_permission(int nargs, char **args)
86{
87 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070088 char *attr = 0;
Colin Cross44b65d02010-04-20 14:32:50 -070089 mode_t perm;
90 uid_t uid;
91 gid_t gid;
92 int prefix = 0;
93 char *endptr;
94 int ret;
95 char *tmp = 0;
96
97 if (nargs == 0)
98 return;
99
100 if (args[0][0] == '#')
101 return;
102
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700103 name = args[0];
104
105 if (!strncmp(name,"/sys/", 5) && (nargs == 5)) {
106 INFO("/sys/ rule %s %s\n",args[0],args[1]);
107 attr = args[1];
108 args++;
109 nargs--;
110 }
111
Colin Cross44b65d02010-04-20 14:32:50 -0700112 if (nargs != 4) {
113 ERROR("invalid line ueventd.rc line for '%s'\n", args[0]);
114 return;
115 }
116
Colin Cross44b65d02010-04-20 14:32:50 -0700117 /* If path starts with mtd@ lookup the mount number. */
118 if (!strncmp(name, "mtd@", 4)) {
119 int n = mtd_name_to_number(name + 4);
120 if (n >= 0)
121 asprintf(&tmp, "/dev/mtd/mtd%d", n);
122 name = tmp;
123 } else {
124 int len = strlen(name);
125 if (name[len - 1] == '*') {
126 prefix = 1;
127 name[len - 1] = '\0';
128 }
129 }
130
131 perm = strtol(args[1], &endptr, 8);
132 if (!endptr || *endptr != '\0') {
133 ERROR("invalid mode '%s'\n", args[1]);
134 free(tmp);
135 return;
136 }
137
138 ret = get_android_id(args[2]);
139 if (ret < 0) {
140 ERROR("invalid uid '%s'\n", args[2]);
141 free(tmp);
142 return;
143 }
144 uid = ret;
145
146 ret = get_android_id(args[3]);
147 if (ret < 0) {
148 ERROR("invalid gid '%s'\n", args[3]);
149 free(tmp);
150 return;
151 }
152 gid = ret;
153
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700154 add_dev_perms(name, attr, perm, uid, gid, prefix);
Colin Cross44b65d02010-04-20 14:32:50 -0700155 free(tmp);
156}