devices.c revision ff5c802e6ce489a36ac28e3d93b7e8541b3e4d81
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#ifdef HAVE_SELINUX 34#include <selinux/selinux.h> 35#include <selinux/label.h> 36#endif 37 38#include <private/android_filesystem_config.h> 39#include <sys/time.h> 40#include <asm/page.h> 41#include <sys/wait.h> 42 43#include <cutils/list.h> 44#include <cutils/uevent.h> 45 46#include "devices.h" 47#include "util.h" 48#include "log.h" 49 50#define SYSFS_PREFIX "/sys" 51#define FIRMWARE_DIR1 "/etc/firmware" 52#define FIRMWARE_DIR2 "/vendor/firmware" 53#define FIRMWARE_DIR3 "/firmware/image" 54 55#ifdef HAVE_SELINUX 56extern struct selabel_handle *sehandle; 57#endif 58 59static int device_fd = -1; 60 61struct uevent { 62 const char *action; 63 const char *path; 64 const char *subsystem; 65 const char *firmware; 66 const char *partition_name; 67 const char *device_name; 68 int partition_num; 69 int major; 70 int minor; 71}; 72 73struct perms_ { 74 char *name; 75 char *attr; 76 mode_t perm; 77 unsigned int uid; 78 unsigned int gid; 79 unsigned short prefix; 80}; 81 82struct perm_node { 83 struct perms_ dp; 84 struct listnode plist; 85}; 86 87struct platform_node { 88 char *name; 89 int name_len; 90 struct listnode list; 91}; 92 93static list_declare(sys_perms); 94static list_declare(dev_perms); 95static list_declare(platform_names); 96 97int 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)); 101 if (!node) 102 return -ENOMEM; 103 104 node->dp.name = strdup(name); 105 if (!node->dp.name) 106 return -ENOMEM; 107 108 if (attr) { 109 node->dp.attr = strdup(attr); 110 if (!node->dp.attr) 111 return -ENOMEM; 112 } 113 114 node->dp.perm = perm; 115 node->dp.uid = uid; 116 node->dp.gid = gid; 117 node->dp.prefix = prefix; 118 119 if (attr) 120 list_add_tail(&sys_perms, &node->plist); 121 else 122 list_add_tail(&dev_perms, &node->plist); 123 124 return 0; 125} 126 127void fixup_sys_perms(const char *upath) 128{ 129 char buf[512]; 130 struct listnode *node; 131 struct perms_ *dp; 132 133 /* 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))) 140 continue; 141 } else { 142 if (strcmp(upath, dp->name + 4)) 143 continue; 144 } 145 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); 153 } 154} 155 156static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid) 157{ 158 mode_t perm; 159 struct listnode *node; 160 struct perm_node *perm_node; 161 struct perms_ *dp; 162 163 /* 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; 169 170 if (dp->prefix) { 171 if (strncmp(path, dp->name, strlen(dp->name))) 172 continue; 173 } else { 174 if (strcmp(path, dp->name)) 175 continue; 176 } 177 *uid = dp->uid; 178 *gid = dp->gid; 179 return dp->perm; 180 } 181 /* Default if nothing found. */ 182 *uid = 0; 183 *gid = 0; 184 return 0600; 185} 186 187static void make_device(const char *path, 188 const char *upath, 189 int block, int major, int minor) 190{ 191 unsigned uid; 192 unsigned gid; 193 mode_t mode; 194 dev_t dev; 195#ifdef HAVE_SELINUX 196 char *secontext = NULL; 197#endif 198 199 mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR); 200#ifdef HAVE_SELINUX 201 if (sehandle) { 202 selabel_lookup(sehandle, &secontext, path, mode); 203 setfscreatecon(secontext); 204 } 205#endif 206 dev = makedev(major, minor); 207 /* 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); 213 mknod(path, mode, dev); 214 chown(path, uid, -1); 215 setegid(AID_ROOT); 216#ifdef HAVE_SELINUX 217 if (secontext) { 218 freecon(secontext); 219 setfscreatecon(NULL); 220 } 221#endif 222} 223 224static 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 286#if LOG_UEVENTS 287 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; 312 uevent->partition_name = NULL; 313 uevent->partition_num = -1; 314 uevent->device_name = NULL; 315 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); 336 } 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; 342 } else if(!strncmp(msg, "DEVNAME=", 8)) { 343 msg += 8; 344 uevent->device_name = msg; 345 } 346 347 /* advance to after the next \0 */ 348 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 357static 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 408static char **parse_platform_block_device(struct uevent *uevent) 409{ 410 const char *device; 411 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; 430 device = path + 18; 431 device = find_platform_device(device); 432 if (!device) 433 goto err; 434 435 INFO("found platform device %s\n", device); 436 437 snprintf(link_path, sizeof(link_path), "/dev/block/platform/%s", device); 438 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 469static void handle_device(const char *action, const char *devpath, 470 const char *path, int block, int major, int minor, char **links) 471{ 472 int i; 473 474 if(!strcmp(action, "add")) { 475 make_device(devpath, path, block, major, minor); 476 if (links) { 477 for (i = 0; links[i]; i++) 478 make_link(devpath, links[i]); 479 } 480 } 481 482 if(!strcmp(action, "remove")) { 483 if (links) { 484 for (i = 0; links[i]; i++) 485 remove_link(devpath, links[i]); 486 } 487 unlink(devpath); 488 } 489 490 if (links) { 491 for (i = 0; links[i]; i++) 492 free(links[i]); 493 free(links); 494 } 495} 496 497static 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 507static 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); 540 make_dir(base, 0755); 541 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); 547} 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")) { 562 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 } 595 } else { 596 /* ignore other USB events */ 597 return; 598 } 599 } else if (!strncmp(uevent->subsystem, "graphics", 8)) { 600 base = "/dev/graphics/"; 601 make_dir(base, 0755); 602 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) { 603 base = "/dev/oncrpc/"; 604 make_dir(base, 0755); 605 } else if (!strncmp(uevent->subsystem, "adsp", 4)) { 606 base = "/dev/adsp/"; 607 make_dir(base, 0755); 608 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) { 609 base = "/dev/msm_camera/"; 610 make_dir(base, 0755); 611 } else if(!strncmp(uevent->subsystem, "input", 5)) { 612 base = "/dev/input/"; 613 make_dir(base, 0755); 614 } else if(!strncmp(uevent->subsystem, "mtd", 3)) { 615 base = "/dev/mtd/"; 616 make_dir(base, 0755); 617 } else if(!strncmp(uevent->subsystem, "sound", 5)) { 618 base = "/dev/snd/"; 619 make_dir(base, 0755); 620 } else if(!strncmp(uevent->subsystem, "misc", 4) && 621 !strncmp(name, "log_", 4)) { 622 base = "/dev/log/"; 623 make_dir(base, 0755); 624 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") || !strcmp(uevent->action, "change")) 639 fixup_sys_perms(uevent->path); 640 641 if (!strncmp(uevent->subsystem, "block", 5)) { 642 handle_block_device_event(uevent); 643 } else if (!strncmp(uevent->subsystem, "platform", 8)) { 644 handle_platform_device_event(uevent); 645 } else { 646 handle_generic_device_event(uevent); 647 } 648} 649 650static 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 696static int is_booting(void) 697{ 698 return access("/dev/.booting", F_OK) == 0; 699} 700 701static void process_firmware_event(struct uevent *uevent) 702{ 703 char *root, *loading, *data, *file1 = NULL, *file2 = NULL, *file3 = NULL; 704 int l, loading_fd, data_fd, fw_fd; 705 int booting = is_booting(); 706 707 INFO("firmware: loading '%s' for '%s'\n", 708 uevent->firmware, uevent->path); 709 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 722 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); 727 if (l == -1) 728 goto data_free_out; 729 730 l = asprintf(&file3, FIRMWARE_DIR3"/%s", uevent->firmware); 731 if (l == -1) 732 goto data_free_out; 733 734 loading_fd = open(loading, O_WRONLY); 735 if(loading_fd < 0) 736 goto file_free_out; 737 738 data_fd = open(data, O_WRONLY); 739 if(data_fd < 0) 740 goto loading_close_out; 741 742try_loading_again: 743 fw_fd = open(file1, O_RDONLY); 744 if(fw_fd < 0) { 745 fw_fd = open(file2, O_RDONLY); 746 if (fw_fd < 0) { 747 fw_fd = open(file3, O_RDONLY); 748 if (fw_fd < 0) { 749 if (booting) { 750 /* If we're not fully booted, we may be missing 751 * filesystems needed for firmware, wait and retry. 752 */ 753 usleep(100000); 754 booting = is_booting(); 755 goto try_loading_again; 756 } 757 INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno); 758 write(loading_fd, "-1", 2); 759 goto data_close_out; 760 } 761 } 762 } 763 764 if(!load_firmware(fw_fd, loading_fd, data_fd)) 765 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware); 766 else 767 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware); 768 769 close(fw_fd); 770data_close_out: 771 close(data_fd); 772loading_close_out: 773 close(loading_fd); 774file_free_out: 775 free(file1); 776 free(file2); 777data_free_out: 778 free(data); 779loading_free_out: 780 free(loading); 781root_free_out: 782 free(root); 783} 784 785static void handle_firmware_event(struct uevent *uevent) 786{ 787 pid_t pid; 788 int ret; 789 790 if(strcmp(uevent->subsystem, "firmware")) 791 return; 792 793 if(strcmp(uevent->action, "add")) 794 return; 795 796 /* we fork, to avoid making large memory allocations in init proper */ 797 pid = fork(); 798 if (!pid) { 799 process_firmware_event(uevent); 800 exit(EXIT_SUCCESS); 801 } 802} 803 804#define UEVENT_MSG_LEN 1024 805void handle_device_fd() 806{ 807 char msg[UEVENT_MSG_LEN+2]; 808 int n; 809 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) { 810 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */ 811 continue; 812 813 msg[n] = '\0'; 814 msg[n+1] = '\0'; 815 816 struct uevent uevent; 817 parse_event(msg, &uevent); 818 819 handle_device_event(&uevent); 820 handle_firmware_event(&uevent); 821 } 822} 823 824/* Coldboot walks parts of the /sys tree and pokes the uevent files 825** to cause the kernel to regenerate device add events that happened 826** before init's device manager was started 827** 828** We drain any pending events from the netlink socket every time 829** we poke another uevent file to make sure we don't overrun the 830** socket's buffer. 831*/ 832 833static void do_coldboot(DIR *d) 834{ 835 struct dirent *de; 836 int dfd, fd; 837 838 dfd = dirfd(d); 839 840 fd = openat(dfd, "uevent", O_WRONLY); 841 if(fd >= 0) { 842 write(fd, "add\n", 4); 843 close(fd); 844 handle_device_fd(); 845 } 846 847 while((de = readdir(d))) { 848 DIR *d2; 849 850 if(de->d_type != DT_DIR || de->d_name[0] == '.') 851 continue; 852 853 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY); 854 if(fd < 0) 855 continue; 856 857 d2 = fdopendir(fd); 858 if(d2 == 0) 859 close(fd); 860 else { 861 do_coldboot(d2); 862 closedir(d2); 863 } 864 } 865} 866 867static void coldboot(const char *path) 868{ 869 DIR *d = opendir(path); 870 if(d) { 871 do_coldboot(d); 872 closedir(d); 873 } 874} 875 876void device_init(void) 877{ 878 suseconds_t t0, t1; 879 struct stat info; 880 int fd; 881#ifdef HAVE_SELINUX 882 struct selinux_opt seopts[] = { 883 { SELABEL_OPT_PATH, "/file_contexts" } 884 }; 885 886 if (is_selinux_enabled() > 0) 887 sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1); 888#endif 889 /* is 64K enough? udev uses 16MB! */ 890 device_fd = uevent_open_socket(64*1024, true); 891 if(device_fd < 0) 892 return; 893 894 fcntl(device_fd, F_SETFD, FD_CLOEXEC); 895 fcntl(device_fd, F_SETFL, O_NONBLOCK); 896 897 if (stat(coldboot_done, &info) < 0) { 898 t0 = get_usecs(); 899 coldboot("/sys/class"); 900 coldboot("/sys/block"); 901 coldboot("/sys/devices"); 902 t1 = get_usecs(); 903 fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000); 904 close(fd); 905 log_event_print("coldboot %ld uS\n", ((long) (t1 - t0))); 906 } else { 907 log_event_print("skipping coldboot, already done\n"); 908 } 909} 910 911int get_device_fd() 912{ 913 return device_fd; 914} 915