Browse Source

lib/sys: add sysfs_chrdev_devno_to_devname()

Signed-off-by: Karel Zak <kzak@redhat.com>
pull/1514/head
Karel Zak 9 months ago
parent
commit
ab5304a7a3
  1. 2
      include/sysfs.h
  2. 34
      lib/sysfs.c

2
include/sysfs.h

@ -109,5 +109,7 @@ int sysfs_blkdev_scsi_host_is(struct path_cxt *pc, const char *type);
int sysfs_blkdev_scsi_has_attribute(struct path_cxt *pc, const char *attr);
int sysfs_blkdev_scsi_path_contains(struct path_cxt *pc, const char *pattern);
char *sysfs_chrdev_devno_to_devname(dev_t devno, char *buf, size_t bufsiz);
#endif /* UTIL_LINUX_SYSFS_H */

34
lib/sysfs.c

@ -1039,6 +1039,40 @@ int sysfs_devno_count_partitions(dev_t devno)
return n;
}
char *sysfs_chrdev_devno_to_devname(dev_t devno, char *buf, size_t bufsiz)
{
char link[PATH_MAX];
struct path_cxt *pc;
char *name;
ssize_t sz;
pc = ul_new_path(_PATH_SYS_DEVCHAR "/%u:%u", major(devno), minor(devno));
if (!pc)
return NULL;
/* read /sys/dev/char/<maj:min> link */
sz = ul_path_readlink(pc, link, sizeof(link), NULL);
ul_unref_path(pc);
if (sz < 0)
return NULL;
name = strrchr(link, '/');
if (!name)
return NULL;
name++;
sz = strlen(name);
if ((size_t) sz + 1 > bufsiz)
return NULL;
memcpy(buf, name, sz + 1);
sysfs_devname_sys_to_dev(buf);
return buf;
}
#ifdef TEST_PROGRAM_SYSFS
#include <errno.h>

Loading…
Cancel
Save