Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4028983
  • 博文数量: 366
  • 博客积分: 9916
  • 博客等级: 中将
  • 技术积分: 7195
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-29 23:27
个人简介

简单!

文章分类

全部博文(366)

文章存档

2013年(51)

2012年(269)

2011年(46)

分类: 嵌入式

2013-01-11 14:24:08

       Linux内核提供了一种机制,使得有热插拔事件(比如插入或拔出U盘)发生时可以执行一个程序,在本文中我称之为hotplug程序。内核在调用 hotplug程序时会传递一个命令行参数,这个参数是发生热插拔事件的子系统名称,常见的有usb, module, drivers, net等。此外内核在调用hotplug程序是还会设置一些环境变量,如SUBSYSTEM, ACTION, PRODUCT, TYPE, INTERFACE, DEVPATH等。

下面是一系列实际的热插拔事件的例子,每一行表示hotplug程序被调用一次:
  1. 1 SUBSYSTEM=net, ACTION=linkup, PRODUCT=, TYPE=, INTERFACE=eth0, DEVPATH=/class/net/eth0
  2. 2 SUBSYSTEM=module, ACTION=add, PRODUCT=, TYPE=, INTERFACE=, DEVPATH=/module/ehci_hcd
  3. 3 SUBSYSTEM=platform, ACTION=add, PRODUCT=, TYPE=, INTERFACE=, DEVPATH=/devices/platform/ehci_hcd
  4. 4 SUBSYSTEM=drivers, ACTION=add, PRODUCT=, TYPE=, INTERFACE=, DEVPATH=/bus/platform/drivers/ehci_hcd
  5. 5 SUBSYSTEM=usb_host, ACTION=add, PRODUCT=, TYPE=, INTERFACE=, DEVPATH=/class/usb_host/usb1
  6. 6 SUBSYSTEM=usb, ACTION=add, PRODUCT=, TYPE=, INTERFACE=, DEVPATH=/devices/platform/ehci_hcd/usb1
  7. 7 SUBSYSTEM=module, ACTION=add, PRODUCT=, TYPE=, INTERFACE=, DEVPATH=/module/ohci_hcd
  8. 8 SUBSYSTEM=platform, ACTION=add, PRODUCT=, TYPE=, INTERFACE=, DEVPATH=/devices/platform/ohci_hcd
  9. 9 SUBSYSTEM=drivers, ACTION=add, PRODUCT=, TYPE=, INTERFACE=, DEVPATH=/bus/platform/drivers/ohci_hcd
  10. 10 SUBSYSTEM=usb_host, ACTION=add, PRODUCT=, TYPE=, INTERFACE=, DEVPATH=/class/usb_host/usb2
  11. 11 SUBSYSTEM=usb, ACTION=add, PRODUCT=0/0/206, TYPE=9/0/1, INTERFACE=9/0/0, DEVPATH=/devices/platform/ehci_hcd/usb1/1-0:1.0
  12. 12 SUBSYSTEM=usb, ACTION=add, PRODUCT=, TYPE=, INTERFACE=, DEVPATH=/devices/platform/ohci_hcd/usb2
  13. 13 SUBSYSTEM=scsi_host, ACTION=add, PRODUCT=, TYPE=, INTERFACE=, DEVPATH=/class/scsi_host/host0
  14. 14 SUBSYSTEM=usb, ACTION=add, PRODUCT=18a5/216/112, TYPE=0/0/0, INTERFACE=8/6/80, DEVPATH=/devices/platform/ehci_hcd/usb1/1-1/1-1:1.0
  15. 15 SUBSYSTEM=usb, ACTION=add, PRODUCT=0/0/206, TYPE=9/0/0, INTERFACE=9/0/0, DEVPATH=/devices/platform/ohci_hcd/usb2/2-0:1.0
  16. 16 SUBSYSTEM=usb, ACTION=add, PRODUCT=, TYPE=, INTERFACE=, DEVPATH=/devices/platform/ehci_hcd/usb1/1-1
  17. 17 SUBSYSTEM=scsi_generic, ACTION=add, PRODUCT=, TYPE=, INTERFACE=, DEVPATH=/class/scsi_generic/sg0
  18. 18 SUBSYSTEM=scsi_device, ACTION=add, PRODUCT=, TYPE=, INTERFACE=, DEVPATH=/class/scsi_device/0:0:0:0
  19. 19 SUBSYSTEM=scsi, ACTION=add, PRODUCT=, TYPE=, INTERFACE=, DEVPATH=/devices/platform/ehci_hcd/usb1/1-1/1-1:1.0/host0/target0:0:0/0:0:0:0
  20. 20 SUBSYSTEM=block, ACTION=add, PRODUCT=, TYPE=, INTERFACE=, DEVPATH=/block/sda
  21. 21 SUBSYSTEM=module, ACTION=add, PRODUCT=, TYPE=, INTERFACE=, DEVPATH=/module/ufsd
  22. 22 SUBSYSTEM=block, ACTION=add, PRODUCT=, TYPE=, INTERFACE=, DEVPATH=/block/sda/sda1

       系统默认的热插拔处理程序是/sbin/hotplug。在MP800H上/sbin/hotplug用的是diethotplug-0.4,在华 硕公开的源码中有它的源代码。这是一个瘦身版的hotplug程序,它的主要工作是在usb存储设备插入时进行自动挂载,挂载目录在/tmp /usbmounts下面,如/tmp/usbmounts/sda1。

       我们可以通过修改/proc/sys/kernel/hotplug来重新指定hotplug程序,通过定制hotplug程序,我们可以按我们希望的方式来处理热插拔事件,比如插入无线网卡时自动连接无线网。下面我将给出一个实现此功能的程序实例。

       程序由两个shell脚本和一个配置文件组成:hotplug脚本是我们定制的hotplug程序,wifi_monitor脚本是无线网络监控程 序,它们都放在/usr/local/sbin下。wifi.conf是无线网络配置文件,放在/usr/local/etc下。

首先用我们的hotplug程序替换系统默认的/sbin/hotplug
$ echo /usr/local/sbin/hotplug > /proc/sys/kernel/hotplug
       程序是这样工作的:热插拔事件发生时,内核执行我们的hotplug程序,即/usr/local/sbin/hotplug。hotplug程序先调用 /sbin/hotplug,让它完成它该做的工作。然后判断该事件是否为usb无线网卡插入事件,如果是则执行wifi_monitor,如果是无线网 络连接事件则通过DHCP获取IP地址。wifi_monitor根据wifi.conf配置文件尝试连接无线网络。它先扫描可用无线网络,如果找到指定 的网络则进行连接,否则等5秒钟再重新扫描。如果扫描3次都没有找到,则等待10秒钟后重新读入配置文件再次开始扫描循环。wifi_monitor还可 以处理无线网络断线的情况,即发现无线网络断开时重新开始扫描循环。

/usr/local/sbin/hotplug

  1. #!/bin/sh

  2. # let /sbin/hotplug do the dirty work first
  3. /sbin/hotplug $1

  4. #define log file. set to /dev/null if you want to suppress logging
  5. LOG=/var/log/hotplug.log

  6. # log the event
  7. echo "[`date`] hotplug SUBSYSTEM=$SUBSYSTEM, ACTION=$ACTION, PRODUCT=$PRODUCT, TYPE=$TYPE, INTERFACE=$INTERFACE, DEVPATH=$DEVPATH" >> $LOG

  8. EVENT="$SUBSYSTEM.$INTERFACE.$ACTION";
  9. if [ "$EVENT" = "net.wlan0.add" ] ; then
  10.   # start wifi monitor
  11.   /usr/local/sbin/wifi_monitor $INTERFACE &
  12. elif [ "$EVENT" = "net.wlan0.linkup" ] ; then
  13.   /sbin/udhcpc -i $INTERFACE -t 15 -b -q -s /etc/udhcpc.script >> $LOG 2>&1
  14. fi

  15. /usr/local/sbin/wifi_monitor
  16. #!/bin/sh
  17. PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin
  18. WPA_CTRL_INTERFACE=/var/lock/wpa_supplicant
  19. WPA_SUPPLICANT_PID=/var/lock/wpa_supplicant.pid
  20. WPA_SUPPLICANT_CONF=/tmp/wpa_supplicant.conf
  21. WIFI_CONF=/usr/local/etc/wifi.conf

  22. if [ -z "$1" ] ; then
  23.   echo "Usage: wifi_monitor "
  24.   exit 1
  25. fi

  26. IFACE=$1
  27. PIDFILE=/var/lock/wifi_monitor.$IFACE.pid
  28. SYSFS="/sys/class/net/$IFACE"

  29. if [ -e $PIDFILE ] && grep -q wifi_monitor /proc/`cat $PIDFILE`/cmdline ; then
  30.   echo "wifi_monitor: another instance is running, quiting..."
  31.   exit
  32. fi

  33. read_conf() {
  34.   for v in ESSID SECURITY WEP_AUTH WEP_KEYINDEX WEP_KEY WPA_PASSPHRASE ; do unset CONF_$v ; done
  35.   for line in `grep -E '^ *[A-Z_]+ *= *[^ #]+' $WIFI_CONF|sed -e 's/#.*//' -e 's/ *//g'` ; do eval CONF_$line ; done
  36. }

  37. reload() {
  38.   echo "wifi_monitor: reloading..."
  39.   ifconfig $IFACE down
  40.   iwconfig $IFACE essid off
  41. }

  42. quit() { exit; }

  43. cleanup() {
  44.   [ -e $WPA_SUPPLICANT_PID ] && pkill -x wpa_supplicant
  45.   rm -f $WPA_SUPPLICANT_PID
  46.   rm -rf $WPA_CTRL_INTERFACE
  47.   rm -f $PIDFILE
  48. }

  49. setup_wifi() {
  50.   [ -n "$CONF_ESSID" ] || { echo "Error: ESSID not defined!" ; return; }
  51.   # scan for AP
  52.   ifconfig $IFACE up
  53.   local count=0
  54.   until [ $count -ge 3 ] || iwlist $IFACE scanning|grep -q $CONF_ESSID ;
  55.   do
  56.     echo "AP $CONF_ESSID not found, sleeping...";
  57.     sleep 5;
  58.     let 'count=count+1';
  59.   done

  60.   if [ $count -lt 3 ] ; then
  61.     [ "x$CONF_SECURITY" != "xWPA" ] && [ -e $WPA_SUPPLICANT_PID ] && kill `cat $WPA_SUPPLICANT_PID`
  62.     case "$CONF_SECURITY" in
  63.       NONE)
  64.         iwconfig $IFACE key off
  65.         iwconfig $IFACE essid "$CONF_ESSID"
  66.         ;;
  67.       WEP)
  68.         [ -n "$CONF_WEP_KEY" ] || { echo "Error: WEP_KEY not defined!" ; return; }
  69.         if [ -n "$CONF_WEP_KEYINDEX" ] ; then
  70.           iwconfig $IFACE key $CONF_WEP_AUTH [$CONF_WEP_KEYINDEX] "$CONF_WEP_KEY"
  71.         else
  72.           iwconfig $IFACE key $CONF_WEP_AUTH "$CONF_WEP_KEY"
  73.         fi
  74.         iwconfig $IFACE essid "$CONF_ESSID"
  75.         ;;
  76.       WPA)
  77.         [ -n "$CONF_WPA_PASSPHRASE" ] || { echo "Error: WPA_PASSPHRASE not defined!" ; return; }
  78.         if [ ! -e $WPA_SUPPLICANT_PID ] || ! pgrep -x wpa_supplicant > /dev/null ; then
  79.           # generate the wpa_supplicant.conf
  80.           if [ "x$LAST_WPA_PASSPHRASE" != "x$CONF_ESSID.$CONF_WPA_PASSPHRASE" ] ; then
  81.                 WPA_PSK=`wpa_passphrase "$CONF_ESSID" "$CONF_WPA_PASSPHRASE"|grep -o 'psk=[0-9a-z]\{64\}'`
  82.                 LAST_WPA_PASSPHRASE="$CONF_ESSID.$CONF_WPA_PASSPHRASE"
  83.           fi
  84.           cat >$WPA_SUPPLICANT_CONF<<-EOF
  85.                 ctrl_interface=$WPA_CTRL_INTERFACE

  86.                 network={
  87.                         ssid="$CONF_ESSID"
  88.                         scan_ssid=1
  89.                         proto=WPA RSN
  90.                         key_mgmt=WPA-PSK
  91.                         pairwise=CCMP TKIP
  92.                         group=CCMP TKIP
  93.                         $WPA_PSK
  94.                 }
  95.                 EOF
  96.           wpa_supplicant -B -Dwext -i$IFACE -c$WPA_SUPPLICANT_CONF -P$WPA_SUPPLICANT_PID
  97.         fi
  98.         ;;
  99.     esac
  100.   fi
  101. }

  102. trap 'reload' HUP
  103. trap 'quit' INT QUIT TERM
  104. trap 'cleanup' EXIT
  105. echo $$ > $PIDFILE

  106. # wait until the kernel structure is ready. FIXME: Is this really necessary?
  107. until [ -d $SYSFS ] ; do sleep 1; done

  108. while [ -d $SYSFS ] ; do
  109.   carrier=`cat $SYSFS/carrier 2>/dev/null`
  110.   if [ "x$carrier" != "x1" ] ; then
  111.     # reload configuration
  112.     read_conf
  113.     setup_wifi
  114.   fi
  115.   sleep 10
  116. done

  117. /usr/local/etc/wifi.conf - WEP实例
  118. ESSID=MYESSID
  119. # wireless security: NONE, WEP or WPA
  120. SECURITY=WEP
  121. # WEP security mode: open or restricted
  122. WEP_AUTH=restricted
  123. WEP_KEYINDEX=1
  124. WEP_KEY="06af3154902d96506b3cbe6bae"
  125. /usr/local/etc/wifi.conf - WPA实例
  126. ESSID=MYESSID
  127. # wireless security: NONE, WEP or WPA
  128. SECURITY=WPA
  129. WPA_PASSPHRASE="testtest"


阅读(3220) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~