【问题标题】:Get struct file from of a given struct socket in kernel space从内核空间中的给定结构套接字获取结构文件
【发布时间】:2012-01-10 01:12:40
【问题描述】:

我需要获取与特定结构套接字关联的结构文件。我在内核源代码中的 socket.c 文件中看到了函数 struct socket *sockfd_lookup(int fd, int *err),它给定一个 fd 返回这个 fd 所属的套接字。有没有办法做相反的事情?我的意思是获取struct socket的fd。我需要在可加载的内核模块中执行此操作。

谢谢

【问题讨论】:

    标签: linux sockets network-programming linux-kernel kernel-module


    【解决方案1】:

    struct socketfile 成员是否不包含您要查找的内容?

    【讨论】:

    • 不幸的是它是 NULL。我从内核调用 int security_socket_post_create(struct socket *sock, int family, int type, int protocol, int kern) 获得了 struct socket。在 int sock_create(int family, int type, int protocol, struct socket **res) 调用退出之前调用它。
    • 我明白了。不幸的是,您的问题没有很好地提出——在 sock_map_fd()/sock_alloc_file() 调用分配文件之前,在 sock_create() 中调用了 security_socket_post_create() 挂钩。所以在那个安全钩子中,还没有与套接字关联的文件。
    【解决方案2】:

    socket 结构有一个成员,它是指向其文件结构的指针。见 linux/net.h struct socket 定义

    【讨论】:

      猜你喜欢
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-12
      • 1970-01-01
      • 1970-01-01
      • 2013-10-20
      • 1970-01-01
      相关资源
      最近更新 更多