Advertisement
Jack2

Untitled

Oct 10th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.02 KB | None | 0 0
  1. /* Coldboot walks parts of the /sys tree and pokes the uevent files
  2. ** to cause the kernel to regenerate device add events that happened
  3. ** before init's device manager was started
  4. **
  5. ** We drain any pending events from the netlink socket every time
  6. ** we poke another uevent file to make sure we don't overrun the
  7. ** socket's buffer.  
  8. */
  9.  
  10. static void do_coldboot(DIR *d)
  11. {
  12.     struct dirent *de;
  13.     int dfd, fd;
  14.  
  15.     dfd = dirfd(d);
  16.  
  17.     fd = openat(dfd, "uevent", O_WRONLY);
  18.     if(fd >= 0) {
  19.         write(fd, "add\n", 4);
  20.         close(fd);
  21.         handle_device_fd();
  22.     }
  23.  
  24.     while((de = readdir(d))) {
  25.         DIR *d2;
  26.  
  27.         if(de->d_type != DT_DIR || de->d_name[0] == '.')
  28.             continue;
  29.  
  30.         fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
  31.         if(fd < 0)
  32.             continue;
  33.  
  34.         d2 = fdopendir(fd);
  35.         if(d2 == 0)
  36.             close(fd);
  37.         else {
  38.             do_coldboot(d2);
  39.             closedir(d2);
  40.         }
  41.     }
  42. }
  43.  
  44. static void coldboot(const char *path)
  45. {
  46.     DIR *d = opendir(path);
  47.     if(d) {
  48.         do_coldboot(d);
  49.         closedir(d);
  50.     }
  51. }
  52.  
  53. void device_init(void)
  54. {
  55.     suseconds_t t0, t1;
  56.     struct stat info;
  57.     int fd;
  58.  
  59.     sehandle = NULL;
  60.     if (is_selinux_enabled() > 0) {
  61.         sehandle = selinux_android_file_context_handle();
  62.         selinux_status_open(true);
  63.     }
  64.  
  65.     /* is 256K enough? udev uses 16MB! */
  66.     device_fd = uevent_open_socket(256*1024, true);
  67.     if(device_fd < 0)
  68.         return;
  69.  
  70.     fcntl(device_fd, F_SETFD, FD_CLOEXEC);
  71.     fcntl(device_fd, F_SETFL, O_NONBLOCK);
  72.  
  73.     if (stat(coldboot_done, &info) < 0) {
  74.         t0 = get_usecs();
  75.         coldboot("/sys/class");
  76.         coldboot("/sys/block");
  77.         coldboot("/sys/devices");
  78.         t1 = get_usecs();
  79.         fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
  80.         close(fd);
  81.         log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
  82.     } else {
  83.         log_event_print("skipping coldboot, already done\n");
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement