5

I'm trying to receive uevent from the kernel via a netlink socket, when a USB device is (dis)connects. I have a python script that does the same job and it works, but I need the same functionality in C. So far I have this:

#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("");
        }
    }
}

This is taken out of context to create a small, working example.

The problem is that I just don't receive anything. I don't get any errors, the call of recvmsg just never returns when I (un)plug a device. Why?

Lasse Meyer
  • 1,429
  • 18
  • 38

2 Answers2

3

The receiving part was incorrect instead of using recvmmsg I am using recv, I have modified the program a bit, following is the code which prints the 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);

    }
}
Prabhakar Lad
  • 1,248
  • 1
  • 8
  • 12
  • Thanks, this works perfectly, though for some reason a lot of messages are empty. The ones that I need are ok though. – Lasse Meyer Dec 04 '16 at 11:45
0

this good use recvmsg than recv (just for connected link).ref man recvmsg. and the program close to success.

src_addr.nl_family = AF_NETLINK;
src_addr.nl_pid = getpid();
src_addr.nl_groups = -1; <--add this so can receive from kernel broadcast
rowan
  • 1,293
  • 1
  • 8
  • 6