2012-03-20 70 views
2

我是C编程新手 这里我想检测USB是否插入在Linux中 我想在我的嵌入式linux设备上检测到这个东西。其中有定制的Linux。 所以我想检测的USB块,我必须安装此块。如sda,sdb or sdc。 我想在C编程为linux的这件事情。我该如何检测USB插入或不在linux中C

在这里,我对USB设备的路径/sys/bus/usb/devices 我可以看到USB信息可以在这里/sys/bus/usb/devices/1-x ,所以我想在那里此USB插入像sda,sdb or sdc获取块的地址。 所以现在取得这个地址名称后,我必须在特定路径上安装USB。

所以任何人可以请建议我如何可以检测USB插入与否,如果插入比我怎么能知道这

+0

您的嵌入式设备是否运行hal或UDisks? – 2012-03-20 05:46:11

+0

我不能让你。你什么意思?可能是Udisks – user1089679 2012-03-20 05:49:10

+0

请参阅:http://stackoverflow.com/questions/20084740/udev-run-program-on-usb-flash-drive-insert。您可以使用C/C++创建代码,以便在插入USB时执行代码(而不是通过代码监视USB)。 – lepe 2015-12-24 03:12:20

回答

2

建立一个热插拔事件处理程序地址:您将获得一个热插拔块事件当块设备出现或消失,您可以使用它来挂载/卸载并运行您的处理程序脚本和应用程序。

+0

对于那第一我需要解析一些路径,需要知道我的地址比我会装入。 – user1089679 2012-03-20 06:20:01

+0

我如何设定? – user1089679 2012-03-20 07:21:01

2

Udisk可以满足您的需求,但您应该对D-Bus有一些了解。 而d-bus守护进程应该首先在你的嵌入式linux中运行。

下面我写的是在我知道D-Bus之前检测udisk程序。这很糟糕,但可能会给你提示。 关键路径:“/ dev/disk/by-uuid” 关键文件:“/ etc/mtab” 这将显示如何通过比较它们来挂载udisk。

/* 
* This program demonstrates method 
* detect U disk and mount it 
* if main exit umount it 
* 
* note: run as root 
*/ 

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <unistd.h> 
#include <fcntl.h> 
#include <sys/types.h> 
#include <dirent.h> 
#include <errno.h> 

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

    dirpath = "/dev/disk/by-uuid"; 
    filename = "/etc/mtab"; 

    //step 1. open dir and file 
    DIR *dirp; 
    FILE *mtab; 

    //FILE *fopen(const char *path, const char *mode); 
    if((mtab = fopen(filename, "r")) == NULL){ 
     fprintf(stderr, "open %s failed\n", filename); 
     exit(1); 
    } 


    //DIR *opendir(const char *name); 
    if((dirp = opendir(dirpath)) == NULL){ 
     fprintf(stdout, "opendir %s failed\n", dirpath); 
     exit(1); 
    }else{ 
     fprintf(stdout, "opendir %s successfully\n", dirpath); 
    } 

    //step 1.5 show content of dirpath 
#if 0 
    struct dirent { 
     ino_t   d_ino;  /* inode number */ 
     off_t   d_off;  /* offset to the next dirent */ 
     unsigned short d_reclen; /* length of this record */ 
     unsigned char d_type;  /* type of file; not supported 
          by all file system types */ 
     char   d_name[256]; /* filename */ 
    }; 
#endif 

    struct dirent *direntp; 

    errno = 0; 
    //struct dirent *readdir(DIR *dirp); 
    while((direntp = readdir(dirp)) != NULL){ 
     fprintf(stdout, "%s\n", direntp->d_name); 

    } 
    if(errno != 0){ 
     perror("readdir failed"); 
     exit(1); 
    } 


    //void rewinddir(DIR *dirp); 
    rewinddir(dirp); 

    //step 1.6 get mounted device name 
    char mdev[64][255]; 
    int i; 
    int devnum; 

    i = 0; 
    //int fscanf(FILE *stream, const char *format, ...); 
    while(fscanf(mtab, "%s", mdev[i]) != EOF){ 
     //int getc(FILE *stream); 
     while(getc(mtab) != '\n') 
      ; 

     //int strncmp(const char *s1, const char *s2, size_t n); 
     if(strncmp(mdev[i], "/dev/sd", 7) == 0 && 
      strncmp(mdev[i], "/dev/sda", 8) != 0){ 
      fprintf(stdout, "mdev: %s\n", mdev[i]); 
      i++; 
     } 
    } 

    strncpy(mdev[i], "", 1); 

    //step 2. check content 
    char buf[255]; 
    char path[255]; 
    char cmd[255]; 
    char *p; 
    int flag;  /* if 0 not mount, if 1 mount */ 

    devnum = i; 

    errno = 0; 
    //struct dirent *readdir(DIR *dirp); 
    while((direntp = readdir(dirp)) != NULL){ 
     flag = 1; 

     if(direntp->d_name[0] == '.'){ /* remove . and .. */ 
      continue; 
     } 
     //int snprintf(char *str, size_t size, const char *format, ...); 
     snprintf(path, sizeof(path) - 1, "%s/%s",dirpath, direntp->d_name); 

     //ssize_t readlink(const char *path, char *buf, size_t bufsiz); 
     if(readlink(path, buf, sizeof(buf) - 1) < 0){ 
      perror("readlink failed"); 
      exit(1); 
     } 

     p = strrchr(buf, '/'); 
     if(p != NULL && strncmp(p, "/sda", 4) != 0){ /* remove sda* */ 
      //char *strchr(const char *s, int c); 
      snprintf(path, sizeof(path) - 1, "/dev%s", p); 

      fprintf(stdout, "step 2. %s, devnum = %d\n", path, devnum); 

      for(i = 0; i < devnum; i++){ /* check mount */ 
       if(strcmp(mdev[i], path) == 0){ 
        flag = 0; 
        break; 
       } 
      } 

      //step 3. mount umounted usb 
      if(flag == 1){ 
       fprintf(stdout, "need mount %s\n", path); 

       //int snprintf(char *str, size_t size, const char *format, ...); 
       snprintf(cmd, sizeof(cmd) - 1, "sudo mount %s /mnt", path); 
       if(system(cmd) < 0){ 
        fprintf(stderr, "system() %s failed: %s\n", 
         path, strerror(errno)); 
       } 
      } 
     } 

    } 
    if(errno != 0){ 
     perror("readdir failed"); 
     exit(1); 
    } 


    sleep(10); 

    //step 4. umount usb 
    //int snprintf(char *str, size_t size, const char *format, ...); 
    snprintf(cmd, sizeof(cmd) - 1, "sudo umount /mnt"); 
    if(system(cmd) < 0){ 
     fprintf(stderr, "system() %s failed: %s\n", 
      path, strerror(errno)); 
    } 

    //step 5. close dir 
    //int closedir(DIR *dirp); 
    closedir(dirp); 
    fclose(mtab); 

    return 0; 

}