[英]Kernel module periodically calling user space program
我想定期从内核模块中调用用户空间程序。但是,当我尝试加载它时,内核程序冻结了系统。 以下是程序,
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */
#include <linux/jiffies.h>
#include <linux/time.h>
#include <linux/proc_fs.h>
#include <asm/uaccess.h>
#include <linux/hrtimer.h>
#include <linux/sched.h>
#include <linux/delay.h>
#define TIME_PERIOD 50000
static struct hrtimer hr_timer;
static ktime_t ktime_period_ns;
static enum hrtimer_restart timer_callback(struct hrtimer *timer){
char userprog[] = "test.sh";
char *argv[] = {userprog, "2", NULL };
char *envp[] = {"HOME=/", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
printk("\n Timer is running");
hrtimer_forward_now(&hr_timer, ktime_period_ns);
printk("callmodule: %s\n", userprog);
call_usermodehelper(userprog, argv, envp, UMH_WAIT_PROC);
return HRTIMER_RESTART;
}
static int __init timer_init() {
ktime_period_ns= ktime_set( 0, TIME_PERIOD);
hrtimer_init ( &hr_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL );
hr_timer.function = timer_callback;
hrtimer_start( &hr_timer, ktime_period_ns, HRTIMER_MODE_REL );
return 0;
}
static int __exit timer_exit(){
int cancelled = hrtimer_cancel(&hr_timer);
if (cancelled)
printk(KERN_ERR "Timer is still running\n");
else
printk(KERN_ERR "Timer is cancelled\n");
}
module_init(timer_init);
module_exit(timer_exit);
MODULE_LICENSE("GPL");
test.sh是一个仅回显注释的脚本。 我已经分别测试了call_usermodehelper部分和timer部分,并且工作正常。 但是,当我将两个代码组合在一起时,系统挂起了。 谁能帮我解决问题。
快速浏览一下,强烈建议您从irq上下文中执行hrtimer回调,这是可以预期的-您还将如何获得较高的分辨率?
但这也意味着您绝不能阻塞,而无论call_usermodehelper为何,回调都可以阻塞,而与传递的NOWAIT无关。
因此看来您是在禁用调试的情况下在内核上测试模块,这从根本上是错误的。
但这无关紧要,因为您首先要实现的目标看起来根本上是错误的。
我只能建议您详细说明实际问题。 现在绝对有分叉+执行与需要高分辨率计时器的事情有关的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.