blob: b55933cbabd76ccc5d6d2a638bc1bcc94cf4e78e [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
Mark Salyzyn154f4602014-02-20 14:59:07 -08002 * Copyright (C) 2007-2014 The Android Open Source Project
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07003 *
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 <errno.h>
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070018#include <fnmatch.h>
Olivier Baillyb93e5812010-11-17 11:47:23 -080019#include <stddef.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070020#include <stdio.h>
21#include <stdlib.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24
25#include <fcntl.h>
26#include <dirent.h>
27#include <unistd.h>
28#include <string.h>
29
30#include <sys/socket.h>
31#include <sys/un.h>
32#include <linux/netlink.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050033
Stephen Smalleye46f9d52012-01-13 08:48:47 -050034#include <selinux/selinux.h>
35#include <selinux/label.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -040036#include <selinux/android.h>
Stephen Smalleye2eb69d2013-04-16 09:30:30 -040037#include <selinux/avc.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050038
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070039#include <private/android_filesystem_config.h>
40#include <sys/time.h>
Colin Cross982a8152010-06-03 12:21:01 -070041#include <sys/wait.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070042
Dima Zavinda04c522011-09-01 17:09:44 -070043#include <cutils/list.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100044#include <cutils/uevent.h>
45
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070046#include "devices.h"
Greg Hackmann3312aa82013-11-18 15:24:40 -080047#include "ueventd_parser.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070048#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070049#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070050
Mark Salyzyn154f4602014-02-20 14:59:07 -080051#define UNUSED __attribute__((__unused__))
52
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070053#define SYSFS_PREFIX "/sys"
Brian Swetland02863b92010-09-19 03:36:39 -070054#define FIRMWARE_DIR1 "/etc/firmware"
55#define FIRMWARE_DIR2 "/vendor/firmware"
Iliyan Malchev029d44e2012-06-08 10:42:26 -070056#define FIRMWARE_DIR3 "/firmware/image"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070057
Stephen Smalleye096e362012-06-11 13:37:39 -040058extern struct selabel_handle *sehandle;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050059
Colin Cross0dd7ca62010-04-13 19:25:51 -070060static int device_fd = -1;
61
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070062struct uevent {
63 const char *action;
64 const char *path;
65 const char *subsystem;
66 const char *firmware;
Colin Crossb0ab94b2010-04-08 16:16:20 -070067 const char *partition_name;
Wei Zhongf97b8872012-03-23 14:15:34 -070068 const char *device_name;
Colin Crossb0ab94b2010-04-08 16:16:20 -070069 int partition_num;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070070 int major;
71 int minor;
72};
73
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070074struct perms_ {
75 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070076 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070077 mode_t perm;
78 unsigned int uid;
79 unsigned int gid;
80 unsigned short prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070081 unsigned short wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070082};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070083
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070084struct perm_node {
85 struct perms_ dp;
86 struct listnode plist;
87};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070088
Colin Crossfadb85e2011-03-30 18:32:12 -070089struct platform_node {
90 char *name;
Dima Zavinf395c922013-03-06 16:23:57 -080091 char *path;
92 int path_len;
Colin Crossfadb85e2011-03-30 18:32:12 -070093 struct listnode list;
94};
95
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070096static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -070097static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -070098static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070099
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700100int add_dev_perms(const char *name, const char *attr,
101 mode_t perm, unsigned int uid, unsigned int gid,
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700102 unsigned short prefix,
103 unsigned short wildcard) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800104 struct perm_node *node = (perm_node*) calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700105 if (!node)
106 return -ENOMEM;
107
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700108 node->dp.name = strdup(name);
109 if (!node->dp.name)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700110 return -ENOMEM;
111
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700112 if (attr) {
113 node->dp.attr = strdup(attr);
114 if (!node->dp.attr)
115 return -ENOMEM;
116 }
117
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700118 node->dp.perm = perm;
119 node->dp.uid = uid;
120 node->dp.gid = gid;
121 node->dp.prefix = prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700122 node->dp.wildcard = wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700123
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700124 if (attr)
125 list_add_tail(&sys_perms, &node->plist);
126 else
127 list_add_tail(&dev_perms, &node->plist);
128
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700129 return 0;
130}
131
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700132void fixup_sys_perms(const char *upath)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700133{
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700134 char buf[512];
135 struct listnode *node;
136 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700137
Nick Kralevich3b4c0bd2014-07-03 16:04:34 -0700138 /* upaths omit the "/sys" that paths in this list
139 * contain, so we add 4 when comparing...
140 */
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700141 list_for_each(node, &sys_perms) {
142 dp = &(node_to_item(node, struct perm_node, plist))->dp;
143 if (dp->prefix) {
144 if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700145 continue;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700146 } else if (dp->wildcard) {
147 if (fnmatch(dp->name + 4, upath, FNM_PATHNAME) != 0)
148 continue;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700149 } else {
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700150 if (strcmp(upath, dp->name + 4))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700151 continue;
152 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700153
154 if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
Nick Kralevich3b4c0bd2014-07-03 16:04:34 -0700155 break;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700156
157 sprintf(buf,"/sys%s/%s", upath, dp->attr);
158 INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
159 chown(buf, dp->uid, dp->gid);
160 chmod(buf, dp->perm);
Nick Kralevich3b4c0bd2014-07-03 16:04:34 -0700161 }
162
163 // Now fixup SELinux file labels
164 int len = snprintf(buf, sizeof(buf), "/sys%s", upath);
165 if ((len < 0) || ((size_t) len >= sizeof(buf))) {
166 // Overflow
167 return;
168 }
169 if (access(buf, F_OK) == 0) {
170 INFO("restorecon_recursive: %s\n", buf);
171 restorecon_recursive(buf);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700172 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700173}
174
Colin Cross43d537e2014-07-02 13:08:13 -0700175static bool perm_path_matches(const char *path, struct perms_ *dp)
176{
177 if (dp->prefix) {
178 if (strncmp(path, dp->name, strlen(dp->name)) == 0)
179 return true;
180 } else if (dp->wildcard) {
181 if (fnmatch(dp->name, path, FNM_PATHNAME) == 0)
182 return true;
183 } else {
184 if (strcmp(path, dp->name) == 0)
185 return true;
186 }
187
188 return false;
189}
190
191static mode_t get_device_perm(const char *path, const char **links,
192 unsigned *uid, unsigned *gid)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700193{
Colin Cross44b65d02010-04-20 14:32:50 -0700194 struct listnode *node;
195 struct perm_node *perm_node;
196 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700197
Colin Cross44b65d02010-04-20 14:32:50 -0700198 /* search the perms list in reverse so that ueventd.$hardware can
199 * override ueventd.rc
200 */
201 list_for_each_reverse(node, &dev_perms) {
Colin Cross43d537e2014-07-02 13:08:13 -0700202 bool match = false;
203
Colin Cross44b65d02010-04-20 14:32:50 -0700204 perm_node = node_to_item(node, struct perm_node, plist);
205 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700206
Colin Cross43d537e2014-07-02 13:08:13 -0700207 if (perm_path_matches(path, dp)) {
208 match = true;
Colin Cross44b65d02010-04-20 14:32:50 -0700209 } else {
Colin Cross43d537e2014-07-02 13:08:13 -0700210 if (links) {
211 int i;
212 for (i = 0; links[i]; i++) {
213 if (perm_path_matches(links[i], dp)) {
214 match = true;
215 break;
216 }
217 }
218 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700219 }
Colin Cross43d537e2014-07-02 13:08:13 -0700220
221 if (match) {
222 *uid = dp->uid;
223 *gid = dp->gid;
224 return dp->perm;
225 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700226 }
Colin Cross44b65d02010-04-20 14:32:50 -0700227 /* Default if nothing found. */
228 *uid = 0;
229 *gid = 0;
230 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700231}
232
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700233static void make_device(const char *path,
Mark Salyzyn154f4602014-02-20 14:59:07 -0800234 const char *upath UNUSED,
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400235 int block, int major, int minor,
236 const char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700237{
238 unsigned uid;
239 unsigned gid;
240 mode_t mode;
241 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500242 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700243
Colin Cross43d537e2014-07-02 13:08:13 -0700244 mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700245
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500246 if (sehandle) {
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400247 selabel_lookup_best_match(sehandle, &secontext, path, links, mode);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500248 setfscreatecon(secontext);
249 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700250
Colin Cross17dcc5c2010-09-03 12:25:34 -0700251 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800252 /* Temporarily change egid to avoid race condition setting the gid of the
253 * device node. Unforunately changing the euid would prevent creation of
254 * some device nodes, so the uid has to be set with chown() and is still
255 * racy. Fixing the gid race at least fixed the issue with system_server
256 * opening dynamic input devices under the AID_INPUT gid. */
257 setegid(gid);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700258 mknod(path, mode, dev);
Nick Pelly6405c692010-01-21 18:13:39 -0800259 chown(path, uid, -1);
260 setegid(AID_ROOT);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700261
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500262 if (secontext) {
263 freecon(secontext);
264 setfscreatecon(NULL);
265 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700266}
267
Dima Zavinf395c922013-03-06 16:23:57 -0800268static void add_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700269{
Dima Zavinf395c922013-03-06 16:23:57 -0800270 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700271 struct listnode *node;
272 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800273 const char *name = path;
274
275 if (!strncmp(path, "/devices/", 9)) {
276 name += 9;
277 if (!strncmp(name, "platform/", 9))
278 name += 9;
279 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700280
281 list_for_each_reverse(node, &platform_names) {
282 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800283 if ((bus->path_len < path_len) &&
284 (path[bus->path_len] == '/') &&
285 !strncmp(path, bus->path, bus->path_len))
Colin Crossfadb85e2011-03-30 18:32:12 -0700286 /* subdevice of an existing platform, ignore it */
287 return;
288 }
289
Dima Zavinf395c922013-03-06 16:23:57 -0800290 INFO("adding platform device %s (%s)\n", name, path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700291
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800292 bus = (platform_node*) calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800293 bus->path = strdup(path);
294 bus->path_len = path_len;
295 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700296 list_add_tail(&platform_names, &bus->list);
297}
298
299/*
Dima Zavinf395c922013-03-06 16:23:57 -0800300 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700301 * platform device prefix. If it doesn't start with a platform device, return
302 * 0.
303 */
Dima Zavinf395c922013-03-06 16:23:57 -0800304static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700305{
Dima Zavinf395c922013-03-06 16:23:57 -0800306 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700307 struct listnode *node;
308 struct platform_node *bus;
309
310 list_for_each_reverse(node, &platform_names) {
311 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800312 if ((bus->path_len < path_len) &&
313 (path[bus->path_len] == '/') &&
314 !strncmp(path, bus->path, bus->path_len))
315 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700316 }
317
318 return NULL;
319}
320
Dima Zavinf395c922013-03-06 16:23:57 -0800321static void remove_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700322{
323 struct listnode *node;
324 struct platform_node *bus;
325
326 list_for_each_reverse(node, &platform_names) {
327 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800328 if (!strcmp(path, bus->path)) {
329 INFO("removing platform device %s\n", bus->name);
330 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700331 list_remove(node);
332 free(bus);
333 return;
334 }
335 }
336}
337
Andrew Boiea885d042013-09-13 17:41:20 -0700338/* Given a path that may start with a PCI device, populate the supplied buffer
339 * with the PCI domain/bus number and the peripheral ID and return 0.
340 * If it doesn't start with a PCI device, or there is some error, return -1 */
341static int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
342{
343 const char *start, *end;
344
345 if (strncmp(path, "/devices/pci", 12))
346 return -1;
347
348 /* Beginning of the prefix is the initial "pci" after "/devices/" */
349 start = path + 9;
350
351 /* End of the prefix is two path '/' later, capturing the domain/bus number
352 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
353 end = strchr(start, '/');
354 if (!end)
355 return -1;
356 end = strchr(end + 1, '/');
357 if (!end)
358 return -1;
359
360 /* Make sure we have enough room for the string plus null terminator */
361 if (end - start + 1 > buf_sz)
362 return -1;
363
364 strncpy(buf, start, end - start);
365 buf[end - start] = '\0';
366 return 0;
367}
368
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700369static inline suseconds_t get_usecs(void)
370{
371 struct timeval tv;
372 gettimeofday(&tv, 0);
373 return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
374}
375
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700376static void parse_event(const char *msg, struct uevent *uevent)
377{
378 uevent->action = "";
379 uevent->path = "";
380 uevent->subsystem = "";
381 uevent->firmware = "";
382 uevent->major = -1;
383 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700384 uevent->partition_name = NULL;
385 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700386 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700387
388 /* currently ignoring SEQNUM */
389 while(*msg) {
390 if(!strncmp(msg, "ACTION=", 7)) {
391 msg += 7;
392 uevent->action = msg;
393 } else if(!strncmp(msg, "DEVPATH=", 8)) {
394 msg += 8;
395 uevent->path = msg;
396 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
397 msg += 10;
398 uevent->subsystem = msg;
399 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
400 msg += 9;
401 uevent->firmware = msg;
402 } else if(!strncmp(msg, "MAJOR=", 6)) {
403 msg += 6;
404 uevent->major = atoi(msg);
405 } else if(!strncmp(msg, "MINOR=", 6)) {
406 msg += 6;
407 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700408 } else if(!strncmp(msg, "PARTN=", 6)) {
409 msg += 6;
410 uevent->partition_num = atoi(msg);
411 } else if(!strncmp(msg, "PARTNAME=", 9)) {
412 msg += 9;
413 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700414 } else if(!strncmp(msg, "DEVNAME=", 8)) {
415 msg += 8;
416 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700417 }
418
Wei Zhongf97b8872012-03-23 14:15:34 -0700419 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700420 while(*msg++)
421 ;
422 }
423
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800424 if (LOG_UEVENTS) {
425 INFO("event { '%s', '%s', '%s', '%s', %d, %d }\n",
426 uevent->action, uevent->path, uevent->subsystem,
427 uevent->firmware, uevent->major, uevent->minor);
428 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700429}
430
Benoit Gobyd2278632010-08-03 14:36:02 -0700431static char **get_character_device_symlinks(struct uevent *uevent)
432{
433 const char *parent;
434 char *slash;
435 char **links;
436 int link_num = 0;
437 int width;
Dima Zavinf395c922013-03-06 16:23:57 -0800438 struct platform_node *pdev;
Benoit Gobyd2278632010-08-03 14:36:02 -0700439
Dima Zavinf395c922013-03-06 16:23:57 -0800440 pdev = find_platform_device(uevent->path);
441 if (!pdev)
Benoit Gobyd2278632010-08-03 14:36:02 -0700442 return NULL;
443
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800444 links = (char**) malloc(sizeof(char *) * 2);
Benoit Gobyd2278632010-08-03 14:36:02 -0700445 if (!links)
446 return NULL;
447 memset(links, 0, sizeof(char *) * 2);
448
449 /* skip "/devices/platform/<driver>" */
Dima Zavinf395c922013-03-06 16:23:57 -0800450 parent = strchr(uevent->path + pdev->path_len, '/');
Tomasz Kondelfca58f42013-11-05 13:17:45 +0100451 if (!parent)
Benoit Gobyd2278632010-08-03 14:36:02 -0700452 goto err;
453
454 if (!strncmp(parent, "/usb", 4)) {
455 /* skip root hub name and device. use device interface */
456 while (*++parent && *parent != '/');
457 if (*parent)
458 while (*++parent && *parent != '/');
459 if (!*parent)
460 goto err;
461 slash = strchr(++parent, '/');
462 if (!slash)
463 goto err;
464 width = slash - parent;
465 if (width <= 0)
466 goto err;
467
468 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
469 link_num++;
470 else
471 links[link_num] = NULL;
472 mkdir("/dev/usb", 0755);
473 }
474 else {
475 goto err;
476 }
477
478 return links;
479err:
480 free(links);
481 return NULL;
482}
483
Andrew Boiea885d042013-09-13 17:41:20 -0700484static char **get_block_device_symlinks(struct uevent *uevent)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700485{
Colin Crossfadb85e2011-03-30 18:32:12 -0700486 const char *device;
Dima Zavinf395c922013-03-06 16:23:57 -0800487 struct platform_node *pdev;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700488 char *slash;
Andrew Boiea885d042013-09-13 17:41:20 -0700489 const char *type;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700490 char buf[256];
491 char link_path[256];
Colin Crossb0ab94b2010-04-08 16:16:20 -0700492 int link_num = 0;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700493 char *p;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700494
Dima Zavinf395c922013-03-06 16:23:57 -0800495 pdev = find_platform_device(uevent->path);
Andrew Boiea885d042013-09-13 17:41:20 -0700496 if (pdev) {
497 device = pdev->name;
498 type = "platform";
499 } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
500 device = buf;
501 type = "pci";
502 } else {
Dima Zavinf395c922013-03-06 16:23:57 -0800503 return NULL;
Andrew Boiea885d042013-09-13 17:41:20 -0700504 }
Dima Zavinf395c922013-03-06 16:23:57 -0800505
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800506 char **links = (char**) malloc(sizeof(char *) * 4);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700507 if (!links)
508 return NULL;
509 memset(links, 0, sizeof(char *) * 4);
510
Andrew Boiea885d042013-09-13 17:41:20 -0700511 INFO("found %s device %s\n", type, device);
Colin Crossfadb85e2011-03-30 18:32:12 -0700512
Andrew Boiea885d042013-09-13 17:41:20 -0700513 snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700514
515 if (uevent->partition_name) {
516 p = strdup(uevent->partition_name);
517 sanitize(p);
Johan Redestig93ca79b2012-04-18 16:41:19 +0200518 if (strcmp(uevent->partition_name, p))
519 NOTICE("Linking partition '%s' as '%s'\n", uevent->partition_name, p);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700520 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
521 link_num++;
522 else
523 links[link_num] = NULL;
524 free(p);
525 }
526
527 if (uevent->partition_num >= 0) {
528 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
529 link_num++;
530 else
531 links[link_num] = NULL;
532 }
533
Dima Zavinf395c922013-03-06 16:23:57 -0800534 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700535 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
536 link_num++;
537 else
538 links[link_num] = NULL;
539
540 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700541}
542
Colin Crosseb5ba832011-03-30 17:37:17 -0700543static void handle_device(const char *action, const char *devpath,
544 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700545{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700546 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700547
Colin Crosseb5ba832011-03-30 17:37:17 -0700548 if(!strcmp(action, "add")) {
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400549 make_device(devpath, path, block, major, minor, (const char **)links);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700550 if (links) {
551 for (i = 0; links[i]; i++)
552 make_link(devpath, links[i]);
553 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700554 }
555
Colin Crosseb5ba832011-03-30 17:37:17 -0700556 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700557 if (links) {
558 for (i = 0; links[i]; i++)
559 remove_link(devpath, links[i]);
560 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700561 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700562 }
563
564 if (links) {
565 for (i = 0; links[i]; i++)
566 free(links[i]);
567 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700568 }
569}
570
Colin Crossfadb85e2011-03-30 18:32:12 -0700571static void handle_platform_device_event(struct uevent *uevent)
572{
Dima Zavinf395c922013-03-06 16:23:57 -0800573 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700574
575 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800576 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700577 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800578 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700579}
580
Colin Crosseb5ba832011-03-30 17:37:17 -0700581static const char *parse_device_name(struct uevent *uevent, unsigned int len)
582{
583 const char *name;
584
585 /* if it's not a /dev device, nothing else to do */
586 if((uevent->major < 0) || (uevent->minor < 0))
587 return NULL;
588
589 /* do we have a name? */
590 name = strrchr(uevent->path, '/');
591 if(!name)
592 return NULL;
593 name++;
594
595 /* too-long names would overrun our buffer */
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800596 if(strlen(name) > len) {
597 ERROR("DEVPATH=%s exceeds %u-character limit on filename; ignoring event\n",
598 name, len);
Colin Crosseb5ba832011-03-30 17:37:17 -0700599 return NULL;
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800600 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700601
602 return name;
603}
604
605static void handle_block_device_event(struct uevent *uevent)
606{
607 const char *base = "/dev/block/";
608 const char *name;
609 char devpath[96];
610 char **links = NULL;
611
612 name = parse_device_name(uevent, 64);
613 if (!name)
614 return;
615
616 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500617 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700618
Dima Zavinf395c922013-03-06 16:23:57 -0800619 if (!strncmp(uevent->path, "/devices/", 9))
Andrew Boiea885d042013-09-13 17:41:20 -0700620 links = get_block_device_symlinks(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700621
622 handle_device(uevent->action, devpath, uevent->path, 1,
623 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700624}
625
Greg Hackmann3312aa82013-11-18 15:24:40 -0800626#define DEVPATH_LEN 96
627
628static bool assemble_devpath(char *devpath, const char *dirname,
629 const char *devname)
630{
631 int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
632 if (s < 0) {
633 ERROR("failed to assemble device path (%s); ignoring event\n",
634 strerror(errno));
635 return false;
636 } else if (s >= DEVPATH_LEN) {
637 ERROR("%s/%s exceeds %u-character limit on path; ignoring event\n",
638 dirname, devname, DEVPATH_LEN);
639 return false;
640 }
641 return true;
642}
643
644static void mkdir_recursive_for_devpath(const char *devpath)
645{
646 char dir[DEVPATH_LEN];
647 char *slash;
648
649 strcpy(dir, devpath);
650 slash = strrchr(dir, '/');
651 *slash = '\0';
652 mkdir_recursive(dir, 0755);
653}
654
Mark Salyzyn154f4602014-02-20 14:59:07 -0800655static inline void __attribute__((__deprecated__)) kernel_logger()
656{
657 INFO("kernel logger is deprecated\n");
658}
659
Colin Crosseb5ba832011-03-30 17:37:17 -0700660static void handle_generic_device_event(struct uevent *uevent)
661{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800662 const char *base;
Colin Crosseb5ba832011-03-30 17:37:17 -0700663 const char *name;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800664 char devpath[DEVPATH_LEN] = {0};
Colin Crosseb5ba832011-03-30 17:37:17 -0700665 char **links = NULL;
666
667 name = parse_device_name(uevent, 64);
668 if (!name)
669 return;
670
Greg Hackmann3312aa82013-11-18 15:24:40 -0800671 struct ueventd_subsystem *subsystem =
672 ueventd_subsystem_find_by_name(uevent->subsystem);
673
674 if (subsystem) {
675 const char *devname;
676
677 switch (subsystem->devname_src) {
678 case DEVNAME_UEVENT_DEVNAME:
679 devname = uevent->device_name;
680 break;
681
682 case DEVNAME_UEVENT_DEVPATH:
683 devname = name;
684 break;
685
686 default:
687 ERROR("%s subsystem's devpath option is not set; ignoring event\n",
688 uevent->subsystem);
689 return;
690 }
691
692 if (!assemble_devpath(devpath, subsystem->dirname, devname))
693 return;
694 mkdir_recursive_for_devpath(devpath);
695 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700696 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700697 if (uevent->device_name) {
Greg Hackmann3312aa82013-11-18 15:24:40 -0800698 if (!assemble_devpath(devpath, "/dev", uevent->device_name))
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800699 return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800700 mkdir_recursive_for_devpath(devpath);
Wei Zhongf97b8872012-03-23 14:15:34 -0700701 }
702 else {
703 /* This imitates the file system that would be created
704 * if we were using devfs instead.
705 * Minors are broken up into groups of 128, starting at "001"
706 */
707 int bus_id = uevent->minor / 128 + 1;
708 int device_id = uevent->minor % 128 + 1;
709 /* build directories */
710 make_dir("/dev/bus", 0755);
711 make_dir("/dev/bus/usb", 0755);
712 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
713 make_dir(devpath, 0755);
714 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
715 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700716 } else {
717 /* ignore other USB events */
718 return;
719 }
720 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
721 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500722 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200723 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
724 base = "/dev/dri/";
725 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700726 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
727 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500728 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700729 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
730 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500731 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700732 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
733 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500734 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700735 } else if(!strncmp(uevent->subsystem, "input", 5)) {
736 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500737 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700738 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
739 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500740 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700741 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
742 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500743 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700744 } else if(!strncmp(uevent->subsystem, "misc", 4) &&
745 !strncmp(name, "log_", 4)) {
Mark Salyzyn154f4602014-02-20 14:59:07 -0800746 kernel_logger();
Colin Crosseb5ba832011-03-30 17:37:17 -0700747 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500748 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700749 name += 4;
750 } else
751 base = "/dev/";
752 links = get_character_device_symlinks(uevent);
753
754 if (!devpath[0])
755 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
756
757 handle_device(uevent->action, devpath, uevent->path, 0,
758 uevent->major, uevent->minor, links);
759}
760
761static void handle_device_event(struct uevent *uevent)
762{
Ruchi Kandoi75b287b2014-04-29 19:14:37 -0700763 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
Colin Crosseb5ba832011-03-30 17:37:17 -0700764 fixup_sys_perms(uevent->path);
765
766 if (!strncmp(uevent->subsystem, "block", 5)) {
767 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700768 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
769 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700770 } else {
771 handle_generic_device_event(uevent);
772 }
773}
774
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700775static int load_firmware(int fw_fd, int loading_fd, int data_fd)
776{
777 struct stat st;
778 long len_to_copy;
779 int ret = 0;
780
781 if(fstat(fw_fd, &st) < 0)
782 return -1;
783 len_to_copy = st.st_size;
784
785 write(loading_fd, "1", 1); /* start transfer */
786
787 while (len_to_copy > 0) {
788 char buf[PAGE_SIZE];
789 ssize_t nr;
790
791 nr = read(fw_fd, buf, sizeof(buf));
792 if(!nr)
793 break;
794 if(nr < 0) {
795 ret = -1;
796 break;
797 }
798
799 len_to_copy -= nr;
800 while (nr > 0) {
801 ssize_t nw = 0;
802
803 nw = write(data_fd, buf + nw, nr);
804 if(nw <= 0) {
805 ret = -1;
806 goto out;
807 }
808 nr -= nw;
809 }
810 }
811
812out:
813 if(!ret)
814 write(loading_fd, "0", 1); /* successful end of transfer */
815 else
816 write(loading_fd, "-1", 2); /* abort transfer */
817
818 return ret;
819}
820
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700821static int is_booting(void)
822{
823 return access("/dev/.booting", F_OK) == 0;
824}
825
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700826static void process_firmware_event(struct uevent *uevent)
827{
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700828 char *root, *loading, *data, *file1 = NULL, *file2 = NULL, *file3 = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700829 int l, loading_fd, data_fd, fw_fd;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700830 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700831
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700832 INFO("firmware: loading '%s' for '%s'\n",
833 uevent->firmware, uevent->path);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700834
835 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
836 if (l == -1)
837 return;
838
839 l = asprintf(&loading, "%sloading", root);
840 if (l == -1)
841 goto root_free_out;
842
843 l = asprintf(&data, "%sdata", root);
844 if (l == -1)
845 goto loading_free_out;
846
Brian Swetland02863b92010-09-19 03:36:39 -0700847 l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
848 if (l == -1)
849 goto data_free_out;
850
851 l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700852 if (l == -1)
853 goto data_free_out;
854
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700855 l = asprintf(&file3, FIRMWARE_DIR3"/%s", uevent->firmware);
856 if (l == -1)
857 goto data_free_out;
858
Nick Kralevich45a884f2015-02-02 14:37:22 -0800859 loading_fd = open(loading, O_WRONLY|O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700860 if(loading_fd < 0)
861 goto file_free_out;
862
Nick Kralevich45a884f2015-02-02 14:37:22 -0800863 data_fd = open(data, O_WRONLY|O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700864 if(data_fd < 0)
865 goto loading_close_out;
866
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700867try_loading_again:
Nick Kralevich45a884f2015-02-02 14:37:22 -0800868 fw_fd = open(file1, O_RDONLY|O_CLOEXEC);
Brian Swetland02863b92010-09-19 03:36:39 -0700869 if(fw_fd < 0) {
Nick Kralevich45a884f2015-02-02 14:37:22 -0800870 fw_fd = open(file2, O_RDONLY|O_CLOEXEC);
Benoit Goby609d8822010-11-09 18:10:24 -0800871 if (fw_fd < 0) {
Nick Kralevich45a884f2015-02-02 14:37:22 -0800872 fw_fd = open(file3, O_RDONLY|O_CLOEXEC);
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700873 if (fw_fd < 0) {
874 if (booting) {
875 /* If we're not fully booted, we may be missing
876 * filesystems needed for firmware, wait and retry.
877 */
878 usleep(100000);
879 booting = is_booting();
880 goto try_loading_again;
881 }
882 INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno);
883 write(loading_fd, "-1", 2);
884 goto data_close_out;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700885 }
Benoit Goby609d8822010-11-09 18:10:24 -0800886 }
Brian Swetland02863b92010-09-19 03:36:39 -0700887 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700888
889 if(!load_firmware(fw_fd, loading_fd, data_fd))
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700890 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700891 else
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700892 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700893
894 close(fw_fd);
895data_close_out:
896 close(data_fd);
897loading_close_out:
898 close(loading_fd);
899file_free_out:
Brian Swetland02863b92010-09-19 03:36:39 -0700900 free(file1);
901 free(file2);
Ajay Dudani76c58892013-06-12 15:55:26 -0700902 free(file3);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700903data_free_out:
904 free(data);
905loading_free_out:
906 free(loading);
907root_free_out:
908 free(root);
909}
910
911static void handle_firmware_event(struct uevent *uevent)
912{
913 pid_t pid;
914
915 if(strcmp(uevent->subsystem, "firmware"))
916 return;
917
918 if(strcmp(uevent->action, "add"))
919 return;
920
921 /* we fork, to avoid making large memory allocations in init proper */
922 pid = fork();
923 if (!pid) {
924 process_firmware_event(uevent);
Kenny Root17baff42014-08-20 16:16:44 -0700925 _exit(EXIT_SUCCESS);
926 } else if (pid < 0) {
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800927 ERROR("could not fork to process firmware event: %s\n", strerror(errno));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700928 }
929}
930
Ruchi Kandoic6037202014-06-23 11:22:09 -0700931#define UEVENT_MSG_LEN 2048
Colin Cross0dd7ca62010-04-13 19:25:51 -0700932void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700933{
Vernon Tang3f582e92011-04-25 13:08:17 +1000934 char msg[UEVENT_MSG_LEN+2];
935 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700936 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700937 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700938 continue;
939
940 msg[n] = '\0';
941 msg[n+1] = '\0';
942
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700943 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700944 parse_event(msg, &uevent);
945
Stephen Smalleye2eb69d2013-04-16 09:30:30 -0400946 if (sehandle && selinux_status_updated() > 0) {
947 struct selabel_handle *sehandle2;
948 sehandle2 = selinux_android_file_context_handle();
949 if (sehandle2) {
950 selabel_close(sehandle);
951 sehandle = sehandle2;
952 }
953 }
954
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700955 handle_device_event(&uevent);
956 handle_firmware_event(&uevent);
957 }
958}
959
960/* Coldboot walks parts of the /sys tree and pokes the uevent files
961** to cause the kernel to regenerate device add events that happened
962** before init's device manager was started
963**
964** We drain any pending events from the netlink socket every time
965** we poke another uevent file to make sure we don't overrun the
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800966** socket's buffer.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700967*/
968
Colin Cross0dd7ca62010-04-13 19:25:51 -0700969static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700970{
971 struct dirent *de;
972 int dfd, fd;
973
974 dfd = dirfd(d);
975
976 fd = openat(dfd, "uevent", O_WRONLY);
977 if(fd >= 0) {
978 write(fd, "add\n", 4);
979 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700980 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700981 }
982
983 while((de = readdir(d))) {
984 DIR *d2;
985
986 if(de->d_type != DT_DIR || de->d_name[0] == '.')
987 continue;
988
989 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
990 if(fd < 0)
991 continue;
992
993 d2 = fdopendir(fd);
994 if(d2 == 0)
995 close(fd);
996 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700997 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700998 closedir(d2);
999 }
1000 }
1001}
1002
Colin Cross0dd7ca62010-04-13 19:25:51 -07001003static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001004{
1005 DIR *d = opendir(path);
1006 if(d) {
Colin Cross0dd7ca62010-04-13 19:25:51 -07001007 do_coldboot(d);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001008 closedir(d);
1009 }
1010}
1011
Colin Cross0dd7ca62010-04-13 19:25:51 -07001012void device_init(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001013{
1014 suseconds_t t0, t1;
Colin Crossf83d0b92010-04-21 12:04:20 -07001015 struct stat info;
1016 int fd;
Kenny Rootb5982bf2012-10-16 23:07:05 -07001017
Stephen Smalleyae6f3d72012-05-01 15:02:53 -04001018 sehandle = NULL;
1019 if (is_selinux_enabled() > 0) {
1020 sehandle = selinux_android_file_context_handle();
Stephen Smalleye2eb69d2013-04-16 09:30:30 -04001021 selinux_status_open(true);
Stephen Smalleyae6f3d72012-05-01 15:02:53 -04001022 }
Kenny Rootb5982bf2012-10-16 23:07:05 -07001023
Andrew Boied562ca72012-12-04 15:47:20 -08001024 /* is 256K enough? udev uses 16MB! */
1025 device_fd = uevent_open_socket(256*1024, true);
Colin Cross0dd7ca62010-04-13 19:25:51 -07001026 if(device_fd < 0)
1027 return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001028
Colin Cross0dd7ca62010-04-13 19:25:51 -07001029 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
1030 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001031
Andreas Gampea016c422014-11-24 19:52:41 -08001032 if (stat(COLDBOOT_DONE, &info) < 0) {
Colin Crossf83d0b92010-04-21 12:04:20 -07001033 t0 = get_usecs();
1034 coldboot("/sys/class");
1035 coldboot("/sys/block");
1036 coldboot("/sys/devices");
1037 t1 = get_usecs();
Nick Kralevich45a884f2015-02-02 14:37:22 -08001038 fd = open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000);
Colin Crossf83d0b92010-04-21 12:04:20 -07001039 close(fd);
Elliott Hughesc0e919c2015-02-04 14:46:36 -08001040 if (LOG_UEVENTS) {
1041 INFO("coldboot %ld uS\n", ((long) (t1 - t0)));
1042 }
1043 } else if (LOG_UEVENTS) {
1044 INFO("skipping coldboot, already done\n");
Colin Crossf83d0b92010-04-21 12:04:20 -07001045 }
Colin Cross0dd7ca62010-04-13 19:25:51 -07001046}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001047
Colin Cross0dd7ca62010-04-13 19:25:51 -07001048int get_device_fd()
1049{
1050 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001051}