2014-11-20 74 views
0

我是內核和KLD編程的新手。我正在修改FreeBSD中用於系統調用模塊的示例文件。我的問題是,是否有可能在系統調用函數內部進行fork或exec?像下面的例子一樣?FreeBSD kldload:無法加載,沒有這樣的文件或目錄

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

沒有編譯錯誤(系統調用),但在使用命令kldload加載它,它會返回一個錯誤: 命令kldload:無法加載./syscall.ko:沒有這樣的文件或目錄

有什麼我可以閱讀和了解更多關於爲什麼會發生這種情況,我能做些什麼?

+0

你試過給它一個完整的路徑嗎? – kichik 2014-11-20 16:23:56

+0

是的,我試過了。我可以看到syscall.ko文件在那裏。 – 2014-11-20 16:28:32

+1

使用kldload -v ./syscall.ko和文件syscall.ko檢查dmesg輸出。確保你正在運行正在運行的內核的源代碼樹和右拱(例如i386 vs amd64)。 – 2014-11-21 04:48:56

回答

1

當kldload返回「沒有這樣的文件或目錄」或其他奇怪的錯誤時,首先執行「dmesg」並查找底部的任何錯誤。在這種情況下,可能是由於缺少符號「execl」。這是因爲execl是一個用戶空間API(man 3 execl),並且您正嘗試在內核中使用它。

你想要做什麼似乎不是一個好主意,但它是可能的。看看sys/kern/kern_exec.c:kern_execve()。

+0

謝謝。這是我正在尋找的迴應。 – 2014-11-24 19:50:23

相關問題