blob: 597f9583e77d597a03fe1026720d0f332624a352 [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>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -040036#include <selinux/android.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050037#endif
38
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070039#include <private/android_filesystem_config.h>
40#include <sys/time.h>
41#include <asm/page.h>
Colin Cross982a8152010-06-03 12:21:01 -070042#include <sys/wait.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070043
Dima Zavinda04c522011-09-01 17:09:44 -070044#include <cutils/list.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100045#include <cutils/uevent.h>
46
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070047#include "devices.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
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070051#define SYSFS_PREFIX "/sys"
Brian Swetland02863b92010-09-19 03:36:39 -070052#define FIRMWARE_DIR1 "/etc/firmware"
53#define FIRMWARE_DIR2 "/vendor/firmware"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070054
Stephen Smalleye46f9d52012-01-13 08:48:47 -050055#ifdef HAVE_SELINUX
Stephen Smalleye096e362012-06-11 13:37:39 -040056extern struct selabel_handle *sehandle;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050057#endif
58
Colin Cross0dd7ca62010-04-13 19:25:51 -070059static int device_fd = -1;
60
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070061struct uevent {
62 const char *action;
63 const char *path;
64 const char *subsystem;
65 const char *firmware;
Colin Crossb0ab94b2010-04-08 16:16:20 -070066 const char *partition_name;
Wei Zhongf97b8872012-03-23 14:15:34 -070067 const char *device_name;
Colin Crossb0ab94b2010-04-08 16:16:20 -070068 int partition_num;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070069 int major;
70 int minor;
71};
72
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070073struct perms_ {
74 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070075 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070076 mode_t perm;
77 unsigned int uid;
78 unsigned int gid;
79 unsigned short prefix;
80};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070081
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070082struct perm_node {
83 struct perms_ dp;
84 struct listnode plist;
85};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070086
Colin Crossfadb85e2011-03-30 18:32:12 -070087struct platform_node {
88 char *name;
89 int name_len;
90 struct listnode list;
91};
92
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070093static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -070094static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -070095static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070096
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070097int add_dev_perms(const char *name, const char *attr,
98 mode_t perm, unsigned int uid, unsigned int gid,
99 unsigned short prefix) {
100 struct perm_node *node = calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700101 if (!node)
102 return -ENOMEM;
103
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700104 node->dp.name = strdup(name);
105 if (!node->dp.name)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700106 return -ENOMEM;
107
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700108 if (attr) {
109 node->dp.attr = strdup(attr);
110 if (!node->dp.attr)
111 return -ENOMEM;
112 }
113
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700114 node->dp.perm = perm;
115 node->dp.uid = uid;
116 node->dp.gid = gid;
117 node->dp.prefix = prefix;
118
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700119 if (attr)
120 list_add_tail(&sys_perms, &node->plist);
121 else
122 list_add_tail(&dev_perms, &node->plist);
123
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700124 return 0;
125}
126
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700127void fixup_sys_perms(const char *upath)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700128{
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700129 char buf[512];
130 struct listnode *node;
131 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700132
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700133 /* upaths omit the "/sys" that paths in this list
134 * contain, so we add 4 when comparing...
135 */
136 list_for_each(node, &sys_perms) {
137 dp = &(node_to_item(node, struct perm_node, plist))->dp;
138 if (dp->prefix) {
139 if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700140 continue;
141 } else {
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700142 if (strcmp(upath, dp->name + 4))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700143 continue;
144 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700145
146 if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
147 return;
148
149 sprintf(buf,"/sys%s/%s", upath, dp->attr);
150 INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
151 chown(buf, dp->uid, dp->gid);
152 chmod(buf, dp->perm);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700153 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700154}
155
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700156static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
157{
158 mode_t perm;
Colin Cross44b65d02010-04-20 14:32:50 -0700159 struct listnode *node;
160 struct perm_node *perm_node;
161 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700162
Colin Cross44b65d02010-04-20 14:32:50 -0700163 /* search the perms list in reverse so that ueventd.$hardware can
164 * override ueventd.rc
165 */
166 list_for_each_reverse(node, &dev_perms) {
167 perm_node = node_to_item(node, struct perm_node, plist);
168 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700169
Colin Cross44b65d02010-04-20 14:32:50 -0700170 if (dp->prefix) {
171 if (strncmp(path, dp->name, strlen(dp->name)))
172 continue;
173 } else {
174 if (strcmp(path, dp->name))
175 continue;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700176 }
Colin Cross44b65d02010-04-20 14:32:50 -0700177 *uid = dp->uid;
178 *gid = dp->gid;
179 return dp->perm;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700180 }
Colin Cross44b65d02010-04-20 14:32:50 -0700181 /* Default if nothing found. */
182 *uid = 0;
183 *gid = 0;
184 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700185}
186
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700187static void make_device(const char *path,
188 const char *upath,
189 int block, int major, int minor)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700190{
191 unsigned uid;
192 unsigned gid;
193 mode_t mode;
194 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500195#ifdef HAVE_SELINUX
196 char *secontext = NULL;
197#endif
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700198
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700199 mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500200#ifdef HAVE_SELINUX
201 if (sehandle) {
202 selabel_lookup(sehandle, &secontext, path, mode);
203 setfscreatecon(secontext);
204 }
205#endif
Colin Cross17dcc5c2010-09-03 12:25:34 -0700206 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800207 /* Temporarily change egid to avoid race condition setting the gid of the
208 * device node. Unforunately changing the euid would prevent creation of
209 * some device nodes, so the uid has to be set with chown() and is still
210 * racy. Fixing the gid race at least fixed the issue with system_server
211 * opening dynamic input devices under the AID_INPUT gid. */
212 setegid(gid);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700213 mknod(path, mode, dev);
Nick Pelly6405c692010-01-21 18:13:39 -0800214 chown(path, uid, -1);
215 setegid(AID_ROOT);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500216#ifdef HAVE_SELINUX
217 if (secontext) {
218 freecon(secontext);
219 setfscreatecon(NULL);
220 }
221#endif
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700222}
223
Colin Crossfadb85e2011-03-30 18:32:12 -0700224static void add_platform_device(const char *name)
225{
226 int name_len = strlen(name);
227 struct listnode *node;
228 struct platform_node *bus;
229
230 list_for_each_reverse(node, &platform_names) {
231 bus = node_to_item(node, struct platform_node, list);
232 if ((bus->name_len < name_len) &&
233 (name[bus->name_len] == '/') &&
234 !strncmp(name, bus->name, bus->name_len))
235 /* subdevice of an existing platform, ignore it */
236 return;
237 }
238
239 INFO("adding platform device %s\n", name);
240
241 bus = calloc(1, sizeof(struct platform_node));
242 bus->name = strdup(name);
243 bus->name_len = name_len;
244 list_add_tail(&platform_names, &bus->list);
245}
246
247/*
248 * given a name that may start with a platform device, find the length of the
249 * platform device prefix. If it doesn't start with a platform device, return
250 * 0.
251 */
252static const char *find_platform_device(const char *name)
253{
254 int name_len = strlen(name);
255 struct listnode *node;
256 struct platform_node *bus;
257
258 list_for_each_reverse(node, &platform_names) {
259 bus = node_to_item(node, struct platform_node, list);
260 if ((bus->name_len < name_len) &&
261 (name[bus->name_len] == '/') &&
262 !strncmp(name, bus->name, bus->name_len))
263 return bus->name;
264 }
265
266 return NULL;
267}
268
269static void remove_platform_device(const char *name)
270{
271 struct listnode *node;
272 struct platform_node *bus;
273
274 list_for_each_reverse(node, &platform_names) {
275 bus = node_to_item(node, struct platform_node, list);
276 if (!strcmp(name, bus->name)) {
277 INFO("removing platform device %s\n", name);
278 free(bus->name);
279 list_remove(node);
280 free(bus);
281 return;
282 }
283 }
284}
285
Chuck Tuffli1e070842008-12-15 14:26:56 -0800286#if LOG_UEVENTS
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700287
288static inline suseconds_t get_usecs(void)
289{
290 struct timeval tv;
291 gettimeofday(&tv, 0);
292 return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
293}
294
295#define log_event_print(x...) INFO(x)
296
297#else
298
299#define log_event_print(fmt, args...) do { } while (0)
300#define get_usecs() 0
301
302#endif
303
304static void parse_event(const char *msg, struct uevent *uevent)
305{
306 uevent->action = "";
307 uevent->path = "";
308 uevent->subsystem = "";
309 uevent->firmware = "";
310 uevent->major = -1;
311 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700312 uevent->partition_name = NULL;
313 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700314 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700315
316 /* currently ignoring SEQNUM */
317 while(*msg) {
318 if(!strncmp(msg, "ACTION=", 7)) {
319 msg += 7;
320 uevent->action = msg;
321 } else if(!strncmp(msg, "DEVPATH=", 8)) {
322 msg += 8;
323 uevent->path = msg;
324 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
325 msg += 10;
326 uevent->subsystem = msg;
327 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
328 msg += 9;
329 uevent->firmware = msg;
330 } else if(!strncmp(msg, "MAJOR=", 6)) {
331 msg += 6;
332 uevent->major = atoi(msg);
333 } else if(!strncmp(msg, "MINOR=", 6)) {
334 msg += 6;
335 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700336 } else if(!strncmp(msg, "PARTN=", 6)) {
337 msg += 6;
338 uevent->partition_num = atoi(msg);
339 } else if(!strncmp(msg, "PARTNAME=", 9)) {
340 msg += 9;
341 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700342 } else if(!strncmp(msg, "DEVNAME=", 8)) {
343 msg += 8;
344 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700345 }
346
Wei Zhongf97b8872012-03-23 14:15:34 -0700347 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700348 while(*msg++)
349 ;
350 }
351
352 log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
353 uevent->action, uevent->path, uevent->subsystem,
354 uevent->firmware, uevent->major, uevent->minor);
355}
356
Benoit Gobyd2278632010-08-03 14:36:02 -0700357static char **get_character_device_symlinks(struct uevent *uevent)
358{
359 const char *parent;
360 char *slash;
361 char **links;
362 int link_num = 0;
363 int width;
364
365 if (strncmp(uevent->path, "/devices/platform/", 18))
366 return NULL;
367
368 links = malloc(sizeof(char *) * 2);
369 if (!links)
370 return NULL;
371 memset(links, 0, sizeof(char *) * 2);
372
373 /* skip "/devices/platform/<driver>" */
374 parent = strchr(uevent->path + 18, '/');
375 if (!*parent)
376 goto err;
377
378 if (!strncmp(parent, "/usb", 4)) {
379 /* skip root hub name and device. use device interface */
380 while (*++parent && *parent != '/');
381 if (*parent)
382 while (*++parent && *parent != '/');
383 if (!*parent)
384 goto err;
385 slash = strchr(++parent, '/');
386 if (!slash)
387 goto err;
388 width = slash - parent;
389 if (width <= 0)
390 goto err;
391
392 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
393 link_num++;
394 else
395 links[link_num] = NULL;
396 mkdir("/dev/usb", 0755);
397 }
398 else {
399 goto err;
400 }
401
402 return links;
403err:
404 free(links);
405 return NULL;
406}
407
Colin Crossb0ab94b2010-04-08 16:16:20 -0700408static char **parse_platform_block_device(struct uevent *uevent)
409{
Colin Crossfadb85e2011-03-30 18:32:12 -0700410 const char *device;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700411 const char *path;
412 char *slash;
413 int width;
414 char buf[256];
415 char link_path[256];
416 int fd;
417 int link_num = 0;
418 int ret;
419 char *p;
420 unsigned int size;
421 struct stat info;
422
423 char **links = malloc(sizeof(char *) * 4);
424 if (!links)
425 return NULL;
426 memset(links, 0, sizeof(char *) * 4);
427
428 /* Drop "/devices/platform/" */
429 path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700430 device = path + 18;
431 device = find_platform_device(device);
432 if (!device)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700433 goto err;
434
Colin Crossfadb85e2011-03-30 18:32:12 -0700435 INFO("found platform device %s\n", device);
436
437 snprintf(link_path, sizeof(link_path), "/dev/block/platform/%s", device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700438
439 if (uevent->partition_name) {
440 p = strdup(uevent->partition_name);
441 sanitize(p);
442 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
443 link_num++;
444 else
445 links[link_num] = NULL;
446 free(p);
447 }
448
449 if (uevent->partition_num >= 0) {
450 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
451 link_num++;
452 else
453 links[link_num] = NULL;
454 }
455
456 slash = strrchr(path, '/');
457 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
458 link_num++;
459 else
460 links[link_num] = NULL;
461
462 return links;
463
464err:
465 free(links);
466 return NULL;
467}
468
Colin Crosseb5ba832011-03-30 17:37:17 -0700469static void handle_device(const char *action, const char *devpath,
470 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700471{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700472 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700473
Colin Crosseb5ba832011-03-30 17:37:17 -0700474 if(!strcmp(action, "add")) {
475 make_device(devpath, path, block, major, minor);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700476 if (links) {
477 for (i = 0; links[i]; i++)
478 make_link(devpath, links[i]);
479 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700480 }
481
Colin Crosseb5ba832011-03-30 17:37:17 -0700482 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700483 if (links) {
484 for (i = 0; links[i]; i++)
485 remove_link(devpath, links[i]);
486 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700487 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700488 }
489
490 if (links) {
491 for (i = 0; links[i]; i++)
492 free(links[i]);
493 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700494 }
495}
496
Colin Crossfadb85e2011-03-30 18:32:12 -0700497static void handle_platform_device_event(struct uevent *uevent)
498{
499 const char *name = uevent->path + 18; /* length of /devices/platform/ */
500
501 if (!strcmp(uevent->action, "add"))
502 add_platform_device(name);
503 else if (!strcmp(uevent->action, "remove"))
504 remove_platform_device(name);
505}
506
Colin Crosseb5ba832011-03-30 17:37:17 -0700507static const char *parse_device_name(struct uevent *uevent, unsigned int len)
508{
509 const char *name;
510
511 /* if it's not a /dev device, nothing else to do */
512 if((uevent->major < 0) || (uevent->minor < 0))
513 return NULL;
514
515 /* do we have a name? */
516 name = strrchr(uevent->path, '/');
517 if(!name)
518 return NULL;
519 name++;
520
521 /* too-long names would overrun our buffer */
522 if(strlen(name) > len)
523 return NULL;
524
525 return name;
526}
527
528static void handle_block_device_event(struct uevent *uevent)
529{
530 const char *base = "/dev/block/";
531 const char *name;
532 char devpath[96];
533 char **links = NULL;
534
535 name = parse_device_name(uevent, 64);
536 if (!name)
537 return;
538
539 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500540 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700541
542 if (!strncmp(uevent->path, "/devices/platform/", 18))
543 links = parse_platform_block_device(uevent);
544
545 handle_device(uevent->action, devpath, uevent->path, 1,
546 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700547}
548
549static void handle_generic_device_event(struct uevent *uevent)
550{
551 char *base;
552 const char *name;
553 char devpath[96] = {0};
554 char **links = NULL;
555
556 name = parse_device_name(uevent, 64);
557 if (!name)
558 return;
559
560 if (!strncmp(uevent->subsystem, "usb", 3)) {
561 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700562 if (uevent->device_name) {
563 /*
564 * create device node provided by kernel if present
565 * see drivers/base/core.c
566 */
567 char *p = devpath;
568 snprintf(devpath, sizeof(devpath), "/dev/%s", uevent->device_name);
569 /* skip leading /dev/ */
570 p += 5;
571 /* build directories */
572 while (*p) {
573 if (*p == '/') {
574 *p = 0;
575 make_dir(devpath, 0755);
576 *p = '/';
577 }
578 p++;
579 }
580 }
581 else {
582 /* This imitates the file system that would be created
583 * if we were using devfs instead.
584 * Minors are broken up into groups of 128, starting at "001"
585 */
586 int bus_id = uevent->minor / 128 + 1;
587 int device_id = uevent->minor % 128 + 1;
588 /* build directories */
589 make_dir("/dev/bus", 0755);
590 make_dir("/dev/bus/usb", 0755);
591 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
592 make_dir(devpath, 0755);
593 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
594 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700595 } else {
596 /* ignore other USB events */
597 return;
598 }
599 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
600 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500601 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700602 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
603 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500604 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700605 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
606 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500607 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700608 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
609 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500610 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700611 } else if(!strncmp(uevent->subsystem, "input", 5)) {
612 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500613 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700614 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
615 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500616 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700617 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
618 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500619 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700620 } else if(!strncmp(uevent->subsystem, "misc", 4) &&
621 !strncmp(name, "log_", 4)) {
622 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500623 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700624 name += 4;
625 } else
626 base = "/dev/";
627 links = get_character_device_symlinks(uevent);
628
629 if (!devpath[0])
630 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
631
632 handle_device(uevent->action, devpath, uevent->path, 0,
633 uevent->major, uevent->minor, links);
634}
635
636static void handle_device_event(struct uevent *uevent)
637{
638 if (!strcmp(uevent->action,"add"))
639 fixup_sys_perms(uevent->path);
640
641 if (!strncmp(uevent->subsystem, "block", 5)) {
642 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700643 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
644 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700645 } else {
646 handle_generic_device_event(uevent);
647 }
648}
649
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700650static int load_firmware(int fw_fd, int loading_fd, int data_fd)
651{
652 struct stat st;
653 long len_to_copy;
654 int ret = 0;
655
656 if(fstat(fw_fd, &st) < 0)
657 return -1;
658 len_to_copy = st.st_size;
659
660 write(loading_fd, "1", 1); /* start transfer */
661
662 while (len_to_copy > 0) {
663 char buf[PAGE_SIZE];
664 ssize_t nr;
665
666 nr = read(fw_fd, buf, sizeof(buf));
667 if(!nr)
668 break;
669 if(nr < 0) {
670 ret = -1;
671 break;
672 }
673
674 len_to_copy -= nr;
675 while (nr > 0) {
676 ssize_t nw = 0;
677
678 nw = write(data_fd, buf + nw, nr);
679 if(nw <= 0) {
680 ret = -1;
681 goto out;
682 }
683 nr -= nw;
684 }
685 }
686
687out:
688 if(!ret)
689 write(loading_fd, "0", 1); /* successful end of transfer */
690 else
691 write(loading_fd, "-1", 2); /* abort transfer */
692
693 return ret;
694}
695
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700696static int is_booting(void)
697{
698 return access("/dev/.booting", F_OK) == 0;
699}
700
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700701static void process_firmware_event(struct uevent *uevent)
702{
Brian Swetland02863b92010-09-19 03:36:39 -0700703 char *root, *loading, *data, *file1 = NULL, *file2 = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700704 int l, loading_fd, data_fd, fw_fd;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700705 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700706
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700707 INFO("firmware: loading '%s' for '%s'\n",
708 uevent->firmware, uevent->path);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700709
710 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
711 if (l == -1)
712 return;
713
714 l = asprintf(&loading, "%sloading", root);
715 if (l == -1)
716 goto root_free_out;
717
718 l = asprintf(&data, "%sdata", root);
719 if (l == -1)
720 goto loading_free_out;
721
Brian Swetland02863b92010-09-19 03:36:39 -0700722 l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
723 if (l == -1)
724 goto data_free_out;
725
726 l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700727 if (l == -1)
728 goto data_free_out;
729
730 loading_fd = open(loading, O_WRONLY);
731 if(loading_fd < 0)
732 goto file_free_out;
733
734 data_fd = open(data, O_WRONLY);
735 if(data_fd < 0)
736 goto loading_close_out;
737
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700738try_loading_again:
Brian Swetland02863b92010-09-19 03:36:39 -0700739 fw_fd = open(file1, O_RDONLY);
740 if(fw_fd < 0) {
741 fw_fd = open(file2, O_RDONLY);
Benoit Goby609d8822010-11-09 18:10:24 -0800742 if (fw_fd < 0) {
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700743 if (booting) {
744 /* If we're not fully booted, we may be missing
745 * filesystems needed for firmware, wait and retry.
746 */
747 usleep(100000);
748 booting = is_booting();
749 goto try_loading_again;
750 }
751 INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno);
Benoit Goby609d8822010-11-09 18:10:24 -0800752 write(loading_fd, "-1", 2);
Brian Swetland02863b92010-09-19 03:36:39 -0700753 goto data_close_out;
Benoit Goby609d8822010-11-09 18:10:24 -0800754 }
Brian Swetland02863b92010-09-19 03:36:39 -0700755 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700756
757 if(!load_firmware(fw_fd, loading_fd, data_fd))
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700758 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700759 else
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700760 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700761
762 close(fw_fd);
763data_close_out:
764 close(data_fd);
765loading_close_out:
766 close(loading_fd);
767file_free_out:
Brian Swetland02863b92010-09-19 03:36:39 -0700768 free(file1);
769 free(file2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700770data_free_out:
771 free(data);
772loading_free_out:
773 free(loading);
774root_free_out:
775 free(root);
776}
777
778static void handle_firmware_event(struct uevent *uevent)
779{
780 pid_t pid;
Colin Cross982a8152010-06-03 12:21:01 -0700781 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700782
783 if(strcmp(uevent->subsystem, "firmware"))
784 return;
785
786 if(strcmp(uevent->action, "add"))
787 return;
788
789 /* we fork, to avoid making large memory allocations in init proper */
790 pid = fork();
791 if (!pid) {
792 process_firmware_event(uevent);
793 exit(EXIT_SUCCESS);
794 }
795}
796
797#define UEVENT_MSG_LEN 1024
Colin Cross0dd7ca62010-04-13 19:25:51 -0700798void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700799{
Vernon Tang3f582e92011-04-25 13:08:17 +1000800 char msg[UEVENT_MSG_LEN+2];
801 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700802 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700803 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700804 continue;
805
806 msg[n] = '\0';
807 msg[n+1] = '\0';
808
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700809 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700810 parse_event(msg, &uevent);
811
812 handle_device_event(&uevent);
813 handle_firmware_event(&uevent);
814 }
815}
816
817/* Coldboot walks parts of the /sys tree and pokes the uevent files
818** to cause the kernel to regenerate device add events that happened
819** before init's device manager was started
820**
821** We drain any pending events from the netlink socket every time
822** we poke another uevent file to make sure we don't overrun the
823** socket's buffer.
824*/
825
Colin Cross0dd7ca62010-04-13 19:25:51 -0700826static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700827{
828 struct dirent *de;
829 int dfd, fd;
830
831 dfd = dirfd(d);
832
833 fd = openat(dfd, "uevent", O_WRONLY);
834 if(fd >= 0) {
835 write(fd, "add\n", 4);
836 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700837 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700838 }
839
840 while((de = readdir(d))) {
841 DIR *d2;
842
843 if(de->d_type != DT_DIR || de->d_name[0] == '.')
844 continue;
845
846 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
847 if(fd < 0)
848 continue;
849
850 d2 = fdopendir(fd);
851 if(d2 == 0)
852 close(fd);
853 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700854 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700855 closedir(d2);
856 }
857 }
858}
859
Colin Cross0dd7ca62010-04-13 19:25:51 -0700860static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700861{
862 DIR *d = opendir(path);
863 if(d) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700864 do_coldboot(d);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700865 closedir(d);
866 }
867}
868
Colin Cross0dd7ca62010-04-13 19:25:51 -0700869void device_init(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700870{
871 suseconds_t t0, t1;
Colin Crossf83d0b92010-04-21 12:04:20 -0700872 struct stat info;
873 int fd;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500874#ifdef HAVE_SELINUX
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400875 sehandle = NULL;
876 if (is_selinux_enabled() > 0) {
877 sehandle = selinux_android_file_context_handle();
878 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500879#endif
Dima Zavin2d55e022011-08-31 18:07:36 -0700880 /* is 64K enough? udev uses 16MB! */
881 device_fd = uevent_open_socket(64*1024, true);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700882 if(device_fd < 0)
883 return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700884
Colin Cross0dd7ca62010-04-13 19:25:51 -0700885 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
886 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700887
Colin Crossf83d0b92010-04-21 12:04:20 -0700888 if (stat(coldboot_done, &info) < 0) {
889 t0 = get_usecs();
890 coldboot("/sys/class");
891 coldboot("/sys/block");
892 coldboot("/sys/devices");
893 t1 = get_usecs();
894 fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
895 close(fd);
896 log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
897 } else {
898 log_event_print("skipping coldboot, already done\n");
899 }
Colin Cross0dd7ca62010-04-13 19:25:51 -0700900}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700901
Colin Cross0dd7ca62010-04-13 19:25:51 -0700902int get_device_fd()
903{
904 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700905}