Linux系统之热插拨事件uevent

文章最后面我会附上medv.conf配置文档medv.txt,是英文的,部分朋友的英语不是很好,但还是希望大家要硬着头皮去读,不要有畏惧心理,相信你读完了后会给你带来很大的帮助。你要相信外文资料讲得很地道,翻译过来没有那种感觉,以上只是个人看法。

class_device_create

class_device_register
class_device_add
kobject_uevent(&class_dev->kobj, KOBJ_ADD);
kobject_uevent_env(kobj, action, NULL);

// action_string = "add";
action_string = action_to_string(action);

/* 分配保存环境变量的内存 */
/* environment values */
buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);

/* 设置环境变量 */
envp [i++] = scratch;
scratch += sprintf(scratch, "ACTION=%s", action_string) + 1;
envp [i++] = scratch;
scratch += sprintf (scratch, "DEVPATH=%s", devpath) + 1;
envp [i++] = scratch;
scratch += sprintf(scratch, "SUBSYSTEM=%s", subsystem) + 1;

/* 调用应用程序: 比如mdev */
/* 启动脚本 echo /sbin/mdev > /proc/sys/kernel/hotplug 
* 设置了uevent_helper为“/sbin/mdev“
*/

argv [0] = uevent_helper;  // = "/sbin/mdev"
argv [1] = (char *)subsystem;
argv [2] = NULL;
call_usermodehelper (argv[0], argv, envp, 0);

分析: busybox mdev.c  

100ask: uevent_helper = /sbin/mdev
envp[0] = HOME=/
envp[1] = PATH=/sbin:/bin:/usr/sbin:/usr/bin
envp[2] = ACTION=add
envp[3] = DEVPATH=/class/sixth_drv/buttons
envp[4] = SUBSYSTEM=sixth_drv
envp[5] = SEQNUM=720
envp[6] = MAJOR=252
envp[7] = MINOR=0

mdev_main
temp = /sys/class/sixth_drv/buttons
make_device(temp, 0);
/* 确定设备文件名,类型,主次设备号 */
device_name = bb_basename(path);  = "buttons"

'c' == > 字符设备节点
根据"/sys/class/sixth_drv/buttons/dev"的内容确定主次设备号

mknod(device_name, mode | type, makedev(major, minor)

我接上U盘,想自动挂载,怎么办?
mdev.conf的格式:
<device regex> <uid>:<gid> <octal permissions> [<@|$|*> <command>]

device regex:正则表达式,表示哪一个设备
uid: owner
gid: 组ID
octal permissions:以八进制表示的属性
@:创建设备节点之后执行命令
$:删除设备节点之前执行命令
*: 创建设备节点之后 和 删除设备节点之前 执行命令
command:要执行的命令

写mdev.conf放在/etc/目下,sh脚本文件放在bin目录下,device regex用正则表达式
写相对方便一点,command可以写在脚本文件中,但是要注意sh脚本文件是可以执行的。

下面是几个例子:
1.
leds 0:0 777
led1 0:0 777
led2 0:0 777
led3 0:0 777
dma  0:0 777
2.
leds?[123]? 0:0 777

/*For your convenience, the shell env var $MDEV is set to the device name.  So if
the device "hdc" was matched, MDEV would be set to "hdc".*/
3.
leds?[123]? 0:0 777 @ echo create /dev/$MDEV > /dev/console

4.
leds?[123]? 0:0 777 * if [ $ACTION = "add" ]; then echo create /dev/$MDEV > /dev/console; else echo remove /dev/$MDEV > /dev/console; fi

5. 
leds?[123]? 0:0 777 * /bin/add_remove_led.sh

把命令写入一个脚本:
add_remove_led.sh

#!/bin/sh
if [ $ACTION = "add" ]; 
then 
echo create /dev/$MDEV > /dev/console; 
else 
echo remove /dev/$MDEV > /dev/console; 
fi

6. U盘自动加载
sda[1-9]+ 0:0 777 * if [ $ACTION = "add" ]; then mount /dev/$MDEV /mnt; else umount /mnt; fi

7.
sda[1-9]+ 0:0 777 * /bin/add_remove_udisk.sh

add_remove_udisk.sh
#!/bin/sh
if [ $ACTION = "add" ]; 
then 
mount /dev/$MDEV /tmp; 
else 
umount /tmp; 

fi

                        mdev.txt

-------------
 MDEV Primer
-------------

For those of us who know how to use mdev, a primer might seem lame.  For
everyone else, mdev is a weird black box that they hear is awesome, but can't
seem to get their head around how it works.  Thus, a primer.

-----------
 Basic Use
-----------

Mdev has two primary uses: initial population and dynamic updates.  Both
require sysfs support in the kernel and have it mounted at /sys.  For dynamic
updates, you also need to have hotplugging enabled in your kernel.

Here's a typical code snippet from the init script:
[0] mount -t proc proc /proc
[1] mount -t sysfs sysfs /sys
[2] echo /sbin/mdev > /proc/sys/kernel/hotplug
[3] mdev -s

Alternatively, without procfs the above becomes:
[1] mount -t sysfs sysfs /sys
[2] sysctl -w kernel.hotplug=/sbin/mdev
[3] mdev -s

Of course, a more "full" setup would entail executing this before the previous
code snippet:
[4] mount -t tmpfs -o size=64k,mode=0755 tmpfs /dev
[5] mkdir /dev/pts
[6] mount -t devpts devpts /dev/pts

The simple explanation here is that [1] you need to have /sys mounted before
executing mdev.  Then you [2] instruct the kernel to execute /sbin/mdev whenever
a device is added or removed so that the device node can be created or
destroyed.  Then you [3] seed /dev with all the device nodes that were created
while the system was booting.

For the "full" setup, you want to [4] make sure /dev is a tmpfs filesystem
(assuming you're running out of flash).  Then you want to [5] create the
/dev/pts mount point and finally [6] mount the devpts filesystem on it.

-------------
 MDEV Config   (/etc/mdev.conf)
-------------

Mdev has an optional config file for controlling ownership/permissions of
device nodes if your system needs something more than the default root/root
660 permissions.

The file has the format:
    <device regex>       <uid>:<gid> <permissions>
 or @<maj[,min1[-min2]]> <uid>:<gid> <permissions>

For example:
    hd[a-z][0-9]* 0:3 660

The config file parsing stops at the first matching line.  If no line is
matched, then the default of 0:0 660 is used.  To set your own default, simply
create your own total match like so:
.* 1:1 777

You can rename/move device nodes by using the next optional field.
<device regex> <uid>:<gid> <permissions> [=path]
So if you want to place the device node into a subdirectory, make sure the path
has a trailing /.  If you want to rename the device node, just place the name.
hda 0:3 660 =drives/
This will move "hda" into the drives/ subdirectory.
hdb 0:3 660 =cdrom
This will rename "hdb" to "cdrom".

Similarly, ">path" renames/moves the device but it also creates
a direct symlink /dev/DEVNAME to the renamed/moved device.

You can also prevent creation of device nodes with the 4th field as "!":
tty[a-z]. 0:0 660 !
pty[a-z]. 0:0 660 !

If you also enable support for executing your own commands, then the file has
the format:
<device regex> <uid>:<gid> <permissions> [=path] [@|$|*<command>]
    or
<device regex> <uid>:<gid> <permissions> [>path] [@|$|*<command>]
    or
<device regex> <uid>:<gid> <permissions> [!] [@|$|*<command>]

For example:
---8<---
# block devices
([hs]d[a-z]) root:disk660>disk/%1/0
([hs]d[a-z])([0-9]+) root:disk660>disk/%1/%2
mmcblk([0-9]+) root:disk660>disk/mmc/%1/0
mmcblk([0-9]+)p([0-9]+) root:disk660>disk/mmc/%1/%2
# network devices
(tun|tap) root:network660>net/%1
---8<---

The special characters have the meaning:
@ Run after creating the device.
$ Run before removing the device.
* Run both after creating and before removing the device.

The command is executed via the system() function (which means you're giving a
command to the shell), so make sure you have a shell installed at /bin/sh.  You
should also keep in mind that the kernel executes hotplug helpers with stdin,
stdout, and stderr connected to /dev/null.

For your convenience, the shell env var $MDEV is set to the device name.  So if
the device "hdc" was matched, MDEV would be set to "hdc".

----------
 FIRMWARE
----------

Some kernel device drivers need to request firmware at runtime in order to
properly initialize a device.  Place all such firmware files into the
/lib/firmware/ directory.  At runtime, the kernel will invoke mdev with the
filename of the firmware which mdev will load out of /lib/firmware/ and into
the kernel via the sysfs interface.  The exact filename is hardcoded in the
kernel, so look there if you need to know how to name the file in userspace.

------------
 SEQUENCING
------------

Kernel does not serialize hotplug events. It increments SEQNUM environmental
variable for each successive hotplug invocation. Normally, mdev doesn't care.
This may reorder hotplug and hot-unplug events, with typical symptoms of
device nodes sometimes not created as expected.

However, if /dev/mdev.seq file is found, mdev will compare its
contents with SEQNUM. It will retry up to two seconds, waiting for them
to match. If they match exactly (not even trailing '\n' is allowed),
or if two seconds pass, mdev runs as usual, then it rewrites /dev/mdev.seq
with SEQNUM+1.

IOW: this will serialize concurrent mdev invocations.

If you want to activate this feature, execute "echo >/dev/mdev.seq" prior to
setting mdev to be the hotplug handler. This writes single '\n' to the file.
NB: mdev recognizes /dev/mdev.seq consisting of single '\n' character
as a special case. IOW: this will not make your first hotplug event
to stall for two seconds.

时间: 2024-10-29 20:25:28

Linux系统之热插拨事件uevent的相关文章

Linux 系统应用编程——进程基础

一.Linux下多任务机制的介绍          Linux有一特性是多任务,多任务处理是指用户可以在同一时间内运行多个应用程序,每个正在执行的应用程序被称为一个任务.          多任务操作系统使用某种调度(shedule)策略(由内核来执行)支持多个任务并发执行.事实上,(单核)处理器在某一时刻只能执行一个任务.每个任务创建时被分配时间片(几十到上百毫秒),任务执行(占用CPU)时,时间片递减.操作系统会在当前任务的时间片用完时调度执行其他任务.由于任务会频繁地切换执行,因此给用户多

Linux系统针对网卡中断的优化处理

中断: 当网卡接收到数据包后,会触发硬中断,通知CPU来收包.硬中断是一个CPU和网卡交互的过程.这其实会消耗CPU资源.特别是在使用速度极快的万兆网卡之后,大量的网络交互使得CPU很大一部分资源消耗在网卡中断处理上.此时,瓶颈并不在网卡,而是在CPU上.因此,现在的网卡都采用多队列的技术,用于充分利用多核心CPU. 中断的详细解释:<Linux的中断和异常扫盲笔记> SMP IRQ affinity 为了防止多个设置发送相同的中断, Linux设计了一套中断请求系统, 使得计算机系统中的每个

Linux系统安全配置详细解析

  1.为LILO增加开机口令 在/etc/lilo.conf文件中增加选项,从而使LILO启动时要求输入口令,以加强系统的安全性.具体设置如下: boot=/dev/hdamap=/boot/mapinstall=/boot/boot.btime-out=60 #等待1分钟promptdefault=linuxpassword=#口令设置image=/boot/vmlinuz-2.2.14-12label=linuxinitrd=/boot/initrd-2.2.14-12.img root=

Linux系统下QT中的多线程编程

Qt 作为一种基于 C++ 的跨平台 GUI 系统,能够提供给用户构造图形用户界面的强大功能.为了满足 用户构造复杂图形界面系统的需求,Qt 提供了丰富的多线程编程支持. Qt 作为一种基于 C++ 的跨平台 GUI 系统,能够提供给用户构造图形用户界面的强大功能.为了满足 用户构造复杂图形界面系统的需求,Qt 提供了丰富的多线程编程支持.从 2.2 版本开始,Qt 主要从下 面三个方面对多线程编程提供支持:一.构造了一些基本的与平台无关的线程类:二.提交用户自定义事 件的 Thread-saf

浅谈Linux系统的安全保护

无论你是Linux的普通桌面用户还是管理多个服务器的系统管理员,你都面临着同样的问题:日益增加的各种威胁.Linux是一个开放式系统,可以在网络上找到许多现成的程序和工具,这既方便了用户,也方便了黑客,因为他们也能很容易地找到程序和工具来潜入Linux系统,或者盗取Linux系统上的重要信息. "知己知彼,百战不殆".作为一个好的系统管理者,要保障整个系统的安全运行.最好的方法是了解攻击的工作原理和机制,了解攻击中使用了哪些工具,如何操作入侵等等.并知道如何从部署linux来降低风险.

Linux系统中的进程管理简介

在Linux系统里,当前正在运行的程序实例称为进程.比如,当你启动Apache的时候,系统会为它分配一个进程ID.然后就可以用这个ID监视和控制这个程序. 进程监视和控制是任何Linux系统管理员的核心任务.一个管理员可以终止("kill").重启一个进程,甚至可以为它指定一个不同的优先级.标准的Linux命令"ps"和"top"通常用于查看当前的进程列表.下面我来说明如何用这些命令和其它命令来管理Linux系统中的进程. 用ps监视进程 一个监

Linux系统platform设备驱动全透析

1.1 platform总线.设备与驱动 在Linux 2.6的设备驱动模型中,关心总线.设备和驱动这3个实体,总线将设备和驱动绑定.在系统每注册一个设备的时候,会寻找与之匹配的驱动:相反的,在系统每注册一个驱动的时候,会寻找与之匹配的设备,而匹配由总线完成. 一个现实的Linux设备和驱动通常都需要挂接在一种总线上,对于本身依附于PCI.USB.I2C.SPI等的设备而言,这自然不是问题,但是在嵌入式系统里面,SoC系统中集成的独立的外设控制器.挂接在SoC内存空间的外设等确不依附于此类总线.

Linux系统下NTP协议的超级配置攻略

  在Linux系统中,为了避免主机时间因为在长时间运行下所导致的时间偏差,进行时间同步(synchronize)的工作是非常必要的.Linux系统下,一般使用ntp服务来同步不同机器的时间.NTP 是网络时间协议(Network Time Protocol)的简称,干嘛用的呢?就是通过网络协议使计算机之间的时间同步化. 安装NTP包 检查是否安装了ntp相关包.如果没有安装ntp相关包,使用rpm或yum安装,安装也非常简单方便. 复制代码 代码如下: [root@localhost ~]#

在Linux系统中使用logrotate来管理日志文件的方法

  日志文件包含了关于系统中发生的事件的有用信息,在排障过程中或者系统性能分析时经常被用到.对于忙碌的服务器,日志文件大小会增长极快,服务器会很快消耗磁盘空间,这成了个问题.除此之外,处理一个单个的庞大日志文件也常常是件十分棘手的事. logrotate是个十分有用的工具,它可以自动对日志进行截断(或轮循).压缩以及删除旧的日志文件.例如,你可以设置logrotate,让/var/log/foo日志文件每30天轮循,并删除超过6个月的日志.配置完后,logrotate的运作完全自动化,不必进行任