1
0
mirror of git://sourceware.org/git/lvm2.git synced 2025-01-02 01:18:26 +03:00

dev-type: use fopen for sysfs file

Directly open sysfs files and save extra stat() call which
is not adding any extra safety in sysfs dir.
This commit is contained in:
Zdenek Kabelac 2021-02-07 14:07:17 +01:00
parent e429e69b65
commit 3bf2ca11d9

View File

@ -944,8 +944,7 @@ static unsigned long _dev_topology_attribute(struct dev_types *dt,
const char *sysfs_dir = dm_sysfs_dir(); const char *sysfs_dir = dm_sysfs_dir();
char path[PATH_MAX], buffer[64]; char path[PATH_MAX], buffer[64];
FILE *fp; FILE *fp;
struct stat info; dev_t primary = 0;
dev_t uninitialized_var(primary);
unsigned long result = default_value; unsigned long result = default_value;
unsigned long value = 0UL; unsigned long value = 0UL;
@ -963,9 +962,9 @@ static unsigned long _dev_topology_attribute(struct dev_types *dt,
* - if not: either the kernel doesn't have topology support * - if not: either the kernel doesn't have topology support
* or the device could be a partition * or the device could be a partition
*/ */
if (stat(path, &info) == -1) { if (!(fp = fopen(path, "r"))) {
if (errno != ENOENT) { if (errno != ENOENT) {
log_sys_debug("stat", path); log_sys_debug("fopen", path);
goto out; goto out;
} }
if (!dev_get_primary_dev(dt, dev, &primary)) if (!dev_get_primary_dev(dt, dev, &primary))
@ -975,25 +974,20 @@ static unsigned long _dev_topology_attribute(struct dev_types *dt,
if (!_snprintf_attr(path, sizeof(path), sysfs_dir, attribute, primary)) if (!_snprintf_attr(path, sizeof(path), sysfs_dir, attribute, primary))
goto_out; goto_out;
if (stat(path, &info) == -1) { if (!(fp = fopen(path, "r"))) {
if (errno != ENOENT) if (errno != ENOENT)
log_sys_debug("stat", path); log_sys_debug("fopen", path);
goto out; goto out;
} }
} }
if (!(fp = fopen(path, "r"))) {
log_sys_debug("fopen", path);
goto out;
}
if (!fgets(buffer, sizeof(buffer), fp)) { if (!fgets(buffer, sizeof(buffer), fp)) {
log_sys_debug("fgets", path); log_sys_debug("fgets", path);
goto out_close; goto out_close;
} }
if (sscanf(buffer, "%lu", &value) != 1) { if (sscanf(buffer, "%lu", &value) != 1) {
log_warn("sysfs file %s not in expected format: %s", path, buffer); log_warn("WARNING: sysfs file %s not in expected format: %s", path, buffer);
goto out_close; goto out_close;
} }