PF_NETLINK类型建立的socket, 检测USB口

摘自:https://blog.csdn.net/wandersky0822/article/details/110202371

 

通过使用PF_NETLINK类型建立的socket, 确实能检测到USB口的变化,这个东西太好了,做U盘自动检测,自动挂载就靠它了。 眼前豁然开朗

当插入U盘时,打印如下信息:

add@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2
add@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2:1.0

add@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2:1.0/host35
add@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2:1.0/host35/scsi_host/host35
add@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2:1.0/host35/target35:0:0
add@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2:1.0/host35/target35:0:0/35:0:0:0

add@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2:1.0/host35/target35:0:0/35:0:0:0/scsi_disk/35:0:0:0
bind@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2:1.0/host35/target35:0:0/35:0:0:0
add@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2:1.0/host35/target35:0:0/35:0:0:0/scsi_generic/sg2
add@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2:1.0/host35/target35:0:0/35:0:0:0/bsg/35:0:0:0
add@/devices/virtual/bdi/8:48
add@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2:1.0/host35/target35:0:0/35:0:0:0/block/sdd
add@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2:1.0/host35/target35:0:0/35:0:0:0/block/sdd/sdd1

当拨出U盘时,打印如下信息:
remove@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2:1.0/host35/target35:0:0/35:0:0:0/bsg/35:0:0:0

remove@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2:1.0/host35/target35:0:0/35:0:0:0/scsi_generic/sg2
remove@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2:1.0/host35/target35:0:0/35:0:0:0/scsi_device/35:0:0:0

remove@/devices/virtual/bdi/8:48
remove@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2:1.0/host35/target35:0:0/35:0:0:0/block/sdd
unbind@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2:1.0/host35/target35:0:0/35:0:0:0
remove@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2:1.0/host35/target35:0:0
remove@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2:1.0/host35/scsi_host/host35
remove@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2:1.0/host35
unbind@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2:1.0
remove@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2/3-2:1.0
unbind@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2
remove@/devices/pci0000:00/0000:00:15.0/0000:03:00.0/usb3/3-2
在电脑虚拟机上可以用, 但是在我的树莓派上却用不了,我怀疑怀疑还是系统的原因,因此采用这种高大上的办法好像适用性不是很广泛, 

于是我又老实了把 检测access  /dev/sda1 节点, 检测到之后,再挂载的办法,也实现了。还是这个办法保险。

posted @ 2021-03-30 10:40  LiuYanYGZ  阅读(121)  评论(0编辑  收藏  举报