这很简单,但是是一个复杂的过程。这是一个安装系统调用的模块。
包括一堆东西
#include <sys/types.h>
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/module.h>
#include <sys/sysent.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/sysproto.h>
定义你的结构来保存参数
struct mykill_args {
int pid;
int signo;
};
定义处理函数
static int
mykill(struct thread *td, void *args)
{
struct mykill_args *uap = args;
uprintf("mykill called. pid=%d, signo=%d\n", uap->pid, uap->signo);
return 0;
}
你需要一个 sysent 对象
static struct sysent mykill_sysent = {
2, /* number of arguments */
mykill /* function handling system call */
};
以及将安装系统调用的偏移量。
/* Choose "the next" value later. */
static int offset = NO_SYSCALL;
load
功能
static int
load(struct module *module, int cmd, void *arg)
{
int error = 0;
switch (cmd) {
case MOD_LOAD:
uprintf("Loading module. Installing syscall at"
" offset %d\n", offset);
break;
case MOD_UNLOAD:
uprintf("Unloading module. syscall uninstalled from"
" offset %d\n", offset);
break;
default:
error = EOPNOTSUPP;
break;
}
return error;
}
安装系统调用
SYSCALL_MODULE(mykill, &offset, &mykill_sysent, load, NULL);
您可以使用运行系统调用syscall(2)
。或者使用 perl :))。这是一个例子
[root@aiur /home/cnicutar/kld-syscall]# kldload ./mykill.ko
Loading module. Installing syscall at offset 210
[cnicutar@aiur ~/kld-syscall]$ perl -e 'syscall(210, 30, 15);'
mykill called. pid=30, signo=15