linux下 监控USB插入事件

时间:2022-04-14 15:18:55

linux下 监控USB

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/un.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/types.h>
#include <linux/netlink.h>
#include <errno.h>

static int init_hotplug_sock(void)
{

struct sockaddr_nl snl;
const int buffersize = 16 * 1024 * 1024;
int retval;

memset(&snl, 0x00, sizeof(struct sockaddr_nl));
snl.nl_family = AF_NETLINK;
snl.nl_pid = getpid();
snl.nl_groups = 1;

int hotplug_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
if (hotplug_sock == -1)
{
printf("error getting socket: %s", strerror(errno));
return -1;
}

/* set receive buffersize */

setsockopt(hotplug_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
retval = bind(hotplug_sock, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl));
if (retval < 0) {
printf("bind failed: %s", strerror(errno));
close(hotplug_sock);
hotplug_sock = -1;
return -1;
}

return hotplug_sock;

}

#define UEVENT_BUFFER_SIZE 2048

int main(int argc, char* argv[])
{

int hotplug_sock = init_hotplug_sock();
while(1)
{
char buf[UEVENT_BUFFER_SIZE*2] = {0};
recv(hotplug_sock, &buf, sizeof(buf), 0);
printf("SnoTest---------------%s\n", buf);
}

return 0;
}

插入事件  

2009-10-09 10:08:20|  分类: linux applicatio|举报|字号 订阅

转自网友

目前找到3种办法可以获得hotplug事件。

一种是hotplug脚本。hotplug事件发生时,内核会调用脚本/sbin/hotplug。可以在这个脚本中加入自己的操作。不过2.6内核做了很大的改变。不再支持hotplug脚本。

还有就是netlink。这是一个特殊的socket,可以接受来自内核的消息。

下面是一个netlink例程。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/un.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/types.h>
#include <linux/netlink.h>
#include <errno.h>

static int init_hotplug_sock(void)
{

    struct sockaddr_nl snl;
    const int buffersize = 16 * 1024 * 1024;
    int retval;

    memset(&snl, 0x00, sizeof(struct sockaddr_nl));
    snl.nl_family = AF_NETLINK;
    snl.nl_pid = getpid();
    snl.nl_groups = 1;

    int hotplug_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
    if (hotplug_sock == -1) 
    {
        printf("error getting socket: %s", strerror(errno));
        return -1;
    }

    /* set receive buffersize */

    setsockopt(hotplug_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
    retval = bind(hotplug_sock, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl));
    if (retval < 0) {
        printf("bind failed: %s", strerror(errno));
        close(hotplug_sock);
        hotplug_sock = -1;
        return -1;
    }

    return hotplug_sock;

}

#define UEVENT_BUFFER_SIZE      2048

int main(int argc, char* argv[])
{

         int hotplug_sock       = init_hotplug_sock();
         while(1)
         {
                   char buf[UEVENT_BUFFER_SIZE*2] = {0};
                   recv(hotplug_sock, &buf, sizeof(buf), 0);  
                   printf("%s\n", buf);
         }

         return 0;
}

 

2.6内核使用了一个用户态的程序udev来挂载/dev。这使得接受和处理来自内核的信息变得极为方便。

udev采用规则匹配的方式,来处理设备的挂载,同时触发其他处理。比如下列规则就可以捕捉来自U盘插入的事件。并调用对应的脚本。
BUS=="usb", KERNEL=="sd*", RUN+="/root/p.sh"

/root/p.sh 的内容 
#!/bin/bash
set >> /root/usb.txt
echo -e "END****" >> /root/usb.txt

这样就可以把发生的事件的参数打印到文本文件中。以下是我的U盘插拔时的信息。
ACTION=add
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()http://wubinjie.spaces.live.com/blog/cns!2055299901bf6246!234.entry
BASH_LINENO=([0]="0")
BASH_SOURCE=([0]="/root/p.sh")
BASH_VERSINFO=([0]="3" [1]="1" [2]="17" [3]="1" [4]="release" [5]="i686-redhat-linux-gnu")
BASH_VERSION='3.1.17(1)-release'
DEVLINKS='/dev/disk/by-id/usb-USB_2.0_6025 /dev/disk/by-path/pci-0000:00:1d.2-usb-0:1:1.0-scsi-0:0:0:0'
DEVNAME=/dev/sda
DEVPATH=/block/sda
DIRSTACK=()
EUID=0
GROUPS=()
HOSTNAME=linux230
HOSTTYPE=i686
ID_BUS=usb
ID_MODEL=6025
ID_PATH=pci-0000:00:1d.2-usb-0:1:1.0-scsi-0:0:0:0
ID_REVISION=2.00
ID_SERIAL=USB_2.0_6025
ID_TYPE=disk
ID_VENDOR=USB_2.0
IFS=$' \t\n'
MACHTYPE=i686-redhat-linux-gnu
MAJOR=8
MINOR=0
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/local/bin:/bin:/usr/bin
PHYSDEVBUS=scsi
PHYSDEVDRIVER=sd
PHYSDEVPATH=/devices/pci0000:00/0000:00:1d.2/usb3/3-1/3-1:1.0/host5/target5:0:0/5:0:0:0
PPID=3549
PS4='+ '
PWD=/
SEQNUM=814
SHELL=/bin/bash
SHELLOPTS=braceexpand:hashall:interactive-comments
SHLVL=1
SUBSYSTEM=block
TERM=dumb
UDEVD_EVENT=1
UDEV_LOG=3
UID=0
_=/bin/bash
END****
ACTION=add
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=([0]="0")
BASH_SOURCE=([0]="/root/p.sh")
BASH_VERSINFO=([0]="3" [1]="1" [2]="17" [3]="1" [4]="release" [5]="i686-redhat-linux-gnu")
BASH_VERSION='3.1.17(1)-release'
DEVLINKS='/dev/disk/by-id/usb-USB_2.0_6025-part1 /dev/disk/by-path/pci-0000:00:1d.2-usb-0:1:1.0-scsi-0:0:0:0-part1'
DEVNAME=/dev/sda1
DEVPATH=/block/sda/sda1
DIRSTACK=()
EUID=0
GROUPS=()
HOSTNAME=linux230
HOSTTYPE=i686
ID_BUS=usb
ID_FS_LABEL=
ID_FS_LABEL_SAFE=
ID_FS_TYPE=vfat
ID_FS_USAGE=filesystem
ID_FS_UUID=
ID_FS_VERSION=FAT32
ID_MODEL=6025
ID_PATH=pci-0000:00:1d.2-usb-0:1:1.0-scsi-0:0:0:0
ID_REVISION=2.00
ID_SERIAL=USB_2.0_6025
ID_TYPE=disk
ID_VENDOR=USB_2.0
IFS=$' \t\n'
MACHTYPE=i686-redhat-linux-gnu
MAJOR=8
MINOR=1
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/local/bin:/bin:/usr/bin
PHYSDEVBUS=scsi
PHYSDEVDRIVER=sd
PHYSDEVPATH=/devices/pci0000:00/0000:00:1d.2/usb3/3-1/3-1:1.0/host5/target5:0:0/5:0:0:0
PPID=3576
PS4='+ '
PWD=/
SEQNUM=815
SHELL=/bin/bash
SHELLOPTS=braceexpand:hashall:interactive-comments
SHLVL=1
SUBSYSTEM=block
TERM=dumb
UDEVD_EVENT=1
UDEV_LOG=3
UID=0
_=/bin/bash
END****

 

可以看到挂载了2个设备 /dev/sda  /dev/sda1 其中一个分区,一个是设备。