如何调用init_module(在用户空间中)

时间:2014-12-15 14:42:09

标签: linux linux-device-driver embedded-linux

我正在试图弄清楚如何从“C”应用程序加载内核模块,我不想使用finit_module,因为在我的系统中没有用于此系统调用的glibc包装器。

以下是我的尝试:

#include <fcntl.h>
#include <sys/mman.h>

int init_module(void *module_image, unsigned long len,
                const char *param_values);

int main() {
    int res = 0;
    void *buf = 0;
    struct stat sb;
    int rc = 0;

    int fd = open("/tmp/my-test.ko", O_RDONLY|O_CLOEXEC);
    if (fd < 0) {
        rc = -1;
        goto EXIT;
    }

    res = fstat(fd, &sb);
    if (res == -1) {
        rc = -2;
        goto EXIT_CLOSE;
    }

    buf = mmap(0, sb.st_size, PROT_READ|PROT_EXEC, MAP_PRIVATE, fd, 0);
    if (buf == 0) {
        rc = -3;
        goto EXIT_CLOSE;
    }

    res = init_module(buf, sb.st_size, 0);
    if (res == -1) {
        rc = -4;
        goto EXIT_FREE;
    }


EXIT_FREE:
    munmap(buf, sb.st_size);

EXIT_CLOSE:
    close(fd);

EXIT:
    return rc;
}

以下是我从strace获得的内容:

$ sudo strace ./a.out
execve("./a.out", ["./a.out"], [/* 28 vars */]) = 0
brk(0)                                  = 0x7be000
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f93015cd000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=116129, ...}) = 0
mmap(NULL, 116129, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f93015a8000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\320\37\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1845024, ...}) = 0
mmap(NULL, 3953344, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f9300fe0000
mprotect(0x7f930119b000, 2097152, PROT_NONE) = 0
mmap(0x7f930139b000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1bb000) = 0x7f930139b000
mmap(0x7f93013a1000, 17088, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f93013a1000
close(3)                                = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f93015c8000
arch_prctl(ARCH_SET_FS, 0x7f93015c8740) = 0
mprotect(0x7f930139b000, 16384, PROT_READ) = 0
mprotect(0x600000, 4096, PROT_READ)     = 0
mprotect(0x7f93015ca000, 4096, PROT_READ) = 0
munmap(0x7f93015a8000, 116129)          = 0
open("/tmp/my-test.ko", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0664, st_size=11036, ...}) = 0
mmap(NULL, 11036, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) = 0x7f93015c0000
init_module(0x7f93015c0000, 11036, NULL) = -1 EFAULT (Bad address)
munmap(0x7f93015c0000, 11036)           = 0
close(3)                                = 0
exit_group(-4)                          = ?
+++ exited with 252 +++

如果我使用insmod或创建我自己的finit_module包装器,模块会正常加载。

应该怎么做?

2 个答案:

答案 0 :(得分:3)

init_module(const char *param_values)的第3个参数是NULL。你应该使它不为空......一个空字符串就足够了。

res = init_module(buf, sb.st_size, "");

答案 1 :(得分:0)

在C

中使用system()库函数
  

system("insmod ~/hello_world.ko");