2

我想定期从内核模块调用用户空间程序。但是内核程序正在冻结系统,而我尝试加载它。以下是程序,

#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 部分和计时器部分,它工作正常。但是,当我结合这两个代码时,系统会挂起。任何人都可以帮我解决这个问题。

4

1 回答 1

0

快速环顾四周强烈表明 hrtimer 回调是从 irq 上下文执行的,这是意料之中的——否则你将如何获得高分辨率?

但这也意味着您不能阻塞,而您的回调可能会由于 call_usermodehelper 而阻塞,而不管 NOWAIT 是否通过。

因此,您似乎正在禁用调试的内核上测试您的模块,这从根本上是错误的。

但这不太相关,因为您首先要实现的目标看起来根本上是错误的。

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

于 2016-02-19T13:45:16.773 回答