blob: 125f98152247c3ac3365a2d96d2382da491cd23c [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2007 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 <errno.h>
Olivier Baillyb93e5812010-11-17 11:47:23 -080018#include <stddef.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070019#include <stdio.h>
20#include <stdlib.h>
21#include <sys/stat.h>
22#include <sys/types.h>
23
24#include <fcntl.h>
25#include <dirent.h>
26#include <unistd.h>
27#include <string.h>
28
29#include <sys/socket.h>
30#include <sys/un.h>
31#include <linux/netlink.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050032
33#ifdef HAVE_SELINUX
34#include <selinux/selinux.h>
35#include <selinux/label.h>
36#endif
37
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070038#include <private/android_filesystem_config.h>
39#include <sys/time.h>
40#include <asm/page.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"
Colin Crossb0ab94b2010-04-08 16:16:20 -070047#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070048#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070049
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070050#define SYSFS_PREFIX "/sys"
Brian Swetland02863b92010-09-19 03:36:39 -070051#define FIRMWARE_DIR1 "/etc/firmware"
52#define FIRMWARE_DIR2 "/vendor/firmware"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070053
Stephen Smalleye46f9d52012-01-13 08:48:47 -050054#ifdef HAVE_SELINUX
55static struct selabel_handle *sehandle;
56#endif
57
Colin Cross0dd7ca62010-04-13 19:25:51 -070058static int device_fd = -1;
59
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070060struct uevent {
61 const char *action;
62 const char *path;
63 const char *subsystem;
64 const char *firmware;
Colin Crossb0ab94b2010-04-08 16:16:20 -070065 const char *partition_name;
Wei Zhongf97b8872012-03-23 14:15:34 -070066 const char *device_name;
Colin Crossb0ab94b2010-04-08 16:16:20 -070067 int partition_num;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070068 int major;
69 int minor;
70};
71
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070072struct perms_ {
73 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070074 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070075 mode_t perm;
76 unsigned int uid;
77 unsigned int gid;
78 unsigned short prefix;
79};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070080
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070081struct perm_node {
82 struct perms_ dp;
83 struct listnode plist;
84};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070085
Colin Crossfadb85e2011-03-30 18:32:12 -070086struct platform_node {
87 char *name;
88 int name_len;
89 struct listnode list;
90};
91
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070092static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -070093static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -070094static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070095
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070096int add_dev_perms(const char *name, const char *attr,
97 mode_t perm, unsigned int uid, unsigned int gid,
98 unsigned short prefix) {
99 struct perm_node *node = calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700100 if (!node)
101 return -ENOMEM;
102
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700103 node->dp.name = strdup(name);
104 if (!node->dp.name)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700105 return -ENOMEM;
106
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700107 if (attr) {
108 node->dp.attr = strdup(attr);
109 if (!node->dp.attr)
110 return -ENOMEM;
111 }
112
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700113 node->dp.perm = perm;
114 node->dp.uid = uid;
115 node->dp.gid = gid;
116 node->dp.prefix = prefix;
117
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700118 if (attr)
119 list_add_tail(&sys_perms, &node->plist);
120 else
121 list_add_tail(&dev_perms, &node->plist);
122
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700123 return 0;
124}
125
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700126void fixup_sys_perms(const char *upath)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700127{
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700128 char buf[512];
129 struct listnode *node;
130 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700131
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700132 /* upaths omit the "/sys" that paths in this list
133 * contain, so we add 4 when comparing...
134 */
135 list_for_each(node, &sys_perms) {
136 dp = &(node_to_item(node, struct perm_node, plist))->dp;
137 if (dp->prefix) {
138 if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700139 continue;
140 } else {
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700141 if (strcmp(upath, dp->name + 4))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700142 continue;
143 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700144
145 if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
146 return;
147
148 sprintf(buf,"/sys%s/%s", upath, dp->attr);
149 INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
150 chown(buf, dp->uid, dp->gid);
151 chmod(buf, dp->perm);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700152 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700153}
154
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700155static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
156{
157 mode_t perm;
Colin Cross44b65d02010-04-20 14:32:50 -0700158 struct listnode *node;
159 struct perm_node *perm_node;
160 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700161
Colin Cross44b65d02010-04-20 14:32:50 -0700162 /* search the perms list in reverse so that ueventd.$hardware can
163 * override ueventd.rc
164 */
165 list_for_each_reverse(node, &dev_perms) {
166 perm_node = node_to_item(node, struct perm_node, plist);
167 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700168
Colin Cross44b65d02010-04-20 14:32:50 -0700169 if (dp->prefix) {
170 if (strncmp(path, dp->name, strlen(dp->name)))
171 continue;
172 } else {
173 if (strcmp(path, dp->name))
174 continue;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700175 }
Colin Cross44b65d02010-04-20 14:32:50 -0700176 *uid = dp->uid;
177 *gid = dp->gid;
178 return dp->perm;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700179 }
Colin Cross44b65d02010-04-20 14:32:50 -0700180 /* Default if nothing found. */
181 *uid = 0;
182 *gid = 0;
183 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700184}
185
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700186static void make_device(const char *path,
187 const char *upath,
188 int block, int major, int minor)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700189{
190 unsigned uid;
191 unsigned gid;
192 mode_t mode;
193 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500194#ifdef HAVE_SELINUX
195 char *secontext = NULL;
196#endif
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700197
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700198 mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500199#ifdef HAVE_SELINUX
200 if (sehandle) {
201 selabel_lookup(sehandle, &secontext, path, mode);
202 setfscreatecon(secontext);
203 }
204#endif
Colin Cross17dcc5c2010-09-03 12:25:34 -0700205 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800206 /* Temporarily change egid to avoid race condition setting the gid of the
207 * device node. Unforunately changing the euid would prevent creation of
208 * some device nodes, so the uid has to be set with chown() and is still
209 * racy. Fixing the gid race at least fixed the issue with system_server
210 * opening dynamic input devices under the AID_INPUT gid. */
211 setegid(gid);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700212 mknod(path, mode, dev);
Nick Pelly6405c692010-01-21 18:13:39 -0800213 chown(path, uid, -1);
214 setegid(AID_ROOT);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500215#ifdef HAVE_SELINUX
216 if (secontext) {
217 freecon(secontext);
218 setfscreatecon(NULL);
219 }
220#endif
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700221}
222
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500223
224static int make_dir(const char *path, mode_t mode)
225{
226 int rc;
227
228#ifdef HAVE_SELINUX
229 char *secontext = NULL;
230
231 if (sehandle) {
232 selabel_lookup(sehandle, &secontext, path, mode);
233 setfscreatecon(secontext);
234 }
235#endif
236
237 rc = mkdir(path, mode);
238
239#ifdef HAVE_SELINUX
240 if (secontext) {
241 freecon(secontext);
242 setfscreatecon(NULL);
243 }
244#endif
245 return rc;
246}
247
248
Colin Crossfadb85e2011-03-30 18:32:12 -0700249static void add_platform_device(const char *name)
250{
251 int name_len = strlen(name);
252 struct listnode *node;
253 struct platform_node *bus;
254
255 list_for_each_reverse(node, &platform_names) {
256 bus = node_to_item(node, struct platform_node, list);
257 if ((bus->name_len < name_len) &&
258 (name[bus->name_len] == '/') &&
259 !strncmp(name, bus->name, bus->name_len))
260 /* subdevice of an existing platform, ignore it */
261 return;
262 }
263
264 INFO("adding platform device %s\n", name);
265
266 bus = calloc(1, sizeof(struct platform_node));
267 bus->name = strdup(name);
268 bus->name_len = name_len;
269 list_add_tail(&platform_names, &bus->list);
270}
271
272/*
273 * given a name that may start with a platform device, find the length of the
274 * platform device prefix. If it doesn't start with a platform device, return
275 * 0.
276 */
277static const char *find_platform_device(const char *name)
278{
279 int name_len = strlen(name);
280 struct listnode *node;
281 struct platform_node *bus;
282
283 list_for_each_reverse(node, &platform_names) {
284 bus = node_to_item(node, struct platform_node, list);
285 if ((bus->name_len < name_len) &&
286 (name[bus->name_len] == '/') &&
287 !strncmp(name, bus->name, bus->name_len))
288 return bus->name;
289 }
290
291 return NULL;
292}
293
294static void remove_platform_device(const char *name)
295{
296 struct listnode *node;
297 struct platform_node *bus;
298
299 list_for_each_reverse(node, &platform_names) {
300 bus = node_to_item(node, struct platform_node, list);
301 if (!strcmp(name, bus->name)) {
302 INFO("removing platform device %s\n", name);
303 free(bus->name);
304 list_remove(node);
305 free(bus);
306 return;
307 }
308 }
309}
310
Chuck Tuffli1e070842008-12-15 14:26:56 -0800311#if LOG_UEVENTS
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700312
313static inline suseconds_t get_usecs(void)
314{
315 struct timeval tv;
316 gettimeofday(&tv, 0);
317 return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
318}
319
320#define log_event_print(x...) INFO(x)
321
322#else
323
324#define log_event_print(fmt, args...) do { } while (0)
325#define get_usecs() 0
326
327#endif
328
329static void parse_event(const char *msg, struct uevent *uevent)
330{
331 uevent->action = "";
332 uevent->path = "";
333 uevent->subsystem = "";
334 uevent->firmware = "";
335 uevent->major = -1;
336 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700337 uevent->partition_name = NULL;
338 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700339 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700340
341 /* currently ignoring SEQNUM */
342 while(*msg) {
343 if(!strncmp(msg, "ACTION=", 7)) {
344 msg += 7;
345 uevent->action = msg;
346 } else if(!strncmp(msg, "DEVPATH=", 8)) {
347 msg += 8;
348 uevent->path = msg;
349 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
350 msg += 10;
351 uevent->subsystem = msg;
352 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
353 msg += 9;
354 uevent->firmware = msg;
355 } else if(!strncmp(msg, "MAJOR=", 6)) {
356 msg += 6;
357 uevent->major = atoi(msg);
358 } else if(!strncmp(msg, "MINOR=", 6)) {
359 msg += 6;
360 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700361 } else if(!strncmp(msg, "PARTN=", 6)) {
362 msg += 6;
363 uevent->partition_num = atoi(msg);
364 } else if(!strncmp(msg, "PARTNAME=", 9)) {
365 msg += 9;
366 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700367 } else if(!strncmp(msg, "DEVNAME=", 8)) {
368 msg += 8;
369 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700370 }
371
Wei Zhongf97b8872012-03-23 14:15:34 -0700372 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700373 while(*msg++)
374 ;
375 }
376
377 log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
378 uevent->action, uevent->path, uevent->subsystem,
379 uevent->firmware, uevent->major, uevent->minor);
380}
381
Benoit Gobyd2278632010-08-03 14:36:02 -0700382static char **get_character_device_symlinks(struct uevent *uevent)
383{
384 const char *parent;
385 char *slash;
386 char **links;
387 int link_num = 0;
388 int width;
389
390 if (strncmp(uevent->path, "/devices/platform/", 18))
391 return NULL;
392
393 links = malloc(sizeof(char *) * 2);
394 if (!links)
395 return NULL;
396 memset(links, 0, sizeof(char *) * 2);
397
398 /* skip "/devices/platform/<driver>" */
399 parent = strchr(uevent->path + 18, '/');
400 if (!*parent)
401 goto err;
402
403 if (!strncmp(parent, "/usb", 4)) {
404 /* skip root hub name and device. use device interface */
405 while (*++parent && *parent != '/');
406 if (*parent)
407 while (*++parent && *parent != '/');
408 if (!*parent)
409 goto err;
410 slash = strchr(++parent, '/');
411 if (!slash)
412 goto err;
413 width = slash - parent;
414 if (width <= 0)
415 goto err;
416
417 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
418 link_num++;
419 else
420 links[link_num] = NULL;
421 mkdir("/dev/usb", 0755);
422 }
423 else {
424 goto err;
425 }
426
427 return links;
428err:
429 free(links);
430 return NULL;
431}
432
Colin Crossb0ab94b2010-04-08 16:16:20 -0700433static char **parse_platform_block_device(struct uevent *uevent)
434{
Colin Crossfadb85e2011-03-30 18:32:12 -0700435 const char *device;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700436 const char *path;
437 char *slash;
438 int width;
439 char buf[256];
440 char link_path[256];
441 int fd;
442 int link_num = 0;
443 int ret;
444 char *p;
445 unsigned int size;
446 struct stat info;
447
448 char **links = malloc(sizeof(char *) * 4);
449 if (!links)
450 return NULL;
451 memset(links, 0, sizeof(char *) * 4);
452
453 /* Drop "/devices/platform/" */
454 path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700455 device = path + 18;
456 device = find_platform_device(device);
457 if (!device)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700458 goto err;
459
Colin Crossfadb85e2011-03-30 18:32:12 -0700460 INFO("found platform device %s\n", device);
461
462 snprintf(link_path, sizeof(link_path), "/dev/block/platform/%s", device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700463
464 if (uevent->partition_name) {
465 p = strdup(uevent->partition_name);
466 sanitize(p);
467 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
468 link_num++;
469 else
470 links[link_num] = NULL;
471 free(p);
472 }
473
474 if (uevent->partition_num >= 0) {
475 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
476 link_num++;
477 else
478 links[link_num] = NULL;
479 }
480
481 slash = strrchr(path, '/');
482 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
483 link_num++;
484 else
485 links[link_num] = NULL;
486
487 return links;
488
489err:
490 free(links);
491 return NULL;
492}
493
Colin Crosseb5ba832011-03-30 17:37:17 -0700494static void handle_device(const char *action, const char *devpath,
495 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700496{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700497 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700498
Colin Crosseb5ba832011-03-30 17:37:17 -0700499 if(!strcmp(action, "add")) {
500 make_device(devpath, path, block, major, minor);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700501 if (links) {
502 for (i = 0; links[i]; i++)
503 make_link(devpath, links[i]);
504 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700505 }
506
Colin Crosseb5ba832011-03-30 17:37:17 -0700507 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700508 if (links) {
509 for (i = 0; links[i]; i++)
510 remove_link(devpath, links[i]);
511 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700512 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700513 }
514
515 if (links) {
516 for (i = 0; links[i]; i++)
517 free(links[i]);
518 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700519 }
520}
521
Colin Crossfadb85e2011-03-30 18:32:12 -0700522static void handle_platform_device_event(struct uevent *uevent)
523{
524 const char *name = uevent->path + 18; /* length of /devices/platform/ */
525
526 if (!strcmp(uevent->action, "add"))
527 add_platform_device(name);
528 else if (!strcmp(uevent->action, "remove"))
529 remove_platform_device(name);
530}
531
Colin Crosseb5ba832011-03-30 17:37:17 -0700532static const char *parse_device_name(struct uevent *uevent, unsigned int len)
533{
534 const char *name;
535
536 /* if it's not a /dev device, nothing else to do */
537 if((uevent->major < 0) || (uevent->minor < 0))
538 return NULL;
539
540 /* do we have a name? */
541 name = strrchr(uevent->path, '/');
542 if(!name)
543 return NULL;
544 name++;
545
546 /* too-long names would overrun our buffer */
547 if(strlen(name) > len)
548 return NULL;
549
550 return name;
551}
552
553static void handle_block_device_event(struct uevent *uevent)
554{
555 const char *base = "/dev/block/";
556 const char *name;
557 char devpath[96];
558 char **links = NULL;
559
560 name = parse_device_name(uevent, 64);
561 if (!name)
562 return;
563
564 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500565 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700566
567 if (!strncmp(uevent->path, "/devices/platform/", 18))
568 links = parse_platform_block_device(uevent);
569
570 handle_device(uevent->action, devpath, uevent->path, 1,
571 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700572}
573
574static void handle_generic_device_event(struct uevent *uevent)
575{
576 char *base;
577 const char *name;
578 char devpath[96] = {0};
579 char **links = NULL;
580
581 name = parse_device_name(uevent, 64);
582 if (!name)
583 return;
584
585 if (!strncmp(uevent->subsystem, "usb", 3)) {
586 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700587 if (uevent->device_name) {
588 /*
589 * create device node provided by kernel if present
590 * see drivers/base/core.c
591 */
592 char *p = devpath;
593 snprintf(devpath, sizeof(devpath), "/dev/%s", uevent->device_name);
594 /* skip leading /dev/ */
595 p += 5;
596 /* build directories */
597 while (*p) {
598 if (*p == '/') {
599 *p = 0;
600 make_dir(devpath, 0755);
601 *p = '/';
602 }
603 p++;
604 }
605 }
606 else {
607 /* This imitates the file system that would be created
608 * if we were using devfs instead.
609 * Minors are broken up into groups of 128, starting at "001"
610 */
611 int bus_id = uevent->minor / 128 + 1;
612 int device_id = uevent->minor % 128 + 1;
613 /* build directories */
614 make_dir("/dev/bus", 0755);
615 make_dir("/dev/bus/usb", 0755);
616 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
617 make_dir(devpath, 0755);
618 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
619 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700620 } else {
621 /* ignore other USB events */
622 return;
623 }
624 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
625 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500626 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700627 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
628 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500629 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700630 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
631 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500632 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700633 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
634 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500635 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700636 } else if(!strncmp(uevent->subsystem, "input", 5)) {
637 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500638 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700639 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
640 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500641 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700642 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
643 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500644 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700645 } else if(!strncmp(uevent->subsystem, "misc", 4) &&
646 !strncmp(name, "log_", 4)) {
647 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500648 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700649 name += 4;
650 } else
651 base = "/dev/";
652 links = get_character_device_symlinks(uevent);
653
654 if (!devpath[0])
655 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
656
657 handle_device(uevent->action, devpath, uevent->path, 0,
658 uevent->major, uevent->minor, links);
659}
660
661static void handle_device_event(struct uevent *uevent)
662{
663 if (!strcmp(uevent->action,"add"))
664 fixup_sys_perms(uevent->path);
665
666 if (!strncmp(uevent->subsystem, "block", 5)) {
667 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700668 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
669 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700670 } else {
671 handle_generic_device_event(uevent);
672 }
673}
674
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700675static int load_firmware(int fw_fd, int loading_fd, int data_fd)
676{
677 struct stat st;
678 long len_to_copy;
679 int ret = 0;
680
681 if(fstat(fw_fd, &st) < 0)
682 return -1;
683 len_to_copy = st.st_size;
684
685 write(loading_fd, "1", 1); /* start transfer */
686
687 while (len_to_copy > 0) {
688 char buf[PAGE_SIZE];
689 ssize_t nr;
690
691 nr = read(fw_fd, buf, sizeof(buf));
692 if(!nr)
693 break;
694 if(nr < 0) {
695 ret = -1;
696 break;
697 }
698
699 len_to_copy -= nr;
700 while (nr > 0) {
701 ssize_t nw = 0;
702
703 nw = write(data_fd, buf + nw, nr);
704 if(nw <= 0) {
705 ret = -1;
706 goto out;
707 }
708 nr -= nw;
709 }
710 }
711
712out:
713 if(!ret)
714 write(loading_fd, "0", 1); /* successful end of transfer */
715 else
716 write(loading_fd, "-1", 2); /* abort transfer */
717
718 return ret;
719}
720
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700721static int is_booting(void)
722{
723 return access("/dev/.booting", F_OK) == 0;
724}
725
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700726static void process_firmware_event(struct uevent *uevent)
727{
Brian Swetland02863b92010-09-19 03:36:39 -0700728 char *root, *loading, *data, *file1 = NULL, *file2 = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700729 int l, loading_fd, data_fd, fw_fd;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700730 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700731
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700732 INFO("firmware: loading '%s' for '%s'\n",
733 uevent->firmware, uevent->path);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700734
735 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
736 if (l == -1)
737 return;
738
739 l = asprintf(&loading, "%sloading", root);
740 if (l == -1)
741 goto root_free_out;
742
743 l = asprintf(&data, "%sdata", root);
744 if (l == -1)
745 goto loading_free_out;
746
Brian Swetland02863b92010-09-19 03:36:39 -0700747 l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
748 if (l == -1)
749 goto data_free_out;
750
751 l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700752 if (l == -1)
753 goto data_free_out;
754
755 loading_fd = open(loading, O_WRONLY);
756 if(loading_fd < 0)
757 goto file_free_out;
758
759 data_fd = open(data, O_WRONLY);
760 if(data_fd < 0)
761 goto loading_close_out;
762
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700763try_loading_again:
Brian Swetland02863b92010-09-19 03:36:39 -0700764 fw_fd = open(file1, O_RDONLY);
765 if(fw_fd < 0) {
766 fw_fd = open(file2, O_RDONLY);
Benoit Goby609d8822010-11-09 18:10:24 -0800767 if (fw_fd < 0) {
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700768 if (booting) {
769 /* If we're not fully booted, we may be missing
770 * filesystems needed for firmware, wait and retry.
771 */
772 usleep(100000);
773 booting = is_booting();
774 goto try_loading_again;
775 }
776 INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno);
Benoit Goby609d8822010-11-09 18:10:24 -0800777 write(loading_fd, "-1", 2);
Brian Swetland02863b92010-09-19 03:36:39 -0700778 goto data_close_out;
Benoit Goby609d8822010-11-09 18:10:24 -0800779 }
Brian Swetland02863b92010-09-19 03:36:39 -0700780 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700781
782 if(!load_firmware(fw_fd, loading_fd, data_fd))
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700783 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700784 else
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700785 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700786
787 close(fw_fd);
788data_close_out:
789 close(data_fd);
790loading_close_out:
791 close(loading_fd);
792file_free_out:
Brian Swetland02863b92010-09-19 03:36:39 -0700793 free(file1);
794 free(file2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700795data_free_out:
796 free(data);
797loading_free_out:
798 free(loading);
799root_free_out:
800 free(root);
801}
802
803static void handle_firmware_event(struct uevent *uevent)
804{
805 pid_t pid;
Colin Cross982a8152010-06-03 12:21:01 -0700806 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700807
808 if(strcmp(uevent->subsystem, "firmware"))
809 return;
810
811 if(strcmp(uevent->action, "add"))
812 return;
813
814 /* we fork, to avoid making large memory allocations in init proper */
815 pid = fork();
816 if (!pid) {
817 process_firmware_event(uevent);
818 exit(EXIT_SUCCESS);
819 }
820}
821
822#define UEVENT_MSG_LEN 1024
Colin Cross0dd7ca62010-04-13 19:25:51 -0700823void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700824{
Vernon Tang3f582e92011-04-25 13:08:17 +1000825 char msg[UEVENT_MSG_LEN+2];
826 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700827 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700828 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700829 continue;
830
831 msg[n] = '\0';
832 msg[n+1] = '\0';
833
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700834 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700835 parse_event(msg, &uevent);
836
837 handle_device_event(&uevent);
838 handle_firmware_event(&uevent);
839 }
840}
841
842/* Coldboot walks parts of the /sys tree and pokes the uevent files
843** to cause the kernel to regenerate device add events that happened
844** before init's device manager was started
845**
846** We drain any pending events from the netlink socket every time
847** we poke another uevent file to make sure we don't overrun the
848** socket's buffer.
849*/
850
Colin Cross0dd7ca62010-04-13 19:25:51 -0700851static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700852{
853 struct dirent *de;
854 int dfd, fd;
855
856 dfd = dirfd(d);
857
858 fd = openat(dfd, "uevent", O_WRONLY);
859 if(fd >= 0) {
860 write(fd, "add\n", 4);
861 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700862 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700863 }
864
865 while((de = readdir(d))) {
866 DIR *d2;
867
868 if(de->d_type != DT_DIR || de->d_name[0] == '.')
869 continue;
870
871 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
872 if(fd < 0)
873 continue;
874
875 d2 = fdopendir(fd);
876 if(d2 == 0)
877 close(fd);
878 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700879 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700880 closedir(d2);
881 }
882 }
883}
884
Colin Cross0dd7ca62010-04-13 19:25:51 -0700885static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700886{
887 DIR *d = opendir(path);
888 if(d) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700889 do_coldboot(d);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700890 closedir(d);
891 }
892}
893
Colin Cross0dd7ca62010-04-13 19:25:51 -0700894void device_init(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700895{
896 suseconds_t t0, t1;
Colin Crossf83d0b92010-04-21 12:04:20 -0700897 struct stat info;
898 int fd;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500899#ifdef HAVE_SELINUX
900 struct selinux_opt seopts[] = {
901 { SELABEL_OPT_PATH, "/file_contexts" }
902 };
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700903
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500904 if (is_selinux_enabled() > 0)
905 sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
906#endif
Dima Zavin2d55e022011-08-31 18:07:36 -0700907 /* is 64K enough? udev uses 16MB! */
908 device_fd = uevent_open_socket(64*1024, true);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700909 if(device_fd < 0)
910 return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700911
Colin Cross0dd7ca62010-04-13 19:25:51 -0700912 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
913 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700914
Colin Crossf83d0b92010-04-21 12:04:20 -0700915 if (stat(coldboot_done, &info) < 0) {
916 t0 = get_usecs();
917 coldboot("/sys/class");
918 coldboot("/sys/block");
919 coldboot("/sys/devices");
920 t1 = get_usecs();
921 fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
922 close(fd);
923 log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
924 } else {
925 log_event_print("skipping coldboot, already done\n");
926 }
Colin Cross0dd7ca62010-04-13 19:25:51 -0700927}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700928
Colin Cross0dd7ca62010-04-13 19:25:51 -0700929int get_device_fd()
930{
931 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700932}