有关在FreeBSD中编写我自己的系统调用的问题 [英] Question about writing my own system call in FreeBSD

查看:87
本文介绍了有关在FreeBSD中编写我自己的系统调用的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我刚刚阅读完FreeBSD的kill(2)的实现,并试图编写自己的"kill".该系统调用使用uidsignum并将信号发送到uid拥有的进程,而不包括调用进程.

OK, so I just finish reading the implementation of kill(2) of FreeBSD, and am trying to write my own "kill". This system call takes uid and signum and sends the signal to processes owned by uid, excluding the calling process.

如何将uid传递给系统调用?在kill(2)中,pid在参数struct kill_args中.是否存在包含uid的结构和struct kill_args包含pid的方式?如果没有,我可以在内核外部定义结构吗?

How can I pass uid to the system call? In kill(2), pid is in argument struct kill_args. Is there a structure that contains uid the way that struct kill_args contains pid? If not, can I define a structure outside the kernel?

推荐答案

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

It's easy, but kind of an involved process. Here's a module that installs a system call.

#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;
}

您需要一个系统对象

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功能

load function

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 :)).这是一个例子

You can run your system call using syscall(2). Or using perl :)). Here's an example

[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

这篇关于有关在FreeBSD中编写我自己的系统调用的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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