android 中的ueventd是一个守护进程,主要作用是接收uevent来创建或删除/dev/xxx(设备节点),ueventd代码不多,下面我们直接针对代码分析。


int ueventd_main(int argc, char **argv)

{

   struct pollfd ufd;

   int nr;

   char tmp[32];

 

   /*

    * init sets the umask to 077 for forked processes. We need to

    * create files with exact permissions, without modification by

    * the umask.

    */

   umask(000);

 

   /* Prevent fire-and-forget children from becoming zombies.

    * If we should need to wait() for some children in the future

    * (as opposed to none right now), double-forking here instead

    * of ignoring SIGCHLD may be the better solution.

    */

   signal(SIGCHLD, SIG_IGN);

 

   open_devnull_stdio();

   klog_init();

 

   INFO("starting ueventd\n");

 

   /* Respect hardware passed in through the kernel cmd line. Here we willlook

    * for androidboot.hardware param in kernel cmdline, and save its valuein

    * hardware[]. */

   import_kernel_cmdline(0, import_kernel_nv);

 

   get_hardware_name(hardware, &revision);

 

 

   //解析ueventd.rcueventd.xx.rc,将文件中的每一行信息存放到一个perm_node结构体,然后将这些结构体组成一个双向链表,每一行包含文件名、uidgid、权限等

   ueventd_parse_config_file("/ueventd.rc");

 

   snprintf(tmp, sizeof(tmp), "/ueventd.%s.rc", hardware);

   ueventd_parse_config_file(tmp);

 

   //1.建立接收ueventsocket   2.如果没有做过coldboot,做一次coldboot

   //所谓coldboot是指让kernel重新生成deviceadd事件

   device_init();

 

   ufd.events = POLLIN;

   ufd.fd = get_device_fd();

      

   //循环poll ufdPOLLIN事件, POLLIN事件表示有数据可读,具体可参考linux poll系统调用的相关资料

   while(1) {

       ufd.revents = 0;

       nr = poll(&ufd, 1, -1);

       if (nr <= 0)

           continue;

       if (ufd.revents == POLLIN)

               handle_device_fd();   //详见下文

    }

}

 

#define UEVENT_MSG_LEN  1024

void handle_device_fd()

{

   char msg[UEVENT_MSG_LEN+2];

   int n;

   while ((n = uevent_kernel_multicast_recv(device_fd, msg,UEVENT_MSG_LEN)) > 0) {

       if(n >= UEVENT_MSG_LEN)   /*overflow -- discard */

           continue;

 

       msg[n] = '\0';

       msg[n+1] = '\0';

 

       struct uevent uevent;

       //解析uevent消息,保存到uevent变量中

       parse_event(msg, &uevent);

 

 

       handle_device_event(&uevent);            //详见下文

 

       //处理subsystemfirmwareuevent,详见下文

       handle_firmware_event(&uevent);

    }

}

 

 

 

 

static void handle_device_event(structuevent *uevent)

{

    //针对actionaddchanger,修复一下文件权限

   if (!strcmp(uevent->action,"add") ||!strcmp(uevent->action, "change"))

       fixup_sys_perms(uevent->path);

     

   //针对blockplatform等设备进行处理 

   if (!strncmp(uevent->subsystem, "block", 5)) {

       handle_block_device_event(uevent);

    }else if (!strncmp(uevent->subsystem, "platform", 8)) {

       handle_platform_device_event(uevent);

    }else {

       handle_generic_device_event(uevent);

    }

}

 

 




 


更多相关文章

  1. [android]在上下文菜单的选中事件中获取列表选中的元素
  2. Android(安卓)技巧 - listview 里面的图片不能响应onClick事件?
  3. Android(安卓)事件触发机制
  4. 我的android 第19天 - 上下文菜单
  5. android 触发AppWidget上控件事件来更新AppWidget
  6. android textview 自动链接网址 修改默认点击事件
  7. 81.s1-禁用checkBox点击事件
  8. Android基础 --- Widget
  9. Android(安卓)可响应drawable点击事件的TextView

随机推荐

  1. Android平台安全分析
  2. Android入门学习笔记(一):Android初认识
  3. android:padding和android:layout_margin
  4. Android安装的时候系统都做了些什么
  5. Android SDK Document 框架导读的翻译和
  6. android 获取versionName和versionCode以
  7. FFmpeg和android播放器
  8. Android应用程序键盘(Keyboard)消息处理机
  9. android 抓包
  10. ANDROID SHAPE鐢诲渾褰㈣儗鏅痏ANDROID瀹