Obtain USB hot swapping information through socket

Source: Internet
Author: User

Static int init_hotplug_sock (void)
183 {
184
185 struct sockaddr_nl SNL;
186 const int buffersize = 16*1024*1024;
187 int retval;
188
189 memset (& SNL, 0x00, sizeof (struct sockaddr_nl ));
190 SNL. nl_family = af_netlink;
191 SNL. nl_pid = getpid ();
192 SNL. nl_groups = 1;
193
194 int hotplug_sock = socket (pf_netlink, sock_dgram, netlink_kobject_uevent );

195 If (hotplug_sock =-1)
196 {
197 printf ("error getting socket: % s", strerror (errno ));
198 return-1;
199}
200
201/* set receive buffersize */
202
203 // setsockopt (hotplug_sock, sol_socket, so_rcvbufforce, & buffersize, sizeof (buffersize ));
204 retval = BIND (hotplug_sock, (struct sockaddr *) & SNL, sizeof (struct sockaddr_nl ));
205 If (retval <0 ){
206 printf ("bind failed: % s", strerror (errno ));
207 close (hotplug_sock );
208 hotplug_sock =-1;
209 return-1;
210}
211 pthread_mutex_init (& mount_mutex, null );
212 return hotplug_sock;
213
214}

 

 

If (hotplug_sock> = 0) & fd_isset (hotplug_sock, & FD ))
{
Char Buf [4096] = {0 };
Recv (hotplug_sock, & Buf, sizeof (BUF), 0 );

}

The plugging status is obtained by judging "add" and "Remove" in the Buf.

 

Hotplug: Add @/devices/platform/brcm-ehci.0/usb1/1-1
Hotplug: Add @/devices/platform/brcm-ehci.0/usb1/1-1/usbdev1.4 _ ep00
Hotplug: Add @/devices/platform/brcm-ehci.0/usb1/1-1/1-. 0
Hotplug: Add @/class/scsi_host/host1
Hotplug: Add @/devices/platform/brcm-ehci.0/usb1/1-1/1-. 0/usbdev1.4 _ ep81
Hotplug: Add @/devices/platform/brcm-ehci.0/usb1/1-1/1-. 0/usbdev1.4 _ ep02
Hotplug: Add @/class/usb_device/usbdev1.4
[Cascade_ui.c: 372] Message: pd900 code = 20f key_eventkind = 0
199997: browsersendcustominput (527)
Hotplug: Add @/devices/platform/brcm-ehci.0/usb1/1-1/1-. 0/host1/target1: 0: 0/1: 0: 0: 0: 0
Hotplug: Add @/class/scsi_disk/1: 0: 0: 0
Hotplug: Add @/block/SDB
Hotplug: Add @/block/sdb/sdb1
Hotplug: Add @/class/scsi_device/1: 0: 0: 0
Hotplug: Add @/class/scsi_generic/sg0
Hotplug: Mount @/block/sdb/sdb1

 

 

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.