*
UseNetlink To achieve

This is a specialsocket, Accept messages from the kernel

The code is as follows:







The operation results are as follows:

When there isUSB When the device is inserted:



When there isUSB When the device is unplugged:



2. Useudev Rules to implement

since2.6 Core start, Can be usedudev 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 usagedevfs( 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. But from2.6 Kernel start, Introducedsysfs 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—— staysysfs 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, Permission, etc.. These files are usually defined in/dev
Directory, But it can also be specified in the configuration file.udev Must be in the kernelsysfs andtmpfs Support,sysfs byudev Provide equipment access anduevent passageway,tmpfs
byudev 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 createsda,sdb Etc, andudev Different devices can be distinguished according to their unique information, And generate new device files( Or links). 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 contents are as follows:




When inserting a device(ACTION=="add") staykernel The device name in issdb4(KERNEL=="sdc1"), And block device(SUBSYSTEM=="block"), When the above three conditions are met, Name the device fileMyusb(NAME="Myusb"), And create soft linksMyusb_link(SYMLINK+="Myusb_link"), Simultaneous execution/home/usbadd.sh Script(RUN+="/home/usbadd.sh").

amongsdc1 It is throughfdisk
-l See what you get, Soft links will be/dev Create below, Name can be customized( No matter in the futureu All disks can be accessed under the same name),usbadd.sh Script anything you want, My content is tou Disk mounted to/mnt/usb Directory:



When the device is unplugged, Function/home/usbremove.sh Script uninstallu disc: