Last active
August 29, 2015 14:11
-
-
Save kangear/887bca43cd761ca22046 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <errno.h> | |
#include <sys/types.h> | |
#include <asm/types.h> | |
//该头文件需要放在netlink.h前面防止编译出现__kernel_sa_family未定义 | |
#include <sys/socket.h> | |
#include <linux/netlink.h> | |
void MonitorNetlinkUevent() | |
{ | |
int sockfd; | |
struct sockaddr_nl sa; | |
int len; | |
char buf[4096]; | |
struct iovec iov; | |
struct msghdr msg; | |
int i; | |
memset(&sa,0,sizeof(sa)); | |
sa.nl_family=AF_NETLINK; | |
sa.nl_groups=NETLINK_KOBJECT_UEVENT; | |
sa.nl_pid = 0;//getpid(); both is ok | |
memset(&msg,0,sizeof(msg)); | |
iov.iov_base=(void *)buf; | |
iov.iov_len=sizeof(buf); | |
msg.msg_name=(void *)&sa; | |
msg.msg_namelen=sizeof(sa); | |
msg.msg_iov=&iov; | |
msg.msg_iovlen=1; | |
sockfd=socket(AF_NETLINK,SOCK_RAW,NETLINK_KOBJECT_UEVENT); | |
if(sockfd==-1) | |
printf("socket creating failed:%s\n",strerror(errno)); | |
if(bind(sockfd,(struct sockaddr *)&sa,sizeof(sa))==-1) | |
printf("bind error:%s\n",strerror(errno)); | |
while(1) { | |
len=recvmsg(sockfd,&msg,0); | |
if(len<0) | |
printf("receive error\n"); | |
else if(len<32||len>sizeof(buf)) | |
printf("invalid message"); | |
for(i=0;i<len;i++) | |
if(*(buf+i)=='\0') | |
buf[i]='\n'; | |
printf("received %d bytes\n%s\n",len,buf); | |
} | |
} | |
int main(int argc,char **argv) | |
{ | |
MonitorNetlinkUevent(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment