关于在FreeBSD中编写自己的系统调用的问题

pip*_*low 4 kernel freebsd system-calls

好的,所以我刚刚读完了FreeBSD的kill(2)的实现,并试图编写自己的"kill".这个系统调用需要uidsignum与信号发送到由UID拥有的进程,但不包括调用进程.

我怎样才能uid转到系统调用?在杀戮(2)中,pid是在争论中struct kill_args.是否有包含结构uid的方式struct kill_args包含pid?如果没有,我可以在内核之外定义一个结构吗?

cni*_*tar 8

这很简单,但是涉及到一个过程.这是一个安装系统调用的模块.

包括一堆东西

#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>
Run Code Online (Sandbox Code Playgroud)

定义您的结构以保存参数

struct mykill_args {
    int pid;
    int signo;
};
Run Code Online (Sandbox Code Playgroud)

定义处理功能

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;
}
Run Code Online (Sandbox Code Playgroud)

你需要一个sysent对象

static struct sysent mykill_sysent = {
    2,          /* number of arguments */
    mykill      /* function handling system call */
};
Run Code Online (Sandbox Code Playgroud)

以及将安装系统调用的偏移量.

/* Choose "the next" value later. */
static int offset = NO_SYSCALL;
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

安装系统调用

SYSCALL_MODULE(mykill, &offset, &mykill_sysent, load, NULL);
Run Code Online (Sandbox Code Playgroud)

您可以使用运行系统调用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
Run Code Online (Sandbox Code Playgroud)