Kernel version:2.6.14
CPU architecture:ARM920T
繼續上一篇文章的內容,分析另一個文件描述符fd的分配函數locate_fd。dup系統調用用於復制一個文件描述符對應的文件,返回值是個文件描述符。在前面的文章中,我們已經分析過了dup的源碼(http://www.linuxidc.com/Linux/2012-12/77128.htm),在這裡我們深入分析locate_fd函數,其定義如下:
static int locate_fd(struct files_struct *files,
struct file *file, unsigned int orig_start)//從orig_start位開始分配fd
{
unsigned int newfd;
unsigned int start;
int error;
struct fdtable *fdt;
error = -EINVAL;
if (orig_start >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)//檢查orig_start是大於進程最大可以打開文件的數量
goto out;
repeat:
fdt = files_fdtable(files);//文件描述符位圖
/*
* Someone might have closed fd's in the range
* orig_start..fdt->next_fd
*/
start = orig_start;
if (start < fdt->next_fd)
start = fdt->next_fd;//如果orig_start小於next_fd,那就從next_fd開始分配
newfd = start;
if (start < fdt->max_fdset) {//max_fdset是描述符問題的位數,下面會具體講解
newfd = find_next_zero_bit(fdt->open_fds->fds_bits,
fdt->max_fdset, start);//分配fd
}
error = -EMFILE;
if (newfd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)//進行判斷,分配的fd不能大於進程最大可以打開的文件數量
goto out;
error = expand_files(files, newfd);//文件描述符表的擴展,這個我們留在下一篇文章中詳細講解
if (error < 0)
goto out;
/*
* If we needed to expand the fs array we
* might have blocked - try again.
*/
if (error)
goto repeat;
/*
* We reacquired files_lock, so we are safe as long as
* we reacquire the fdtable pointer and use it while holding
* the lock, no one can free it during that time.
*/
fdt = files_fdtable(files);
if (start <= fdt->next_fd)
fdt->next_fd = newfd + 1;//更新next_fd值
error = newfd;
out:
return error;
}
max_fdset值的分析和rlim_cur差不多,最初的值時從父進程繼承過來的。
linux/arch/arm/kernel/init_task.c
struct task_struct init_task = INIT_TASK(init_task);
#define INIT_TASK(tsk) \
{ \
...
.files = &init_files, \
...
}
init_files的定義如下:
static struct files_struct init_files = INIT_FILES;
linux/init_task.h
#define INIT_FDTABLE \
{ \
.max_fds = NR_OPEN_DEFAULT, \
.max_fdset = __FD_SETSIZE, \
.next_fd = 0, \
.fd = &init_files.fd_array[0], \
.close_on_exec = &init_files.close_on_exec_init, \
.open_fds = &init_files.open_fds_init, \
.rcu = RCU_HEAD_INIT, \
.free_files = NULL, \
.next = NULL, \
}
#define NR_OPEN_DEFAULT BITS_PER_LONG
#define __FD_SETSIZE 1024
#define INIT_FILES \
{ \
.count = ATOMIC_INIT(1), \
.file_lock = SPIN_LOCK_UNLOCKED, \
.fdt = &init_files.fdtab, \
.fdtab = INIT_FDTABLE, \
.close_on_exec_init = { { 0, } }, \
.open_fds_init = { { 0, } }, \
.fd_array = { NULL, } \
}
BITS_PER_LONG是long型數據的字節數,即4*8=3,也就是說max_fds = 32。max_fdset為1024。max_fdset是進程打開文件描述符位圖open_fds的大小。open_fds是fd_set的指針。
typedef __kernel_fd_set fd_set;
#undef __NFDBITS
#define __NFDBITS (8 * sizeof(unsigned long))
#undef __FD_SETSIZE
#define __FD_SETSIZE 1024
#undef __FDSET_LONGS
#define __FDSET_LONGS (__FD_SETSIZE/__NFDBITS)
#undef __FDELT
#define __FDELT(d) ((d) / __NFDBITS)
#undef __FDMASK
#define __FDMASK(d) (1UL << ((d) % __NFDBITS))
typedef struct {
unsigned long fds_bits [__FDSET_LONGS];
} __kernel_fd_set;