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

时间:2016-11-24 13:51:32

标签: c sockets linux-kernel netlink

当USB设备(dis)连接时,我正试图通过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的调用永远不会返回。为什么呢?

2 个答案:

答案 0 :(得分:1)

接收部分不正确而不是使用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);

    }
}

答案 1 :(得分:0)

这很好用recvmsg比recv(仅用于连接链接).ref man recvmsg。 该计划接近成功。

src_addr.nl_family = AF_NETLINK;
src_addr.nl_pid = getpid();
src_addr.nl_groups = -1; <--add this so can receive from kernel broadcast