summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2022-08-27 19:45:56 +0200
committerYu Watanabe <watanabe.yu+github@gmail.com>2022-08-28 02:08:21 +0200
commitf79fdea6fda42e0de4857a31545558e7471da422 (patch)
tree7d7eb3d364070c4b6087bc1ce1b4e0287390f766 /src
parentdevnum-util: split-out device_path_make_inaccessible() (diff)
downloadsystemd-f79fdea6fda42e0de4857a31545558e7471da422.tar.xz
systemd-f79fdea6fda42e0de4857a31545558e7471da422.zip
sd-device: introduce devpath_from_devnum()
Diffstat (limited to 'src')
-rw-r--r--src/libsystemd/meson.build1
-rw-r--r--src/libsystemd/sd-device/device-private.h1
-rw-r--r--src/libsystemd/sd-device/device-util.c33
-rw-r--r--src/libsystemd/sd-device/device-util.h3
-rw-r--r--src/libsystemd/sd-device/sd-device.c2
-rw-r--r--src/libsystemd/sd-device/test-sd-device.c31
6 files changed, 70 insertions, 1 deletions
diff --git a/src/libsystemd/meson.build b/src/libsystemd/meson.build
index 7f10e87d6c..9238e32980 100644
--- a/src/libsystemd/meson.build
+++ b/src/libsystemd/meson.build
@@ -128,6 +128,7 @@ libsystemd_sources = files(
'sd-device/device-monitor.c',
'sd-device/device-private.c',
'sd-device/device-private.h',
+ 'sd-device/device-util.c',
'sd-device/device-util.h',
'sd-device/sd-device.c',
'sd-hwdb/hwdb-internal.h',
diff --git a/src/libsystemd/sd-device/device-private.h b/src/libsystemd/sd-device/device-private.h
index d993b77b49..d33cddcf80 100644
--- a/src/libsystemd/sd-device/device-private.h
+++ b/src/libsystemd/sd-device/device-private.h
@@ -10,6 +10,7 @@
#include "macro.h"
+int device_new_from_mode_and_devnum(sd_device **ret, mode_t mode, dev_t devnum);
int device_new_from_nulstr(sd_device **ret, char *nulstr, size_t len);
int device_new_from_strv(sd_device **ret, char **strv);
int device_new_from_watch_handle_at(sd_device **ret, int dirfd, int wd);
diff --git a/src/libsystemd/sd-device/device-util.c b/src/libsystemd/sd-device/device-util.c
new file mode 100644
index 0000000000..f1cd67aa28
--- /dev/null
+++ b/src/libsystemd/sd-device/device-util.c
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "device-private.h"
+#include "device-util.h"
+#include "devnum-util.h"
+#include "string-util.h"
+
+int devpath_from_devnum(mode_t mode, dev_t devnum, char **ret) {
+ _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
+ _cleanup_free_ char *s = NULL;
+ const char *devname;
+ int r;
+
+ assert(ret);
+
+ if (major(devnum) == 0 && minor(devnum) == 0)
+ return device_path_make_inaccessible(mode, ret);
+
+ r = device_new_from_mode_and_devnum(&dev, mode, devnum);
+ if (r < 0)
+ return r;
+
+ r = sd_device_get_devname(dev, &devname);
+ if (r < 0)
+ return r;
+
+ s = strdup(devname);
+ if (!s)
+ return -ENOMEM;
+
+ *ret = TAKE_PTR(s);
+ return 0;
+}
diff --git a/src/libsystemd/sd-device/device-util.h b/src/libsystemd/sd-device/device-util.h
index d92cbd6cc7..6150c45082 100644
--- a/src/libsystemd/sd-device/device-util.h
+++ b/src/libsystemd/sd-device/device-util.h
@@ -2,6 +2,7 @@
#pragma once
#include <stdbool.h>
+#include <sys/types.h>
#include "sd-device.h"
@@ -79,3 +80,5 @@
#define log_device_notice_errno(device, error, ...) log_device_full_errno(device, LOG_NOTICE, error, __VA_ARGS__)
#define log_device_warning_errno(device, error, ...) log_device_full_errno(device, LOG_WARNING, error, __VA_ARGS__)
#define log_device_error_errno(device, error, ...) log_device_full_errno(device, LOG_ERR, error, __VA_ARGS__)
+
+int devpath_from_devnum(mode_t mode, dev_t devnum, char **ret);
diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c
index 51bee24d51..26b9e51c93 100644
--- a/src/libsystemd/sd-device/sd-device.c
+++ b/src/libsystemd/sd-device/sd-device.c
@@ -279,7 +279,7 @@ _public_ int sd_device_new_from_syspath(sd_device **ret, const char *syspath) {
return device_new_from_syspath(ret, syspath, /* strict = */ true);
}
-static int device_new_from_mode_and_devnum(sd_device **ret, mode_t mode, dev_t devnum) {
+int device_new_from_mode_and_devnum(sd_device **ret, mode_t mode, dev_t devnum) {
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
_cleanup_free_ char *syspath = NULL;
const char *t, *subsystem;
diff --git a/src/libsystemd/sd-device/test-sd-device.c b/src/libsystemd/sd-device/test-sd-device.c
index 66c8d70faa..74fdd7b600 100644
--- a/src/libsystemd/sd-device/test-sd-device.c
+++ b/src/libsystemd/sd-device/test-sd-device.c
@@ -14,6 +14,7 @@
#include "nulstr-util.h"
#include "path-util.h"
#include "rm-rf.h"
+#include "stat-util.h"
#include "string-util.h"
#include "tests.h"
#include "time-util.h"
@@ -499,4 +500,34 @@ TEST(sd_device_new_from_path) {
}
}
+static void test_devpath_from_devnum_one(const char *path) {
+ _cleanup_free_ char *resolved = NULL;
+ struct stat st;
+
+ log_debug("> %s", path);
+
+ if (stat(path, &st) < 0) {
+ assert_se(errno == ENOENT);
+ log_notice("Path %s not found, skipping test", path);
+ return;
+ }
+
+ assert_se(devpath_from_devnum(st.st_mode, st.st_rdev, &resolved) >= 0);
+ assert_se(path_equal(path, resolved));
+}
+
+TEST(devpath_from_devnum) {
+ test_devpath_from_devnum_one("/dev/null");
+ test_devpath_from_devnum_one("/dev/zero");
+ test_devpath_from_devnum_one("/dev/full");
+ test_devpath_from_devnum_one("/dev/random");
+ test_devpath_from_devnum_one("/dev/urandom");
+ test_devpath_from_devnum_one("/dev/tty");
+
+ if (is_device_node("/run/systemd/inaccessible/blk") > 0) {
+ test_devpath_from_devnum_one("/run/systemd/inaccessible/chr");
+ test_devpath_from_devnum_one("/run/systemd/inaccessible/blk");
+ }
+}
+
DEFINE_TEST_MAIN(LOG_INFO);