gpt4 book ai didi

c - 如何使用 Netlink 套接字接收内核 uevents?

转载 作者:行者123 更新时间:2023-12-01 13:52:42 26 4
gpt4 key购买 nike

当 USB 设备(断开)连接时,我试图通过 netlink 套接字从内核接收 uevent。我有一个 python 脚本,它可以完成相同的工作并且可以工作,但是我需要在 C 中使用相同的功能。到目前为止,我有这个:

#include <sys/types.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define NL_MAX_PAYLOAD 8192

int main () {
int nl_socket;
struct sockaddr_nl src_addr, dest_addr;
struct nlmsghdr* nlh;
struct msghdr msg;
struct iovec iov;

// Prepare source address
memset(&src_addr, 0, sizeof(src_addr));
src_addr.nl_family = AF_NETLINK;
src_addr.nl_pid = getpid();

// Prepare destination address
memset(&dest_addr, 0, sizeof(dest_addr));
dest_addr.nl_family = AF_NETLINK;
dest_addr.nl_pid = 0;
dest_addr.nl_groups = 0;

// Prepare netlink message
nlh = (struct nlmsghdr*) malloc(NLMSG_SPACE(NL_MAX_PAYLOAD));
memset(nlh, 0, NLMSG_SPACE(NL_MAX_PAYLOAD));
nlh->nlmsg_len = NLMSG_SPACE(NL_MAX_PAYLOAD);
nlh->nlmsg_pid = getpid();
nlh->nlmsg_flags = 0;
iov.iov_base = (void *) nlh;
iov.iov_len = nlh->nlmsg_len;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;

nl_socket = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
if (nl_socket < 0) {
printf("Failed to create socket for DeviceFinder");
exit(1);
}

bind(nl_socket, (struct sockaddr*) &src_addr, sizeof(src_addr));

while (1) {
int r = recvmsg(nl_socket, &msg, 0);
printf("%i\n", r);
if (r < 0) {
perror("");
}
}
}

这是脱离上下文以创建一个小的工作示例。

问题是我只是没有收到任何东西。我没有收到任何错误,当我(拔出)插入设备时, recvmsg 的调用永远不会返回。为什么?

最佳答案

接收部分不正确,而不是使用 recvmmsg 我正在使用 recv,我稍微修改了程序,以下是打印 uevents 的代码:

#include <sys/types.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define NL_MAX_PAYLOAD 8192

int main () {
int nl_socket;
struct sockaddr_nl src_addr;
char msg[NL_MAX_PAYLOAD];
int ret;

// Prepare source address
memset(&src_addr, 0, sizeof(src_addr));
src_addr.nl_family = AF_NETLINK;
src_addr.nl_pid = getpid();
src_addr.nl_groups = -1;

nl_socket = socket(AF_NETLINK, (SOCK_DGRAM | SOCK_CLOEXEC), NETLINK_KOBJECT_UEVENT);
if (nl_socket < 0) {
printf("Failed to create socket for DeviceFinder");
exit(1);
}

ret = bind(nl_socket, (struct sockaddr*) &src_addr, sizeof(src_addr));
if (ret) {
printf("Failed to bind netlink socket..");
close(nl_socket);
return 1;
}

printf("Waiting for events now...\n");
while (1) {
int r = recv(nl_socket, msg, sizeof(msg), MSG_DONTWAIT);
if (r == -1)
continue;
if (r < 0) {
perror("");
continue;
}
printf("length:%i\n msg:%s", r, msg);

}
}

关于c - 如何使用 Netlink 套接字接收内核 uevents?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40788161/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com