英文:
Call a executable from Linux Kernel Module
问题
I am trying to call a executable from Linux Kernel Module, which is a interrupt service routine.
- I created a GPIO interrupt service routine, compiled and created a
gpio_irq.ko
file.
#include <linux/module.h>
#include <linux/init.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
/* Meta Information */
MODULE_LICENSE("GPL");
/* Reference code is taken from here */
MODULE_AUTHOR("Johannes 4 GNU/Linux");
MODULE_DESCRIPTION("A simple LKM for a gpio interrupt");
/** variable contains pin number o interrupt controller to which GPIO 499 is mapped to */
unsigned int irq_number;
unsigned int toggle_value = 0;
/**
* Interrupt service routine is called, when interrupt is triggered
*/
static irq_handler_t gpio_irq_handler(unsigned int irq, void *dev_id, struct pt_regs *regs) {
printk("gpio_irq: Interrupt was triggered and ISR was called!\n");
if (toggle_value == 0)
{
toggle_value = 1;
if(gpio_direction_output(498, 1)) {
printk("Error!\nCan not set GPIO 498 output value to 1!\n");
gpio_free(498);
}
}
if (toggle_value == 1)
{
toggle_value = 0;
if(gpio_direction_output(498, 0)) {
printk("Error!\nCan not set GPIO 498 output value to 0!\n");
gpio_free(498);
}
}
return (irq_handler_t) IRQ_HANDLED;
}
/**
* This function is called, when the module is loaded into the kernel
*/
static int __init ModuleInit(void) {
printk("qpio_irq: Loading module... ");
/* Setup the gpio */
if(gpio_request(499, "rpi-gpio-499")) {
printk("Error!\nCan not allocate GPIO 499\n");
return -1;
}
if(gpio_request(498, "rpi-gpio-498")) {
printk("Error!\nCan not allocate GPIO 498\n");
return -1;
}
/* Set GPIO direction */
if(gpio_direction_input(499)) {
printk("Error!\nCan not set GPIO 499 to input!\n");
gpio_free(499);
return -1;
}
/* Setup the interrupt */
irq_number = gpio_to_irq(499);
if(request_irq(irq_number, (irq_handler_t) gpio_irq_handler, IRQF_TRIGGER_RISING, "my_gpio_irq", NULL) != 0){
printk("Error!\nCan not request interrupt nr.: %d\n", irq_number);
gpio_free(499);
return -1;
}
printk("Done!\n");
printk("GPIO 499 is mapped to IRQ Nr.: %d\n", irq_number);
return 0;
}
/**
* This function is called, when the module is removed from the kernel
*/
static void __exit ModuleExit(void) {
printk("gpio_irq: Unloading module... ");
free_irq(irq_number, NULL);
gpio_free(499);
gpio_free(498);
}
module_init(ModuleInit);
module_exit(ModuleExit);
-
Loaded that .ko file to linux kernel using
sudo insmod gpio_irq.ko
-
With this, I am able to get interrupt and service the routine.
But,
I also want to call another executable from static irq_handler_t gpio_irq_handler(...)
which is located at /home/user_name/Documents/
I know that, this can be done using system()
call as explained in here
But, I am unable add <#include <stdlib.h>
in my Linux Kernel Module. Compiler is throwing error saying that "Fatal Error: stdlib : No such file or directory"
Can someone guide me on how to call executable from Linux Kernel Moudule?
英文:
I am trying to call a executable from Linux Kernel Module, which is a interrupt service routine.
- I created a GPIO interrupt service routine, compiled and created a
gpio_irq.ko
file.
#include <linux/module.h>
#include <linux/init.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
/* Meta Information */
MODULE_LICENSE("GPL");
/* Reference code is taken from here */
MODULE_AUTHOR("Johannes 4 GNU/Linux");
MODULE_DESCRIPTION("A simple LKM for a gpio interrupt");
/** variable contains pin number o interrupt controller to which GPIO 499 is mapped to */
unsigned int irq_number;
unsigned int toggle_value = 0;
/**
* Interrupt service routine is called, when interrupt is triggered
*/
static irq_handler_t gpio_irq_handler(unsigned int irq, void *dev_id, struct pt_regs *regs) {
printk("gpio_irq: Interrupt was triggered and ISR was called!\n");
if (toggle_value == 0)
{
toggle_value = 1;
if(gpio_direction_output(498, 1)) {
printk("Error!\nCan not set GPIO 498 output value to 1!\n");
gpio_free(498);
}
}
if (toggle_value == 1)
{
toggle_value = 0;
if(gpio_direction_output(498, 0)) {
printk("Error!\nCan not set GPIO 498 output value to 0!\n");
gpio_free(498);
}
}
return (irq_handler_t) IRQ_HANDLED;
}
/**
* This function is called, when the module is loaded into the kernel
*/
static int __init ModuleInit(void) {
printk("qpio_irq: Loading module... ");
/* Setup the gpio */
if(gpio_request(499, "rpi-gpio-499")) {
printk("Error!\nCan not allocate GPIO 499\n");
return -1;
}
if(gpio_request(498, "rpi-gpio-498")) {
printk("Error!\nCan not allocate GPIO 498\n");
return -1;
}
/* Set GPIO direction */
if(gpio_direction_input(499)) {
printk("Error!\nCan not set GPIO 499 to input!\n");
gpio_free(499);
return -1;
}
/* Setup the interrupt */
irq_number = gpio_to_irq(499);
if(request_irq(irq_number, (irq_handler_t) gpio_irq_handler, IRQF_TRIGGER_RISING, "my_gpio_irq", NULL) != 0){
printk("Error!\nCan not request interrupt nr.: %d\n", irq_number);
gpio_free(499);
return -1;
}
printk("Done!\n");
printk("GPIO 499 is mapped to IRQ Nr.: %d\n", irq_number);
return 0;
}
/**
* This function is called, when the module is removed from the kernel
*/
static void __exit ModuleExit(void) {
printk("gpio_irq: Unloading module... ");
free_irq(irq_number, NULL);
gpio_free(499);
gpio_free(498);
}
module_init(ModuleInit);
module_exit(ModuleExit);
-
Loaded that .ko file to linux kernel using
sudo insmod gpio_irq.ko
-
With this, I am able to get interrupt and service the routine.
But,
I also want to call another executable from static irq_handler_t gpio_irq_handler(...)
which is located at /home/user_name/Documents/
I know that, this can be done using system()
call as explained in here
But, I am unable add <#include <stdlib.h>
in my Linux Kernel Module. Compiler is throwing error saying that "Fatal Error: stdlib : No such file or directory"
Can someone guide me on how to call executable from Linux Kernel Moudule?
答案1
得分: 3
你的方法存在问题,因为加载和运行可执行文件需要许多系统调用和中断。你无法在中断例程内部执行这些操作,因为中断被禁用。
第二个原因是C运行时库,包括stdlib.h中的所有内容,都是用户空间库,不在内核中可用。
第三个原因是加载和运行可执行文件非常耗时,这正是你想要解决的问题。
幸运的是,多年前就开发出了一种简单的方法来解决你的问题,即在中断和执行用户空间任务之间最小化延迟。
启动你的用户空间进程,这样可以摆脱所有启动开销,然后发出一个read
调用到你的内核模块。这将阻塞,直到你的模块响应。读取操作可以返回一个表示状态的单个字节。读取响应的延迟远远小于启动可执行文件(如果这是可能的话)。
英文:
The problem with your approach is that to load and run an executable requires many syscalls and interrupts. You can't do this from inside an interrupt routine since interrupts are disabled.
A second reason is that the C runtime, including everything in stdlib.h, is a user-space library. None of it is available in the kernel.
A third reason is that loading and running an executable is time-consuming, which is the problem that you want to solve.
Fortunately, there is a simple method developed years ago to solve your problem of minimizing latency between your interrupt and execution of the user-space task.
Start your user space process, which gets all of the startup overhead out of the way, then issue a read
call to your kernel module. This will block until your module responds. The read could return a single byte indicating status. The latency of the read response will be far less than starting an executable (if it were even possible).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论