linux驱动调用(运行/执行)应用程序
原文 https://blog.csdn.net/eZiMu/article/details/79118863
概述:
linux驱动程序调用(运行/执行)应用程序,即驱动调用用户空间的应用程序。本文主要是从系统API的使用角度讲述。
API声明的位置:
声明在include/linux/kmod.h里面,相应函数实现在kernel/kmod.c里面。
函数call_usermodehelper()使用:
函数call_usermodehelper()声明如下:
extern int call_usermodehelper(char *path, char **argv, char **envp, int wait);
- 1
参数说明:
path:需要执行的应用程序,及路径。
argv:传递给应用程序的参数,如果没有参数用NULL。
envp:环境变量。如果在path指定的地方没有找到应用程序,那么将会在envp环 境变量中,查找应用程序。
wait:控制标志,可用下面的4个宏定义其中之一。在kmod.h中定义如下控制标志:
//驱动程序不等待应用程序的返回
#define UMH_NO_WAIT 0 /* don't wait at all */
//驱动程序等待应用程序执行完毕返回,但不处理返回的值
#define UMH_WAIT_EXEC 1 /* wait for the exec, but not the process */
//驱动程序等待应用程序执行完毕返回,并处理返回值
#define UMH_WAIT_PROC 2 /* wait for the process to complete */
//个人理解,应用程序不会返回时,驱动等待,直到应用程序进程被杀死后返回
#define UMH_KILLABLE 4 /* wait for EXEC/PROC killable */
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
用法例子:
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/kmod.h>
MODULE_AUTHOR("zimu");
MODULE_LICENSE("GPL");
static int __init practiceCall(void)
{
char *path = "/home/zimu/practice/a.out";
char *arv[] = {path,"",NULL};//无参数,注意要按照这个格式pathzai 第一个位置,结束标志NULL
char *env[] = {"/home/zimu","/home/zimu/bin:/home/zimu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin",NULL};//多路径可以用逗号隔开,也可以在一对引号中用分号隔开,NULL结束标志
printk("%s:call_usermodehelper\n",__func__);
call_usermodehelper(path,arv,env,UMH_WAIT_PROC);
return 0;
}
static void __exit practiceCallExit(void)
{
printk("%s:call_usermodehelper\n",__func__);
}
module_init(practiceCall);
module_exit(practiceCallExit);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
驱动执行在/home/zimu/practice/路径下的应用程序a.out,如果在指定路径找不到a.out,将会在环境变量”/home/zimu”,”/home/zimu/bin:/home/zimu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin”路径下查找a.out。a.out无参数。
a.out的源码如下:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
main()
{
int fd = -1;
fd = open("/home/zimu/practice/file.txt",O_RDWR|O_CREAT);
write(fd,":practice\n",10);
close(fd);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
a.out的功能是在/home/zimu/practice/路径下,打开file.txt文件,如果文件不存在,就创建file.txt文件,并向文件中写入“:practice”字符,以回车键结束。
加载例子中的驱动模块时,就会在/home/zimu/practice/路径下创建file.txt文件。
注意:驱动调用的应用程序,都是在root用户下运行的
posted on 2018-06-14 16:42 Kevin_HeYongyuan 阅读(858) 评论(0) 收藏 举报
浙公网安备 33010602011771号