When a user adds or deletes a device to the system, the kernel produces a hot-swappable event and finds the user-space program for processing the device connection in the/proc/sys/kernel/hotplug file, which includes/sbin/hotplug and/sbin/mdev.
Echo/sbin/hotplug >/proc/sys/kernel/hotplug
Or
Echo/sbin/mdev >/proc/sys/kernel/hotplug
Mdev-s HotPlug
is a bash script that has the following similar code:
Dir= "/ETC/HOTPLUG.D" for I-"
${dir}/$1" *.hotplug "${dir}/" default/*.hotplug; do
if [f $I]; then
test-x $ I && $I $;
Fi
done
exit 1
1 The/sbin/hotplug script is invoked when the driver execution kobject_uevent invokes the Hotplughelper.
2 The script searches all hotplug-suffix programs in the/ETC/HOTPLUG.D directory and invokes the
3 The argument passed to the invoked program is the name of the event.
4 The invoked program can also read a large number of environment variables, including action, DEVPATH, subsystem, and so on.
5 The invoked program according to these environment variables in the/lib/module/kernel_version/modules.*map file to find the corresponding need to load the module and load.
(*.map is when the driver uses Module_device_table macros, the DEPMOD program uses this information and creates/lib/module/kernel_version/modules.*map files.) ) Udev/mdev/vold
A solution that provides user space with a dynamic/dev directory that uses a fixed device name.
Mdev is a simplified udev, is the BusyBox with the program, suitable for the use of embedded systems. /sbin/mdev is a link that points to/bin/busybox
The vold mechanism in the Android system is the same as the Udev, the Android source NetlinkManager.cpp is also listening to the socket based on NetLink and parsing the received message.
Udev the name and permissions for each device are set by the file specified by the/ETC/UDEV/RULES.D directory, and if Udev cannot find the permission file for the device being created, the default permissions are set to 660, and the owner Root:root
Hot-swappable Equipment
Because the command was run at startup: Echo/sbin/mdev >/proc/sys/kernel/hotplug, the/sbin/mdev will be invoked when a hot plug event is generated. Mdev based on the action and Devpath in the environment variable to determine the action of this hot-swappable event and which directory in the/sys, then see if there are any dev files in the directory, and if so, use this information to create the device node files in the/dev directory.
Cold Plug and Pull equipment
Cold plug the device in the boot, the Udev has been inserted before the boot, for cold-swappable devices, the Linux kernel provides SYSFS below a uevent node, you can write an add to the node, causing the kernel to resend NetLink, Then Udev will be able to receive the cold plug of NetLink news.
Mdev-s
Locate a file called Dev in the/sys/class and/sys/block directory tree to create the appropriate device node in the/dev directory according to the primary and secondary device number of the device node recorded in the dev file.
Devices and drivers in the/sys/bus/and/sys/class directories are symbolic connections to files in the/sys/devices directory.
Troubleshoot "Cannot create/proc/sys/kernel/hotplug:nonexistent directory" error when using Mdev
Make sure the following options are compiled when compiling the kernel:
Config_proc_fs=y
Config_proc_sysctl=y
config_hotplug=y
config_net=y
If Config_hotplug and config_net do not select or select all,/proc/sys/kernel will not create hotplug files. (See KERNEL/SYSCTL.C) principle
struct Device *device_create (struct class *class, struct device *parent, dev_t devt, void *drvdata, const char *FMT, ...)
int device_register (struct device *dev) device_initialize (
Dev) device_add (
Dev) kobject_add
(& Dev->kobj, Dev->kobj.parent, NULL);
Kobject_uevent (&dev->kobj, Kobj_add);
The above device_create and device_register can be used to add struct device, in fact, in Device_create is called Device_register.
In Kobject_uevent, the Uevent event can be sent to the user space program UDEVD by NetLink, or the user space program can be directly invoked Hotplug_helper
It is to be noted that
If dev_t = 0 in Device_add, the last UDEVD will not create the appropriate device node in the/dev directory
If you dev_t!= 0 in Device_add, Mdev or hotplug generates device nodes based on the content (Major:minor) in Dev.
Device_add is to add devices under the/sys/devices/directory, devices and drivers under the/sys/bus/and/sys/class directories are symbolic connections to files in the/sys/devices directory.
Udev fully working in user state, using the hot Plug event sent by the kernel when the device is added or removed to work
At the time of hot swap, the details of the device are sent by the kernel through the netlink socket, and what is called the Uevent,udev naming policy, permission control, and event handling is done in user state, and he uses the information received from the kernel to create device file nodes.
The following program is used to receive information from the kernel netlink, and inserting the device into the system will print the information received from the kernel.
Udev is to receive netlink messages in this way and work according to his content and the Udev rules that the user sets.
#include <linux/netlink.h> Static void die (char *s) {Write (2, S, strlen (s));
Exit (1);
int main (int argc, char *argv[]) {Struct sockaddr_nl nls;
Struct POLLFD PFD;
Char buf[512];
Open HotPlug Event Netlilnk socket memset (NLS, 0, sizeof (struct sockaddr_nl));
nls.nl_family = Af_netlink;
Nls.nl_pid = Getpid ();
Nls.nl_group =-1;
Pfd.event = Pollin;
PFD.FD = socket (Pf_netlink, SOCK_DGRAM, netlink_kobject_uevent);
If (PFD. Fd = = 1) Die ("No root\n");
Listen to NetLink socket If (Bind (PFD.FD, (void *) &nls, sizeof (struct sockaddr_nl));
Die ("Bind failed\n");
while ( -1!= poll (&PFD, 1,-1)) {Int I, Len = recv (pfd.fd, buf, sizeof (BUF), msg_dontwait);
If (len = = 1) Die ("recv\n");
I = 0;
Print the data to stdout while (I < len) {Printf ("%s\n", buf + i); i = i + strlen (buf + i) + 1}} Die ("Poll\n ");
return 0; }
You can use the Udev tool Udevadm info to find the kernel information and SYSFS attribute information that the rule file can take advantage of.
such as running "Udevadm info-a-p/sys/devices/platform/serial8250/tty/ttys0"
If the node in the/dev directory has been created, but does not know his corresponding/sys/specific node path,
Udevadm info-q path-n/dev/Festival named
Reference articles:
1. Linux Device model (hot-swappable, Mdev and firmware)
2. Mdev HotPlug Equipment
3. Linux device model, SYSFS file system and Udev device file