sock_map_fd()
获取文件描述符,创建file结构实例;socket与file绑定,将file结构添加到进程打开的文件指针数组中。
寻思着,根据fd,找到file;根据file找到socket。
E:\linux-4.1.45\linux-4.1.45\net\socket.c
static int sock_map_fd(struct socket *sock, int flags)
{
struct file *newfile;
int fd = get_unused_fd_flags(flags);//获取未使用的文件描述符
if (unlikely(fd < 0))
return fd;
newfile = sock_alloc_file(sock, flags, NULL);
if (likely(!IS_ERR(newfile))) {
fd_install(fd, newfile);//将file结构添加到进程打开的文件指针数组中。
return fd;
}
put_unused_fd(fd);
return PTR_ERR(newfile);
}
sock_alloc_file()
在这个函数中创建file结构体,其文件操作函数为socket_file_ops;将socket与file绑定。
E:\linux-4.1.45\linux-4.1.45\net\socket.c
/*
* Obtains the first available file descriptor and sets it up for use.
*
* These functions create file structures and maps them to fd space
* of the current process. On success it re