【发布时间】:2013-11-15 10:08:24
【问题描述】:
我编写了一个使用SIGALRM 和信号处理程序的程序。
我现在正尝试将此作为测试模块添加到内核中。
我发现我必须用它们的底层系统调用替换 libc 提供的许多函数。示例是 timer_create 和 sys_timer_create timer_settime 和 sys_timer_settime 等等。
但是,我遇到了sigaction 的问题。
编译内核抛出如下错误arch/arm/mach-vexpress/cpufreq_test.c:157:2: error: implicit declaration of function 'sys_sigaction' [-Werror=implicit-function-declaration]
我在下面附上了相关的代码块
int estimate_from_cycles() {
timer_t timer;
struct itimerspec old;
struct sigaction sig_action;
struct sigevent sig_event;
sigset_t sig_mask;
memset(&sig_action, 0, sizeof(struct sigaction));
sig_action.sa_handler = alarm_handler;
sigemptyset(&sig_action.sa_mask);
VERBOSE("Blocking signal %d\n", SIGALRM);
sigemptyset(&sig_mask);
sigaddset(&sig_mask, SIGALRM);
if(sys_sigaction(SIGALRM, &sig_action, NULL)) {
ERROR("Could not assign sigaction\n");
return -1;
}
if (sigprocmask(SIG_SETMASK, &sig_mask, NULL) == -1) {
ERROR("sigprocmask failed\n");
return -1;
}
memset (&sig_event, 0, sizeof (struct sigevent));
sig_event.sigev_notify = SIGEV_SIGNAL;
sig_event.sigev_signo = SIGALRM;
sig_event.sigev_value.sival_ptr = &timer;
if (sys_timer_create(CLOCK_PROCESS_CPUTIME_ID, &sig_event, &timer)) {
ERROR("Could not create timer\n");
return -1;
}
if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) == -1) {
ERROR("sigprocmask unblock failed\n");
return -1;
}
cycles = 0;
VERBOSE("Entering main loop\n");
if(sys_timer_settime(timer, 0, &time_period, &old)) {
ERROR("Could not set timer\n");
return -1;
}
while(1) {
ADD(CYCLES_REGISTER, 1);
}
return 0;
}
这种获取用户空间代码并单独更改调用的方法是否足以在内核空间中运行代码?
【问题讨论】:
-
不,不是。内核模块必须运行的环境与用户级程序运行的环境有很大不同,某些可以调用的函数的名称只是一个相当大的冰山一角......
-
那么在内核空间中执行信号处理/警报之类的正确方法是什么?
-
我不这么认为。你能举例说明如何向内核发送信号吗?
标签: c linux linux-kernel signals