如何在C中检测到Linux是否插入USB

时间:2012-03-20 05:43:44

标签: c linux usb

我是C编程的新手 在这里,我想检测是否在Linux中插入USB 我想在我的嵌入式linux设备上检测到这些东西。哪个有定制的linux。 所以我想检测我必须安装此块的USB块。比如sda,sdb or sdc。 我想在Linux的C编程中使用这个东西。

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

所以任何人都可以建议我如何插入usb插入或不插入如何插入比我怎么知道这个地址

2 个答案:

答案 0 :(得分:2)

设置hotplug事件处理程序:当块设备出现或消失时,您将收到一个hotplug块事件,您可以使用该事件来挂载/卸载并运行处理程序脚本和应用程序。

答案 1 :(得分: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;

}