]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
Fix usage of readdir_r()
authorLucas De Marchi <lucas.demarchi@intel.com>
Wed, 28 Aug 2013 02:29:47 +0000 (23:29 -0300)
committerLucas De Marchi <lucas.demarchi@intel.com>
Thu, 29 Aug 2013 04:33:51 +0000 (01:33 -0300)
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.

libkmod/libkmod-config.c
libkmod/libkmod-module.c
tools/depmod.c

index 24978c14d082cc2a05a1689180f923af0f135bbc..c5f4803a397c451998f71a0027c5946130b6dcb7 100644 (file)
@@ -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,
index 0fc11013d801d338da68e42392d926351c476b12..3874194189c8a1a141deed73e92d735121c4d256 100644 (file)
@@ -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) {
index 985cf3ae001a9c17dcb9c7475bafaaed4d4a6d54..58f0f584fa2ebd1c09e5f6280433ea4600696446 100644 (file)
@@ -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);