相当于modinfo()的INSIDE内核? [英] modinfo() equivalent INSIDE kernel?
问题描述
我有两个模块A,B.A具有全局可用的函数f(),即,导出了f()符号. B可能希望偶尔调用f().但是,只有在加载了模块A的情况下,B才应调用f(). B判断A是否已加载的最佳方法是什么?
此问题的b部分是否有一种方法可以检查是否导出了f()?
我不确定哪种方法更有效.
我假设您先加载模块B,然后加载模块A.我的策略是在A首次初始化时让A向B注册一组函数. B保持静态分配的函数指针(或指向充满函数指针的结构的指针),并提供导出的函数来注册和注销处理程序.当A加载时,它向B注册其功能(或功能结构).当A卸载时,其取消注册其功能.它可能是这样的:
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);
}
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 exported?
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);
}
这篇关于相当于modinfo()的INSIDE内核?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!