6 #include <sys/socket.h>
10 #include <linux/types.h>
11 #include <linux/netlink.h>
19 int main(int argc, char *argv[])
21 struct sockaddr_nl nls;
25 // Open hotplug event netlink socket
27 memset(&nls,0,sizeof(struct sockaddr_nl));
28 nls.nl_family = AF_NETLINK;
29 nls.nl_pid = getpid();
33 pfd.fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
37 // Listen to netlink socket
39 if (bind(pfd.fd, (void *)&nls, sizeof(struct sockaddr_nl)))
42 while (-1 != poll(&pfd, 1, -1)) {
43 struct sockaddr_nl snl;
44 struct iovec iov = { buf, sizeof(buf) };
45 //char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
46 //struct msghdr hdr = { &snl, sizeof(snl), &iov, 1, cred_msg, sizeof(cred_msg), 0 };
47 struct msghdr hdr = { &snl, sizeof(snl), &iov, 1, NULL, 0, 0 };
50 len = recvmsg(pfd.fd, &hdr, MSG_DONTWAIT);
54 // Print the data to stdout.
55 printf("=== nl_groups %d, nl_pid %d\n", snl.nl_groups, snl.nl_pid);
58 printf("%s\n", buf+i);