yulutong 发表于 2012-11-15 08:45:45

linux 应用程序如何得到U盘插入信息

用hotplug已经完成自动挂载。
那么应用程序如何知道内核有U盘拔插信息,用什么方法通讯好。

lkm_unication 发表于 2012-11-15 08:49:56

使用netlink

yulutong 发表于 2012-11-15 09:43:27

网上有写用netlink ,是GCC编译的,用交叉编译的话会报错。楼上是否有相关例子供参考。
hot.c: In function `init_hotplug_sock':
hot.c:45: 错误: `NETLINK_KOBJECT_UEVENT' undeclared (first use in this function)
hot.c:45: 错误: (Each undeclared identifier is reported only once
hot.c:45: 错误: for each function it appears in.)
hot.c:59: 错误: `SO_RCVBUFFORCE' undeclared (first

yulutong 发表于 2012-11-15 10:30:38

网上找到对应的宏定义解决能捕捉的U盘的热拔插了
#define NETLINK_ROUTE          0       /* Routing/device hook                        */
#define NETLINK_W1             1       /* 1-wire subsystem                           */
#define NETLINK_USERSOCK       2       /* Reserved for user mode socket protocols      */
#define NETLINK_FIREWALL       3       /* Firewalling hook                           */
#define NETLINK_INET_DIAG      4       /* INET socket monitoring                     */
#define NETLINK_NFLOG          5       /* netfilter/iptables ULOG */
#define NETLINK_XFRM         6       /* ipsec */
#define NETLINK_SELINUX      7       /* SELinux event notifications */
#define NETLINK_ISCSI          8       /* Open-iSCSI */
#define NETLINK_AUDIT          9       /* auditing */
#define NETLINK_FIB_LOOKUP   10
#define NETLINK_CONNECTOR      11
#define NETLINK_NETFILTER      12      /* netfilter subsystem */
#define NETLINK_IP6_FW         13
#define NETLINK_DNRTMSG      14      /* DECnet routing messages */
#define NETLINK_KOBJECT_UEVENT 15      /* Kernel messages to userspace */
#define NETLINK_GENERIC      16


#define SO_DEBUG      1
#define SO_REUSEADDR    2
#define SO_RCVBUF       8
#define SO_SNDBUFFORCE32
#define SO_RCVBUFFORCE33
#define SO_KEEPALIVE    9

lkm_unication 发表于 2012-11-15 12:42:58

netlink 是kernel提供接口的, 如果版本不同, 有些feature是没有的.

luck851 发表于 2012-11-15 12:56:01

直接查找/dev下的节点不行吗?

chinmel 发表于 2012-11-15 13:13:30

同意LS,如果是定时查看dev目录下的设备 也许是个办法

yulutong 发表于 2012-11-15 13:31:26

lkm_unication 发表于 2012-11-15 12:42 static/image/common/back.gif
netlink 是kernel提供接口的, 如果版本不同, 有些feature是没有的.

内核版本不会改变了。
请问:

我的主函数能执行到 printf("yes\n"); 为什么会挂载不成功? result 返回是-1
while (1)
    {
      char bufusb = { 0 };
      recv (hotplug_sock, &bufusb, sizeof (bufusb), 0);
      printf ("(debug)   %s\n", bufusb);
   if(!strcmp("add@/block/sda/sda1",&bufusb)) //相等返回0             
                {
                      printf("yes\n");       
                 int result = mount("/dev/sda1", "/mnt/usb", "vfat",MS_MGC_VAL, 0);
                   printf("result=%d\n",result);
              }
   



如果在命令窗口查是没问题,应该程序中该怎么写来着。现在是串口终端会写了,应用函数不会。

lkm_unication 发表于 2012-11-15 19:03:45

程序运行的权限?

wgyoume 发表于 2012-11-19 15:57:54

使用udev试试

xi_liang 发表于 2012-11-19 16:21:22

硬件来说,有个插入检测引脚的,一般是接到ARM的中断,程序里可以通过这个判断。
软件的热插拔功能,嵌入式linux有个简化版的udev,即mdev可以实现

yulutong 发表于 2012-11-20 20:23:57

xi_liang 发表于 2012-11-19 16:21 static/image/common/back.gif
硬件来说,有个插入检测引脚的,一般是接到ARM的中断,程序里可以通过这个判断。
软件的热插拔功能,嵌入式 ...

我的意思是内核怎么给应用程序消息。现在开一个线程方法解决
页: [1]
查看完整版本: linux 应用程序如何得到U盘插入信息