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 <ctype.h>
18 #include <fcntl.h>
19 #include <grp.h>
20 #include <poll.h>
21 #include <pwd.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <sys/types.h>
28
29 #include <android-base/properties.h>
30 #include <android-base/stringprintf.h>
31 #include <selinux/selinux.h>
32
33 #include "ueventd.h"
34 #include "log.h"
35 #include "util.h"
36 #include "devices.h"
37 #include "ueventd_parser.h"
38
ueventd_main(int argc,char ** argv)39 int ueventd_main(int argc, char **argv)
40 {
41 /*
42 * init sets the umask to 077 for forked processes. We need to
43 * create files with exact permissions, without modification by
44 * the umask.
45 */
46 umask(000);
47
48 /* Prevent fire-and-forget children from becoming zombies.
49 * If we should need to wait() for some children in the future
50 * (as opposed to none right now), double-forking here instead
51 * of ignoring SIGCHLD may be the better solution.
52 */
53 signal(SIGCHLD, SIG_IGN);
54
55 InitKernelLogging(argv);
56
57 LOG(INFO) << "ueventd started!";
58
59 selinux_callback cb;
60 cb.func_log = selinux_klog_callback;
61 selinux_set_callback(SELINUX_CB_LOG, cb);
62
63 ueventd_parse_config_file("/ueventd.rc");
64 ueventd_parse_config_file("/vendor/ueventd.rc");
65 ueventd_parse_config_file("/odm/ueventd.rc");
66
67 /*
68 * keep the current product name base configuration so
69 * we remain backwards compatible and allow it to override
70 * everything
71 * TODO: cleanup platform ueventd.rc to remove vendor specific
72 * device node entries (b/34968103)
73 */
74 std::string hardware = android::base::GetProperty("ro.hardware", "");
75 ueventd_parse_config_file(android::base::StringPrintf("/ueventd.%s.rc", hardware.c_str()).c_str());
76
77 device_init();
78
79 pollfd ufd;
80 ufd.events = POLLIN;
81 ufd.fd = get_device_fd();
82
83 while (true) {
84 ufd.revents = 0;
85 int nr = poll(&ufd, 1, -1);
86 if (nr <= 0) {
87 continue;
88 }
89 if (ufd.revents & POLLIN) {
90 handle_device_fd();
91 }
92 }
93
94 return 0;
95 }
96
set_device_permission(int nargs,char ** args)97 void set_device_permission(int nargs, char **args)
98 {
99 char *name;
100 char *attr = 0;
101 mode_t perm;
102 uid_t uid;
103 gid_t gid;
104 int prefix = 0;
105 int wildcard = 0;
106 char *endptr;
107
108 if (nargs == 0)
109 return;
110
111 if (args[0][0] == '#')
112 return;
113
114 name = args[0];
115
116 if (!strncmp(name,"/sys/", 5) && (nargs == 5)) {
117 LOG(INFO) << "/sys/ rule " << args[0] << " " << args[1];
118 attr = args[1];
119 args++;
120 nargs--;
121 }
122
123 if (nargs != 4) {
124 LOG(ERROR) << "invalid line ueventd.rc line for '" << args[0] << "'";
125 return;
126 }
127
128 int len = strlen(name);
129 char *wildcard_chr = strchr(name, '*');
130 if ((name[len - 1] == '*') && (wildcard_chr == (name + len - 1))) {
131 prefix = 1;
132 name[len - 1] = '\0';
133 } else if (wildcard_chr) {
134 wildcard = 1;
135 }
136
137 perm = strtol(args[1], &endptr, 8);
138 if (!endptr || *endptr != '\0') {
139 LOG(ERROR) << "invalid mode '" << args[1] << "'";
140 return;
141 }
142
143 struct passwd* pwd = getpwnam(args[2]);
144 if (!pwd) {
145 LOG(ERROR) << "invalid uid '" << args[2] << "'";
146 return;
147 }
148 uid = pwd->pw_uid;
149
150 struct group* grp = getgrnam(args[3]);
151 if (!grp) {
152 LOG(ERROR) << "invalid gid '" << args[3] << "'";
153 return;
154 }
155 gid = grp->gr_gid;
156
157 if (add_dev_perms(name, attr, perm, uid, gid, prefix, wildcard) != 0) {
158 PLOG(ERROR) << "add_dev_perms(name=" << name <<
159 ", attr=" << attr <<
160 ", perm=" << std::oct << perm << std::dec <<
161 ", uid=" << uid << ", gid=" << gid <<
162 ", prefix=" << prefix << ", wildcard=" << wildcard <<
163 ")";
164 return;
165 }
166 }
167