From: Lucas De Marchi Date: Sat, 31 Dec 2011 21:28:31 +0000 (-0200) Subject: Add call to check if resources are valid X-Git-Tag: v3~23 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c4dc3ca8a22d96fde95c34ba0cd7e0b3aa11032d;p=thirdparty%2Fkmod.git Add call to check if resources are valid --- diff --git a/TODO b/TODO index 6ecbf200..03962c81 100644 --- 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() diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c index 794cc54e..7acd8d88 100644 --- a/libkmod/libkmod.c +++ b/libkmod/libkmod.c @@ -29,6 +29,7 @@ #include #include #include +#include #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 diff --git a/libkmod/libkmod.h b/libkmod/libkmod.h index 9f48cf02..a57f518b 100644 --- a/libkmod/libkmod.h +++ b/libkmod/libkmod.h @@ -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 * diff --git a/libkmod/libkmod.sym b/libkmod/libkmod.sym index f5bebf90..316fcc25 100644 --- a/libkmod/libkmod.sym +++ b/libkmod/libkmod.sym @@ -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;