ueventd.c revision 8f91282ebe1963b9d27f8779ad1342302b293bd2
1/*
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>
18#include <fcntl.h>
19#include <string.h>
20#include <stdlib.h>
21#include <stdio.h>
22#include <ctype.h>
23#include <signal.h>
24
25#include <private/android_filesystem_config.h>
26
27#include "ueventd.h"
28#include "log.h"
29#include "util.h"
30#include "devices.h"
31#include "ueventd_parser.h"
32
33static char hardware[32];
34static unsigned revision = 0;
35
36int ueventd_main(int argc, char **argv)
37{
38    struct pollfd ufd;
39    int nr;
40    char tmp[32];
41
42        /* 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
49    open_devnull_stdio();
50    klog_init();
51
52    INFO("starting ueventd\n");
53
54    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
61    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}
75
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;
88    char *attr = 0;
89    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
103    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
112    if (nargs != 4) {
113        ERROR("invalid line ueventd.rc line for '%s'\n", args[0]);
114        return;
115    }
116
117    /* 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
154    add_dev_perms(name, attr, perm, uid, gid, prefix);
155    free(tmp);
156}
157