[关闭]
@zifehng 2017-06-15T03:16:12.000000Z 字数 6084 阅读 3923

Android ueventd浅析

Android ueventd


本文基于mstar828、Android 5.0.1,其他平台仅供参考

在linux2.6之后,udev取代了devfs,但是在android中却没有udev或者mdev[1],而是由ueventd进程实现了类似功能(管理设备节点权限、创建设备节点)。

ueventd通过两种方式创建设备节点:

1. ueventd启动过程

在init.rc中,当触发条件为“early-init”时ueventd被启动:
system/core/rootdir/init.rc

  1. on early-init
  2. # Set init and its forked children's oom_adj.
  3. write /proc/1/oom_score_adj -1000
  4. # Apply strict SELinux checking of PROT_EXEC on mmap/mprotect calls.
  5. write /sys/fs/selinux/checkreqprot 0
  6. # Set the security context for the init process.
  7. # This should occur before anything else (e.g. ueventd) is started.
  8. setcon u:r:init:s0
  9. # Set the security context of /adb_keys if present.
  10. restorecon /adb_keys
  11. start ueventd

在运行环境中查看命令“/sbin/ueventd”,其实它是"/init"的软链接:

  1. shell@wwt:/ # ls sbin -l
  2. -rwxr-x--- root root 499152 1970-01-01 08:00 adbd
  3. -rwxr-x--- root root 3325472 1970-01-01 08:00 healthd
  4. lrwxrwxrwx root root 1970-01-01 08:00 ueventd -> ../init
  5. lrwxrwxrwx root root 1970-01-01 08:00 watchdogd -> ../init

通过分析Android.mk可知,ueventd.c、watchdog.c与init.c被编译成了同一个可执行文件“/init”,并创建了软链接“/sbin/ueventd”、“/sbin/watchdog”指向“/init”:

system/core/init/Android.mk

  1. LOCAL_SRC_FILES:= \
  2. builtins.c \
  3. init.c \
  4. devices.c \
  5. property_service.c \
  6. util.c \
  7. parser.c \
  8. keychords.c \
  9. signal_handler.c \
  10. init_parser.c \
  11. ueventd.c \
  12. ueventd_parser.c \
  13. watchdogd.c
  14. ......
  15. LOCAL_MODULE:= init
  16. ......
  17. # Make a symlink from /sbin/ueventd and /sbin/watchdogd to /init
  18. SYMLINKS := \
  19. $(TARGET_ROOT_OUT)/sbin/ueventd \
  20. $(TARGET_ROOT_OUT)/sbin/watchdogd

原来在文件init.c的main()函数中有一个巧妙的处理:可以通过判断第一个运行参数来启动不同的进程:

system/core/init/init.c

  1. int main(int argc, char **argv)
  2. {
  3. ......
  4. if (!strcmp(basename(argv[0]), "ueventd"))
  5. return ueventd_main(argc, argv);
  6. if (!strcmp(basename(argv[0]), "watchdogd"))
  7. return watchdogd_main(argc, argv);
  8. ......
  9. }

因此,脚本init.rc中的命令“start ueventd”最终执行的是ueventd_main()函数。

2. ueventd代码分析

2.1 main

ueventd_main()函数就是ueventd进程的主体,实现了以下几个功能:

system/core/init/ueventd.c

  1. int ueventd_main(int argc, char **argv)
  2. {
  3. struct pollfd ufd;
  4. int nr;
  5. char tmp[32];
  6. INFO("starting ueventd\n");
  7. ......
  8. // 解析ueventd.rc文件
  9. ueventd_parse_config_file("/ueventd.rc");
  10. // 解析厂商相关的ueventd.$(TARGET_BOARD_PLATFORM).rc文件
  11. snprintf(tmp, sizeof(tmp), "/ueventd.%s.rc", hardware);
  12. ueventd_parse_config_file(tmp);
  13. // 创建netlink sockfd(全局变量device_fd),用于监听uevent事件
  14. // 执行coldboot,递归扫描/sys目录下uevent文件,创建相应设备节点
  15. device_init();
  16. ufd.events = POLLIN;
  17. // 获取device_init()创建的sockfd
  18. ufd.fd = get_device_fd();
  19. while(1) {
  20. ufd.revents = 0;
  21. // 通过sockfd监听内核uevent事件
  22. nr = poll(&ufd, 1, -1);
  23. if (nr <= 0)
  24. continue;
  25. if (ufd.revents & POLLIN)
  26. // 当接收到内核uevent事件时,创建相应设备节点
  27. handle_device_fd();
  28. }
  29. }

2.2 device_init

device_init()函数做了两件事:

这里我们对coldboot()函数代码进行重点分析,调用关系如下:

main() -> device_init()-> coldboot() -> do_coldboot()

system/core/init/devices.c

  1. void device_init(void)
  2. {
  3. ......
  4. // 创建netlink sockfd(全局变量device_fd),用于监听uevent事件
  5. device_fd = uevent_open_socket(256*1024, true);
  6. if(device_fd < 0)
  7. return;
  8. fcntl(device_fd, F_SETFD, FD_CLOEXEC);
  9. fcntl(device_fd, F_SETFL, O_NONBLOCK);
  10. // 递归扫描/sys目录下uevent文件,创建相应设备节点
  11. if (stat(coldboot_done, &info) < 0) {
  12. ......
  13. coldboot("/sys/class");
  14. coldboot("/sys/block");
  15. coldboot("/sys/devices");
  16. ......
  17. }
  18. ......
  19. }
  1. static void coldboot(const char *path)
  2. {
  3. DIR *d = opendir(path);
  4. if(d) {
  5. do_coldboot(d);
  6. closedir(d);
  7. }
  8. }
  1. static void do_coldboot(DIR *d)
  2. {
  3. struct dirent *de;
  4. int dfd, fd;
  5. // 获得目录文件描述符
  6. dfd = dirfd(d);
  7. // 打开目录中的uevent节点,写入“add\n”触发内核uevent事件并处理
  8. fd = openat(dfd, "uevent", O_WRONLY);
  9. if(fd >= 0) {
  10. write(fd, "add\n", 4);
  11. close(fd);
  12. handle_device_fd();
  13. }
  14. // 递归调用do_coldboot(),扫描uevent节点
  15. while((de = readdir(d))) {
  16. DIR *d2;
  17. if(de->d_type != DT_DIR || de->d_name[0] == '.')
  18. continue;
  19. fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
  20. if(fd < 0)
  21. continue;
  22. d2 = fdopendir(fd);
  23. if(d2 == 0)
  24. close(fd);
  25. else {
  26. do_coldboot(d2);
  27. closedir(d2);
  28. }
  29. }
  30. }

2.3 handle_device_id

在main()函数中通过poll监听到内核uevent事件后,由handler_device_id()函数进行处理:

这一部分代码的调用关系如下:

main() -> handle_device_id() -> handle_device_event() -> handle_generic_device_event() -> handle_device() -> make_device() -> mknode()

system/core/init/devices.c

  1. void handle_device_fd()
  2. {
  3. char msg[UEVENT_MSG_LEN+2];
  4. int n;
  5. // 通过sockfd调用recvmsg()获取内核uevent事件,以字符串形式存入msg
  6. while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
  7. ......
  8. struct uevent uevent;
  9. // 将字符串msg解析成uevent
  10. parse_event(msg, &uevent);
  11. ......
  12. // 处理设备相关uevent事件
  13. handle_device_event(&uevent);
  14. // 处理固件相关uevent事件(暂不分析)
  15. handle_firmware_event(&uevent);
  16. }
  17. }
  1. static void handle_device_event(struct uevent *uevent)
  2. {
  3. ......
  4. handle_generic_device_event(uevent);
  5. ......
  6. }
  1. static void handle_generic_device_event(struct uevent *uevent)
  2. {
  3. ......
  4. // 根据uevent事件中子系统名称,创建/dev目录及其子目录
  5. } else if(!strncmp(uevent->subsystem, "input", 5)) {
  6. base = "/dev/input/";
  7. make_dir(base, 0755);
  8. } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
  9. base = "/dev/mtd/";
  10. make_dir(base, 0755);
  11. } else if(!strncmp(uevent->subsystem, "sound", 5)) {
  12. base = "/dev/snd/";
  13. make_dir(base, 0755);
  14. } else if(!strncmp(uevent->subsystem, "misc", 4) &&
  15. !strncmp(name, "log_", 4)) {
  16. kernel_logger();
  17. base = "/dev/log/";
  18. make_dir(base, 0755);
  19. name += 4;
  20. } else
  21. base = "/dev/";
  22. links = get_character_device_symlinks(uevent);
  23. if (!devpath[0])
  24. snprintf(devpath, sizeof(devpath), "%s%s", base, name);
  25. // 根据uevent事件中的信息创建/删除节点及链接
  26. handle_device(uevent->action, devpath, uevent->path, 0,
  27. uevent->major, uevent->minor, links);
  28. }
  1. static void handle_device(const char *action, const char *devpath,
  2. const char *path, int block, int major, int minor, char **links)
  3. {
  4. ......
  5. // 当uevent事件中的atcion为“add”时,创建节点及链接
  6. if(!strcmp(action, "add")) {
  7. make_device(devpath, path, block, major, minor, (const char **)links);
  8. if (links) {
  9. for (i = 0; links[i]; i++)
  10. make_link(devpath, links[i]);
  11. }
  12. }
  13. // 当uevent事件中的atcion为“remove”,删除链接
  14. if(!strcmp(action, "remove")) {
  15. if (links) {
  16. for (i = 0; links[i]; i++)
  17. remove_link(devpath, links[i]);
  18. }
  19. unlink(devpath);
  20. }
  21. ......
  22. }
  1. static void make_device(const char *path,
  2. const char *upath UNUSED,
  3. int block, int major, int minor,
  4. const char **links)
  5. {
  6. ......
  7. // 合成设备号
  8. dev = makedev(major, minor);
  9. ......
  10. // 根据文件路径、权限、设备号创建节点
  11. mknod(path, mode, dev);
  12. ......
  13. }

[1] 由busybox提供的简化版udev,适用于嵌入式应用场合
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注