免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5693 | 回复: 7

怎么识别U盘的拔插? [复制链接]

论坛徽章:
0
发表于 2009-12-26 10:15 |显示全部楼层
5可用积分
另外,当U盘插上之后,我怎么读取U盘的内容?

论坛徽章:
0
发表于 2009-12-26 12:05 |显示全部楼层
设备热插拔事件可以设置一个回调命令的吧

U盘好像总是在/dev/disk/下有个特殊名称的,开头好像一定是U什么什么的

论坛徽章:
0
发表于 2009-12-26 14:36 |显示全部楼层
能不能说的明白点?

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
发表于 2009-12-26 17:03 |显示全部楼层
mount

论坛徽章:
0
发表于 2009-12-26 19:25 |显示全部楼层
1、插入u盘到计算机,如果目前只插入了一个u盘而且你的硬盘不是scsi的硬盘接口的话,那它的硬件名称为:sda1。

2、在mnt目录下先建立一个usb的目录(如:[root@localhost root]# mkdir /mnt/usb)

3、挂载U盘:mount -t vfat /dev/sda1 /mnt/usb

4、卸载U盘:umount /mnt/usb

5、删除usb目录:rm -rf /mnt/usb

论坛徽章:
0
发表于 2009-12-27 12:52 |显示全部楼层
不知道你是要命令还是要程序   
命令:udevmonitor就可以了


  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include <sys/socket.h>
  7. #include <linux/netlink.h>

  8. #define UEVENT_MSG_LEN 4096
  9. struct luther_gliethttp {
  10.     const char *action;
  11.     const char *path;
  12.     const char *subsystem;
  13.     const char *firmware;
  14.     int major;
  15.     int minor;
  16. };
  17. static int open_luther_gliethttp_socket(void);
  18. static void parse_event(const char *msg, struct luther_gliethttp *luther_gliethttp);

  19. int main(int argc, char* argv[])
  20. {
  21.     int device_fd = -1;
  22.     char msg[UEVENT_MSG_LEN+2];
  23.     int n;
  24.    
  25.     device_fd = open_luther_gliethttp_socket();
  26.     printf("device_fd = %d\n", device_fd);

  27.     do {
  28.         while((n = recv(device_fd, msg, UEVENT_MSG_LEN, 0)) > 0) {
  29.             struct luther_gliethttp luther_gliethttp;

  30.             if(n == UEVENT_MSG_LEN) /* overflow -- discard */
  31.                 continue;

  32.             msg[n] = '\0';
  33.             msg[n+1] = '\0';

  34.             parse_event(msg, &luther_gliethttp);
  35.         }
  36.     } while(1);
  37. }

  38. static int open_luther_gliethttp_socket(void)
  39. {
  40.     struct sockaddr_nl addr;
  41.     int sz = 64*1024;
  42.     int s;

  43.     memset(&addr, 0, sizeof(addr));
  44.     addr.nl_family = AF_NETLINK;
  45.     addr.nl_pid = getpid();
  46.     addr.nl_groups = 0xffffffff;

  47.     s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
  48.     if (s < 0)
  49.         return -1;

  50.     setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz));

  51.     if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  52.         close(s);
  53.         return -1;
  54.     }

  55.     return s;
  56. }

  57. static void parse_event(const char *msg, struct luther_gliethttp *luther_gliethttp)
  58. {
  59.     luther_gliethttp->action = "";
  60.     luther_gliethttp->path = "";
  61.     luther_gliethttp->subsystem = "";
  62.     luther_gliethttp->firmware = "";
  63.     luther_gliethttp->major = -1;
  64.     luther_gliethttp->minor = -1;

  65.     /* currently ignoring SEQNUM */
  66.     printf("========================================================\n");
  67.     while (*msg) {

  68.         printf("%s\n", msg);

  69.         if (!strncmp(msg, "ACTION=", 7)) {
  70.             msg += 7;
  71.             luther_gliethttp->action = msg;
  72.         } else if (!strncmp(msg, "DEVPATH=", 8)) {
  73.             msg += 8;
  74.             luther_gliethttp->path = msg;
  75.         } else if (!strncmp(msg, "SUBSYSTEM=", 10)) {
  76.             msg += 10;
  77.             luther_gliethttp->subsystem = msg;
  78.         } else if (!strncmp(msg, "FIRMWARE=", 9)) {
  79.             msg += 9;
  80.             luther_gliethttp->firmware = msg;
  81.         } else if (!strncmp(msg, "MAJOR=", 6)) {
  82.             msg += 6;
  83.             luther_gliethttp->major = atoi(msg);
  84.         } else if (!strncmp(msg, "MINOR=", 6)) {
  85.             msg += 6;
  86.             luther_gliethttp->minor = atoi(msg);
  87.         }

  88.         /* advance to after the next \0 */
  89.         while(*msg++)
  90.             ;
  91.     }

  92.     printf("event { '%s', '%s', '%s', '%s', %d, %d }\n",
  93.                     luther_gliethttp->action, luther_gliethttp->path, luther_gliethttp->subsystem,
  94.                     luther_gliethttp->firmware, luther_gliethttp->major, luther_gliethttp->minor);
  95. }
复制代码


上面那个方法在我的电脑上都已经帮你test过了

论坛徽章:
0
发表于 2013-10-29 20:39 |显示全部楼层
学习了解!谢谢!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP