首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >udev scan udisk

udev scan udisk

原创
作者头像
tankaro
修改2025-01-14 09:27:12
修改2025-01-14 09:27:12
1980
举报
代码语言:txt
复制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>
#include <sys/epoll.h>

#include <libudev.h>

//#define HAVE_DEBUG_SCAN_OTHERS_TYPE
#define MAIN_VERSION                0
#define MINIOR_VERSION              0
#define REVISION_VERSION            0
#define CONSTRUCTION_VERSION        1
#define VERSION_STR(s)              #s
#define SC_VERSION(a, b, c, d)      ("scan_udisk " VERSION_STR(a) "." VERSION_STR(b) "." VERSION_STR(c) "." VERSION_STR(d))
const char g_libVersion[] = SC_VERSION(MAIN_VERSION, MINIOR_VERSION, REVISION_VERSION, CONSTRUCTION_VERSION);

#define ELF_NUM_VERSION        		((MAIN_VERSION<<24) | (MINIOR_VERSION<<16) | (REVISION_VERSION<<8) | (CONSTRUCTION_VERSION<<0))

#define MAX_BUFFER_SIZE             (256)
#define ARRAY_SIZE(x)               (sizeof(x) / sizeof((x)[0]))

static int get_elf_num_version(void);
static char *get_elf_str_version(void);
static int scan_udisk(void);
#ifdef HAVE_DEBUG_SCAN_OTHERS_TYPE
static void get_others_type(struct udev_device *device);
#endif

int main(int argc, char *argv[])
{
    printf("[%s +%d %s] get_elf_str_version()=%s\n", __FILE__, __LINE__, __func__, get_elf_str_version());
    printf("[%s +%d %s] get_elf_num_version()=0x%08x\n", __FILE__, __LINE__, __func__, get_elf_num_version());
    scan_udisk();

    return 0;
}

static int get_elf_num_version(void)
{
    //printf("[%s +%d %s] ELF_NUM_VERSION=0x%x\n", __FILE__, __LINE__, __func__, ELF_NUM_VERSION);
	return ELF_NUM_VERSION;
}
static char *get_elf_str_version(void)
{
    //printf("[%s +%d %s] (char *)g_libVersion=%s\n", __FILE__, __LINE__, __func__, (char *)g_libVersion);
    return (char *)g_libVersion;
}

static int scan_udisk(void)
{
    int ret = -1;
    char usb_device_type_name[MAX_BUFFER_SIZE] = "block";

    struct udev *udev;
    struct udev_enumerate *enumerate;
    struct udev_list_entry *devices, *entry;

    // 创建udev上下文
    udev = udev_new();
    if (!udev) {
        printf("[%s +%d %s] failed to udev_new\n", __FILE__, __LINE__, __func__);
        ret = -1;
        goto err_udev_new;
    }
    // 创建udev枚举器
    enumerate = udev_enumerate_new(udev);
    if (!enumerate) {
        printf("[%s +%d %s] failed to udev_enumerate_new\n", __FILE__, __LINE__, __func__);
        ret = -1;
        goto err_udev_enumerate_new;
    }

    //printf("[%s +%d %s] usb_device_type_name=%s\n", __FILE__, __LINE__, __func__, usb_device_type_name);
    // 设置枚举器过滤器为"block"子系统
    if (0 != udev_enumerate_add_match_subsystem(enumerate, usb_device_type_name)) {
        printf("[%s +%d %s] failed to udev_enumerate_add_match_subsystem\n", __FILE__, __LINE__, __func__);
        ret = -1;
        goto err_udev_enumerate_add_match_subsystem;
    }

    if (0 != udev_enumerate_scan_devices(enumerate)) {
        printf("[%s +%d %s] failed to udev_enumerate_scan_devices\n", __FILE__, __LINE__, __func__);
        ret = -1;
        goto err_udev_enumerate_scan_devices;
    }


    // 获取设备列表
    devices = udev_enumerate_get_list_entry(enumerate);
    if (!devices) {
        printf("[%s +%d %s] failed to udev_enumerate_get_list_entry\n", __FILE__, __LINE__, __func__);
        ret = -1;
        goto err_udev_enumerate_get_list_entry;
    }
    // 遍历设备列表
    udev_list_entry_foreach(entry, devices) {
        const char *syspath = udev_list_entry_get_name(entry);
        struct udev_device *device = udev_device_new_from_syspath(udev, syspath);
        if (!device)
        {
            printf("[%s +%d %s] failed to udev_device_new_from_syspath\n", __FILE__, __LINE__, __func__);
        }
        char *tmp = (char *)strstr(syspath, "usb");
        if (tmp != NULL)
        {
        }
        else
        {
            continue;
        }
        // 通过父设备的父设备来判断是否为USB设备
        struct udev_device *parent = udev_device_get_parent_with_subsystem_devtype(device, "usb", "usb_device");
        if (parent) {
            // 获取设备的类别信息
            const char *devclass = udev_device_get_property_value(device, "DEVTYPE");

            // 获取设备的挂载路径
            const char *devnode = udev_device_get_devnode(device);

            if (devclass && devnode) {
                //printf("USB Device class: %s, Mount Path: %s\n", devclass, devnode);

                char *tmp = (char *)strstr(devclass, "partition");
                if (tmp != NULL)
                {
                    ret = 0;
                    printf("[%s +%d %s] USB Device class: (%s), Mount Path: (%s)\n", __FILE__, __LINE__, __func__, devclass, devnode);
                    #ifdef HAVE_DEBUG_SCAN_OTHERS_TYPE
                    get_others_type(device);
                    #endif
                }
                else
                {
                }
            }
            else
            {
                printf("[%s +%d %s] failed to udev_device_get_devnode\n", __FILE__, __LINE__, __func__);
            }
        }
        else
        {
            printf("[%s +%d %s] failed to udev_device_get_parent_with_subsystem_devtype\n", __FILE__, __LINE__, __func__);
        }

        udev_device_unref(device);
    }

err_udev_enumerate_get_list_entry:
err_udev_enumerate_scan_devices:
err_udev_enumerate_add_match_subsystem:
    // 释放资源
    udev_enumerate_unref(enumerate);
err_udev_enumerate_new:
    udev_unref(udev);
err_udev_new:

    return ret;
}

#ifdef HAVE_DEBUG_SCAN_OTHERS_TYPE
static void get_others_type(struct udev_device *device)
{
    const char *devclass = NULL;
    static const char *blacklist[] = {
        "ACTION",
        "SUBSYSTEM",
        "DEVTYPE",
        "MAJOR",
        "MINOR",
        "DRIVER",
        "IFINDEX",
        "DEVNAME",
        "DEVLINKS",
        "DEVPATH",
        "TAGS",
        "ID_FS_TYPE",
    };
    //printf("[%s +%d %s] ARRAY_SIZE(blacklist)=%ld\n", __FILE__, __LINE__, __func__, ARRAY_SIZE(blacklist));

    for(int i = 0; i < (int)ARRAY_SIZE(blacklist); i++)
    {
        //printf("[%s +%d %s] blacklist[i=%d] = %s\n", __FILE__, __LINE__, __func__, i, blacklist[i]);
        devclass = udev_device_get_property_value(device, blacklist[i]);
        if(NULL != devclass)
        {
            printf("[%s +%d %s] devclass=%s blacklist[i=%d] = %s\n", __FILE__, __LINE__, __func__, devclass, i, blacklist[i]);

        }
    }
}
#endif

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档