sys_dup()的主要工作就是用来“复制”一个打开的文件号,使两个文件号都指向同一个文件。先看一下涉及到的数据结构:
1 数据结构
在Linux中每一个进程的数据是存储在一个task_struct结构(定义在sched.h中)中的。
struct task_struct {
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
/* filesystem information */
struct fs_struct *fs;
/* open file information */
struct files_struct *files;
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
};
该结构中有一个用于保存打开文件信息的成员:files,该成员类型是:struct files_struct*(定义在file.h)。
/*
* Open file table structure
*/
struct files_struct {
atomic_t count;
spinlock_t file_lock; /* Protects all the below members. Nests inside tsk->alloc_lock */
int max_fds;
int max_fdset;
int next_fd;
struct file ** fd; /* current fd array */
fd_set *close_on_exec;
fd_set *open_fds;
fd_set close_on_exec_init;
fd_set open_fds_init;
struct file * fd_array[NR_OPEN_DEFAULT];
};
可以看到该结构中保存了所有与进程打开文件相关的信息,其中fd_array是一个struct file*(定义在file.h)类型的数组。
struct file {
struct list_head f_list;
struct dentry *f_dentry;
struct vfsmount *f_vfsmnt;
struct file_operations *f_op;
atomic_t f_count;
unsigned int f_flags;
mode_t f_mode;
int f_error;
loff_t f_pos;
struct fown_struct f_owner;
unsigned int f_uid, f_gid;
struct file_ra_state f_ra;
size_t f_maxcount;
unsigned long f_version;
void *f_security;
/* needed for tty driver, and maybe others */
void *private_data;
#ifdef CONFIG_EPOLL
/* Used by fs/eventpoll.c to link all the hooks to this file */
struct list_head f_ep_links;
spinlock_t f_ep_lock;
#endif /* #ifdef CONFIG_EPOLL */
struct address_space *f_mapping;
};
struct file就是保存了每个打开文件信息的数据结构。
2 实现
asmlinkage long sys_dup(unsigned int fildes)
{
int ret = -EBADF;
struct file * file = fget(fildes);
if (file)
ret = dupfd(file, 0);
return ret;
}
在sys_dup函数中,关键的就是两步,fget获取指定文件描述符的struct file指针,然后调用dupfd,至于dupfd的具体实现,我们接着往下走。
struct file fastcall *fget(unsigned int fd)
{
struct file *file;
struct files_struct *files = current->files;
spin_lock(&files->file_lock);
file = fcheck_files(files, fd);
if (file)
get_file(file);
spin_unlock(&files->file_lock);
return file;
}
可以看到fget函数的实现就是首先获取一个files_struct指针,我们知道files_struct保存了所有打开文件信息(其中current是当前进程的struct task_struct指针),然后加锁,调用fcheck_files,获取file指针,如果file不为空,则调用get_file,下面我们看下这两个函数的实现。
static inline struct file * fcheck_files(struct files_struct *files, unsigned int fd)
{
struct file * file = NULL;
if (fd < files->max_fds)
file = files->fd[fd];
return file;
}
#define get_file(x) atomic_inc(&(x)->f_count)
现在已经可以知道,fcheck_files函数的具体步骤是首先判断给定的文件描述符fd是否小于最大文件描述符max_fds,如果小于,则返回fd数组中对应该fd下标的指针,即取出原来文件的file指针
get_file的作用是原子的增加f_count,也就是该文件的引用计数(在close的时候会减这个值)。
现在再回到sys_dup中,看一下dumfd的实现。
static int dupfd(struct file *file, unsigned int start)
{
struct files_struct * files = current->files;
int fd;
spin_lock(&files->file_lock);
fd = locate_fd(files, file, start);
if (fd >= 0) {
FD_SET(fd, files->open_fds);
FD_CLR(fd, files->close_on_exec);
spin_unlock(&files->file_lock);
fd_install(fd, file);
} else {
spin_unlock(&files->file_lock);
fput(file);
}
return fd;
}
该函数的具体步骤如下:
1、通过current->files获取struct files_struct指针。
2、加锁,完成后会解锁。
3、调用locate_fd函数获取一个fd,具体获取规则下面再看。
4、如果获取到的fd>=0,则调用FD_SET、FD_CLR、解锁、fd_install。关键在于fd_install;否则调用解锁、fput。
下面再看一下locate_fd、fd_install、fput的实现。
/*
* locate_fd finds a free file descriptor in the open_fds fdset,
* expanding the fd arrays if necessary. Must be called with the
* file_lock held for write.
*/
static int locate_fd(struct files_struct *files,
struct file *file, unsigned int orig_start)
{
unsigned int newfd;
unsigned int start;
int error;
error = -EINVAL;
if (orig_start >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
goto out;
repeat:
/*
* Someone might have closed fd's in the range
* orig_start..files->next_fd
*/
start = orig_start;
if (start < files->next_fd)
start = files->next_fd;
newfd = start;
if (start < files->max_fdset) {
newfd = find_next_zero_bit(files->open_fds->fds_bits,
files->max_fdset, start);
}
error = -EMFILE;
if (newfd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
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;
if (start <= files->next_fd)
files->next_fd = newfd + 1;
error = newfd;
out:
return error;
}
根据该函数的注释即可知道它的所用就是:找到一个没有被使用的文件描述符,从start开始
void fastcall fd_install(unsigned int fd, struct file * file)
{
struct files_struct *files = current->files;
spin_lock(&files->file_lock);
if (unlikely(files->fd[fd] != NULL))
BUG();
files->fd[fd] = file;
spin_unlock(&files->file_lock);
}
fd_install的作用就是把fd指针数组对应fd下标的指针赋值为file。
到此回到dupfd,返回获取的fd,这就是新的拷贝,然后sys_dup就返回该值。