From dd785730739fe90be76d9ab9ee72b33d13c57f02 Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Wed, 22 May 2024 23:23:54 +0200 Subject: [PATCH] libdm: parse only block section for proc_devices Check for begining of 'B'lock section in /proc/devices file before starting to sscanf() for major. --- device_mapper/ioctl/libdm-iface.c | 5 ++++- libdm/ioctl/libdm-iface.c | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/device_mapper/ioctl/libdm-iface.c b/device_mapper/ioctl/libdm-iface.c index 5800154f3..d47566977 100644 --- a/device_mapper/ioctl/libdm-iface.c +++ b/device_mapper/ioctl/libdm-iface.c @@ -198,6 +198,7 @@ static int _get_proc_number(const char *file, const char *name, char *line = NULL; size_t len; uint32_t num; + unsigned blocksection = (strcmp(file, PROC_DEVICES) == 0) ? 0 : 1; if (!(fl = fopen(file, "r"))) { log_sys_error("fopen", file); @@ -205,7 +206,9 @@ static int _get_proc_number(const char *file, const char *name, } while (getline(&line, &len, fl) != -1) { - if (sscanf(line, "%u %255s\n", &num, &nm[0]) == 2) { + if (!blocksection && (line[0] == 'B')) + blocksection = 1; + else if (sscanf(line, "%u %255s\n", &num, &nm[0]) == 2) { if (!strcmp(name, nm)) { if (number) { *number = num; diff --git a/libdm/ioctl/libdm-iface.c b/libdm/ioctl/libdm-iface.c index a579578c9..2ddd10358 100644 --- a/libdm/ioctl/libdm-iface.c +++ b/libdm/ioctl/libdm-iface.c @@ -197,6 +197,7 @@ static int _get_proc_number(const char *file, const char *name, char *line = NULL; size_t len; uint32_t num; + unsigned blocksection = (strcmp(file, PROC_DEVICES) == 0) ? 0 : 1; if (!(fl = fopen(file, "r"))) { log_sys_error("fopen", file); @@ -204,7 +205,9 @@ static int _get_proc_number(const char *file, const char *name, } while (getline(&line, &len, fl) != -1) { - if (sscanf(line, "%u %255s\n", &num, &nm[0]) == 2) { + if (!blocksection && (line[0] == 'B')) + blocksection = 1; + else if (sscanf(line, "%u %255s\n", &num, &nm[0]) == 2) { if (!strcmp(name, nm)) { if (number) { *number = num;