use Netlink To achieve

This is a special socket, Accept messages from the kernel

The code is as follows :

The operation results are as follows :

When there is USB When the device is inserted :

When there is USB When the device is unplugged :

2. use udev Rules to implement

since 2.6 Core start , Can be used udev Name of each equipment in the assistance management system . for example , Disk device sorting , Network card device sorting, etc .udev Be able to dynamically /dev
Create custom , Identifying device files or device links .

2.4 Kernel usage devfs( Device file system ) Create device file at device initialization , Device drivers can specify device numbers , owner , User space and other information ,devfs
Running in the kernel environment , There are many disadvantages : Main / Insufficient auxiliary equipment number , Inflexible naming , Can't specify device name and other issues . From 2.6 Kernel start , Introduced sysfs file system .sysfs
Organize devices and buses connected to the system into a hierarchical file , And provide access to user space .udev Running in user mode , Not in the kernel .udev
The initialization script for creates a device node at system startup , And when a new device is inserted —— Add driver module —— stay sysfs After registering new data on ,udev Innovation of new equipment nodes .
udev Is a tool that works in user space , It can dynamically update device files according to the status of hardware devices in the system , Include creation of device files , delete , Authority, etc . These files are usually defined in /dev
Directory , But it can also be specified in the configuration file .udev Must be in the kernel sysfs and tmpfs support ,sysfs by udev Provide equipment access and uevent passageway ,tmpfs
by udev Storage space for equipment documents .
be careful ,udev Through the modification of device files generated by the kernel , Or add aliases to customize device files . however ,udev
Is a user mode program , It does not change kernel behavior . in other words , The kernel will still create sda,sdb Etc , and udev Different devices can be distinguished according to their unique information , And generate new device files ( Or link ). In the application of users , Just use the newly generated device file .
udev Work flow chart of :

Relevant examples are as follows :

stay /etc/udev/rules.d Create related rule file under folder , The content is as follows :

When inserting a device (ACTION=="add") stay kernel The device name in is sdb4(KERNEL=="sdc1"), And block device (SUBSYSTEM=="block"), When the above three conditions are met , Name the device file Myusb(NAME="Myusb"), And create soft links Myusb_link(SYMLINK+="Myusb_link"), Simultaneous execution /home/usbadd.sh script (RUN+="/home/usbadd.sh").

among sdc1 Is through fdisk
-l See what you get , Soft links will be /dev Create below , Name can be customized ( No matter in the future u All disks can be accessed under the same name ),usbadd.sh Script anything you want , My content is to u Disk mount to /mnt/usb Directory :

When the device is unplugged , function /home/usbremove.sh Script uninstall u disc :