modinfo() equivalent INSIDE kernel?
I have two modules A, B. A has a function f() that is globally acessible, i.e. the f() symbol is exported. B may want to call f() occasionally. But B should only call f() if module A is loaded. What is the best way for B to tell if A is loaded?
Part b to this question is there is a way to check if f() is export开发者_如何转开发ed?
I'm not sure which method is more effecient.
I assume you load module B first, then optionally module A. My strategy would be to have A register a set of functions with B when A first initializes. B keeps a statically allocated function pointer (or a pointer to a struct full of function pointers) and provides exported functions to register and unregisters a handler. When A loads, it registers its function (or struct of functions) with B. When A unloads, it unregisters its functions.
It might go something like this:
B.h
typedef int (*foo_t)(int);
int B_register_foo(foo_t);
int B_unregister_foo(foo_t);
B.c
static foo_t foo = NULL;
int B_register_foo(foo_t f) {
if (!foo) {
foo = f;
return 0;
}
return -EFOO;
}
void B_unregister_foo(foo_t f) {
if (foo == f)
foo = NULL;
}
EXPORT_SYMBOL(B_register_foo);
EXPORT_SYMBOL(B_unregister_foo);
void B_maybe_call_foo(int arg) {
return (foo) ? foo(arg) : 0;
}
A.c
static int A_foo(int arg);
static int __init init_A(void) {
if (B_register_foo(A_foo))
return -EFOO;
return 0;
}
static void __exit exit_A(void) {
B_unregister_foo(A_foo);
}
If B uses at least one of A's symbols, then by the time B is loaded, a module providing the required symbol(s) (in other words, a module like A) is also already loaded.
Part b to this question is there is a way to check if f() is exported?
If the symbol were not available, you would not be able to load the module (B) requesting it.
精彩评论