Why do I get "error: too few arguments to function ‘sock->ops->accept’"

Why do I get "error: too few arguments to function ‘sock->ops->accept’"

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

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

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

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

我希望它能工作,但没有。如果在实施中只有三个,为什么会出现 "too few arguments" 错误?我该如何解决?

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

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

详见用户 MofX's , the ->accept() handler has a fourth parameter bool kern in current kernel versions (since 4.11). According to the commit description, this is analogous to the kern parameter passed in to ->create(), and distinguishes whether kernel_accept() or sys_accept4() was the caller. See the 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