linux下 USB动态监测 hotplug事件监测



include/linux/netlink.h:22:#define NETLINK_KOBJECT_UEVENT       15      /* Kernel messages to userspace */
user_headers/include/linux/netlink.h:22:#define NETLINK_KOBJECT_UEVENT  15      /* Kernel messages to userspace */

http://www.cpplive.com/html/1362.html

内核预定义的协议类型有:

  1. #define NETLINK_ROUTE 0 //路由守护进程  
  2. #define NETLINK_W1 1 //1-wire 子系统  
  3. #define NETLINK_USERSOCK 2 //用户态套结字协议  
  4. #define NETLINK_FIREWALL 3 //防火墙  
  5. #define NETLINK_INET_DIAG 4 //套结字监视  
  6. #define NETLINK_NFLOG 5 //网络数据过滤日志  
  7. #define NETLINK_XFRM 6 //ipsec 安全策略  
  8. #define NETLINK_SELINUX 7 //SELinux 事件通知  
  9. #define NETLINK_ISCSI 8 //iSCSI网络存储子系统  
  10. #define NETLINK_AUDIT 9 //进程审计  
  11. #define NETLINK_FIB_LOOKUP 10 //转发信息表查询  
  12. #define NETLINK_CONNECTOR 11 //netlink连接器  
  13. #define NETLINK_NETFILTER 12 //网络数据过滤系统  
  14. #define NETLINK_IP6_FW 13 //IPv6 防火墙  
  15. #define NETLINK_DNRTMSG 14 //DECnet路由信息  
  16. #define NETLINK_KOBJECT_UEVENT 15 //内核事件向用户态通知  
  17. #define NETLINK_GENERIC 16 //通用netlink(用户自定义功能)  
http://blog.csdn.net/qwyang/article/details/6425555

总体规划:

使用netlink接口向内核注册hotplug事件通知,获取事件通知后进行相关动作如(Action==add)挂载或(action==remove)卸载,使用Socket IPC机制向需要响应USB热拔插事件的应用程序报告。

功能包括:
1、获取usb卷标
2、挂载、卸载usb
3、动态监测usb热拔插事件

#include <stdio.h>
#include <unistd.h>
#include <fcntl.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>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stddef.h>


int init_hotplug_sock(void)
{
    struct sockaddr_nl snl;
    const int buffersize = 16 * 1024;
    int retval;
int hotplug_sock;

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

memset(&snl, 0x00, sizeof(struct sockaddr_nl));
snl.nl_family = AF_NETLINK;
    snl.nl_pid = getpid();
    snl.nl_groups = 1;
    retval = bind(hotplug_sock, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl));
    if (retval < 0)
    {
close(hotplug_sock);
        printf("bind failed: %s", strerror(errno));
        hotplug_sock = -1;
        return -1;
    }
    return hotplug_sock;
}


int get_device_path(char *path, int *id)
{
int fd, ret, i;
char tmp[20];

sleep(4);
for(i=1; i<9; i++)
{
//只识别 sda*;  不识别sdb*
sprintf(tmp, "/dev/sda%d", i);
fd = access(tmp, F_OK);
if(fd == 0)
{
strcpy(path, tmp);
*id = i;
printf("; the path-->%s  %d\n", path, id);
break;
}
}
//int access(const char *pathname, int mode搜索); //mode填F_OK试试。
//返回0表示存在;返回-1表示不存在。
    return 0;
}


int add_dir(int id, char *path)
{
    int ret;
    mode_t dMode = S_IRWXU|S_IRWXG|S_IRWXO;


sprintf(path, "/mnt/usb%d",id);
ret = mkdir(path, dMode);
    if(ret == -1)
printf("make_dir error.\n");
else
printf("make_dir succes.\n");
return 0;
}


int rm_dir(char *mount_path)
{
    int ret;
ret = rmdir(mount_path);
    if(ret == -1)
        printf("rm_dir error %s.\n", mount_path);
else
printf("rm_dir succes %s.\n", mount_path);
return 0;
}


unsigned char flag_add=1;
unsigned char flag_remove=1;
int main()
{
    int hotplug_sock;
int sum=0, size;
int id=0;
    char buf[128]= {0};
char *str;
char device_path[30]= {0};
char mount_path[30]= {0};
char cmd[64]= {0};
    
//创建 socket; 
    hotplug_sock = init_hotplug_sock();
while(1)
{
sleep(1);

        size=recv(hotplug_sock,buf,sizeof(buf),0);
        while(sum < size)
        {
            str=buf+sum;
            sum+=strlen(str);
            buf[sum]='\n';
        }
        buf[sum]=0;

//printf("buf = %s.\n", buf);
if(flag_add == 1)
{
if(strstr(buf,"ACTION=add") || strstr(buf,"add")) // 字符串搜索
//if(strstr(buf,"add")) // 字符串搜索
{
flag_add=0;
flag_remove=1;
printf("ACTION=add\n");
if(get_device_path(device_path, &id)==0)
{
printf("device_path = %s\n", device_path);
printf("id = %d\n", id);
add_dir(id, mount_path);

sprintf(cmd,"mount %s %s", device_path, mount_path);
system(cmd);
printf("cmd=%s\n",cmd);
}
}
}


//printf("buf = %s.\n", buf);
//if(strstr(buf,"ACTION=remove"))
if(flag_remove == 1)
{
if(strstr(buf,"remove"))
{
flag_add=1;
flag_remove=0;
printf("ACTION=remove\n");
//if(get_device_path(device_path, &id)==0)
//{
sprintf(cmd,"umount -f %s", mount_path);
system(cmd);
printf("cmd=%s\n",cmd);
rm_dir(mount_path);
//}
}
}
memset(buf, 0, sizeof(buf));
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值