blob: a95111a249adf00b51d22a8791c89fe3ea7ba375 [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) {
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700104 struct perm_node *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{
194 mode_t perm;
Colin Cross44b65d02010-04-20 14:32:50 -0700195 struct listnode *node;
196 struct perm_node *perm_node;
197 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700198
Colin Cross44b65d02010-04-20 14:32:50 -0700199 /* search the perms list in reverse so that ueventd.$hardware can
200 * override ueventd.rc
201 */
202 list_for_each_reverse(node, &dev_perms) {
Colin Cross43d537e2014-07-02 13:08:13 -0700203 bool match = false;
204
Colin Cross44b65d02010-04-20 14:32:50 -0700205 perm_node = node_to_item(node, struct perm_node, plist);
206 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700207
Colin Cross43d537e2014-07-02 13:08:13 -0700208 if (perm_path_matches(path, dp)) {
209 match = true;
Colin Cross44b65d02010-04-20 14:32:50 -0700210 } else {
Colin Cross43d537e2014-07-02 13:08:13 -0700211 if (links) {
212 int i;
213 for (i = 0; links[i]; i++) {
214 if (perm_path_matches(links[i], dp)) {
215 match = true;
216 break;
217 }
218 }
219 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700220 }
Colin Cross43d537e2014-07-02 13:08:13 -0700221
222 if (match) {
223 *uid = dp->uid;
224 *gid = dp->gid;
225 return dp->perm;
226 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700227 }
Colin Cross44b65d02010-04-20 14:32:50 -0700228 /* Default if nothing found. */
229 *uid = 0;
230 *gid = 0;
231 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700232}
233
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700234static void make_device(const char *path,
Mark Salyzyn154f4602014-02-20 14:59:07 -0800235 const char *upath UNUSED,
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400236 int block, int major, int minor,
237 const char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700238{
239 unsigned uid;
240 unsigned gid;
241 mode_t mode;
242 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500243 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700244
Colin Cross43d537e2014-07-02 13:08:13 -0700245 mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700246
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500247 if (sehandle) {
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400248 selabel_lookup_best_match(sehandle, &secontext, path, links, mode);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500249 setfscreatecon(secontext);
250 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700251
Colin Cross17dcc5c2010-09-03 12:25:34 -0700252 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800253 /* Temporarily change egid to avoid race condition setting the gid of the
254 * device node. Unforunately changing the euid would prevent creation of
255 * some device nodes, so the uid has to be set with chown() and is still
256 * racy. Fixing the gid race at least fixed the issue with system_server
257 * opening dynamic input devices under the AID_INPUT gid. */
258 setegid(gid);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700259 mknod(path, mode, dev);
Nick Pelly6405c692010-01-21 18:13:39 -0800260 chown(path, uid, -1);
261 setegid(AID_ROOT);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700262
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500263 if (secontext) {
264 freecon(secontext);
265 setfscreatecon(NULL);
266 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700267}
268
Dima Zavinf395c922013-03-06 16:23:57 -0800269static void add_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700270{
Dima Zavinf395c922013-03-06 16:23:57 -0800271 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700272 struct listnode *node;
273 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800274 const char *name = path;
275
276 if (!strncmp(path, "/devices/", 9)) {
277 name += 9;
278 if (!strncmp(name, "platform/", 9))
279 name += 9;
280 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700281
282 list_for_each_reverse(node, &platform_names) {
283 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800284 if ((bus->path_len < path_len) &&
285 (path[bus->path_len] == '/') &&
286 !strncmp(path, bus->path, bus->path_len))
Colin Crossfadb85e2011-03-30 18:32:12 -0700287 /* subdevice of an existing platform, ignore it */
288 return;
289 }
290
Dima Zavinf395c922013-03-06 16:23:57 -0800291 INFO("adding platform device %s (%s)\n", name, path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700292
293 bus = calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800294 bus->path = strdup(path);
295 bus->path_len = path_len;
296 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700297 list_add_tail(&platform_names, &bus->list);
298}
299
300/*
Dima Zavinf395c922013-03-06 16:23:57 -0800301 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700302 * platform device prefix. If it doesn't start with a platform device, return
303 * 0.
304 */
Dima Zavinf395c922013-03-06 16:23:57 -0800305static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700306{
Dima Zavinf395c922013-03-06 16:23:57 -0800307 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700308 struct listnode *node;
309 struct platform_node *bus;
310
311 list_for_each_reverse(node, &platform_names) {
312 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800313 if ((bus->path_len < path_len) &&
314 (path[bus->path_len] == '/') &&
315 !strncmp(path, bus->path, bus->path_len))
316 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700317 }
318
319 return NULL;
320}
321
Dima Zavinf395c922013-03-06 16:23:57 -0800322static void remove_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700323{
324 struct listnode *node;
325 struct platform_node *bus;
326
327 list_for_each_reverse(node, &platform_names) {
328 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800329 if (!strcmp(path, bus->path)) {
330 INFO("removing platform device %s\n", bus->name);
331 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700332 list_remove(node);
333 free(bus);
334 return;
335 }
336 }
337}
338
Andrew Boiea885d042013-09-13 17:41:20 -0700339/* Given a path that may start with a PCI device, populate the supplied buffer
340 * with the PCI domain/bus number and the peripheral ID and return 0.
341 * If it doesn't start with a PCI device, or there is some error, return -1 */
342static int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
343{
344 const char *start, *end;
345
346 if (strncmp(path, "/devices/pci", 12))
347 return -1;
348
349 /* Beginning of the prefix is the initial "pci" after "/devices/" */
350 start = path + 9;
351
352 /* End of the prefix is two path '/' later, capturing the domain/bus number
353 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
354 end = strchr(start, '/');
355 if (!end)
356 return -1;
357 end = strchr(end + 1, '/');
358 if (!end)
359 return -1;
360
361 /* Make sure we have enough room for the string plus null terminator */
362 if (end - start + 1 > buf_sz)
363 return -1;
364
365 strncpy(buf, start, end - start);
366 buf[end - start] = '\0';
367 return 0;
368}
369
Chuck Tuffli1e070842008-12-15 14:26:56 -0800370#if LOG_UEVENTS
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700371
372static inline suseconds_t get_usecs(void)
373{
374 struct timeval tv;
375 gettimeofday(&tv, 0);
376 return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
377}
378
379#define log_event_print(x...) INFO(x)
380
381#else
382
383#define log_event_print(fmt, args...) do { } while (0)
384#define get_usecs() 0
385
386#endif
387
388static void parse_event(const char *msg, struct uevent *uevent)
389{
390 uevent->action = "";
391 uevent->path = "";
392 uevent->subsystem = "";
393 uevent->firmware = "";
394 uevent->major = -1;
395 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700396 uevent->partition_name = NULL;
397 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700398 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700399
400 /* currently ignoring SEQNUM */
401 while(*msg) {
402 if(!strncmp(msg, "ACTION=", 7)) {
403 msg += 7;
404 uevent->action = msg;
405 } else if(!strncmp(msg, "DEVPATH=", 8)) {
406 msg += 8;
407 uevent->path = msg;
408 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
409 msg += 10;
410 uevent->subsystem = msg;
411 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
412 msg += 9;
413 uevent->firmware = msg;
414 } else if(!strncmp(msg, "MAJOR=", 6)) {
415 msg += 6;
416 uevent->major = atoi(msg);
417 } else if(!strncmp(msg, "MINOR=", 6)) {
418 msg += 6;
419 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700420 } else if(!strncmp(msg, "PARTN=", 6)) {
421 msg += 6;
422 uevent->partition_num = atoi(msg);
423 } else if(!strncmp(msg, "PARTNAME=", 9)) {
424 msg += 9;
425 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700426 } else if(!strncmp(msg, "DEVNAME=", 8)) {
427 msg += 8;
428 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700429 }
430
Wei Zhongf97b8872012-03-23 14:15:34 -0700431 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700432 while(*msg++)
433 ;
434 }
435
436 log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
437 uevent->action, uevent->path, uevent->subsystem,
438 uevent->firmware, uevent->major, uevent->minor);
439}
440
Benoit Gobyd2278632010-08-03 14:36:02 -0700441static char **get_character_device_symlinks(struct uevent *uevent)
442{
443 const char *parent;
444 char *slash;
445 char **links;
446 int link_num = 0;
447 int width;
Dima Zavinf395c922013-03-06 16:23:57 -0800448 struct platform_node *pdev;
Benoit Gobyd2278632010-08-03 14:36:02 -0700449
Dima Zavinf395c922013-03-06 16:23:57 -0800450 pdev = find_platform_device(uevent->path);
451 if (!pdev)
Benoit Gobyd2278632010-08-03 14:36:02 -0700452 return NULL;
453
454 links = malloc(sizeof(char *) * 2);
455 if (!links)
456 return NULL;
457 memset(links, 0, sizeof(char *) * 2);
458
459 /* skip "/devices/platform/<driver>" */
Dima Zavinf395c922013-03-06 16:23:57 -0800460 parent = strchr(uevent->path + pdev->path_len, '/');
Tomasz Kondelfca58f42013-11-05 13:17:45 +0100461 if (!parent)
Benoit Gobyd2278632010-08-03 14:36:02 -0700462 goto err;
463
464 if (!strncmp(parent, "/usb", 4)) {
465 /* skip root hub name and device. use device interface */
466 while (*++parent && *parent != '/');
467 if (*parent)
468 while (*++parent && *parent != '/');
469 if (!*parent)
470 goto err;
471 slash = strchr(++parent, '/');
472 if (!slash)
473 goto err;
474 width = slash - parent;
475 if (width <= 0)
476 goto err;
477
478 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
479 link_num++;
480 else
481 links[link_num] = NULL;
482 mkdir("/dev/usb", 0755);
483 }
484 else {
485 goto err;
486 }
487
488 return links;
489err:
490 free(links);
491 return NULL;
492}
493
Andrew Boiea885d042013-09-13 17:41:20 -0700494static char **get_block_device_symlinks(struct uevent *uevent)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700495{
Colin Crossfadb85e2011-03-30 18:32:12 -0700496 const char *device;
Dima Zavinf395c922013-03-06 16:23:57 -0800497 struct platform_node *pdev;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700498 char *slash;
Andrew Boiea885d042013-09-13 17:41:20 -0700499 const char *type;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700500 int width;
501 char buf[256];
502 char link_path[256];
503 int fd;
504 int link_num = 0;
505 int ret;
506 char *p;
507 unsigned int size;
508 struct stat info;
509
Dima Zavinf395c922013-03-06 16:23:57 -0800510 pdev = find_platform_device(uevent->path);
Andrew Boiea885d042013-09-13 17:41:20 -0700511 if (pdev) {
512 device = pdev->name;
513 type = "platform";
514 } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
515 device = buf;
516 type = "pci";
517 } else {
Dima Zavinf395c922013-03-06 16:23:57 -0800518 return NULL;
Andrew Boiea885d042013-09-13 17:41:20 -0700519 }
Dima Zavinf395c922013-03-06 16:23:57 -0800520
Colin Crossb0ab94b2010-04-08 16:16:20 -0700521 char **links = malloc(sizeof(char *) * 4);
522 if (!links)
523 return NULL;
524 memset(links, 0, sizeof(char *) * 4);
525
Andrew Boiea885d042013-09-13 17:41:20 -0700526 INFO("found %s device %s\n", type, device);
Colin Crossfadb85e2011-03-30 18:32:12 -0700527
Andrew Boiea885d042013-09-13 17:41:20 -0700528 snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700529
530 if (uevent->partition_name) {
531 p = strdup(uevent->partition_name);
532 sanitize(p);
Johan Redestig93ca79b2012-04-18 16:41:19 +0200533 if (strcmp(uevent->partition_name, p))
534 NOTICE("Linking partition '%s' as '%s'\n", uevent->partition_name, p);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700535 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
536 link_num++;
537 else
538 links[link_num] = NULL;
539 free(p);
540 }
541
542 if (uevent->partition_num >= 0) {
543 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
544 link_num++;
545 else
546 links[link_num] = NULL;
547 }
548
Dima Zavinf395c922013-03-06 16:23:57 -0800549 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700550 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
551 link_num++;
552 else
553 links[link_num] = NULL;
554
555 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700556}
557
Colin Crosseb5ba832011-03-30 17:37:17 -0700558static void handle_device(const char *action, const char *devpath,
559 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700560{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700561 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700562
Colin Crosseb5ba832011-03-30 17:37:17 -0700563 if(!strcmp(action, "add")) {
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400564 make_device(devpath, path, block, major, minor, (const char **)links);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700565 if (links) {
566 for (i = 0; links[i]; i++)
567 make_link(devpath, links[i]);
568 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700569 }
570
Colin Crosseb5ba832011-03-30 17:37:17 -0700571 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700572 if (links) {
573 for (i = 0; links[i]; i++)
574 remove_link(devpath, links[i]);
575 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700576 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700577 }
578
579 if (links) {
580 for (i = 0; links[i]; i++)
581 free(links[i]);
582 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700583 }
584}
585
Colin Crossfadb85e2011-03-30 18:32:12 -0700586static void handle_platform_device_event(struct uevent *uevent)
587{
Dima Zavinf395c922013-03-06 16:23:57 -0800588 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700589
590 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800591 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700592 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800593 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700594}
595
Colin Crosseb5ba832011-03-30 17:37:17 -0700596static const char *parse_device_name(struct uevent *uevent, unsigned int len)
597{
598 const char *name;
599
600 /* if it's not a /dev device, nothing else to do */
601 if((uevent->major < 0) || (uevent->minor < 0))
602 return NULL;
603
604 /* do we have a name? */
605 name = strrchr(uevent->path, '/');
606 if(!name)
607 return NULL;
608 name++;
609
610 /* too-long names would overrun our buffer */
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800611 if(strlen(name) > len) {
612 ERROR("DEVPATH=%s exceeds %u-character limit on filename; ignoring event\n",
613 name, len);
Colin Crosseb5ba832011-03-30 17:37:17 -0700614 return NULL;
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800615 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700616
617 return name;
618}
619
620static void handle_block_device_event(struct uevent *uevent)
621{
622 const char *base = "/dev/block/";
623 const char *name;
624 char devpath[96];
625 char **links = NULL;
626
627 name = parse_device_name(uevent, 64);
628 if (!name)
629 return;
630
631 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500632 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700633
Dima Zavinf395c922013-03-06 16:23:57 -0800634 if (!strncmp(uevent->path, "/devices/", 9))
Andrew Boiea885d042013-09-13 17:41:20 -0700635 links = get_block_device_symlinks(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700636
637 handle_device(uevent->action, devpath, uevent->path, 1,
638 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700639}
640
Greg Hackmann3312aa82013-11-18 15:24:40 -0800641#define DEVPATH_LEN 96
642
643static bool assemble_devpath(char *devpath, const char *dirname,
644 const char *devname)
645{
646 int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
647 if (s < 0) {
648 ERROR("failed to assemble device path (%s); ignoring event\n",
649 strerror(errno));
650 return false;
651 } else if (s >= DEVPATH_LEN) {
652 ERROR("%s/%s exceeds %u-character limit on path; ignoring event\n",
653 dirname, devname, DEVPATH_LEN);
654 return false;
655 }
656 return true;
657}
658
659static void mkdir_recursive_for_devpath(const char *devpath)
660{
661 char dir[DEVPATH_LEN];
662 char *slash;
663
664 strcpy(dir, devpath);
665 slash = strrchr(dir, '/');
666 *slash = '\0';
667 mkdir_recursive(dir, 0755);
668}
669
Mark Salyzyn154f4602014-02-20 14:59:07 -0800670static inline void __attribute__((__deprecated__)) kernel_logger()
671{
672 INFO("kernel logger is deprecated\n");
673}
674
Colin Crosseb5ba832011-03-30 17:37:17 -0700675static void handle_generic_device_event(struct uevent *uevent)
676{
677 char *base;
678 const char *name;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800679 char devpath[DEVPATH_LEN] = {0};
Colin Crosseb5ba832011-03-30 17:37:17 -0700680 char **links = NULL;
681
682 name = parse_device_name(uevent, 64);
683 if (!name)
684 return;
685
Greg Hackmann3312aa82013-11-18 15:24:40 -0800686 struct ueventd_subsystem *subsystem =
687 ueventd_subsystem_find_by_name(uevent->subsystem);
688
689 if (subsystem) {
690 const char *devname;
691
692 switch (subsystem->devname_src) {
693 case DEVNAME_UEVENT_DEVNAME:
694 devname = uevent->device_name;
695 break;
696
697 case DEVNAME_UEVENT_DEVPATH:
698 devname = name;
699 break;
700
701 default:
702 ERROR("%s subsystem's devpath option is not set; ignoring event\n",
703 uevent->subsystem);
704 return;
705 }
706
707 if (!assemble_devpath(devpath, subsystem->dirname, devname))
708 return;
709 mkdir_recursive_for_devpath(devpath);
710 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700711 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700712 if (uevent->device_name) {
Greg Hackmann3312aa82013-11-18 15:24:40 -0800713 if (!assemble_devpath(devpath, "/dev", uevent->device_name))
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800714 return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800715 mkdir_recursive_for_devpath(devpath);
Wei Zhongf97b8872012-03-23 14:15:34 -0700716 }
717 else {
718 /* This imitates the file system that would be created
719 * if we were using devfs instead.
720 * Minors are broken up into groups of 128, starting at "001"
721 */
722 int bus_id = uevent->minor / 128 + 1;
723 int device_id = uevent->minor % 128 + 1;
724 /* build directories */
725 make_dir("/dev/bus", 0755);
726 make_dir("/dev/bus/usb", 0755);
727 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
728 make_dir(devpath, 0755);
729 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
730 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700731 } else {
732 /* ignore other USB events */
733 return;
734 }
735 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
736 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500737 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200738 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
739 base = "/dev/dri/";
740 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700741 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
742 base = "/dev/oncrpc/";
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, "adsp", 4)) {
745 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500746 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700747 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
748 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500749 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700750 } else if(!strncmp(uevent->subsystem, "input", 5)) {
751 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500752 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700753 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
754 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500755 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700756 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
757 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500758 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700759 } else if(!strncmp(uevent->subsystem, "misc", 4) &&
760 !strncmp(name, "log_", 4)) {
Mark Salyzyn154f4602014-02-20 14:59:07 -0800761 kernel_logger();
Colin Crosseb5ba832011-03-30 17:37:17 -0700762 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500763 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700764 name += 4;
765 } else
766 base = "/dev/";
767 links = get_character_device_symlinks(uevent);
768
769 if (!devpath[0])
770 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
771
772 handle_device(uevent->action, devpath, uevent->path, 0,
773 uevent->major, uevent->minor, links);
774}
775
776static void handle_device_event(struct uevent *uevent)
777{
Ruchi Kandoi75b287b2014-04-29 19:14:37 -0700778 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
Colin Crosseb5ba832011-03-30 17:37:17 -0700779 fixup_sys_perms(uevent->path);
780
781 if (!strncmp(uevent->subsystem, "block", 5)) {
782 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700783 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
784 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700785 } else {
786 handle_generic_device_event(uevent);
787 }
788}
789
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700790static int load_firmware(int fw_fd, int loading_fd, int data_fd)
791{
792 struct stat st;
793 long len_to_copy;
794 int ret = 0;
795
796 if(fstat(fw_fd, &st) < 0)
797 return -1;
798 len_to_copy = st.st_size;
799
800 write(loading_fd, "1", 1); /* start transfer */
801
802 while (len_to_copy > 0) {
803 char buf[PAGE_SIZE];
804 ssize_t nr;
805
806 nr = read(fw_fd, buf, sizeof(buf));
807 if(!nr)
808 break;
809 if(nr < 0) {
810 ret = -1;
811 break;
812 }
813
814 len_to_copy -= nr;
815 while (nr > 0) {
816 ssize_t nw = 0;
817
818 nw = write(data_fd, buf + nw, nr);
819 if(nw <= 0) {
820 ret = -1;
821 goto out;
822 }
823 nr -= nw;
824 }
825 }
826
827out:
828 if(!ret)
829 write(loading_fd, "0", 1); /* successful end of transfer */
830 else
831 write(loading_fd, "-1", 2); /* abort transfer */
832
833 return ret;
834}
835
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700836static int is_booting(void)
837{
838 return access("/dev/.booting", F_OK) == 0;
839}
840
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700841static void process_firmware_event(struct uevent *uevent)
842{
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700843 char *root, *loading, *data, *file1 = NULL, *file2 = NULL, *file3 = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700844 int l, loading_fd, data_fd, fw_fd;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700845 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700846
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700847 INFO("firmware: loading '%s' for '%s'\n",
848 uevent->firmware, uevent->path);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700849
850 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
851 if (l == -1)
852 return;
853
854 l = asprintf(&loading, "%sloading", root);
855 if (l == -1)
856 goto root_free_out;
857
858 l = asprintf(&data, "%sdata", root);
859 if (l == -1)
860 goto loading_free_out;
861
Brian Swetland02863b92010-09-19 03:36:39 -0700862 l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
863 if (l == -1)
864 goto data_free_out;
865
866 l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700867 if (l == -1)
868 goto data_free_out;
869
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700870 l = asprintf(&file3, FIRMWARE_DIR3"/%s", uevent->firmware);
871 if (l == -1)
872 goto data_free_out;
873
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700874 loading_fd = open(loading, O_WRONLY);
875 if(loading_fd < 0)
876 goto file_free_out;
877
878 data_fd = open(data, O_WRONLY);
879 if(data_fd < 0)
880 goto loading_close_out;
881
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700882try_loading_again:
Brian Swetland02863b92010-09-19 03:36:39 -0700883 fw_fd = open(file1, O_RDONLY);
884 if(fw_fd < 0) {
885 fw_fd = open(file2, O_RDONLY);
Benoit Goby609d8822010-11-09 18:10:24 -0800886 if (fw_fd < 0) {
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700887 fw_fd = open(file3, O_RDONLY);
888 if (fw_fd < 0) {
889 if (booting) {
890 /* If we're not fully booted, we may be missing
891 * filesystems needed for firmware, wait and retry.
892 */
893 usleep(100000);
894 booting = is_booting();
895 goto try_loading_again;
896 }
897 INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno);
898 write(loading_fd, "-1", 2);
899 goto data_close_out;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700900 }
Benoit Goby609d8822010-11-09 18:10:24 -0800901 }
Brian Swetland02863b92010-09-19 03:36:39 -0700902 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700903
904 if(!load_firmware(fw_fd, loading_fd, data_fd))
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700905 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700906 else
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700907 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700908
909 close(fw_fd);
910data_close_out:
911 close(data_fd);
912loading_close_out:
913 close(loading_fd);
914file_free_out:
Brian Swetland02863b92010-09-19 03:36:39 -0700915 free(file1);
916 free(file2);
Ajay Dudani76c58892013-06-12 15:55:26 -0700917 free(file3);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700918data_free_out:
919 free(data);
920loading_free_out:
921 free(loading);
922root_free_out:
923 free(root);
924}
925
926static void handle_firmware_event(struct uevent *uevent)
927{
928 pid_t pid;
Colin Cross982a8152010-06-03 12:21:01 -0700929 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700930
931 if(strcmp(uevent->subsystem, "firmware"))
932 return;
933
934 if(strcmp(uevent->action, "add"))
935 return;
936
937 /* we fork, to avoid making large memory allocations in init proper */
938 pid = fork();
939 if (!pid) {
940 process_firmware_event(uevent);
Kenny Root17baff42014-08-20 16:16:44 -0700941 _exit(EXIT_SUCCESS);
942 } else if (pid < 0) {
943 log_event_print("could not fork to process firmware event: %s\n", strerror(errno));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700944 }
945}
946
Ruchi Kandoic6037202014-06-23 11:22:09 -0700947#define UEVENT_MSG_LEN 2048
Colin Cross0dd7ca62010-04-13 19:25:51 -0700948void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700949{
Vernon Tang3f582e92011-04-25 13:08:17 +1000950 char msg[UEVENT_MSG_LEN+2];
951 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700952 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700953 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700954 continue;
955
956 msg[n] = '\0';
957 msg[n+1] = '\0';
958
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700959 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700960 parse_event(msg, &uevent);
961
Stephen Smalleye2eb69d2013-04-16 09:30:30 -0400962 if (sehandle && selinux_status_updated() > 0) {
963 struct selabel_handle *sehandle2;
964 sehandle2 = selinux_android_file_context_handle();
965 if (sehandle2) {
966 selabel_close(sehandle);
967 sehandle = sehandle2;
968 }
969 }
970
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700971 handle_device_event(&uevent);
972 handle_firmware_event(&uevent);
973 }
974}
975
976/* Coldboot walks parts of the /sys tree and pokes the uevent files
977** to cause the kernel to regenerate device add events that happened
978** before init's device manager was started
979**
980** We drain any pending events from the netlink socket every time
981** we poke another uevent file to make sure we don't overrun the
982** socket's buffer.
983*/
984
Colin Cross0dd7ca62010-04-13 19:25:51 -0700985static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700986{
987 struct dirent *de;
988 int dfd, fd;
989
990 dfd = dirfd(d);
991
992 fd = openat(dfd, "uevent", O_WRONLY);
993 if(fd >= 0) {
994 write(fd, "add\n", 4);
995 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700996 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700997 }
998
999 while((de = readdir(d))) {
1000 DIR *d2;
1001
1002 if(de->d_type != DT_DIR || de->d_name[0] == '.')
1003 continue;
1004
1005 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
1006 if(fd < 0)
1007 continue;
1008
1009 d2 = fdopendir(fd);
1010 if(d2 == 0)
1011 close(fd);
1012 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -07001013 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001014 closedir(d2);
1015 }
1016 }
1017}
1018
Colin Cross0dd7ca62010-04-13 19:25:51 -07001019static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001020{
1021 DIR *d = opendir(path);
1022 if(d) {
Colin Cross0dd7ca62010-04-13 19:25:51 -07001023 do_coldboot(d);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001024 closedir(d);
1025 }
1026}
1027
Colin Cross0dd7ca62010-04-13 19:25:51 -07001028void device_init(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001029{
1030 suseconds_t t0, t1;
Colin Crossf83d0b92010-04-21 12:04:20 -07001031 struct stat info;
1032 int fd;
Kenny Rootb5982bf2012-10-16 23:07:05 -07001033
Stephen Smalleyae6f3d72012-05-01 15:02:53 -04001034 sehandle = NULL;
1035 if (is_selinux_enabled() > 0) {
1036 sehandle = selinux_android_file_context_handle();
Stephen Smalleye2eb69d2013-04-16 09:30:30 -04001037 selinux_status_open(true);
Stephen Smalleyae6f3d72012-05-01 15:02:53 -04001038 }
Kenny Rootb5982bf2012-10-16 23:07:05 -07001039
Andrew Boied562ca72012-12-04 15:47:20 -08001040 /* is 256K enough? udev uses 16MB! */
1041 device_fd = uevent_open_socket(256*1024, true);
Colin Cross0dd7ca62010-04-13 19:25:51 -07001042 if(device_fd < 0)
1043 return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001044
Colin Cross0dd7ca62010-04-13 19:25:51 -07001045 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
1046 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001047
Colin Crossf83d0b92010-04-21 12:04:20 -07001048 if (stat(coldboot_done, &info) < 0) {
1049 t0 = get_usecs();
1050 coldboot("/sys/class");
1051 coldboot("/sys/block");
1052 coldboot("/sys/devices");
1053 t1 = get_usecs();
1054 fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
1055 close(fd);
1056 log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
1057 } else {
1058 log_event_print("skipping coldboot, already done\n");
1059 }
Colin Cross0dd7ca62010-04-13 19:25:51 -07001060}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001061
Colin Cross0dd7ca62010-04-13 19:25:51 -07001062int get_device_fd()
1063{
1064 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001065}