2018-09-11 init ueventd

From: https://blog.csdn.net/zifehng/article/details/72522949

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

如果执行“./ueventd”,进入第一个条件分支,执行uevent_main()函数;
如果执行“./watchdog”,进入第二个条件分支,执行watchdogd_main()函数;
如果执行”./init”,跳过所有分支条件,继续执行main()函数。

# Make a symlink from /sbin/ueventd and /sbin/watchdogd to /init
SYMLINKS := \
    $(TARGET_ROOT_OUT)/sbin/ueventd \
    $(TARGET_ROOT_OUT)/sbin/watchdogd

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

system/core/rootdir/init.rc

on early-init
    # Set init and its forked children's oom_adj.
    write /proc/1/oom_score_adj -1000

    # Apply strict SELinux checking of PROT_EXEC on mmap/mprotect calls.
    write /sys/fs/selinux/checkreqprot 0

    # Set the security context for the init process.
    # This should occur before anything else (e.g. ueventd) is started.
    setcon u:r:init:s0

    # Set the security context of /adb_keys if present.
    restorecon /adb_keys

    start ueventd

system/core/init/init.c

int main(int argc, char **argv)
{

    ......

    if (!strcmp(basename(argv[0]), "ueventd"))
        return ueventd_main(argc, argv);

    if (!strcmp(basename(argv[0]), "watchdogd"))
        return watchdogd_main(argc, argv);

    ......

}

2. ueventd代码分析

2.1 main

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

  • 解析ueventd.rc文件,管理设备节点权限;
  • 递归扫描/sys目录,根据uevent文件,静态创建设备节点;
  • 通过netlink获取内核uevent事件,动态创建设备节点。

你可能感兴趣的:(2018-09-11 init ueventd)