Android如何生成设备节点

转自:http://blog.csdn.net/xiyu_1986/article/details/7173090

在Android中,由于没有mdev和udev,所以它没有办法动态的生成设备节点,那么它是如何做的呢?

我们可以在system/core/init/下的init.c和devices.c中找到答案:

init.c中

 
 
  1. int main(int argc, char **argv)
  2. {
  3. ...
  4. /* Get the basic filesystem setup we need put
  5. * together in the initramdisk on / and then we'll
  6. * let the rc file figure out the rest.
  7. */
  8. mkdir("/dev", 0755);
  9. mkdir("/proc", 0755);
  10. mkdir("/sys", 0755);
  11. mount("tmpfs", "/dev", "tmpfs", 0, "mode=0755");
  12. mkdir("/dev/pts", 0755);
  13. mkdir("/dev/socket", 0755);
  14. mount("devpts", "/dev/pts", "devpts", 0, NULL);
  15. mount("proc", "/proc", "proc", 0, NULL);
  16. mount("sysfs", "/sys", "sysfs", 0, NULL);
  17. for(;;) {
  18. ...
  19. if (ufds[0].revents == POLLIN)
  20. handle_device_fd(device_fd);
  21. if (ufds[1].revents == POLLIN)
  22. handle_property_set_fd(property_set_fd);
  23. if (ufds[3].revents == POLLIN)
  24. handle_keychord(keychord_fd);
  25. }
  26. return 0;
  27. }

我们再来看看handle_device_fd(),该函数定义在devices.c中

 
 
  1. void handle_device_fd(int fd)
  2. {
  3. ...
  4. handle_device_event(&uevent);
  5. handle_firmware_event(&uevent);
  6. }
  7. }

而handle_device_event定义如下:

 
 
  1. static void handle_device_event(struct uevent *uevent)
  2. {
  3. ...
  4. if(!strcmp(uevent->action, "add")) {
  5. make_device(devpath, block, uevent->major, uevent->minor);
  6. return;
  7. }
  8. ...
  9. }

make_device定义如下:

 
 
  1. static void make_device(const char *path, int block, int major, int minor)
  2. {
  3. ...
  4. mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
  5. dev = (major << 8) | minor;
  6. ...
  7. setegid(gid);
  8. mknod(path, mode, dev);
  9. chown(path, uid, -1);
  10. setegid(AID_ROOT);
  11. }

我们看看get_device_perm如下实现:

 
 
  1. static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
  2. {
  3. mode_t perm;
  4. if (get_device_perm_inner(qemu_perms, path, uid, gid, &perm) == 0) {
  5. return perm;
  6. } else if (get_device_perm_inner(devperms, path, uid, gid, &perm) == 0) {
  7. return perm;
  8. } else {
  9. struct listnode *node;
  10. struct perm_node *perm_node;
  11. struct perms_ *dp;
  12. /* Check partners list. */
  13. list_for_each(node, &devperms_partners) {
  14. perm_node = node_to_item(node, struct perm_node, plist);
  15. dp = &perm_node->dp;
  16. if (dp->prefix) {
  17. if (strncmp(path, dp->name, strlen(dp->name)))
  18. continue;
  19. } else {
  20. if (strcmp(path, dp->name))
  21. continue;
  22. }
  23. /* Found perm in partner list. */
  24. *uid = dp->uid;
  25. *gid = dp->gid;
  26. return dp->perm;
  27. }
  28. /* Default if nothing found. */
  29. *uid = 0;
  30. *gid = 0;
  31. return 0600;
  32. }
  33. }

我们最后可以看到在devperms中定义了要生成的设备节点:

 
 
  1. static struct perms_ devperms[] = {
  2. { "/dev/null", 0666, AID_ROOT, AID_ROOT, 0 },
  3. { "/dev/zero", 0666, AID_ROOT, AID_ROOT, 0 },
  4. { "/dev/full", 0666, AID_ROOT, AID_ROOT, 0 },
  5. { "/dev/ptmx", 0666, AID_ROOT, AID_ROOT, 0 },
  6. { "/dev/tty", 0666, AID_ROOT, AID_ROOT, 0 },
  7. { "/dev/random", 0666, AID_ROOT, AID_ROOT, 0 },
  8. { "/dev/urandom", 0666, AID_ROOT, AID_ROOT, 0 },
  9. { "/dev/ashmem", 0666, AID_ROOT, AID_ROOT, 0 },
  10. { "/dev/binder", 0666, AID_ROOT, AID_ROOT, 0 },
  11. /* logger should be world writable (for logging) but not readable */
  12. { "/dev/log/", 0662, AID_ROOT, AID_LOG, 1 },
  13. /* the msm hw3d client device node is world writable/readable. */
  14. { "/dev/msm_hw3dc", 0666, AID_ROOT, AID_ROOT, 0 },
  15. /* gpu driver for adreno200 is globally accessible */
  16. { "/dev/kgsl", 0666, AID_ROOT, AID_ROOT, 0 },
  17. /* these should not be world writable */
  18. { "/dev/diag", 0660, AID_RADIO, AID_RADIO, 0 },
  19. { "/dev/diag_arm9", 0660, AID_RADIO, AID_RADIO, 0 },
  20. { "/dev/android_adb", 0660, AID_ADB, AID_ADB, 0 },
  21. { "/dev/android_adb_enable", 0660, AID_ADB, AID_ADB, 0 },
  22. { "/dev/ttyMSM0", 0600, AID_BLUETOOTH, AID_BLUETOOTH, 0 },
  23. { "/dev/ttyHS0", 0600, AID_BLUETOOTH, AID_BLUETOOTH, 0 },
  24. { "/dev/uinput", 0660, AID_SYSTEM, AID_BLUETOOTH, 0 },
  25. { "/dev/alarm", 0664, AID_SYSTEM, AID_RADIO, 0 },
  26. { "/dev/tty0", 0660, AID_ROOT, AID_SYSTEM, 0 },
  27. { "/dev/graphics/", 0660, AID_ROOT, AID_GRAPHICS, 1 },
  28. { "/dev/msm_hw3dm", 0660, AID_SYSTEM, AID_GRAPHICS, 0 },
  29. { "/dev/input/", 0660, AID_ROOT, AID_INPUT, 1 },
  30. { "/dev/eac", 0660, AID_ROOT, AID_AUDIO, 0 },
  31. { "/dev/cam", 0660, AID_ROOT, AID_CAMERA, 0 },
  32. { "/dev/pmem", 0660, AID_SYSTEM, AID_GRAPHICS, 0 },
  33. { "/dev/pmem_adsp", 0660, AID_SYSTEM, AID_AUDIO, 1 },
  34. { "/dev/pmem_camera", 0660, AID_SYSTEM, AID_CAMERA, 1 },
  35. { "/dev/oncrpc/", 0660, AID_ROOT, AID_SYSTEM, 1 },
  36. { "/dev/adsp/", 0660, AID_SYSTEM, AID_AUDIO, 1 },
  37. { "/dev/snd/", 0660, AID_SYSTEM, AID_AUDIO, 1 },
  38. { "/dev/mt9t013", 0660, AID_SYSTEM, AID_SYSTEM, 0 },
  39. { "/dev/msm_camera/", 0660, AID_SYSTEM, AID_SYSTEM, 1 },
  40. { "/dev/akm8976_daemon",0640, AID_COMPASS, AID_SYSTEM, 0 },
  41. { "/dev/akm8976_aot", 0640, AID_COMPASS, AID_SYSTEM, 0 },
  42. { "/dev/akm8973_daemon",0640, AID_COMPASS, AID_SYSTEM, 0 },
  43. { "/dev/akm8973_aot", 0640, AID_COMPASS, AID_SYSTEM, 0 },
  44. { "/dev/bma150", 0640, AID_COMPASS, AID_SYSTEM, 0 },
  45. { "/dev/cm3602", 0640, AID_COMPASS, AID_SYSTEM, 0 },
  46. { "/dev/akm8976_pffd", 0640, AID_COMPASS, AID_SYSTEM, 0 },
  47. { "/dev/lightsensor", 0640, AID_SYSTEM, AID_SYSTEM, 0 },
  48. { "/dev/msm_pcm_out", 0660, AID_SYSTEM, AID_AUDIO, 1 },
  49. { "/dev/msm_pcm_in", 0660, AID_SYSTEM, AID_AUDIO, 1 },
  50. { "/dev/msm_pcm_ctl", 0660, AID_SYSTEM, AID_AUDIO, 1 },
  51. { "/dev/msm_snd", 0660, AID_SYSTEM, AID_AUDIO, 1 },
  52. { "/dev/msm_mp3", 0660, AID_SYSTEM, AID_AUDIO, 1 },
  53. { "/dev/audience_a1026", 0660, AID_SYSTEM, AID_AUDIO, 1 },
  54. { "/dev/tpa2018d1", 0660, AID_SYSTEM, AID_AUDIO, 1 },
  55. { "/dev/msm_audpre", 0660, AID_SYSTEM, AID_AUDIO, 0 },
  56. { "/dev/msm_audio_ctl", 0660, AID_SYSTEM, AID_AUDIO, 0 },
  57. { "/dev/htc-acoustic", 0660, AID_SYSTEM, AID_AUDIO, 0 },
  58. { "/dev/vdec", 0660, AID_SYSTEM, AID_AUDIO, 0 },
  59. { "/dev/q6venc", 0660, AID_SYSTEM, AID_AUDIO, 0 },
  60. { "/dev/snd/dsp", 0660, AID_SYSTEM, AID_AUDIO, 0 },
  61. { "/dev/snd/dsp1", 0660, AID_SYSTEM, AID_AUDIO, 0 },
  62. { "/dev/snd/mixer", 0660, AID_SYSTEM, AID_AUDIO, 0 },
  63. { "/dev/smd0", 0640, AID_RADIO, AID_RADIO, 0 },
  64. { "/dev/qemu_trace", 0666, AID_SYSTEM, AID_SYSTEM, 0 },
  65. { "/dev/qmi", 0640, AID_RADIO, AID_RADIO, 0 },
  66. { "/dev/qmi0", 0640, AID_RADIO, AID_RADIO, 0 },
  67. { "/dev/qmi1", 0640, AID_RADIO, AID_RADIO, 0 },
  68. { "/dev/qmi2", 0640, AID_RADIO, AID_RADIO, 0 },
  69. /* CDMA radio interface MUX */
  70. { "/dev/ts0710mux", 0640, AID_RADIO, AID_RADIO, 1 },
  71. { "/dev/ppp", 0660, AID_RADIO, AID_VPN, 0 },
  72. { "/dev/tun", 0640, AID_VPN, AID_VPN, 0 },
  73. { NULL, 0, 0, 0, 0 },
  74. };

在Android中,没有独立的类似于udev或者mdev的用户程序,这个功能集成到了init中做了。代码见:system/core/init/init.c文件,如下:

if (ufds[0].revents == POLLIN)

handle_device_fd(device_fd);

其中handle_device_fd(device_fd)函数在system/core/init/devices.c中实现,参数device_fd 由函数device_init()->open_uevent_socket()->socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT)函数调用返回。

函数handle_device_fd(device_fd)中,根据传进来的device_fd参数,调用recv(fd, msg, UEVENT_MSG_LEN, 0)函数,将内核探测到的设备并通过NETLINK机制传过来的socket描述符转化成消息。接着调用parse_event(msg, &uevent);函数将消息翻译成uevent事件,并将改事件传递给handle_device_event(&uevent)函数。

handle_device_event(&uevent)函数中,依据参数uevent->subsystem类型创建dev下的相应目录,如:/dev/graphics。紧接着根据uevent->action是"add"还是"remove"来实现设备节点的创建与删除。如果uevent->action是"add",则调用make_device(devpath, block, uevent->major, uevent->minor)函数生成设备节点。如果uevent->action是"remove",则调用unlink(devpath)对设备节点进行删除。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值