在Android中,由于没有mdev和udev,所以它没有办法动态的生成设备节点,那么它是如何做的呢?
我们可以在system/core/init/下的init.c和devices.c中找到答案:
init.c中
- int
main(int argc, char **argv) - {
-
... -
-
mkdir("/dev", 0755); -
mkdir("/proc", 0755); -
mkdir("/sys", 0755); -
-
mount("tmpfs", "/dev", "tmpfs", 0, "mode=0755"); -
mkdir("/dev/pts", 0755); -
mkdir("/dev/socket", 0755); -
mount("devpts", "/dev/pts", "devpts", 0, NULL); -
mount("proc", "/proc", "proc", 0, NULL); -
mount("sysfs", "/sys", "sysfs", 0, NULL); -
-
for(;;) { -
... -
if (ufds[0].revents == POLLIN) -
handle_device_fd(device_fd); -
-
if (ufds[1].revents == POLLIN) -
handle_property_set_fd(property_set_fd); -
if (ufds[3].revents == POLLIN) -
handle_keychord(keychord_fd); -
} -
-
return 0; - }
我们再来看看handle_device_fd(),该函数定义在devices.c中
- void
handle_device_fd(int fd) - {
-
... -
handle_device_event(&uevent); -
handle_firmware_event(&uevent); -
} - }
而handle_device_event定义如下:
- static
void handle_device_event(struct uevent *uevent) - {
-
... -
if(!strcmp(uevent->action, "add")) { -
make_device(devpath, block, uevent->major, uevent->minor); -
return; -
} -
... - }
make_device定义如下:
- static
void make_device(const char *path, int block, int major, int minor) - {
-
... -
mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR); -
dev = (major << 8) | minor; -
... -
setegid(gid); -
mknod(path, mode, dev); -
chown(path, uid, -1); -
setegid(AID_ROOT); - }
我们看看get_device_perm如下实现:
- static
mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid) - {
-
mode_t perm; -
-
if (get_device_perm_inner(qemu_perms, path, uid, gid, &perm) == 0) { -
return perm; -
} else if (get_device_perm_inner(devpe