只讲原理是不行的,我们直接来看一下__lib_prenit函数代码的相关实现:
- void __attribute__((constructor)) __libc_prenit(void); void __libc_prenit(void)
- { // ...
- __libc_init_common(elfdata); // 调用这个函数 // ...
- }
- __libc_init_common函数为:
- void __libc_init_common(uintptr_t *elfdata)
- { // ...
- __system_properties_init(); // 初始化客户端的属性存储区域 }
- __system_properties_init函数有回到了我们熟悉的/bionic/libc/bionic/system_properties.c文件:
- static int get_fd_from_env(void) {
- char *env = getenv("ANDROID_PROPERTY_WORKSPACE");
- if (! env) { return -1;
- }
- return atoi(env); }
- static int map_prop_area()
- { bool formFile = true;
- int result = -1; int fd;
- int ret;
- fd = open(property_filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); if (fd >= 0) {
- /* For old kernels that don't support O_CLOEXEC */ ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
- if (ret < 0) goto cleanup;
- }
- if ((fd < 0) && (error == ENOENT)) { fd = get_fd_from_env();
- fromFile = false; }
- if (fd < 0) {
- return -1; }
- struct stat fd_stat;
- if (fstat(fd, &fd_stat) < 0) { goto cleanup;
- }
- if ((fd_stat.st_uid != 0) || (fd_stat.st_gid != 0)
- || (fd_stat.st_mode & (S_IWGRP | S_IWOTH) != 0) || (fd_stat.st_size < sizeof(prop_area))) {










