Android 系统文件限制小探

我们可以通过ulimit -a 用来显示当前的各种资源限制。

rk3399_all:/ # ulimit -a
time(cpu-seconds)    unlimited
file(blocks)         unlimited
coredump(blocks)     unlimited
data(KiB)            unlimited
stack(KiB)           8192
lockedmem(KiB)       64
nofiles(descriptors) 1024
processes            15426
sigpending           15426
msgqueue(bytes)      819200
maxnice              40
maxrtprio            0
resident-set(KiB)    unlimited
address-space(KiB)   unlimited

在Linux系统中,每个进程可以使用的FD数量是有上限的,在Android中这个上限为1024,表示每个进程可以创建的file descriptors 不能超多1024个。当系统某一文件的打开句柄数超过1024时,就会报错:

"Too many open files"

那么,这个值到底是哪里定义的呢?

这边根据descriptors的属性,判断应该在kernel的某个头文件中,终于在:linux/posix_types.h 找到:

/*
 * This allows for 1024 file descriptors: if NR_OPEN is ever grown
 * beyond that you'll have to change this too. But 1024 fd's seem to be
 * enough even for such "real" unices like OSF/1, so hopefully this is
 * one limit that doesn't have to be changed [again].
 *
 * Note that POSIX wants the FD_CLEAR(fd,fdsetp) defines to be in
 * <sys/time.h> (and thus <linux/time.h>) - but this is a more logical
 * place for them. Solved by having dummy defines in <sys/time.h>.
 */

/*
 * This macro may have been defined in <gnu/types.h>. But we always
 * use the one here.
 */
#undef __FD_SETSIZE
#define __FD_SETSIZE    1024

这么说来,是否我们直接在kernel中修改这个定义,就能生效。

兴冲冲的修改了这个1024的值,编译固件测试发现:Too yong too simple.

搜索研究一通,发现在bionic/libc/kernel$ vim README.TXT 有如下一段话:

Bionic comes with a processed set of all of the uapi Linux kernel headers that
can safely be included by userland applications and libraries.

These clean headers are automatically generated by several scripts located
in the 'bionic/kernel/tools' directory. The tools process the original
unmodified kernel headers in order to get rid of many annoying
declarations and constructs that usually result in compilation failure.

好吧,原来还要再经过一系列的获取,然后通过bionic/libc/kernel/tools 里面的脚本来生成。

这边验证修改:

external/kernel-headers/original/uapi$ vim linux/posix_types.h +22

#define __FD_SETSIZE    1036

然后执行工具里:

./update_all.py

再来看看:

cc@ubuntu:~/7.1/external/kernel-headers/original/uapi$ gd .
diff --git a/original/uapi/linux/posix_types.h b/original/uapi/linux/posix_types.h
index 988f76e..44a2fc8 100644
--- a/original/uapi/linux/posix_types.h
+++ b/original/uapi/linux/posix_types.h
@@ -19,7 +19,7 @@
  * use the one here.
  */
 #undef __FD_SETSIZE
-#define __FD_SETSIZE   1024
+#define __FD_SETSIZE   1036
 

这边文件的修改是生效了。

但是,最终生成的固件,编译还是1024,也就是说,上面的修改,还是没有效果。通过了解,确认 __FD_SETSIZE 这个是用于 select 相关调用传参用的。所以修改无效。

那么,究竟在哪里可以达到修改RLIMIT_NOFILE的值呢?

通过init.rc中,我们可以发现setrlimit 语句。试试:

setrlimit nofile 1048 1048

还是无效,看看其它类似的的配置,是三个数值,那么nofile的数值是多少?

查找相关定义可以发现:

vim bionic/libc/kernel/uapi/asm-generic/resource.h

#ifndef RLIMIT_NOFILE
#define RLIMIT_NOFILE 7
#endif
"bioni

这样子,那在init.rc 中 加句:

setrlimit 7 1048 1048

打包生成个固件,ulimit -a 确认,生效了,真不容易。

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值