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