GNU/Linux systemd/sd-device 创建和过滤 sd_device_enumerator 时出现问题

问题描述 投票:0回答:2

前言:

我不是一个好的开发人员,我才刚刚开始,下面的代码是我正在构建的一个更大程序的一部分,只是暂时放置在主 cpp 文件中。我知道结构仍然需要改进,只是我想先让它运行起来。

问题:

我最近几天拼命寻找文档,但因为除了这个 [https://www.freedesktop.org/software/systemd/man/sd-device.html] 我什么也找不到,并且我已经尝试解决github上的源代码。

我之前曾使用

libudev
库来读取
BLOCK
设备并从中获取有效的值,但是在进一步了解此主题后,我了解到不再建议使用
libudev
并切换到
systemd/sd-device
.

下面的代码不会输出错误,但也不会运行循环,因为“sd_device_enumerator_get_device_next(enumerator)”始终返回一个

NULL
指针。即使经过很长时间的搜索,我也找不到
systemd/sd-device
的任何文档,甚至阅读 src 数据我也无法弄清楚。

这是我的尝试,这基本上应该运行所有

BLOCK
设备并读出某些值,为了测试我已经输入了
"ID_PART_TABLE_TYPE"

main.h

#ifndef main_h
#define main_h

#include <string>
#include <bitset>
#include <iostream>
#include <fstream>
#include <sstream>
#include <regex>
#include <vector>
#include <cstring>

#include <stdio.h>
#include <dirent.h>

#include <systemd/sd-device.h>

using std::cout;
using std::endl;
using std::string;
using std::to_string;
using std::cin;
using std::fstream;
using std::ifstream;
using std::ofstream;
using std::vector;

// File: classes/drives-info.cpp
/*
class drivesInfo{
public:

    vector<string> blockDevs;
    vector< vector<string> > blockDevsParts;
        
    int get_blockDevs();


};
*/



#endif

主.cpp

#include "main.h"


int test0() {
    int r;
    sd_device_enumerator *enumerator = NULL;
    sd_device *dev = NULL;

    // Initialize the Device-Enumerator
    r = sd_device_enumerator_new(&enumerator);
    if (r < 0) {
        printf("Error | Initialize the Device-Enumerator: %s\n", strerror(-r));
    }

    // Set Filter to BLOCK
    r = sd_device_enumerator_add_match_subsystem(enumerator, "block", 0);
    if (r < 0) {
        printf("Error | Set Filter to BLOCK: %s\n", strerror(-r));
    }
    
    // Loop to run through all BLOCK devices
    if (r >= 0) {
        while ((dev = sd_device_enumerator_get_device_next(enumerator)) != NULL) {
            const char *pt_type = NULL;
            r = sd_device_get_property_value(dev, "ID_PART_TABLE_TYPE", &pt_type);
            if (r >= 0) {
                printf("Device: %s\n");
                printf("ID_PART_TABLE_TYPE: %s\n", pt_type);
            }

            sd_device_unref(dev);
            dev = NULL;
            cout << "loop" << endl;
        }
    }

    if (r < 0) {
        printf("Error | at Loop to run through all BLOCK devices: %s\n", strerror(-r));
    }

    if (enumerator) {
        sd_device_enumerator_unref(enumerator);
    }
    if (dev) {
        sd_device_unref(dev);
    }

    return r < 0 ? 1 : 0;
}

int main() {

//     drivesInfo drives;
//     
//     drives.get_blockDevs();
// 
//     for (const string &entry : drives.blockDevs){
//         cout << entry << endl;
//     }

    
    test0();

    return 0;
}

c++ linux systemd block-device
2个回答
2
投票

您必须先致电

sd_device_enumerator_get_device_first

int test0() {
    sd_device_enumerator *enumerator = NULL;
    int r = sd_device_enumerator_new(&enumerator);
    if (r < 0) {
        printf("Error | Initialize the Device-Enumerator: %s\n", strerror(-r));
        return -1;
    }
    r = sd_device_enumerator_add_match_subsystem(enumerator, "block", 0);
    if (r < 0) {
        printf("Error | Set Filter to BLOCK: %s\n", strerror(-r));
        return -1;
    }
    sd_device *dev = NULL;
    for (dev = sd_device_enumerator_get_device_first(enumerator); dev != NULL;
         dev = sd_device_enumerator_get_device_next(enumerator)) {
        const char *pt_type = NULL;
        r = sd_device_get_property_value(dev, "ID_MODEL", &pt_type);
        if (r >= 0) {
            printf("%s\n", pt_type);
        }
        sd_device_unref(dev);
    }
    sd_device_enumerator_unref(enumerator);
    return r < 0 ? 1 : 0;
}

在我的电脑上输出:

$ g++ -fsanitize=address,undefined -lsystemd 1.cpp && ./a.out 
xHCI_Host_Controller
GK630_Gaming_Keyboard
GK630_Gaming_Keyboard
GK630_Gaming_Keyboard
GK630_Gaming_Keyboard
GK630_Gaming_Keyboard
GK630_Gaming_Keyboard
GK630_Gaming_Keyboard
GK630_Gaming_Keyboard
GK630_Gaming_Keyboard
GK630_Gaming_Keyboard
xHCI_Host_Controller
EHCI_Host_Controller
0024
CSR8510_A10
EHCI_Host_Controller
0024
UDisk
USB_Optical_Mouse
USB_Optical_Mouse
USB_Optical_Mouse
USB_Optical_Mouse
D3162-B1

您可能感兴趣 https://github.com/systemd/systemd/blob/91010bd6ccd363be9e09f22c60d45f5e27c1d2a8/src/libsystemd/sd-device/device-util.h#L52


0
投票

对 sd_device_enumerator_add_match_subsystem() 的调用需要是 sd_device_enumerator_add_match_subsystem(enumerator, "block", 1) (注意第三个参数)。键盘和鼠标不是块设备。参见来源:

https://github.com/systemd/systemd/blob/91010bd6ccd363be9e09f22c60d45f5e27c1d2a8/src/libsystemd/sd-device/device-enumerator.c#L126

© www.soinside.com 2019 - 2024. All rights reserved.