]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
Add call to check if resources are valid
authorLucas De Marchi <lucas.demarchi@profusion.mobi>
Sat, 31 Dec 2011 21:28:31 +0000 (19:28 -0200)
committerLucas De Marchi <lucas.demarchi@profusion.mobi>
Sun, 1 Jan 2012 08:18:16 +0000 (06:18 -0200)
TODO
libkmod/libkmod.c
libkmod/libkmod.h
libkmod/libkmod.sym

diff --git a/TODO b/TODO
index 6ecbf20044d181bdbe6d8735a10cca0961ad636e..03962c819bee5062a1020f69449db37455bca979 100644 (file)
--- a/TODO
+++ b/TODO
@@ -19,10 +19,6 @@ Features:
 * provide 1:1 compatibility with module-init-tools's modprobe
    - dump configuration
 
-* Add kmod_validate(). It checks if all config files and indexes are still
-  valid. We need to save the mtime of each config dir when starting up and when
-  this function is called we stat the dir and compare the mtimes.
-
 * review API, maybe unify all of these setters:
    - kmod_module_version_get_symbol()
    - kmod_module_version_get_crc()
index 794cc54eb23df2c78ada0191353e9fa84701fa0b..7acd8d884a772df1afc1c07aaf3ebd5e3da26739 100644 (file)
@@ -29,6 +29,7 @@
 #include <string.h>
 #include <ctype.h>
 #include <sys/utsname.h>
+#include <sys/stat.h>
 
 #include "libkmod.h"
 #include "libkmod-private.h"
@@ -611,6 +612,62 @@ int kmod_lookup_alias_from_commands(struct kmod_ctx *ctx, const char *name,
        return nmatch;
 }
 
+static bool is_cache_invalid(const char *path, unsigned long long stamp)
+{
+       struct stat st;
+
+       if (stat(path, &st) < 0)
+               return true;
+
+       if (stamp != ts_usec(&st.st_mtim))
+               return true;
+
+       return false;
+}
+
+/**
+ * kmod_validate_resources:
+ * @ctx: kmod library context
+ *
+ * Check if indexes and configuration files changed on disk and the current
+ * context is not valid anymore.
+ *
+ * Returns KMOD_RESOURCES_OK if resources are still valid,
+ * KMOD_RESOURCES_MUST_RELOAD if it's sufficient to call
+ * kmod_unload_resources() and kmod_load_resources() or
+ * KMOD_RESOURCES_MUST_RECREATE if @ctx must be re-created.
+ */
+KMOD_EXPORT int kmod_validate_resources(struct kmod_ctx *ctx)
+{
+       struct kmod_list *l;
+       size_t i;
+
+       if (ctx == NULL || ctx->config == NULL)
+               return KMOD_RESOURCES_MUST_RECREATE;
+
+       kmod_list_foreach(l, ctx->config->paths) {
+               struct kmod_config_path *cf = l->data;
+
+               if (is_cache_invalid(cf->path, cf->stamp))
+                       return KMOD_RESOURCES_MUST_RECREATE;
+       }
+
+       for (i = 0; i < _KMOD_INDEX_LAST; i++) {
+               char path[PATH_MAX];
+
+               if (ctx->indexes[i] == NULL)
+                       continue;
+
+               snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname,
+                                                       index_files[i]);
+
+               if (is_cache_invalid(path, ctx->indexes_stamp[i]))
+                       return KMOD_RESOURCES_MUST_RELOAD;
+       }
+
+       return KMOD_RESOURCES_OK;
+}
+
 /**
  * kmod_load_resources:
  * @ctx: kmod library context
index 9f48cf026b5adb2e6d8373006f85ce912df3c6ed..a57f518b5e895a6325f650e560cd10f4f95df18f 100644 (file)
@@ -52,6 +52,14 @@ void kmod_set_userdata(struct kmod_ctx *ctx, const void *userdata);
 int kmod_load_resources(struct kmod_ctx *ctx);
 void kmod_unload_resources(struct kmod_ctx *ctx);
 
+enum kmod_resources {
+       KMOD_RESOURCES_OK = 0,
+       KMOD_RESOURCES_MUST_RELOAD = 1,
+       KMOD_RESOURCES_MUST_RECREATE = 2,
+};
+
+int kmod_validate_resources(struct kmod_ctx *ctx);
+
 /*
  * kmod_list
  *
index f5bebf90b79b83a9429a8d630e789090d1918f71..316fcc25d638c5f1c433c0c275276e6980fbac38 100644 (file)
@@ -66,6 +66,8 @@ global:
 
 LIBKMOD_3 {
 global:
+       kmod_validate_resources;
+
        kmod_module_get_symbols;
        kmod_module_symbol_get_symbol;
        kmod_module_symbol_get_crc;