/** * This program's outputs. * root@slax:~# ./a.out remove@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2.1/3-2.1:1.0/host5/target5:0:0/5:0:0:0/bsg/5:0:0:0change@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2.1/3-2.1:1.0/host5/target5:0:0/5:0:0:0/block/sdbremove@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2.1/3-2.1:1.0/host5/target5:0:0/5:0:0:0/scsi_device/5:0:0:0remove@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2.1/3-2.1:1.0/host5/target5:0:0/5:0:0:0/scsi_disk/5:0:0:0remove@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2.1/3-2.1:1.0/host5/target5:0:0/5:0:0:0/block/sdb/sdb1remove@/devices/virtual/bdi/8:16remove@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2.1/3-2.1:1.0/host5/target5:0:0/5:0:0:0/block/sdbremove@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2.1/3-2.1:1.0/host5/target5:0:0/5:0:0:0remove@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2.1/3-2.1:1.0/host5/scsi_host/host5remove@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2.1/3-2.1:1.0/host5remove@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2.1/3-2.1:1.0remove@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2.1remove@/host5/target5:0:0add@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2.1add@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2.1/3-2.1:1.0add@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2.1/3-2.1:1.0/host6add@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2.1/3-2.1:1.0/host6/scsi_host/host6add@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2.1/3-2.1:1.0/host6/target6:0:0add@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2.1/3-2.1:1.0/host6/target6:0:0/6:0:0:0add@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2.1/3-2.1:1.0/host6/target6:0:0/6:0:0:0/scsi_disk/6:0:0:0add@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2.1/3-2.1:1.0/host6/target6:0:0/6:0:0:0/scsi_device/6:0:0:0add@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2.1/3-2.1:1.0/host6/target6:0:0/6:0:0:0/bsg/6:0:0:0add@/devices/virtual/bdi/8:16add@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2.1/3-2.1:1.0/host6/target6:0:0/6:0:0:0/block/sdbadd@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2.1/3-2.1:1.0/host6/target6:0:0/6:0:0:0/block/sdb/sdb1 */#include <stdio.h>#include <errno.h>#include <string.h>#include <sys/socket.h>#include <linux/netlink.h>#define UEVENT_BUFFER_SIZE 2048static int init_hotplug_sock(void){ struct sockaddr_nl snl; const int buffersize = 16 * 1024 * 1024; int retval; memset(&snl, 0x00, sizeof(struct sockaddr_nl)); snl.nl_family = AF_NETLINK; snl.nl_pid = getpid(); snl.nl_groups = 1; int hotplug_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT); if (hotplug_sock == -1) { printf("error getting socket: %s", strerror(errno)); return -1; } /* set receive buffersize */ setsockopt(hotplug_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize)); retval = bind(hotplug_sock, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl)); if (retval < 0) { printf("bind failed: %s", strerror(errno)); close(hotplug_sock); hotplug_sock = -1; return -1; } return hotplug_sock;}int main(int argc, char* argv[]){ int hotplug_sock = init_hotplug_sock(); while(1) { //printf("sunqidong debug\n"); char buf[UEVENT_BUFFER_SIZE*2] = {0}; recv(hotplug_sock, &buf, sizeof(buf), 0); printf("%s\n", buf); } return 0;}