首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在C++中检测linux的USB是否已插入

问如何在C++中检测linux的USB是否已插入
EN

Stack Overflow用户
提问于 2012-03-20 13:43:44
回答 2查看 8.2K关注 0票数 2

我是一个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是否插入?如果插入,我如何知道usb的地址

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-20 13:50:22

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

票数 2
EN

Stack Overflow用户

发布于 2012-03-20 19:09:21

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

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

代码语言:javascript
复制
/*
 * 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;

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9781873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档