1 /* 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> 18 #include <stddef.h> 19 #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> 32 33 #include <selinux/selinux.h> 34 #include <selinux/label.h> 35 #include <selinux/android.h> 36 37 #include <private/android_filesystem_config.h> 38 #include <sys/time.h> 39 #include <asm/page.h> 40 #include <sys/wait.h> 41 42 #include <cutils/list.h> 43 #include <cutils/uevent.h> 44 45 #include "devices.h" 46 #include "util.h" 47 #include "log.h" 48 49 #define SYSFS_PREFIX "/sys" 50 #define FIRMWARE_DIR1 "/etc/firmware" 51 #define FIRMWARE_DIR2 "/vendor/firmware" 52 #define FIRMWARE_DIR3 "/firmware/image" 53 54 extern struct selabel_handle *sehandle; 55 56 static int device_fd = -1; 57 58 struct uevent { 59 const char *action; 60 const char *path; 61 const char *subsystem; 62 const char *firmware; 63 const char *partition_name; 64 const char *device_name; 65 int partition_num; 66 int major; 67 int minor; 68 }; 69 70 struct perms_ { 71 char *name; 72 char *attr; 73 mode_t perm; 74 unsigned int uid; 75 unsigned int gid; 76 unsigned short prefix; 77 }; 78 79 struct perm_node { 80 struct perms_ dp; 81 struct listnode plist; 82 }; 83 84 struct platform_node { 85 char *name; 86 char *path; 87 int path_len; 88 struct listnode list; 89 }; 90 91 static list_declare(sys_perms); 92 static list_declare(dev_perms); 93 static list_declare(platform_names); 94 95 int add_dev_perms(const char *name, const char *attr, 96 mode_t perm, unsigned int uid, unsigned int gid, 97 unsigned short prefix) { 98 struct perm_node *node = calloc(1, sizeof(*node)); 99 if (!node) 100 return -ENOMEM; 101 102 node->dp.name = strdup(name); 103 if (!node->dp.name) 104 return -ENOMEM; 105 106 if (attr) { 107 node->dp.attr = strdup(attr); 108 if (!node->dp.attr) 109 return -ENOMEM; 110 } 111 112 node->dp.perm = perm; 113 node->dp.uid = uid; 114 node->dp.gid = gid; 115 node->dp.prefix = prefix; 116 117 if (attr) 118 list_add_tail(&sys_perms, &node->plist); 119 else 120 list_add_tail(&dev_perms, &node->plist); 121 122 return 0; 123 } 124 125 void fixup_sys_perms(const char *upath) 126 { 127 char buf[512]; 128 struct listnode *node; 129 struct perms_ *dp; 130 char *secontext; 131 132 /* 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))) 139 continue; 140 } else { 141 if (strcmp(upath, dp->name + 4)) 142 continue; 143 } 144 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); 152 if (sehandle) { 153 secontext = NULL; 154 selabel_lookup(sehandle, &secontext, buf, 0); 155 if (secontext) { 156 setfilecon(buf, secontext); 157 freecon(secontext); 158 } 159 } 160 } 161 } 162 163 static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid) 164 { 165 mode_t perm; 166 struct listnode *node; 167 struct perm_node *perm_node; 168 struct perms_ *dp; 169 170 /* search the perms list in reverse so that ueventd.$hardware can 171 * override ueventd.rc 172 */ 173 list_for_each_reverse(node, &dev_perms) { 174 perm_node = node_to_item(node, struct perm_node, plist); 175 dp = &perm_node->dp; 176 177 if (dp->prefix) { 178 if (strncmp(path, dp->name, strlen(dp->name))) 179 continue; 180 } else { 181 if (strcmp(path, dp->name)) 182 continue; 183 } 184 *uid = dp->uid; 185 *gid = dp->gid; 186 return dp->perm; 187 } 188 /* Default if nothing found. */ 189 *uid = 0; 190 *gid = 0; 191 return 0600; 192 } 193 194 static void make_device(const char *path, 195 const char *upath, 196 int block, int major, int minor) 197 { 198 unsigned uid; 199 unsigned gid; 200 mode_t mode; 201 dev_t dev; 202 char *secontext = NULL; 203 204 mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR); 205 206 if (sehandle) { 207 selabel_lookup(sehandle, &secontext, path, mode); 208 setfscreatecon(secontext); 209 } 210 211 dev = makedev(major, minor); 212 /* Temporarily change egid to avoid race condition setting the gid of the 213 * device node. Unforunately changing the euid would prevent creation of 214 * some device nodes, so the uid has to be set with chown() and is still 215 * racy. Fixing the gid race at least fixed the issue with system_server 216 * opening dynamic input devices under the AID_INPUT gid. */ 217 setegid(gid); 218 mknod(path, mode, dev); 219 chown(path, uid, -1); 220 setegid(AID_ROOT); 221 222 if (secontext) { 223 freecon(secontext); 224 setfscreatecon(NULL); 225 } 226 } 227 228 static void add_platform_device(const char *path) 229 { 230 int path_len = strlen(path); 231 struct listnode *node; 232 struct platform_node *bus; 233 const char *name = path; 234 235 if (!strncmp(path, "/devices/", 9)) { 236 name += 9; 237 if (!strncmp(name, "platform/", 9)) 238 name += 9; 239 } 240 241 list_for_each_reverse(node, &platform_names) { 242 bus = node_to_item(node, struct platform_node, list); 243 if ((bus->path_len < path_len) && 244 (path[bus->path_len] == '/') && 245 !strncmp(path, bus->path, bus->path_len)) 246 /* subdevice of an existing platform, ignore it */ 247 return; 248 } 249 250 INFO("adding platform device %s (%s)\n", name, path); 251 252 bus = calloc(1, sizeof(struct platform_node)); 253 bus->path = strdup(path); 254 bus->path_len = path_len; 255 bus->name = bus->path + (name - path); 256 list_add_tail(&platform_names, &bus->list); 257 } 258 259 /* 260 * given a path that may start with a platform device, find the length of the 261 * platform device prefix. If it doesn't start with a platform device, return 262 * 0. 263 */ 264 static struct platform_node *find_platform_device(const char *path) 265 { 266 int path_len = strlen(path); 267 struct listnode *node; 268 struct platform_node *bus; 269 270 list_for_each_reverse(node, &platform_names) { 271 bus = node_to_item(node, struct platform_node, list); 272 if ((bus->path_len < path_len) && 273 (path[bus->path_len] == '/') && 274 !strncmp(path, bus->path, bus->path_len)) 275 return bus; 276 } 277 278 return NULL; 279 } 280 281 static void remove_platform_device(const char *path) 282 { 283 struct listnode *node; 284 struct platform_node *bus; 285 286 list_for_each_reverse(node, &platform_names) { 287 bus = node_to_item(node, struct platform_node, list); 288 if (!strcmp(path, bus->path)) { 289 INFO("removing platform device %s\n", bus->name); 290 free(bus->path); 291 list_remove(node); 292 free(bus); 293 return; 294 } 295 } 296 } 297 298 #if LOG_UEVENTS 299 300 static inline suseconds_t get_usecs(void) 301 { 302 struct timeval tv; 303 gettimeofday(&tv, 0); 304 return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec; 305 } 306 307 #define log_event_print(x...) INFO(x) 308 309 #else 310 311 #define log_event_print(fmt, args...) do { } while (0) 312 #define get_usecs() 0 313 314 #endif 315 316 static void parse_event(const char *msg, struct uevent *uevent) 317 { 318 uevent->action = ""; 319 uevent->path = ""; 320 uevent->subsystem = ""; 321 uevent->firmware = ""; 322 uevent->major = -1; 323 uevent->minor = -1; 324 uevent->partition_name = NULL; 325 uevent->partition_num = -1; 326 uevent->device_name = NULL; 327 328 /* currently ignoring SEQNUM */ 329 while(*msg) { 330 if(!strncmp(msg, "ACTION=", 7)) { 331 msg += 7; 332 uevent->action = msg; 333 } else if(!strncmp(msg, "DEVPATH=", 8)) { 334 msg += 8; 335 uevent->path = msg; 336 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) { 337 msg += 10; 338 uevent->subsystem = msg; 339 } else if(!strncmp(msg, "FIRMWARE=", 9)) { 340 msg += 9; 341 uevent->firmware = msg; 342 } else if(!strncmp(msg, "MAJOR=", 6)) { 343 msg += 6; 344 uevent->major = atoi(msg); 345 } else if(!strncmp(msg, "MINOR=", 6)) { 346 msg += 6; 347 uevent->minor = atoi(msg); 348 } else if(!strncmp(msg, "PARTN=", 6)) { 349 msg += 6; 350 uevent->partition_num = atoi(msg); 351 } else if(!strncmp(msg, "PARTNAME=", 9)) { 352 msg += 9; 353 uevent->partition_name = msg; 354 } else if(!strncmp(msg, "DEVNAME=", 8)) { 355 msg += 8; 356 uevent->device_name = msg; 357 } 358 359 /* advance to after the next \0 */ 360 while(*msg++) 361 ; 362 } 363 364 log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n", 365 uevent->action, uevent->path, uevent->subsystem, 366 uevent->firmware, uevent->major, uevent->minor); 367 } 368 369 static char **get_character_device_symlinks(struct uevent *uevent) 370 { 371 const char *parent; 372 char *slash; 373 char **links; 374 int link_num = 0; 375 int width; 376 struct platform_node *pdev; 377 378 pdev = find_platform_device(uevent->path); 379 if (!pdev) 380 return NULL; 381 382 links = malloc(sizeof(char *) * 2); 383 if (!links) 384 return NULL; 385 memset(links, 0, sizeof(char *) * 2); 386 387 /* skip "/devices/platform/<driver>" */ 388 parent = strchr(uevent->path + pdev->path_len, '/'); 389 if (!*parent) 390 goto err; 391 392 if (!strncmp(parent, "/usb", 4)) { 393 /* skip root hub name and device. use device interface */ 394 while (*++parent && *parent != '/'); 395 if (*parent) 396 while (*++parent && *parent != '/'); 397 if (!*parent) 398 goto err; 399 slash = strchr(++parent, '/'); 400 if (!slash) 401 goto err; 402 width = slash - parent; 403 if (width <= 0) 404 goto err; 405 406 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0) 407 link_num++; 408 else 409 links[link_num] = NULL; 410 mkdir("/dev/usb", 0755); 411 } 412 else { 413 goto err; 414 } 415 416 return links; 417 err: 418 free(links); 419 return NULL; 420 } 421 422 static char **parse_platform_block_device(struct uevent *uevent) 423 { 424 const char *device; 425 struct platform_node *pdev; 426 char *slash; 427 int width; 428 char buf[256]; 429 char link_path[256]; 430 int fd; 431 int link_num = 0; 432 int ret; 433 char *p; 434 unsigned int size; 435 struct stat info; 436 437 pdev = find_platform_device(uevent->path); 438 if (!pdev) 439 return NULL; 440 device = pdev->name; 441 442 char **links = malloc(sizeof(char *) * 4); 443 if (!links) 444 return NULL; 445 memset(links, 0, sizeof(char *) * 4); 446 447 INFO("found platform device %s\n", device); 448 449 snprintf(link_path, sizeof(link_path), "/dev/block/platform/%s", device); 450 451 if (uevent->partition_name) { 452 p = strdup(uevent->partition_name); 453 sanitize(p); 454 if (strcmp(uevent->partition_name, p)) 455 NOTICE("Linking partition '%s' as '%s'\n", uevent->partition_name, p); 456 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0) 457 link_num++; 458 else 459 links[link_num] = NULL; 460 free(p); 461 } 462 463 if (uevent->partition_num >= 0) { 464 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0) 465 link_num++; 466 else 467 links[link_num] = NULL; 468 } 469 470 slash = strrchr(uevent->path, '/'); 471 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0) 472 link_num++; 473 else 474 links[link_num] = NULL; 475 476 return links; 477 } 478 479 static void handle_device(const char *action, const char *devpath, 480 const char *path, int block, int major, int minor, char **links) 481 { 482 int i; 483 484 if(!strcmp(action, "add")) { 485 make_device(devpath, path, block, major, minor); 486 if (links) { 487 for (i = 0; links[i]; i++) 488 make_link(devpath, links[i]); 489 } 490 } 491 492 if(!strcmp(action, "remove")) { 493 if (links) { 494 for (i = 0; links[i]; i++) 495 remove_link(devpath, links[i]); 496 } 497 unlink(devpath); 498 } 499 500 if (links) { 501 for (i = 0; links[i]; i++) 502 free(links[i]); 503 free(links); 504 } 505 } 506 507 static void handle_platform_device_event(struct uevent *uevent) 508 { 509 const char *path = uevent->path; 510 511 if (!strcmp(uevent->action, "add")) 512 add_platform_device(path); 513 else if (!strcmp(uevent->action, "remove")) 514 remove_platform_device(path); 515 } 516 517 static const char *parse_device_name(struct uevent *uevent, unsigned int len) 518 { 519 const char *name; 520 521 /* if it's not a /dev device, nothing else to do */ 522 if((uevent->major < 0) || (uevent->minor < 0)) 523 return NULL; 524 525 /* do we have a name? */ 526 name = strrchr(uevent->path, '/'); 527 if(!name) 528 return NULL; 529 name++; 530 531 /* too-long names would overrun our buffer */ 532 if(strlen(name) > len) 533 return NULL; 534 535 return name; 536 } 537 538 static void handle_block_device_event(struct uevent *uevent) 539 { 540 const char *base = "/dev/block/"; 541 const char *name; 542 char devpath[96]; 543 char **links = NULL; 544 545 name = parse_device_name(uevent, 64); 546 if (!name) 547 return; 548 549 snprintf(devpath, sizeof(devpath), "%s%s", base, name); 550 make_dir(base, 0755); 551 552 if (!strncmp(uevent->path, "/devices/", 9)) 553 links = parse_platform_block_device(uevent); 554 555 handle_device(uevent->action, devpath, uevent->path, 1, 556 uevent->major, uevent->minor, links); 557 } 558 559 static void handle_generic_device_event(struct uevent *uevent) 560 { 561 char *base; 562 const char *name; 563 char devpath[96] = {0}; 564 char **links = NULL; 565 566 name = parse_device_name(uevent, 64); 567 if (!name) 568 return; 569 570 if (!strncmp(uevent->subsystem, "usb", 3)) { 571 if (!strcmp(uevent->subsystem, "usb")) { 572 if (uevent->device_name) { 573 /* 574 * create device node provided by kernel if present 575 * see drivers/base/core.c 576 */ 577 char *p = devpath; 578 snprintf(devpath, sizeof(devpath), "/dev/%s", uevent->device_name); 579 /* skip leading /dev/ */ 580 p += 5; 581 /* build directories */ 582 while (*p) { 583 if (*p == '/') { 584 *p = 0; 585 make_dir(devpath, 0755); 586 *p = '/'; 587 } 588 p++; 589 } 590 } 591 else { 592 /* This imitates the file system that would be created 593 * if we were using devfs instead. 594 * Minors are broken up into groups of 128, starting at "001" 595 */ 596 int bus_id = uevent->minor / 128 + 1; 597 int device_id = uevent->minor % 128 + 1; 598 /* build directories */ 599 make_dir("/dev/bus", 0755); 600 make_dir("/dev/bus/usb", 0755); 601 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id); 602 make_dir(devpath, 0755); 603 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id); 604 } 605 } else { 606 /* ignore other USB events */ 607 return; 608 } 609 } else if (!strncmp(uevent->subsystem, "graphics", 8)) { 610 base = "/dev/graphics/"; 611 make_dir(base, 0755); 612 } else if (!strncmp(uevent->subsystem, "drm", 3)) { 613 base = "/dev/dri/"; 614 make_dir(base, 0755); 615 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) { 616 base = "/dev/oncrpc/"; 617 make_dir(base, 0755); 618 } else if (!strncmp(uevent->subsystem, "adsp", 4)) { 619 base = "/dev/adsp/"; 620 make_dir(base, 0755); 621 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) { 622 base = "/dev/msm_camera/"; 623 make_dir(base, 0755); 624 } else if(!strncmp(uevent->subsystem, "input", 5)) { 625 base = "/dev/input/"; 626 make_dir(base, 0755); 627 } else if(!strncmp(uevent->subsystem, "mtd", 3)) { 628 base = "/dev/mtd/"; 629 make_dir(base, 0755); 630 } else if(!strncmp(uevent->subsystem, "sound", 5)) { 631 base = "/dev/snd/"; 632 make_dir(base, 0755); 633 } else if(!strncmp(uevent->subsystem, "misc", 4) && 634 !strncmp(name, "log_", 4)) { 635 base = "/dev/log/"; 636 make_dir(base, 0755); 637 name += 4; 638 } else 639 base = "/dev/"; 640 links = get_character_device_symlinks(uevent); 641 642 if (!devpath[0]) 643 snprintf(devpath, sizeof(devpath), "%s%s", base, name); 644 645 handle_device(uevent->action, devpath, uevent->path, 0, 646 uevent->major, uevent->minor, links); 647 } 648 649 static void handle_device_event(struct uevent *uevent) 650 { 651 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change")) 652 fixup_sys_perms(uevent->path); 653 654 if (!strncmp(uevent->subsystem, "block", 5)) { 655 handle_block_device_event(uevent); 656 } else if (!strncmp(uevent->subsystem, "platform", 8)) { 657 handle_platform_device_event(uevent); 658 } else { 659 handle_generic_device_event(uevent); 660 } 661 } 662 663 static int load_firmware(int fw_fd, int loading_fd, int data_fd) 664 { 665 struct stat st; 666 long len_to_copy; 667 int ret = 0; 668 669 if(fstat(fw_fd, &st) < 0) 670 return -1; 671 len_to_copy = st.st_size; 672 673 write(loading_fd, "1", 1); /* start transfer */ 674 675 while (len_to_copy > 0) { 676 char buf[PAGE_SIZE]; 677 ssize_t nr; 678 679 nr = read(fw_fd, buf, sizeof(buf)); 680 if(!nr) 681 break; 682 if(nr < 0) { 683 ret = -1; 684 break; 685 } 686 687 len_to_copy -= nr; 688 while (nr > 0) { 689 ssize_t nw = 0; 690 691 nw = write(data_fd, buf + nw, nr); 692 if(nw <= 0) { 693 ret = -1; 694 goto out; 695 } 696 nr -= nw; 697 } 698 } 699 700 out: 701 if(!ret) 702 write(loading_fd, "0", 1); /* successful end of transfer */ 703 else 704 write(loading_fd, "-1", 2); /* abort transfer */ 705 706 return ret; 707 } 708 709 static int is_booting(void) 710 { 711 return access("/dev/.booting", F_OK) == 0; 712 } 713 714 static void process_firmware_event(struct uevent *uevent) 715 { 716 char *root, *loading, *data, *file1 = NULL, *file2 = NULL, *file3 = NULL; 717 int l, loading_fd, data_fd, fw_fd; 718 int booting = is_booting(); 719 720 INFO("firmware: loading '%s' for '%s'\n", 721 uevent->firmware, uevent->path); 722 723 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path); 724 if (l == -1) 725 return; 726 727 l = asprintf(&loading, "%sloading", root); 728 if (l == -1) 729 goto root_free_out; 730 731 l = asprintf(&data, "%sdata", root); 732 if (l == -1) 733 goto loading_free_out; 734 735 l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware); 736 if (l == -1) 737 goto data_free_out; 738 739 l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware); 740 if (l == -1) 741 goto data_free_out; 742 743 l = asprintf(&file3, FIRMWARE_DIR3"/%s", uevent->firmware); 744 if (l == -1) 745 goto data_free_out; 746 747 loading_fd = open(loading, O_WRONLY); 748 if(loading_fd < 0) 749 goto file_free_out; 750 751 data_fd = open(data, O_WRONLY); 752 if(data_fd < 0) 753 goto loading_close_out; 754 755 try_loading_again: 756 fw_fd = open(file1, O_RDONLY); 757 if(fw_fd < 0) { 758 fw_fd = open(file2, O_RDONLY); 759 if (fw_fd < 0) { 760 fw_fd = open(file3, O_RDONLY); 761 if (fw_fd < 0) { 762 if (booting) { 763 /* If we're not fully booted, we may be missing 764 * filesystems needed for firmware, wait and retry. 765 */ 766 usleep(100000); 767 booting = is_booting(); 768 goto try_loading_again; 769 } 770 INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno); 771 write(loading_fd, "-1", 2); 772 goto data_close_out; 773 } 774 } 775 } 776 777 if(!load_firmware(fw_fd, loading_fd, data_fd)) 778 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware); 779 else 780 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware); 781 782 close(fw_fd); 783 data_close_out: 784 close(data_fd); 785 loading_close_out: 786 close(loading_fd); 787 file_free_out: 788 free(file1); 789 free(file2); 790 free(file3); 791 data_free_out: 792 free(data); 793 loading_free_out: 794 free(loading); 795 root_free_out: 796 free(root); 797 } 798 799 static void handle_firmware_event(struct uevent *uevent) 800 { 801 pid_t pid; 802 int ret; 803 804 if(strcmp(uevent->subsystem, "firmware")) 805 return; 806 807 if(strcmp(uevent->action, "add")) 808 return; 809 810 /* we fork, to avoid making large memory allocations in init proper */ 811 pid = fork(); 812 if (!pid) { 813 process_firmware_event(uevent); 814 exit(EXIT_SUCCESS); 815 } 816 } 817 818 #define UEVENT_MSG_LEN 1024 819 void handle_device_fd() 820 { 821 char msg[UEVENT_MSG_LEN+2]; 822 int n; 823 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) { 824 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */ 825 continue; 826 827 msg[n] = '\0'; 828 msg[n+1] = '\0'; 829 830 struct uevent uevent; 831 parse_event(msg, &uevent); 832 833 handle_device_event(&uevent); 834 handle_firmware_event(&uevent); 835 } 836 } 837 838 /* Coldboot walks parts of the /sys tree and pokes the uevent files 839 ** to cause the kernel to regenerate device add events that happened 840 ** before init's device manager was started 841 ** 842 ** We drain any pending events from the netlink socket every time 843 ** we poke another uevent file to make sure we don't overrun the 844 ** socket's buffer. 845 */ 846 847 static void do_coldboot(DIR *d) 848 { 849 struct dirent *de; 850 int dfd, fd; 851 852 dfd = dirfd(d); 853 854 fd = openat(dfd, "uevent", O_WRONLY); 855 if(fd >= 0) { 856 write(fd, "add\n", 4); 857 close(fd); 858 handle_device_fd(); 859 } 860 861 while((de = readdir(d))) { 862 DIR *d2; 863 864 if(de->d_type != DT_DIR || de->d_name[0] == '.') 865 continue; 866 867 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY); 868 if(fd < 0) 869 continue; 870 871 d2 = fdopendir(fd); 872 if(d2 == 0) 873 close(fd); 874 else { 875 do_coldboot(d2); 876 closedir(d2); 877 } 878 } 879 } 880 881 static void coldboot(const char *path) 882 { 883 DIR *d = opendir(path); 884 if(d) { 885 do_coldboot(d); 886 closedir(d); 887 } 888 } 889 890 void device_init(void) 891 { 892 suseconds_t t0, t1; 893 struct stat info; 894 int fd; 895 896 sehandle = NULL; 897 if (is_selinux_enabled() > 0) { 898 sehandle = selinux_android_file_context_handle(); 899 } 900 901 /* is 256K enough? udev uses 16MB! */ 902 device_fd = uevent_open_socket(256*1024, true); 903 if(device_fd < 0) 904 return; 905 906 fcntl(device_fd, F_SETFD, FD_CLOEXEC); 907 fcntl(device_fd, F_SETFL, O_NONBLOCK); 908 909 if (stat(coldboot_done, &info) < 0) { 910 t0 = get_usecs(); 911 coldboot("/sys/class"); 912 coldboot("/sys/block"); 913 coldboot("/sys/devices"); 914 t1 = get_usecs(); 915 fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000); 916 close(fd); 917 log_event_print("coldboot %ld uS\n", ((long) (t1 - t0))); 918 } else { 919 log_event_print("skipping coldboot, already done\n"); 920 } 921 } 922 923 int get_device_fd() 924 { 925 return device_fd; 926 } 927