aboutsummaryrefslogtreecommitdiff
path: root/libkmod/libkmod-module.c
diff options
context:
space:
mode:
authorLucas De Marchi <lucas.demarchi@intel.com>2013-08-27 23:29:47 -0300
committerLucas De Marchi <lucas.demarchi@intel.com>2013-08-29 01:33:51 -0300
commit7e0385c47ae7c313a59de3ea431af7b5d18807d7 (patch)
tree88653cafaf974851937d6edccc843bb4d284d177 /libkmod/libkmod-module.c
parentbd4e7340bcd9f95e04a6309667ffe1a5427edcaa (diff)
downloadkmod-7e0385c47ae7c313a59de3ea431af7b5d18807d7.tar.gz
Fix usage of readdir_r()
With readdir_r() we should be providing enough space to store the dir name. This could be accomplished by define an union like systemd does: union dirent_storage { struct dirent de; uint8_t storage[offsetof(struct dirent, d_name) + ((NAME_MAX + 1 + sizeof(long)) & ~(sizeof(long) - 1))]; }; However in all places that we use readdir_r() we have no concerns about reentrance nor we have problems with threads. Thus use the simpler readdir() instead. We also remove the error logging here (that could be added back by checking errno), but it was not adding much value so it's gone.
Diffstat (limited to 'libkmod/libkmod-module.c')
-rw-r--r--libkmod/libkmod-module.c52
1 files changed, 18 insertions, 34 deletions
diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c
index 0fc1101..3874194 100644
--- a/libkmod/libkmod-module.c
+++ b/libkmod/libkmod-module.c
@@ -1869,6 +1869,7 @@ KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *
{
char dname[PATH_MAX];
struct kmod_list *list = NULL;
+ struct dirent *dent;
DIR *d;
if (mod == NULL || mod->ctx == NULL)
@@ -1883,32 +1884,22 @@ KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *
return NULL;
}
- for (;;) {
- struct dirent de, *entp;
+ for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
struct kmod_module *holder;
struct kmod_list *l;
int err;
- err = readdir_r(d, &de, &entp);
- if (err != 0) {
- ERR(mod->ctx, "could not iterate for module '%s': %s\n",
- mod->name, strerror(-err));
- goto fail;
- }
-
- if (entp == NULL)
- break;
-
- if (de.d_name[0] == '.') {
- if (de.d_name[1] == '\0' ||
- (de.d_name[1] == '.' && de.d_name[2] == '\0'))
+ if (dent->d_name[0] == '.') {
+ if (dent->d_name[1] == '\0' ||
+ (dent->d_name[1] == '.' && dent->d_name[2] == '\0'))
continue;
}
- err = kmod_module_new_from_name(mod->ctx, de.d_name, &holder);
+ err = kmod_module_new_from_name(mod->ctx, dent->d_name,
+ &holder);
if (err < 0) {
ERR(mod->ctx, "could not create module for '%s': %s\n",
- de.d_name, strerror(-err));
+ dent->d_name, strerror(-err));
goto fail;
}
@@ -1958,6 +1949,7 @@ KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module
{
char dname[PATH_MAX];
struct kmod_list *list = NULL;
+ struct dirent *dent;
DIR *d;
int dfd;
@@ -1975,31 +1967,23 @@ KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module
dfd = dirfd(d);
- for (;;) {
- struct dirent de, *entp;
+ for (dent = readdir(d); dent; dent = readdir(d)) {
struct kmod_module_section *section;
struct kmod_list *l;
unsigned long address;
size_t namesz;
int fd, err;
- err = readdir_r(d, &de, &entp);
- if (err != 0) {
- ERR(mod->ctx, "could not iterate for module '%s': %s\n",
- mod->name, strerror(-err));
- goto fail;
- }
-
- if (de.d_name[0] == '.') {
- if (de.d_name[1] == '\0' ||
- (de.d_name[1] == '.' && de.d_name[2] == '\0'))
+ if (dent->d_name[0] == '.') {
+ if (dent->d_name[1] == '\0' ||
+ (dent->d_name[1] == '.' && dent->d_name[2] == '\0'))
continue;
}
- fd = openat(dfd, de.d_name, O_RDONLY|O_CLOEXEC);
+ fd = openat(dfd, dent->d_name, O_RDONLY|O_CLOEXEC);
if (fd < 0) {
ERR(mod->ctx, "could not open '%s/%s': %m\n",
- dname, de.d_name);
+ dname, dent->d_name);
goto fail;
}
@@ -2007,11 +1991,11 @@ KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module
close(fd);
if (err < 0) {
ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
- dname, de.d_name);
+ dname, dent->d_name);
goto fail;
}
- namesz = strlen(de.d_name) + 1;
+ namesz = strlen(dent->d_name) + 1;
section = malloc(sizeof(*section) + namesz);
if (section == NULL) {
@@ -2020,7 +2004,7 @@ KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module
}
section->address = address;
- memcpy(section->name, de.d_name, namesz);
+ memcpy(section->name, dent->d_name, namesz);
l = kmod_list_append(list, section);
if (l != NULL) {