blob: d51ffde0e30c913e7039608c7feed8479dbfaa61 [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>
23#include <private/android_filesystem_config.h>
Colin Crossf83d0b92010-04-21 12:04:20 -070024
25#include "ueventd.h"
26#include "log.h"
27#include "util.h"
28#include "devices.h"
Colin Cross44b65d02010-04-20 14:32:50 -070029#include "ueventd_parser.h"
30
31static char hardware[32];
32static unsigned revision = 0;
Colin Crossf83d0b92010-04-21 12:04:20 -070033
34int ueventd_main(int argc, char **argv)
35{
36 struct pollfd ufd;
37 int nr;
Colin Cross44b65d02010-04-20 14:32:50 -070038 char tmp[32];
Colin Crossf83d0b92010-04-21 12:04:20 -070039
40 open_devnull_stdio();
41 log_init();
42
43 INFO("starting ueventd\n");
44
Colin Cross44b65d02010-04-20 14:32:50 -070045 get_hardware_name(hardware, &revision);
46
47 ueventd_parse_config_file("/ueventd.rc");
48
49 snprintf(tmp, sizeof(tmp), "/ueventd.%s.rc", hardware);
50 ueventd_parse_config_file(tmp);
51
Colin Crossf83d0b92010-04-21 12:04:20 -070052 device_init();
53
54 ufd.events = POLLIN;
55 ufd.fd = get_device_fd();
56
57 while(1) {
58 ufd.revents = 0;
59 nr = poll(&ufd, 1, -1);
60 if (nr <= 0)
61 continue;
62 if (ufd.revents == POLLIN)
63 handle_device_fd();
64 }
65}
Colin Cross44b65d02010-04-20 14:32:50 -070066
67static int get_android_id(const char *id)
68{
69 unsigned int i;
70 for (i = 0; i < ARRAY_SIZE(android_ids); i++)
71 if (!strcmp(id, android_ids[i].name))
72 return android_ids[i].aid;
73 return 0;
74}
75
76void set_device_permission(int nargs, char **args)
77{
78 char *name;
79 mode_t perm;
80 uid_t uid;
81 gid_t gid;
82 int prefix = 0;
83 char *endptr;
84 int ret;
85 char *tmp = 0;
86
87 if (nargs == 0)
88 return;
89
90 if (args[0][0] == '#')
91 return;
92
93 if (nargs != 4) {
94 ERROR("invalid line ueventd.rc line for '%s'\n", args[0]);
95 return;
96 }
97
98 name = args[0];
99 /* If path starts with mtd@ lookup the mount number. */
100 if (!strncmp(name, "mtd@", 4)) {
101 int n = mtd_name_to_number(name + 4);
102 if (n >= 0)
103 asprintf(&tmp, "/dev/mtd/mtd%d", n);
104 name = tmp;
105 } else {
106 int len = strlen(name);
107 if (name[len - 1] == '*') {
108 prefix = 1;
109 name[len - 1] = '\0';
110 }
111 }
112
113 perm = strtol(args[1], &endptr, 8);
114 if (!endptr || *endptr != '\0') {
115 ERROR("invalid mode '%s'\n", args[1]);
116 free(tmp);
117 return;
118 }
119
120 ret = get_android_id(args[2]);
121 if (ret < 0) {
122 ERROR("invalid uid '%s'\n", args[2]);
123 free(tmp);
124 return;
125 }
126 uid = ret;
127
128 ret = get_android_id(args[3]);
129 if (ret < 0) {
130 ERROR("invalid gid '%s'\n", args[3]);
131 free(tmp);
132 return;
133 }
134 gid = ret;
135
136 add_dev_perms(name, perm, uid, gid, prefix);
137 free(tmp);
138}