概要
call_usermodehelper — 准备并启动用户模式应用程序
函数原型
int call_usermodehelper ( | const char * , path |
---|---|
char ** , argv | |
char ** , envp | |
int wait) ; |
参数
-
const char * path
用户模式可执行文件的路径
-
char ** argv
用于过程的参数向量
-
char ** envp
流程环境
-
int wait
等待应用程序完成并返回状态。 当设置为UMH_NO_WAIT时根本不等待,但你没有得到有用的错误 当程序无法执行时。这使得呼叫安全 从中断上下文。
如果设置UMH_WAIT_PROC时,需要等待调用完成,返回0,执行完成,可通过返回值判断是否执行成功。
描述
此函数等效于 use 和 call_usermodehelper_setup; call_usermodehelper_exec
例子
在内核态可以通过 call_usermodehelpere() 函数实现对用户态函数的调用,其最终也是通过内核态函数 do_execve() 实现。
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
static int __init test_init(void)
{
int ret = -1;
char path[] = "/sbin/reboot"; //要执行的用户态指令
char *argv[] = {path, NULL};
char *envp[] = {NULL};
printk("call_usermodehelper module isstarting..!\n");
ret = call_usermodehelper(path, argv, envp, UMH_WAIT_PROC); //等待调用完成
printk("ret=%d\n", ret); //返回0,执行成功
return 0;
}
static void __exit test_exit(void)
{
}
module_init(test_init);
module_exit(test_exit);
MODULE_LICENSE("GPL");
编译成模块后,使用insmod 命令加载模块后,系统会重启,并打印call_usermodehelper module isstarting…!