应用层捕获uevent事件

    技术2025-10-05  5

    device_add函数最终会调用kobject_event_env函数,如果event_helper存在的话,那么会调用这个函数

     

    event_helper可以通过如下方法设置 echo /your_helper_path/your_helper_cmd > /proc/sys/kernel/hotplug

     

    在嵌入式中这个event_helper是mdev,当然你可以自己写一个helper,我自己写了一个

    ------------------------------------

    #include <stdio.h>

     

    int main(int argc, char **argv)

    {

        char *action;

        char *envpath;

        char *subsystem;

     

        action = getenv("ACTION");

        envpath = getenv("DEVPATH");

        subsystem = getenv("SUBSYSTEM");

     

        if (subsystem && !strcmp(subsystem, "net")) {

            mkdir("/hao", 0777);

        }

    }

     

    ---------------------------------------

    之所以创建目录是printf没法打印信息,通过查看/hao的存在来确定该event helper已经被调用

     

    其实很简单device_add会通过两种方式通知应用层,一个是netlink,另外一个是event helper的调用,上层应用想要知道是否有设备添加,要么侦听netlink, 要么在event helper进行事件处理和分发,android也是如此。

     

    最新回复(0)