diff options
author | Lucas De Marchi <lucas.demarchi@intel.com> | 2013-08-27 23:29:47 -0300 |
---|---|---|
committer | Lucas De Marchi <lucas.demarchi@intel.com> | 2013-08-29 01:33:51 -0300 |
commit | 7e0385c47ae7c313a59de3ea431af7b5d18807d7 (patch) | |
tree | 88653cafaf974851937d6edccc843bb4d284d177 | |
parent | bd4e7340bcd9f95e04a6309667ffe1a5427edcaa (diff) | |
download | kmod-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.
-rw-r--r-- | libkmod/libkmod-config.c | 22 | ||||
-rw-r--r-- | libkmod/libkmod-module.c | 52 | ||||
-rw-r--r-- | tools/depmod.c | 16 |
3 files changed, 26 insertions, 64 deletions
diff --git a/libkmod/libkmod-config.c b/libkmod/libkmod-config.c index 24978c1..c5f4803 100644 --- a/libkmod/libkmod-config.c +++ b/libkmod/libkmod-config.c @@ -821,6 +821,7 @@ static int conf_files_list(struct kmod_ctx *ctx, struct kmod_list **list, DIR *d; int err; struct stat st; + struct dirent *dent; if (stat(path, &st) != 0) { err = -errno; @@ -845,30 +846,15 @@ static int conf_files_list(struct kmod_ctx *ctx, struct kmod_list **list, return -EINVAL; } - for (;;) { - struct dirent ent, *entp; - - err = readdir_r(d, &ent, &entp); - if (err != 0) { - ERR(ctx, "reading entry %s\n", strerror(-err)); - goto fail_read; - } - - if (entp == NULL) - break; - - if (conf_files_filter_out(ctx, d, path, entp->d_name)) + for (dent = readdir(d); dent != NULL; dent = readdir(d)) { + if (conf_files_filter_out(ctx, d, path, dent->d_name)) continue; - conf_files_insert_sorted(ctx, list, path, entp->d_name); + conf_files_insert_sorted(ctx, list, path, dent->d_name); } closedir(d); return 0; - -fail_read: - closedir(d); - return err; } int kmod_config_new(struct kmod_ctx *ctx, struct kmod_config **p_config, 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) { diff --git a/tools/depmod.c b/tools/depmod.c index 985cf3a..58f0f58 100644 --- a/tools/depmod.c +++ b/tools/depmod.c @@ -835,6 +835,7 @@ static int cfg_files_insert_sorted(struct cfg_file ***p_files, size_t *p_n_files static int cfg_files_list(struct cfg_file ***p_files, size_t *p_n_files, const char *path) { + struct dirent *dent; DIR *d; int err = 0; struct stat st; @@ -859,20 +860,11 @@ static int cfg_files_list(struct cfg_file ***p_files, size_t *p_n_files, return -EINVAL; } - for (;;) { - struct dirent ent, *entp; - - err = readdir_r(d, &ent, &entp); - if (err != 0) { - ERR("reading entry %s\n", strerror(-err)); - break; - } - if (entp == NULL) - break; - if (cfg_files_filter_out(d, path, entp->d_name)) + for (dent = readdir(d); dent != NULL; dent = readdir(d)) { + if (cfg_files_filter_out(d, path, dent->d_name)) continue; - cfg_files_insert_sorted(p_files, p_n_files, path, entp->d_name); + cfg_files_insert_sorted(p_files, p_n_files, path, dent->d_name); } closedir(d); |