#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <asm/types.h>
#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));
printf("begin to monitor evnet:\n");
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;
}
插入U盘输出log
-------------------------------------------------------------------------------------------------
[ 768.307008] ==11==> hub_port_init 1
[ 768.310661] Plug in USB Port3
[ 768.495004] usb 5-1: new high speed USB device number 4 using Mstar-ehci-3
[ 768.939901] usb 5-1: New USB device found, idVendor=0781, idProduct=5567
[ 768.946801] usb 5-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 768.954111] usb 5-1: Product: Cruzer Blade
[ 768.958276] usb 5-1: Manufacturer: SanDisk
[ 768.962516] usb 5-1: SerialNumber: 20051233121175827626
received 250 bytes
add@/devices/platform/Mstar-ehci-3.2/usb5/5-[ 768.973136] scsi2 : usb-storage 5-1:1.0
1
ACTION=add
DEVPATH=/devices/platform/Mstar-e[ 768.981426] scsi 2:0:0:0: Direct-Access SanDisk Cruzer Blade 1.26 PQ: 0 ANSI: 5
hci-3.2/usb5/5-1
SUBSYSTEM=usb
MAJOR=189
MINO[ 768.994160] sd 2:0:0:0: [sda] 15633408 512-byte logical blocks: (8.00 GB/7.45 GiB)
R=515
DEVNAME=bus/usb/005/004
[ 769.003400] sd 2:0:0:0: [sda] Write Protect is off
DEVTYPE=usb_devi[ 769.010369] sd 2:0:0:0: [sda] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
ce
PRODUCT=781/5567/126
TYPE=0/0/0
BUSNUM=005
DEVNUM=004
SE[ 769.024390] emmc_partition()
QNUM=1019
=1018
c08isc06ip50
SEQNUM=1015
SEQNUM=1008
received 275 bytes[ 769.034325] 0x0
add@/devices/p[ 769.037551] sda: sda1
latform/Mstar-ehci-3.2/usb5/5-1/5-1:1.0
ACTION=add
DEVPATH=/devices/platform/Mstar-ehci-3.2/usb5/5-1/5-1:1.0
SUBSYSTEM=usb
DEVTYPE=usb_interface
PRODUCT=781/5567/126
TYPE[ 769.056032] sd 2:0:0:0: [sda] Attached SCSI removable disk
=0/0/0
INTERFACE=8/6/80
MODALIAS=usb:v0781p5567d0126dc00dsc00dp00ic08isc06ip50
SEQNUM=1020
=1015
SEQNUM=1008
received 180 bytes
add@/devices/platform/Mstar-ehci-3.2/usb5/5-1/5-1:1.0/host2
ACTION=add
DEVPATH=/devices/platform/Mstar-ehci-3.2/usb5/5-1/5-1:1.0/host2
SUBSYSTEM=scsi
DEVTYPE=scsi_host
SEQNUM=1021
TYPE=0/0/0
INTERFACE=8/6/80
MODALIAS=usb:v0781p5567d0126dc00dsc00dp00ic08isc06ip50
SEQNUM=1020
=1015
SEQNUM=1008
received 199 bytes
add@/devices/platform/Mstar-ehci-3.2/usb5/5-1/5-1:1.0/host2/scsi_host/host2
ACTION=add
DEVPATH=/devices/platform/Mstar-ehci-3.2/usb5/5-1/5-1:1.0/host2/scsi_host/host2
SUBSYSTEM=scsi_host
SEQNUM=1022
E=8/6/80
MODALIAS=usb:v0781p5567d0126dc00dsc00dp00ic08isc06ip50
SEQNUM=1020
=1015
SEQNUM=1008
received 220 bytes
add@/devices/platform/Mstar-ehci-3.2/usb5/5-1/usb_device/usbdev5.4
ACTION=add
DEVPATH=/devices/platform/Mstar-ehci-3.2/usb5/5-1/usb_device/usbdev5.4
SUBSYSTEM=usb_device
MAJOR=189
MINOR=515
DEVNAME=usbdev5.4
SEQNUM=1023
:v0781p5567d0126dc00dsc00dp00ic08isc06ip50
SEQNUM=1020
=1015
SEQNUM=1008
received 206 bytes
add@/devices/platform/Mstar-ehci-3.2/usb5/5-1/5-1:1.0/host2/target2:0:0
ACTION=add
DEVPATH=/devices/platform/Mstar-ehci-3.2/usb5/5-1/5-1:1.0/host2/target2:0:0
SUBSYSTEM=scsi
DEVTYPE=scsi_target
SEQNUM=1024
4
SEQNUM=1023
:v0781p5567d0126dc00dsc00dp00ic08isc06ip50
SEQNUM=1020
=1015
SEQNUM=1008
received 243 bytes
add@/devices/platform/Mstar-ehci-3.2/usb5/5-1/5-1:1.0/host2/target2:0:0/2:0:0:0
ACTION=add
DEVPATH=/devices/platform/Mstar-ehci-3.2/usb5/5-1/5-1:1.0/host2/target2:0:0/2:0:0:0
SUBSYSTEM=scsi
DEVTYPE=scsi_device
MODALIAS=scsi:t-0x00
SEQNUM=1025
00dp00ic08isc06ip50
SEQNUM=1020
=1015
SEQNUM=1008
received 243 bytes
add@/devices/platform/Mstar-ehci-3.2/usb5/5-1/5-1:1.0/host2/target2:0:0/2:0:0:0/scsi_disk/2:0:0:0
ACTION=add
DEVPATH=/devices/platform/Mstar-ehci-3.2/usb5/5-1/5-1:1.0/host2/target2:0:0/2:0:0:0/scsi_disk/2:0:0:0
SUBSYSTEM=scsi_disk
SEQNUM=1026
00dp00ic08isc06ip50
SEQNUM=1020
=1015
SEQNUM=1008
received 249 bytes
add@/devices/platform/Mstar-ehci-3.2/usb5/5-1/5-1:1.0/host2/target2:0:0/2:0:0:0/scsi_device/2:0:0:0
ACTION=add
DEVPATH=/devices/platform/Mstar-ehci-3.2/usb5/5-1/5-1:1.0/host2/target2:0:0/2:0:0:0/scsi_device/2:0:0:0
SUBSYSTEM=scsi_device
SEQNUM=1027
ic08isc06ip50
SEQNUM=1020
=1015
SEQNUM=1008
received 99 bytes
add@/devices/virtual/bdi/8:0
ACTION=add
DEVPATH=/devices/virtual/bdi/8:0
SUBSYSTEM=bdi
SEQNUM=1028
ACTION=add
DEVPATH=/devices/platform/Mstar-ehci-3.2/usb5/5-1/5-1:1.0/host2/target2:0:0/2:0:0:0/scsi_device/2:0:0:0
SUBSYSTEM=scsi_device
SEQNUM=1027
ic08isc06ip50
SEQNUM=1020
=1015
SEQNUM=1008
received 273 bytes
add@/devices/platform/Mstar-ehci-3.2/usb5/5-1/5-1:1.0/host2/target2:0:0/2:0:0:0/block/sda
ACTION=add
DEVPATH=/devices/platform/Mstar-ehci-3.2/usb5/5-1/5-1:1.0/host2/target2:0:0/2:0:0:0/block/sda
SUBSYSTEM=block
MAJOR=8
MINOR=0
DEVNAME=sda
DEVTYPE=disk
NPARTS=1
SEQNUM=1029
0
=1015
SEQNUM=1008
received 288 bytes
add@/devices/platform/Mstar-ehci-3.2/usb5/5-1/5-1:1.0/host2/target2:0:0/2:0:0:0/block/sda/sda1
ACTION=add
DEVPATH=/devices/platform/Mstar-ehci-3.2/usb5/5-1/5-1:1.0/host2/target2:0:0/2:0:0:0/block/sda/sda1
SUBSYSTEM=block
MAJOR=8
MINOR=1
DEVNAME=sda1
DEVTYPE=partition
PARTN=1
SEQNUM=1030
=1008