From: Stanislav Brabec <sbrabec@suse.cz>
To: util-linux@vger.kernel.org
Subject: [PATCH] Fix /sys to /dev node name translation
Date: Mon, 18 May 2015 21:31:34 +0200 [thread overview]
Message-ID: <555A3E16.6090003@suse.cz> (raw)
linux/drivers/base/core.c: device_get_devnode() defines a translation of
'!' in sysfs nodes to '/' in /dev nodes. The same translation has to be
done to properly support device nodes with slash (e. g. device nodes of
cciss driver and several other drivers).
Replace '!' by '/' in sysfs_devno_to_devpath() exactly like kernel does.
The problem is spread across more places of the code.
One of the chunks fixes lsblk -f, other chunks fixes code that apparently
assumes that /sys/block and /dev node names are the same, however I do
not have any reproducer for them. I fixed all places I found, but it is
possible that there are even more occurrences.
Signed-off-by: Stanislav Brabec <sbrabec@suse.cz>
---
lib/sysfs.c | 14 ++++++++++++--
libblkid/src/devno.c | 7 ++++++-
misc-utils/lsblk.c | 7 +++++++
3 files changed, 25 insertions(+), 3 deletions(-)
diff --git a/lib/sysfs.c b/lib/sysfs.c
index 759d97b..ffdc8a1 100644
--- a/lib/sysfs.c
+++ b/lib/sysfs.c
@@ -112,7 +112,7 @@ dev_t sysfs_devname_to_devno(const char *name, const char *parent)
char *sysfs_devno_to_devpath(dev_t devno, char *buf, size_t bufsiz)
{
struct sysfs_cxt cxt;
- char *name;
+ char *name, *s;
size_t sz;
struct stat st;
@@ -130,6 +130,11 @@ char *sysfs_devno_to_devpath(dev_t devno, char *buf, size_t bufsiz)
if (sz + sizeof("/dev/") > bufsiz)
return NULL;
+ /* replace '!' in the name with '/', see
+ * linux/drivers/base/core.c: device_get_devnode() */
+ while ((s = strchr(name, '!')))
+ s[0] = '/';
+
/* create the final "/dev/<name>" string */
memmove(buf + 5, name, sz + 1);
memcpy(buf, "/dev/", 5);
@@ -776,7 +781,7 @@ int sysfs_devno_to_wholedisk(dev_t dev, char *diskname,
* - basename ../../block/sda = sda
*/
char linkpath[PATH_MAX];
- char *name;
+ char *name, *s;
ssize_t linklen;
linklen = sysfs_readlink(&cxt, NULL, linkpath, sizeof(linkpath) - 1);
@@ -789,6 +794,11 @@ int sysfs_devno_to_wholedisk(dev_t dev, char *diskname,
if (!name)
goto err;
+ /* replace '!' in the name with '/', see
+ * linux/drivers/base/core.c: device_get_devnode() */
+ while ((s = strchr(name, '!')))
+ s[0] = '/';
+
if (diskname && len) {
strncpy(diskname, name, len);
diskname[len - 1] = '\0';
diff --git a/libblkid/src/devno.c b/libblkid/src/devno.c
index f4a36e4..cedd2db 100644
--- a/libblkid/src/devno.c
+++ b/libblkid/src/devno.c
@@ -179,7 +179,7 @@ static const char *devdirs[] = { "/devices", "/devfs", "/dev", NULL };
static char *scandev_devno_to_devpath(dev_t devno)
{
struct dir_list *list = NULL, *new_list = NULL;
- char *devname = NULL;
+ char *devname = NULL, *s;
const char **dir;
/*
@@ -208,6 +208,11 @@ static char *scandev_devno_to_devpath(dev_t devno)
new_list = NULL;
}
}
+ /* replace '!' in the name with '/', see
+ * linux/drivers/base/core.c: device_get_devnode() */
+ if (devname)
+ while ((s = strchr(devname, '!')))
+ s[0] = '/';
free_dirlist(&list);
free_dirlist(&new_list);
diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c
index 1b4ffc1..5cdae7d 100644
--- a/misc-utils/lsblk.c
+++ b/misc-utils/lsblk.c
@@ -413,6 +413,7 @@ static struct dirent *xreaddir(DIR *dp)
static char *get_device_path(struct blkdev_cxt *cxt)
{
char path[PATH_MAX];
+ char *s;
assert(cxt);
assert(cxt->name);
@@ -421,6 +422,12 @@ static char *get_device_path(struct blkdev_cxt *cxt)
return canonicalize_dm_name(cxt->name);
snprintf(path, sizeof(path), "/dev/%s", cxt->name);
+
+ /* replace '!' in the name with '/', see
+ * linux/drivers/base/core.c: device_get_devnode() */
+ while ((s = strchr(path, '!')))
+ s[0] = '/';
+
return xstrdup(path);
}
--
2.4.1
--
Best Regards / S pozdravem,
Stanislav Brabec
software developer
---------------------------------------------------------------------
SUSE LINUX, s. r. o. e-mail: sbrabec@suse.cz
Lihovarská 1060/12 tel: +49 911 7405384547
190 00 Praha 9 fax: +420 284 084 001
Czech Republic http://www.suse.cz/
PGP: 830B 40D5 9E05 35D8 5E27 6FA3 717C 209F A04F CD76
next reply other threads:[~2015-05-18 19:31 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-05-18 19:31 Stanislav Brabec [this message]
2015-05-19 8:08 ` [PATCH] Fix /sys to /dev node name translation Karel Zak
2015-05-19 12:35 ` Stanislav Brabec
2015-05-21 11:11 ` Karel Zak
2015-05-22 13:52 ` Stanislav Brabec
2015-05-25 16:21 ` Stanislav Brabec
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=555A3E16.6090003@suse.cz \
--to=sbrabec@suse.cz \
--cc=util-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.