summaryrefslogtreecommitdiffstats
path: root/src/libsystemd/sd-device/device-util.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 15:35:18 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 15:35:18 +0000
commitb750101eb236130cf056c675997decbac904cc49 (patch)
treea5df1a06754bdd014cb975c051c83b01c9a97532 /src/libsystemd/sd-device/device-util.c
parentInitial commit. (diff)
downloadsystemd-b750101eb236130cf056c675997decbac904cc49.tar.xz
systemd-b750101eb236130cf056c675997decbac904cc49.zip
Adding upstream version 252.22.upstream/252.22
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/libsystemd/sd-device/device-util.c')
-rw-r--r--src/libsystemd/sd-device/device-util.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/libsystemd/sd-device/device-util.c b/src/libsystemd/sd-device/device-util.c
new file mode 100644
index 0000000..b192914
--- /dev/null
+++ b/src/libsystemd/sd-device/device-util.c
@@ -0,0 +1,65 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "device-private.h"
+#include "device-util.h"
+#include "devnum-util.h"
+#include "fd-util.h"
+#include "string-util.h"
+
+int devname_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;
+}
+
+int device_open_from_devnum(mode_t mode, dev_t devnum, int flags, char **ret) {
+ _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
+ _cleanup_close_ int fd = -1;
+ int r;
+
+ r = device_new_from_mode_and_devnum(&dev, mode, devnum);
+ if (r < 0)
+ return r;
+
+ fd = sd_device_open(dev, flags);
+ if (fd < 0)
+ return fd;
+
+ if (ret) {
+ const char *devname;
+ char *s;
+
+ r = sd_device_get_devname(dev, &devname);
+ if (r < 0)
+ return r;
+
+ s = strdup(devname);
+ if (!s)
+ return -ENOMEM;
+
+ *ret = s;
+ }
+
+ return TAKE_FD(fd);
+}