]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
libkmod: dump index files
authorLucas De Marchi <lucas.demarchi@profusion.mobi>
Mon, 16 Jan 2012 17:56:17 +0000 (15:56 -0200)
committerLucas De Marchi <lucas.demarchi@profusion.mobi>
Mon, 16 Jan 2012 18:05:46 +0000 (16:05 -0200)
Provide a function to dump the index files to a certain fd. It could be
more optimized (particularly the functions to dump the index that were
copied and pasted from m-i-t), but it seems like the only user of it is
'modprobe -c', used for debugging purposes. So, keep it as is.

TODO
libkmod/libkmod-index.c
libkmod/libkmod-index.h
libkmod/libkmod.c
libkmod/libkmod.h
libkmod/libkmod.sym

diff --git a/TODO b/TODO
index 2e6987bcbd49e2e4d1b261b6fb9b5fc1b65e4515..d1d553af302c7597fe119dd6e5bfc8c32e190f04 100644 (file)
--- a/TODO
+++ b/TODO
@@ -11,18 +11,6 @@ Features:
 * create test-mock library to be LD_PRELOAD'ed before running the binaries
   so we're able to create unit tests
 
-* Add functions to dump configuration. Create a list with the config items
-  (blacklist, aliases, etc) or just dump to a fd?
-
-* Add functions to list all modules known by modules.dep
-
-* provide 1:1 compatibility with module-init-tools's modprobe
-   - dump modules.alias and modules.symbols
-
-* Add docs to kmod_config_* functions
-
-* Add manpages: copy them from module-init-tools and make the necessary changes
-
 * review API, maybe unify all of these setters:
    - kmod_module_version_get_symbol()
    - kmod_module_version_get_crc()
@@ -63,10 +51,14 @@ Things to be added/removed in kernel (check what is really needed):
 ===================================================================
 
 * list of currently loaded modules
+       - readdir() in /sys/modules: dirs without a 'initstate' file mean the
+         modules is builtin.
 
 * module's size should be available under /sys
+       - DONE in 3.3: http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=commit;h=cca3e707301862ca9b9327e6a732463982f8cd1b
 
 * kill /proc/modules ?
+       - Unlikely, given other tools might depend on it
 
 Things that are different from module-init-tools on purpose (!TODO)
 ===================================================================
index 15e48987d03c2a36dfcafa64c134d176fcdfe9ad..9d3b9395db0ce99b34d478dddc6a88f34e9f570c 100644 (file)
@@ -211,6 +211,19 @@ static bool buf_pushchar(struct buffer *buf, char ch)
        return true;
 }
 
+static unsigned buf_pushchars(struct buffer *buf, const char *str)
+{
+       unsigned i = 0;
+       int ch;
+
+       while ((ch = str[i])) {
+               buf_pushchar(buf, ch);
+               i++;
+       }
+
+       return i;
+}
+
 static unsigned buf_freadchars(struct buffer *buf, FILE *in)
 {
        unsigned i = 0;
@@ -383,6 +396,48 @@ static struct index_node_f *index_readchild(const struct index_node_f *parent,
        return NULL;
 }
 
+static void index_dump_node(struct index_node_f *node, struct buffer *buf,
+                                                               int fd)
+{
+       struct index_value *v;
+       int ch, pushed;
+
+       pushed = buf_pushchars(buf, node->prefix);
+
+       for (v = node->values; v != NULL; v = v->next) {
+               write_str_safe(fd, buf->bytes, buf->used);
+               write_str_safe(fd, " ", 1);
+               write_str_safe(fd, v->value, strlen(v->value));
+               write_str_safe(fd, "\n", 1);
+       }
+
+       for (ch = node->first; ch <= node->last; ch++) {
+               struct index_node_f *child = index_readchild(node, ch);
+
+               if (!child)
+                       continue;
+
+               buf_pushchar(buf, ch);
+               index_dump_node(child, buf, fd);
+               buf_popchar(buf);
+       }
+
+       buf_popchars(buf, pushed);
+       index_close(node);
+}
+
+void index_dump(struct index_file *in, int fd, const char *prefix)
+{
+       struct index_node_f *root;
+       struct buffer buf;
+
+       buf_init(&buf);
+       buf_pushchars(&buf, prefix);
+       root = index_readroot(in);
+       index_dump_node(root, &buf, fd);
+       buf_release(&buf);
+}
+
 static char *index_search__node(struct index_node_f *node, const char *key, int i)
 {
        char *value;
@@ -810,6 +865,50 @@ static struct index_mm_node *index_mm_readchild(const struct index_mm_node *pare
        return NULL;
 }
 
+static void index_mm_dump_node(struct index_mm_node *node, struct buffer *buf,
+                                                               int fd)
+{
+       struct index_mm_value *itr, *itr_end;
+       int ch, pushed;
+
+       pushed = buf_pushchars(buf, node->prefix);
+
+       itr = node->values.values;
+       itr_end = itr + node->values.len;
+       for (; itr < itr_end; itr++) {
+               write_str_safe(fd, buf->bytes, buf->used);
+               write_str_safe(fd, " ", 1);
+               write_str_safe(fd, itr->value, itr->len);
+               write_str_safe(fd, "\n", 1);
+       }
+
+       for (ch = node->first; ch <= node->last; ch++) {
+               struct index_mm_node *child = index_mm_readchild(node, ch);
+
+               if (child == NULL)
+                       continue;
+
+               buf_pushchar(buf, ch);
+               index_mm_dump_node(child, buf, fd);
+               buf_popchar(buf);
+       }
+
+       buf_popchars(buf, pushed);
+       index_mm_free_node(node);
+}
+
+void index_mm_dump(struct index_mm *idx, int fd, const char *prefix)
+{
+       struct index_mm_node *root;
+       struct buffer buf;
+
+       buf_init(&buf);
+       buf_pushchars(&buf, prefix);
+       root = index_mm_readroot(idx);
+       index_mm_dump_node(root, &buf, fd);
+       buf_release(&buf);
+}
+
 static char *index_mm_search_node(struct index_mm_node *node, const char *key,
                                                                        int i)
 {
index e9cd456d67a88e58694a13ba7847c134a4cc9ddc..0134ac501bf697e0a7f36218e207a0ce9c793951 100644 (file)
@@ -114,6 +114,7 @@ struct index_file;
 struct index_file *index_file_open(const char *filename);
 void index_file_close(struct index_file *idx);
 char *index_search(struct index_file *idx, const char *key);
+void index_dump(struct index_file *in, int fd, const char *prefix);
 struct index_value *index_searchwild(struct index_file *idx, const char *key);
 
 void index_values_free(struct index_value *values);
@@ -125,5 +126,6 @@ struct index_mm *index_mm_open(struct kmod_ctx *ctx, const char *filename,
 void index_mm_close(struct index_mm *index);
 char *index_mm_search(struct index_mm *idx, const char *key);
 struct index_value *index_mm_searchwild(struct index_mm *idx, const char *key);
+void index_mm_dump(struct index_mm *idx, int fd, const char *prefix);
 
 #endif
index 86969450a63ecbad872e4cacd91a04da5de7bd91..f544c66d73bc65184aee21980240083556969d6f 100644 (file)
@@ -746,6 +746,39 @@ KMOD_EXPORT void kmod_unload_resources(struct kmod_ctx *ctx)
        }
 }
 
+KMOD_EXPORT int kmod_dump_index(struct kmod_ctx *ctx, enum kmod_index type,
+                                                                       int fd)
+{
+       if (ctx == NULL)
+               return -ENOSYS;
+
+       if (type < 0 || type >= _KMOD_INDEX_MODULES_SIZE)
+               return -ENOENT;
+
+       if (ctx->indexes[type] != NULL) {
+               DBG(ctx, "use mmaped index '%s'\n", index_files[type].fn);
+               index_mm_dump(ctx->indexes[type], fd,
+                                               index_files[type].prefix);
+       } else {
+               char fn[PATH_MAX];
+               struct index_file *idx;
+
+               snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname,
+                                               index_files[type].fn);
+
+               DBG(ctx, "file=%s\n", fn);
+
+               idx = index_file_open(fn);
+               if (idx == NULL)
+                       return -ENOSYS;
+
+               index_dump(idx, fd, index_files[type].prefix);
+               index_file_close(idx);
+       }
+
+       return 0;
+}
+
 const struct kmod_list *kmod_get_blacklists(const struct kmod_ctx *ctx)
 {
        return ctx->config->blacklists;
index a3939d344d2e52a040f9bda95172e98cb33e1dc0..64fe064cd880a2097e3cb5fa71ed1008dc23c717 100644 (file)
@@ -69,6 +69,8 @@ enum kmod_index {
        _KMOD_INDEX_PAD = (1 << 31),
 };
 
+int kmod_dump_index(struct kmod_ctx *ctx, enum kmod_index idx, int fd);
+
 /*
  * kmod_list
  *
index 500af4e2d23f468624b3118916bbfe2986f413e6..f2d704de5d4785ad1e2a59972241a40ee8549285 100644 (file)
@@ -94,4 +94,5 @@ global:
        kmod_config_iter_get_value;
        kmod_config_iter_next;
        kmod_config_iter_free_iter;
+       kmod_dump_index;
 } LIBKMOD_3;