'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',
#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);
--- /dev/null
+/* 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;
+}
#pragma once
#include <stdbool.h>
+#include <sys/types.h>
#include "sd-device.h"
#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);
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;
#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"
}
}
+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);