From 221631d5118b09a57bc416ebe64b856bffb97f5e Mon Sep 17 00:00:00 2001 From: Lucas De Marchi Date: Thu, 24 Nov 2011 16:41:01 -0200 Subject: [PATCH] Accept dir where we should lookup for modules --- libkmod/libkmod-private.h | 2 ++ libkmod/libkmod.c | 25 ++++++++++++++++++++++++- libkmod/libkmod.h | 2 +- test/test-init.c | 2 +- test/test-loaded.c | 2 +- test/test-rmmod.c | 2 +- 6 files changed, 30 insertions(+), 5 deletions(-) diff --git a/libkmod/libkmod-private.h b/libkmod/libkmod-private.h index 0ce9167..56ada3a 100644 --- a/libkmod/libkmod-private.h +++ b/libkmod/libkmod-private.h @@ -51,4 +51,6 @@ struct kmod_list *kmod_list_remove(struct kmod_list *list); struct kmod_list *kmod_list_remove_data(struct kmod_list *list, const void *data) __must_check; +const char *kmod_get_dirname(struct kmod_ctx *ctx) __attribute__((nonnull(1))); + #endif diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c index 9307f71..6417cab 100644 --- a/libkmod/libkmod.c +++ b/libkmod/libkmod.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "libkmod.h" #include "libkmod-private.h" @@ -49,6 +50,7 @@ struct kmod_ctx { int priority, const char *file, int line, const char *fn, const char *format, va_list args); void *userdata; + const char *dirname; int log_priority; }; @@ -71,6 +73,11 @@ static void log_stderr(struct kmod_ctx *ctx, vfprintf(stderr, format, args); } +const char *kmod_get_dirname(struct kmod_ctx *ctx) +{ + return ctx->dirname; +} + /** * kmod_get_userdata: * @ctx: kmod library context @@ -118,6 +125,16 @@ static int log_priority(const char *priority) return 0; } +static const char *get_kernel_release(void) +{ + struct utsname u; + + if (uname(&u) < 0) + return NULL; + + return strdup(u.release); +} + /** * kmod_new: * @@ -129,7 +146,7 @@ static int log_priority(const char *priority) * * Returns: a new kmod library context **/ -KMOD_EXPORT struct kmod_ctx *kmod_new(void) +KMOD_EXPORT struct kmod_ctx *kmod_new(const char *dirname) { const char *env; struct kmod_ctx *ctx; @@ -142,6 +159,11 @@ KMOD_EXPORT struct kmod_ctx *kmod_new(void) ctx->log_fn = log_stderr; ctx->log_priority = LOG_ERR; + if (dirname != NULL) + ctx->dirname = strdup(dirname); + else + ctx->dirname = get_kernel_release(); + /* environment overwrites config */ env = getenv("KMOD_LOG"); if (env != NULL) @@ -185,6 +207,7 @@ KMOD_EXPORT struct kmod_ctx *kmod_unref(struct kmod_ctx *ctx) if (--ctx->refcount > 0) return ctx; info(ctx, "context %p released\n", ctx); + free((char *)ctx->dirname); free(ctx); return NULL; } diff --git a/libkmod/libkmod.h b/libkmod/libkmod.h index 2aa0e01..d68d2ef 100644 --- a/libkmod/libkmod.h +++ b/libkmod/libkmod.h @@ -36,7 +36,7 @@ extern "C" { * environment, user variables, allows custom logging */ struct kmod_ctx; -struct kmod_ctx *kmod_new(void); +struct kmod_ctx *kmod_new(const char *dirname); struct kmod_ctx *kmod_ref(struct kmod_ctx *ctx); struct kmod_ctx *kmod_unref(struct kmod_ctx *ctx); void kmod_set_log_fn(struct kmod_ctx *ctx, diff --git a/test/test-init.c b/test/test-init.c index 140b669..18317aa 100644 --- a/test/test-init.c +++ b/test/test-init.c @@ -10,7 +10,7 @@ int main(int argc, char *argv[]) { struct kmod_ctx *ctx; - ctx = kmod_new(); + ctx = kmod_new(NULL); if (ctx == NULL) exit(EXIT_FAILURE); diff --git a/test/test-loaded.c b/test/test-loaded.c index c81c288..e5cfe13 100644 --- a/test/test-loaded.c +++ b/test/test-loaded.c @@ -15,7 +15,7 @@ int main(int argc, char *argv[]) struct kmod_list *list, *itr; int err; - ctx = kmod_new(); + ctx = kmod_new(NULL); if (ctx == NULL) exit(EXIT_FAILURE); diff --git a/test/test-rmmod.c b/test/test-rmmod.c index 759743e..d7690b3 100644 --- a/test/test-rmmod.c +++ b/test/test-rmmod.c @@ -19,7 +19,7 @@ int main(int argc, char *argv[]) if (argc == 2) modname = argv[1]; - ctx = kmod_new(); + ctx = kmod_new(NULL); if (ctx == NULL) exit(EXIT_FAILURE); -- 2.7.4