C 为什么我没有收到来自内核的消息?
我正在尝试使用通用netlink和libnl将消息从内核发送到用户空间,我的代码中用于此目的的部分实现如下:C 为什么我没有收到来自内核的消息?,c,linux-kernel,kernel,netlink,C,Linux Kernel,Kernel,Netlink,我正在尝试使用通用netlink和libnl将消息从内核发送到用户空间,我的代码中用于此目的的部分实现如下: int struct my_callback(struct sk_buff *skb, struct genl_info *info) { struct sk_buff *obuff; void *msg_head; if ((obuff = genlmsg_new(0, GFP_KERNEL)) == NULL) { // I've tried to chan
int struct my_callback(struct sk_buff *skb, struct genl_info *info)
{
struct sk_buff *obuff;
void *msg_head;
if ((obuff = genlmsg_new(0, GFP_KERNEL)) == NULL) { // I've tried to change the len to NLMSG_GOODSIZE but not worked
pr_err("Failed allocating message to an reply\n");
return 0;
}
if ((msg_head = genlmsg_put_reply(obuff, info, &lunatik_family, 0, LIST_STATES)) == NULL) {
pr_err("Failed to put generic netlink header\n");
return 0;
}
//I've tried to put a genlmsg_end(obuff, msg_head); but didn't work as well
if (genlmsg_reply(obuff, info) < 0) {
pr_err("Failed to send message to user space\n");
return 0;
}
pr_info("Message sent to user-space\n");
return 0;
}
我在dmesg上的输出是:
发送到用户空间的消息
我在用户空间的输出是:
从内核接收到消息:0
我应该收到3而不是0,我注意到我只收到ACK消息,而不是我发送的消息,我想知道为什么会发生这种情况以及我做错了什么。genl\u ctrl\u resolve()的结果有两方面:
- 如果<0,则为错误代码
- 如果>=0,则为族标识号
if ((err = genl_ctrl_resolve(sock, LUNATIK_FAMILY)) < 0)
return err;
nl_socket_modify_cb(sock, NL_CB_MSG_IN, NL_CB_CUSTOM, req_handler, NULL);
return 0;
还有一件事:nl\u socket\u modify\u cb()
还返回一个错误代码。而不是
if ((err = genl_ctrl_resolve(sock, LUNATIK_FAMILY)) < 0)
return err;
nl_socket_modify_cb(sock, NL_CB_MSG_IN, NL_CB_CUSTOM, req_handler, NULL);
return 0;
做
genl\u ctrl\u resolve()
的结果有两方面:
- 如果<0,则为错误代码
- 如果>=0,则为族标识号
if ((err = genl_ctrl_resolve(sock, LUNATIK_FAMILY)) < 0)
return err;
nl_socket_modify_cb(sock, NL_CB_MSG_IN, NL_CB_CUSTOM, req_handler, NULL);
return 0;
还有一件事:nl\u socket\u modify\u cb()
还返回一个错误代码。而不是
if ((err = genl_ctrl_resolve(sock, LUNATIK_FAMILY)) < 0)
return err;
nl_socket_modify_cb(sock, NL_CB_MSG_IN, NL_CB_CUSTOM, req_handler, NULL);
return 0;
做
内核版本?你有没有看过这个:?我的内核版本是5.3.0,我已经读过这个页面了消息的类型是什么
nlhdr->nlmsg_type
这是nlmsg_ERROR
,但是struct nlmsgerr
中的ERROR
字段为0,这意味着我接收的消息只是一条确认消息。.内核版本?你有没有看过这个:?我的内核版本是5.3.0,我已经读过这个页面了消息的类型是什么nlhdr->nlmsg_type
这是nlmsg_ERROR
,但是struct nlmsgerr
中的ERROR
字段为0,这意味着我接收的消息只是一条确认消息。。