cdown / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone
25f9c8
From 0db1f9965e6791c651d0bccd095cbe3a87c6579c Mon Sep 17 00:00:00 2001
25f9c8
From: Karel Zak <kzak@redhat.com>
25f9c8
Date: Thu, 25 Nov 2021 11:52:46 +0100
25f9c8
Subject: [PATCH 70/74] lib/sys: add sysfs_chrdev_devno_to_devname()
25f9c8
25f9c8
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=2026511
25f9c8
Upstream: http://github.com/util-linux/util-linux/commit/ab5304a7a34bfa45d9bee205ca4e26f03db6e79d
25f9c8
Signed-off-by: Karel Zak <kzak@redhat.com>
25f9c8
---
25f9c8
 include/pathnames.h |  1 +
25f9c8
 include/sysfs.h     |  2 ++
25f9c8
 lib/sysfs.c         | 33 +++++++++++++++++++++++++++++++++
25f9c8
 3 files changed, 36 insertions(+)
25f9c8
25f9c8
diff --git a/include/pathnames.h b/include/pathnames.h
25f9c8
index 59cc66736..77f8b6e85 100644
25f9c8
--- a/include/pathnames.h
25f9c8
+++ b/include/pathnames.h
25f9c8
@@ -102,6 +102,7 @@
25f9c8
 
25f9c8
 #define _PATH_SYS_BLOCK		"/sys/block"
25f9c8
 #define _PATH_SYS_DEVBLOCK	"/sys/dev/block"
25f9c8
+#define _PATH_SYS_DEVCHAR	"/sys/dev/char"
25f9c8
 #define _PATH_SYS_CLASS		"/sys/class"
25f9c8
 #define _PATH_SYS_SCSI		"/sys/bus/scsi"
25f9c8
 
25f9c8
diff --git a/include/sysfs.h b/include/sysfs.h
25f9c8
index 9a72a2009..e2fd0c1ba 100644
25f9c8
--- a/include/sysfs.h
25f9c8
+++ b/include/sysfs.h
25f9c8
@@ -92,6 +92,8 @@ extern int sysfs_scsi_host_is(struct sysfs_cxt *cxt, const char *type);
25f9c8
 extern int sysfs_scsi_has_attribute(struct sysfs_cxt *cxt, const char *attr);
25f9c8
 extern int sysfs_scsi_path_contains(struct sysfs_cxt *cxt, const char *pattern);
25f9c8
 
25f9c8
+extern char *sysfs_chrdev_devno_to_devname(dev_t devno, char *buf, size_t bufsiz);
25f9c8
+
25f9c8
 /**
25f9c8
  * sysfs_devname_sys_to_dev:
25f9c8
  * @name: devname to be converted in place
25f9c8
diff --git a/lib/sysfs.c b/lib/sysfs.c
25f9c8
index e5437f43a..ceec41d10 100644
25f9c8
--- a/lib/sysfs.c
25f9c8
+++ b/lib/sysfs.c
25f9c8
@@ -1036,6 +1036,39 @@ int sysfs_scsi_path_contains(struct sysfs_cxt *cxt, const char *pattern)
25f9c8
 	return strstr(linkc, pattern) != NULL;
25f9c8
 }
25f9c8
 
25f9c8
+char *sysfs_chrdev_devno_to_devname(dev_t devno, char *buf, size_t bufsiz)
25f9c8
+{
25f9c8
+	char link[PATH_MAX];
25f9c8
+	char path[PATH_MAX];
25f9c8
+	char *name;
25f9c8
+	ssize_t	sz;
25f9c8
+
25f9c8
+	sz = snprintf(path, sizeof(path),
25f9c8
+		      _PATH_SYS_DEVCHAR "/%u:%u", major(devno), minor(devno));
25f9c8
+	if (sz <= 0)
25f9c8
+		return NULL;
25f9c8
+
25f9c8
+        /* read /sys/dev/char/<maj:min> link */
25f9c8
+	sz = readlink(path, link, sizeof(link) - 1);
25f9c8
+	if (sz < 0)
25f9c8
+		return NULL;
25f9c8
+	link[sz] = '\0';
25f9c8
+
25f9c8
+	name = strrchr(link, '/');
25f9c8
+	if (!name)
25f9c8
+		return NULL;
25f9c8
+
25f9c8
+	name++;
25f9c8
+	sz = strlen(name);
25f9c8
+	if ((size_t) sz + 1 > bufsiz)
25f9c8
+		return NULL;
25f9c8
+
25f9c8
+	memcpy(buf, name, sz + 1);
25f9c8
+	sysfs_devname_sys_to_dev(buf);
25f9c8
+	return buf;
25f9c8
+
25f9c8
+}
25f9c8
+
25f9c8
 #ifdef TEST_PROGRAM_SYSFS
25f9c8
 #include <errno.h>
25f9c8
 #include <err.h>
25f9c8
-- 
25f9c8
2.31.1
25f9c8