Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#define SOL_NETLINK 270
int main()
{
int sk, val = 1;
struct sockaddr_nl addr;
struct msghdr msg;
struct {
struct nlmsghdr hdr;
} req;
struct iovec iov;
char buf[4096];
char cbuf[4096];
sk = socket(PF_NETLINK, SOCK_RAW, NETLINK_KOBJECT_UEVENT);
if (sk < 0)
return 1;
addr.nl_family = AF_NETLINK;
addr.nl_groups = 1;
addr.nl_pid = 0;
if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)))
return 1;
if (setsockopt(sk, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, &val, sizeof(val)))
return 1;
memset(&msg, 0, sizeof(msg));
memset(&addr, 0, sizeof(struct sockaddr_nl));
msg.msg_namelen = 0;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_name = &addr;
msg.msg_namelen = sizeof(struct sockaddr_nl);
msg.msg_control = cbuf;
msg.msg_controllen = sizeof(cbuf);
while (1) {
iov.iov_base = buf;
iov.iov_len = sizeof(buf);
recvmsg(sk, &msg, 0);
printf("Get an event\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.