内核模块定期调用用户空间程序 [英] Kernel module periodically calling user space program

查看:104
本文介绍了内核模块定期调用用户空间程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想定期从内核模块调用用户空间程序.但是,当我尝试加载它时,内核程序冻结了系统. 以下是程序,

I want to call a user space program from a kernel module periodically.But the kernel program is freezing the system, while I try to load it. following is the 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部分,并且工作正常.但是,当我将两个代码组合在一起时,系统挂起了. 谁能帮我解决问题.

test.sh is a script which just echoes a comment. I have tested the call_usermodehelper part and timer part individually and it is working fine. But while I am combining the two codes, the system hangs. Can anybody please help me to solve the problem.

推荐答案

快速浏览强烈表明,hrtimer回调是从irq上下文执行的,这是可以预期的-您还将如何获得更高的分辨率?

Quick look around strongly suggests that hrtimer callbacks are executed from an irq context, which is expected - how else are you going to get high resoluton?

但这也意味着您不能阻塞,而回调函数由于call_usermodehelper会阻塞,而与传递的NOWAIT无关.

But this also means you must no block, while your callback can block due to call_usermodehelper regardless of NOWAIT passed.

所以看来您是在禁用调试的情况下在内核上测试模块,这从根本上是错误的.

So it seems you are testing your module on a kernel with debugging disabled, which is fundamentally wrong.

但这无关紧要,因为您首先要实现的目标看起来根本上是错误的.

But this is less relevant as the thing you are trying to achieve in the first place looks fundamentally wrong.

我只能建议您详细说明实际问题是什么.现在绝对有分叉+执行与需要高分辨率计时器的事情有关的方式.

I can only recommend you elaborate what is the actual problem. There is absolutely now way that forking + execing has anything to do with something requiring a high resolution timer.

这篇关于内核模块定期调用用户空间程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆