FreeBSD kldload:无法加载,没有这样的文件或目录

时间:2014-11-20 16:22:26

标签: c operating-system kernel freebsd system-calls

我是内核和KLD编程的新手。我想在FreeBSD中修改系统调用模块的示例文件。我的问题是,是否有可能在系统调用函数内部fork或exec?如下例所示?

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

/*
 * The function for implementing the syscall.
 */
static int hello (struct thread *td, void *arg)
{
    printf("Running...\n");
    /******************************************************/
    /*Something like this?*/
    /******************************************************/
    execl("/bin/pwd", "pwd", NULL);
    return 0;
}

/*
 * The `sysent' for the new syscall
 */

static struct sysent hello_sysent = {
    0,          /* sy_narg */
    hello           /* sy_call */
};

/*
 * The offset in sysent where the syscall is allocated.
 */

static int offset = NO_SYSCALL;

/*
 * The function called at load/unload.
 */

static int
load (struct module *module, int cmd, void *arg)
{
    int error = 0;

    switch (cmd) {
    case MOD_LOAD :
        uprintf ("syscall loaded at %d\n", offset);
        break;
    case MOD_UNLOAD :
        uprintf ("syscall unloaded from %d\n", offset);
        break;
    default :
        uprintf("There was some error!");
        error = EINVAL;
        break;
    }
    return error;
}

SYSCALL_MODULE(syscall, &offset, &hello_sysent, load, NULL);

没有编译错误(syscall),但在使用kldload加载时,它会返回错误: kldload:无法加载./syscall.ko:没有这样的文件或目录

有什么我可以阅读并了解更多关于为什么会发生这种情况以及我该怎么办?

1 个答案:

答案 0 :(得分:1)

当kldload返回&#34;没有这样的文件或目录&#34;或其他一些奇怪的错误时,首先要做&#34; dmesg&#34;并在底部查找任何错误。在这种情况下,可能是由于缺少符号&#34; execl&#34;。那是因为execl是一个用户空间API(man 3 execl),而你正在尝试在内核中使用它。

您尝试做的事情似乎并不是一个好主意,但这是可能的。查看sys / kern / kern_exec.c:kern_execve()。