Skip to content

Commit

Permalink
udev: Add udev_device_get_properties_list_entry
Browse files Browse the repository at this point in the history
This CL exposed  udev_device_get_properties_list_entry()[1] in udev.h
to be used in a follow-up CL that will cache a device event's properties.

[1] https://manpages.debian.org/stretch/libudev-dev/udev_device_get_properties_list_entry.3.en.html

      + Printed all device event properties during an incoming event and
        compared them to `udevadm monitor --property`s output.

(cherry picked from commit 84f0d16)

Bug: b:232845611
Test: device_unittests --single-process-tests --gtest_filter="UdevTest.*"
Change-Id: I08d8ddf976fa53b1b8dd4d80120b359d7629bfc6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3680402
Reviewed-by: Thomas Anderson <thomasanderson@chromium.org>
Reviewed-by: Reilly Grant <reillyg@chromium.org>
Commit-Queue: Gil Dekel <gildekel@chromium.org>
Cr-Original-Commit-Position: refs/heads/main@{#1009678}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3689476
Auto-Submit: Gil Dekel <gildekel@chromium.org>
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Cr-Commit-Position: refs/branch-heads/5060@{#614}
Cr-Branched-From: b83393d-refs/heads/main@{#1002911}
  • Loading branch information
Gil Dekel authored and Chromium LUCI CQ committed Jun 6, 2022
1 parent 085a758 commit bf33032
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 12 deletions.
1 change: 1 addition & 0 deletions build/linux/libudev/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ libudev_functions = [
"udev_device_get_devtype",
"udev_device_get_parent",
"udev_device_get_parent_with_subsystem_devtype",
"udev_device_get_properties_list_entry",
"udev_device_get_property_value",
"udev_device_get_subsystem",
"udev_device_get_sysattr_value",
Expand Down
40 changes: 28 additions & 12 deletions device/udev_linux/fake_udev_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "device/udev_linux/fake_udev_loader.h"

#include <utility>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_file.h"
Expand All @@ -13,21 +15,37 @@ struct udev {
// empty
};

struct udev_list_entry {
explicit udev_list_entry(std::string name) : name(std::move(name)) {}
udev_list_entry(const udev_list_entry& other) = delete;
udev_list_entry& operator=(const udev_list_entry& other) = delete;

const std::string name;
udev_list_entry* next = nullptr;
};

struct udev_device {
udev_device(std::string name,
std::string syspath,
std::string subsystem,
absl::optional<std::string> devnode,
absl::optional<std::string> devtype,
std::map<std::string, std::string> sysattrs,
std::map<std::string, std::string> properties)
std::map<std::string, std::string> prop_map)
: name(std::move(name)),
syspath(std::move(syspath)),
subsystem(std::move(subsystem)),
devnode(std::move(devnode)),
devtype(std::move(devtype)),
sysattrs(std::move(sysattrs)),
properties(std::move(properties)) {}
sysattrs(std::move(sysattrs)) {
properties = std::move(prop_map);
for (auto const& pair : properties) {
auto prop = std::make_unique<udev_list_entry>(pair.first);
if (!udev_prop_list.empty())
udev_prop_list.back()->next = prop.get();
udev_prop_list.push_back(std::move(prop));
}
}
udev_device(const udev_device& other) = delete;
udev_device& operator=(const udev_device& other) = delete;

Expand All @@ -38,15 +56,7 @@ struct udev_device {
const absl::optional<std::string> devtype;
std::map<std::string, std::string> sysattrs;
std::map<std::string, std::string> properties;
};

struct udev_list_entry {
explicit udev_list_entry(std::string name) : name(std::move(name)) {}
udev_list_entry(const udev_list_entry& other) = delete;
udev_list_entry& operator=(const udev_list_entry& other) = delete;

const std::string name;
udev_list_entry* next = nullptr;
std::vector<std::unique_ptr<udev_list_entry>> udev_prop_list;
};

struct udev_enumerate {
Expand Down Expand Up @@ -160,6 +170,12 @@ udev_device* FakeUdevLoader::udev_device_get_parent_with_subsystem_devtype(
return nullptr;
}

udev_list_entry* FakeUdevLoader::udev_device_get_properties_list_entry(
struct udev_device* device) {
DCHECK(device);
return device->udev_prop_list.front().get();
}

const char* FakeUdevLoader::udev_device_get_property_value(udev_device* device,
const char* key) {
DCHECK(device && key);
Expand Down
2 changes: 2 additions & 0 deletions device/udev_linux/fake_udev_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class FakeUdevLoader : public device::UdevLoader {
udev_device* device,
const char* subsystem,
const char* devtype) override;
udev_list_entry* udev_device_get_properties_list_entry(
struct udev_device* device) override;
const char* udev_device_get_property_value(udev_device* device,
const char* key) override;
const char* udev_device_get_subsystem(udev_device* device) override;
Expand Down
5 changes: 5 additions & 0 deletions device/udev_linux/udev.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ udev_device* udev_device_get_parent_with_subsystem_devtype(
udev_device, subsystem, devtype);
}

udev_list_entry* udev_device_get_properties_list_entry(
struct udev_device* udev_device) {
return UdevLoader::Get()->udev_device_get_properties_list_entry(udev_device);
}

const char* udev_device_get_property_value(udev_device* udev_device,
const char* key) {
return UdevLoader::Get()->udev_device_get_property_value(udev_device, key);
Expand Down
2 changes: 2 additions & 0 deletions device/udev_linux/udev.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ udev_device* udev_device_get_parent_with_subsystem_devtype(
udev_device* udev_device,
const char* subsystem,
const char* devtype);
udev_list_entry* udev_device_get_properties_list_entry(
struct udev_device* udev_device);
const char* udev_device_get_property_value(udev_device* udev_device,
const char* key);
const char* udev_device_get_subsystem(udev_device* udev_device);
Expand Down
5 changes: 5 additions & 0 deletions device/udev_linux/udev0_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ udev_device* Udev0Loader::udev_device_get_parent_with_subsystem_devtype(
udev_device, subsystem, devtype);
}

udev_list_entry* Udev0Loader::udev_device_get_properties_list_entry(
struct udev_device* udev_device) {
return lib_loader_->udev_device_get_properties_list_entry(udev_device);
}

const char* Udev0Loader::udev_device_get_property_value(
udev_device* udev_device,
const char* key) {
Expand Down
2 changes: 2 additions & 0 deletions device/udev_linux/udev0_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class Udev0Loader : public UdevLoader {
udev_device* udev_device,
const char* subsystem,
const char* devtype) override;
udev_list_entry* udev_device_get_properties_list_entry(
struct udev_device* udev_device) override;
const char* udev_device_get_property_value(udev_device* udev_device,
const char* key) override;
const char* udev_device_get_subsystem(udev_device* udev_device) override;
Expand Down
5 changes: 5 additions & 0 deletions device/udev_linux/udev1_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ udev_device* Udev1Loader::udev_device_get_parent_with_subsystem_devtype(
udev_device, subsystem, devtype);
}

udev_list_entry* Udev1Loader::udev_device_get_properties_list_entry(
struct udev_device* udev_device) {
return lib_loader_->udev_device_get_properties_list_entry(udev_device);
}

const char* Udev1Loader::udev_device_get_property_value(
udev_device* udev_device,
const char* key) {
Expand Down
2 changes: 2 additions & 0 deletions device/udev_linux/udev1_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class Udev1Loader : public UdevLoader {
udev_device* udev_device,
const char* subsystem,
const char* devtype) override;
udev_list_entry* udev_device_get_properties_list_entry(
struct udev_device* udev_device) override;
const char* udev_device_get_property_value(udev_device* udev_device,
const char* key) override;
const char* udev_device_get_subsystem(udev_device* udev_device) override;
Expand Down
2 changes: 2 additions & 0 deletions device/udev_linux/udev_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class UdevLoader {
udev_device* udev_device,
const char* subsystem,
const char* devtype) = 0;
virtual udev_list_entry* udev_device_get_properties_list_entry(
struct udev_device* udev_device) = 0;
virtual const char* udev_device_get_property_value(udev_device* udev_device,
const char* key) = 0;
virtual const char* udev_device_get_subsystem(udev_device* udev_device) = 0;
Expand Down
42 changes: 42 additions & 0 deletions device/udev_linux/udev_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
// found in the LICENSE file.

#include "device/udev_linux/udev.h"
#include <vector>

#include "base/files/file_path.h"
#include "device/udev_linux/fake_udev_loader.h"
#include "device/udev_linux/udev_loader.h"

#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace device {
Expand Down Expand Up @@ -54,6 +56,46 @@ TEST(UdevTest, GetSysPropSimple) {
EXPECT_TRUE(attr_value.empty());
}

TEST(UdevTest, GetFullSysPropertiesList) {
testing::FakeUdevLoader fake_udev;
std::map<std::string, std::string> props = {
{"TAGS", ":powerd:"},
{"DEVNAME", "/dev/dri/card0"},
{"DEVTYPE", "drm_minor"},
{"HOTPLUG", "1"},
{"USEC_INITIALIZED", "1234567"},
{"MINOR", "0"},
{"ID_PATH_TAG", "pci-0000_00_02_0"},
{"PROPERTY", "456"},
{"DEVPATH", "/devices/pci0000:00/0000:00:02.0/drm/card0"},
{"ID_PATH", "pci-0000:00:02.0"},
{"SUBSYSTEM", "drm"},
{"CONNECTOR", "123"},
{"MAJOR", "226"},
{"SEQNUM", "1234"},
{"ACTION", "change"}};
udev_device* device = fake_udev.AddFakeDevice(
/*name=*/"/dev/dri/card0",
/*syspath=*/"/devices/pci0000:00/0000:00:02.0/drm/card0",
/*subsystem=*/"drm", /*devnode=*/absl::nullopt, /*devtype=*/absl::nullopt,
/*sysattrs=*/{}, std::move(props));

udev_list_entry* prop_list = udev_device_get_properties_list_entry(device);
udev_list_entry* entry;
std::vector<std::string> output_prop_list;
udev_list_entry_foreach(entry, prop_list) {
output_prop_list.emplace_back(udev_list_entry_get_name(entry));
}

// Expect the properties to be sorted when enumerated.
EXPECT_THAT(
output_prop_list,
testing::UnorderedElementsAre(
"ACTION", "CONNECTOR", "DEVNAME", "DEVPATH", "DEVTYPE", "HOTPLUG",
"ID_PATH", "ID_PATH_TAG", "MAJOR", "MINOR", "PROPERTY", "SEQNUM",
"SUBSYSTEM", "TAGS", "USEC_INITIALIZED"));
}

TEST(UdevTest, GetSysAttrNoAttrs) {
testing::FakeUdevLoader fake_udev;
udev_device* device = fake_udev.AddFakeDevice(
Expand Down

0 comments on commit bf33032

Please sign in to comment.