Linux内核分析:dup、dup2的实现

时间:2021-12-19 20:00:46

一、首先需要看一下这两个函数的作用:

 #include <unistd.h>

 int dup(int oldfd);
int dup2(int oldfd, int newfd);

根据manual的解释:

dup:创建一份oldfd的拷贝,使用最小的文件描述符作为新的文件描述符。

dup2:创建一份oldfd的拷贝,使用指定的newfd作为新的文件描述符。

要看这两个函数是怎么实现的,首先得知道Linux对于文件描述符是怎么处理的,参考这篇文章

二、分析dup

 static inline long
dup (int fd)
{
return sys_dup(fd);
}

这里看到dup调用了函数sys_dup。

 asmlinkage long sys_dup(unsigned int fildes)
{
int ret = -EBADF;
struct file * file = fget(fildes); if (file)
ret = dupfd(file, );
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下标的指针。

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 >= ) {
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 < )
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 + ; error = newfd; out:
return error;
}

根据该函数的注释即可知道它的所用就是:找到一个没有被使用的文件描述符,从start开始(这里就是dup和dup2的区别所在)。

 /*
* Install a file pointer in the fd array.
*
* The VFS is full of places where we drop the files lock between
* setting the open_fds bitmap and installing the file in the file
* array. At any such point, we are vulnerable to a dup2() race
* installing a file in the array before us. We need to detect this and
* fput() the struct file we are about to overwrite in this case.
*
* It should never happen - if we allow dup2() do it, _really_ bad things
* will follow.
*/ 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就返回该值。

三、dup2

dup2的实现跟dup类似,关键的差别就是dup2返回指定的newfd,dup返回最小可用的fd。