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