内核与应用交互
内核层调用应用层程序
参考博客:
linux驱动调用(运行/执行)应用程序.
只需要在内核层执行该函数即可:
extern int call_usermodehelper(char *path, char **argv, char **envp, int wait);
内核层程序:
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/kmod.h>
MODULE_AUTHOR("darui");
MODULE_LICENSE("GPL");
static int __init practiceCall(void)
{
int ret = 0;
char *path = "/home/darui/practice/a";
char *arv[] = {
path,"",NULL};//无参数,注意要按照这个格式path在 第一个位置,必须NULL结束
char *env[] = {
"/home/darui","/usr/local/sbin",NULL};//多路径可以用逗号隔开,也可以在一对引号中用分号隔开,必须NULL结束标志
printk("%s:call_usermodehelper\n",__func__);
ret = call_usermodehelper(path,arv,env,UMH_WAIT_PROC);
if(ret < 0)
{
printk("error");
return -1;
}
return 0;
}
static void __exit practiceCallExit(void)
{
printk("%s:call_usermodehelper\n",__func__);
}
module_init(practiceCall);
module_exit(practiceCallExit);
注意有个坑点,执行程序不能是a.sh等程序,应该是只能是编译后的程序,执行shell脚本会出错(卡了很久)
应用层程序源码:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
int fd = -1;
fd = open("/home/darui/practice/file.txt",O_RDWR|O_CREAT);
write(fd,":practice\n",10);
close(fd);
}
gcc -o a a.c编译即可,会创建file.txt文件
原理:
call_usermodehelper
——》call_usermodehelper_setup + call_usermodehelper_exec
————》queue_work
——————》__call_usermodehelper
————————》do_execve
——————————》
1、在进程栈中push进应用层程序,
2、iret指令保存内核现场进行堆栈切换,
3、执行完应用层,ret_from_sys_call返回