欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

tcp/ip

程序员文章站 2022-06-30 16:29:50
...

sock_map_fd
-----------sock_alloc_file
-----------fd_install

sock_alloc(**void**) //return socket *

static const struct file_operations socket_file_ops = {
    .owner =    THIS_MODULE,
    .llseek =   no_llseek,
    .read_iter =    sock_read_iter,
    .write_iter =   sock_write_iter,
    .poll =     sock_poll,
    .unlocked_ioctl = sock_ioctl,
#ifdef CONFIG_COMPAT
    .compat_ioctl = compat_sock_ioctl,
#endif
    .mmap =     sock_mmap,
    .release =  sock_close,
    .fasync =   sock_fasync,
    .sendpage = sock_sendpage,
    .splice_write = generic_splice_sendpage,
    .splice_read =  sock_splice_read,
};

/linux-2.6.38.8/net/ipv4/tcp.c

int sock_map_fd(struct socket *sock, int flags)
{
    struct file *newfile;
    int fd = sock_alloc_file(sock, &newfile, flags);

    if (likely(fd >= 0))
        fd_install(fd, newfile);

    return fd;
}


static int sock_alloc_file(struct socket *sock, struct file **f, int flags){
          struct file *file;
    ..............
     file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
          &socket_file_ops);
   sock->file = file;
   file->private_data = sock;
}

转载于:https://www.jianshu.com/p/627763256ec4