#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <sys/un.h> #include <sys/ioctl.h> #include <sys/socket.h> #include <linux/types.h> #include <linux/netlink.h> #include <errno.h> #include <unistd.h> #include <arpa/inet.h> #include <netinet/in.h> #define UEVENT_BUFFER_SIZE 2048 static int init_hotplug_sock() { const int buffersize = 1024; int ret; struct sockaddr_nl snl; bzero(&snl, sizeof(struct sockaddr_nl)); snl.nl_family = AF_NETLINK; snl.nl_pid = getpid(); snl.nl_groups = 1; int s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT ); if (s == -1) { perror("socket"); return -1; } setsockopt(s, SOL_SOCKET, SO_RCVBUF, &buffersize, sizeof(buffersize)); ret = bind(s, (struct sockaddr *)&snl, sizeof(struct sockaddr_nl)); if (ret < 0) { perror("bind"); close(s); return -1; } return s; } int main(int argc, char* argv[]) { int hotplug_sock = init_hotplug_sock(); while(1) { /* Netlink message buffer */ char buf[UEVENT_BUFFER_SIZE * 2] = {0}; recv(hotplug_sock, &buf, sizeof(buf), 0); printf("%s\n", buf); } return 0; }
Insert USB dongle into USB socket
add@/devices/pci0000:00/0000:00:1d.7/usb1/1-6
add@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0
add@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/host8
add@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/host8/scsi_host/host8
add@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/usb_endpoint/usbdev1.11_ep01
add@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/usb_endpoint/usbdev1.11_ep82
add@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/usb_endpoint/usbdev1.11_ep83
add@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/usb_endpoint/usbdev1.11_ep00
add@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/host8/target8:0:0
add@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/host8/target8:0:0/8:0:0:0
add@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/host8/target8:0:0/8:0:0:0/scsi_disk/8:0:0:0
change@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/host8/target8:0:0/8:0:0:0
add@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/host8/target8:0:0/8:0:0:0/block/sdb
add@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/host8/target8:0:0/8:0:0:0/block/sdb/sdb1
add@/devices/virtual/bdi/8:16
add@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/host8/target8:0:0/8:0:0:0/scsi_device/8:0:0:0
add@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/host8/target8:0:0/8:0:0:0/scsi_generic/sg2
Remove USB dongle from USB socket
remove@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/usb_endpoint/usbdev1.11_ep01
remove@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/usb_endpoint/usbdev1.11_ep82
remove@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/usb_endpoint/usbdev1.11_ep83
remove@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/host8/target8:0:0/8:0:0:0/scsi_generic/sg2
remove@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/host8/target8:0:0/8:0:0:0/scsi_device/8:0:0:0
remove@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/host8/target8:0:0/8:0:0:0/scsi_disk/8:0:0:0
remove@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/host8/target8:0:0/8:0:0:0/block/sdb/sdb1
remove@/devices/virtual/bdi/8:16
remove@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/host8/target8:0:0/8:0:0:0/block/sdb
remove@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/host8/target8:0:0/8:0:0:0
remove@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/host8/scsi_host/host8
remove@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0/host8
remove@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/1-6:1.0
remove@/devices/pci0000:00/0000:00:1d.7/usb1/1-6/usb_endpoint/usbdev1.11_ep00
remove@/devices/pci0000:00/0000:00:1d.7/usb1/1-6
remove@/host8/target8:0:0
沒有留言:
張貼留言