#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <linux/netlink.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
int netfd;
struct sockaddr_nl srcaddr={0};
int ret;
char buff[1024]={0};
netfd = socket(AF_NETLINK,SOCK_RAW,NETLINK_KOBJECT_UEVENT);
if(netfd <= 0)
{
printf("error\n");
return -1;
}
srcaddr.nl_family = AF_NETLINK;
srcaddr.nl_groups = 1;
srcaddr.nl_pid = getpid();
if( bind(netfd,(struct sockaddr *)&srcaddr,sizeof(srcaddr)) != 0 )
{
printf("call bind error\n");
close(netfd);
return -1;
}
while(1)
{
ret = recv(netfd,buff,1024,0);
if(ret <= 0)
{
printf("ret error\n");
continue;
}
printf("%s\n",buff);
}
close(netfd);
return 0;
}