Detection of Uvent hotplug events from kernel, source code, test available

Source: Internet
Author: User

#include <stdio.h>
#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) = = ' = ')
Buf[i]= ' \ n ';
printf ("Received%d bytes\n%s\n", len,buf);
}
}


int main (int argc,char **argv)
{
Monitornetlinkuevent ();
return 0;
}



Insert U disk output 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 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 Blade cruzer 1.26 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)
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 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 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 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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.