【问题标题】:Why do I get "error: too few arguments to function ‘sock->ops->accept’"为什么我会收到“错误:函数‘sock->ops->accept’的参数太少”
【发布时间】:2019-12-12 17:20:05
【问题描述】:

我正在用套接字编写内核模块。当我尝试为接受连接编写代码时,我得到:

“错误:函数‘sock->ops->accept’的参数太少 ret = sock->ops->accept(sock, client_sock, 0);"

我查看了 socket 接受的实现,只有三个参数,所以我不知道发生了什么。

struct socket *sock = NULL, *client_sock = NULL;
//some code here, create socket, bind, listen
ret = sock->ops->accept(sock, client_sock, 0);

我希望它应该可以工作,但事实并非如此。如果在实现中只有三个,为什么会出现“参数太少”错误?我该如何解决?

【问题讨论】:

标签: c sockets linux-kernel kernel-module unix-socket


【解决方案1】:

proto_ops::accept 中有四个参数

struct proto_ops {
    ...
    int     (*accept)    (struct socket *sock,
                      struct socket *newsock, int flags, bool kern);
};

见:https://elixir.bootlin.com/linux/latest/source/include/linux/net.h#L147

【讨论】:

    【解决方案2】:

    ->accept() 处理程序的原型在内核版本 4.10 和 4.11 之间被此提交更改:“net: Work around lockdep limitation in sockets that use sockets”。

    正如用户MofXanswer 中所述,->accept() 处理程序在当前内核版本(自 4.11 起)中具有第四个参数bool kern。根据提交描述,这类似于传递给->create()kern参数,区分kernel_accept()sys_accept4()是调用者。详情请参阅commit description

    如果您希望您的代码在 4.11 之前和之后都适用于内核,则需要使用条件编译:

    #include <linux/version.h>
    
    #if LINUX_VERSION_CODE >= KERNEL_VERSION(4,11,0)
    #define KV_ACCEPT_HAS_BOOL_KERN
    #endif
    
    #ifdef KV_ACCEPT_HAS_BOOL_KERN
        // your code needs to determine whether 'kern' should be false or true here...
        ret = sock->ops->accept(sock, client_sock, 0, kern);
    #else
        ret = sock->ops->accept(sock, client_sock, 0);
    #endif
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-05
      • 2015-11-03
      • 2022-12-13
      • 1970-01-01
      • 2011-10-08
      • 2020-01-19
      相关资源
      最近更新 更多