天天看點

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;

scratch += sprintf (scratch, "devpath=%s", devpath) + 1;

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

mount /dev/$mdev /tmp; 

umount /tmp; 

                        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:

[2] sysctl -w kernel.hotplug=/sbin/mdev

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>]

<device regex> <uid>:<gid> <permissions> [!] [@|$|*<command>]

---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

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.

繼續閱讀