c - 如何从 shell 触发内核模块?

标签 c linux linux-kernel kernel-module

我正在使用 Ubuntu 和 VirtualBox。 我正在为我的 shell 定义一个新命令来输出子进程的一些特征(如兄弟树等)。为了输出这些特性,我创建了一个内核模块并使用了task_struct。我还在我的 shell 之外测试了我的内核模块并且它可以工作。 现在我的问题是如何在我的 shell 中触发这个内核模块(用 C 代码)以便加载我的内核模块?

我搜索了一下,发现需要用到modprobe或insmod之类的系统调用,但不明白怎么用。我尝试了下面的代码,但没有用:

setuid(0);

system("/sbin/insmod /.../mymodule.ko");

感谢您的帮助。

最佳答案

使用system()加载模块

您正在尝试在您的应用程序中成为 root(通过执行 setuid(0)),但您没有权限这样做(如果您运行作为普通用户编程)。相反,您应该检查您的程序是否从根目录运行(使用 getuid())。此外,最好测试一下您的模块文件是否存在。下面是此类代码的示例(它已经过测试并执行所有需要的检查):

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

#define ROOT_UID    0
#define INSMOD_PATH "/sbin/insmod"
#define MOD_PATH    "/.../mymodule.ko"

int main(void)
{
    uid_t uid;
    int res;

    /* Check if program being run by root */
    uid = getuid();
    if (uid != ROOT_UID) {
        fprintf(stderr, "Error: Please run this program as root\n");
        return EXIT_FAILURE;
    }

    /* Check if module file exists */
    if (access(MOD_PATH, F_OK) == -1) {
        fprintf(stderr, "Error: File \"%s\" doesn't exist\n", MOD_PATH);
        return EXIT_FAILURE;
    }

    /* Load module */
    res = system(INSMOD_PATH " " MOD_PATH);
    if (res != 0) {
        fprintf(stderr, "Error loading module: %d\n", res);
        return EXIT_FAILURE;
    }

    printf("Module \"%s\" was successfully loaded\n", MOD_PATH);

    return EXIT_SUCCESS;
}

将这段代码保存为main.c 文件。请务必将 MOD_PATH 定义替换为模块文件的实际路径。

使用下一个命令编译它:

$ gcc -Wall -O2 main.c -o load_module

现在做下一步:

$ su
# ./load_module
  1. 第一个命令将您的用户切换到 root(您将被要求输入 root 密码)。如果您不知道 root 密码,请尝试使用 sudo -s 命令代替 su
  2. 第二个命令运行您的程序。

请注意命令提示符处的最后一个字符:

  • # 表示此时您拥有 root 权限
  • $ 表示您只有普通用户权限。

使用finit_module()加载模块

在 C 中使用 system() 函数通常被认为是一种不好的做法(因为它需要花费大量时间来执行并且基本上只是试图替换一个更简单的 Bash 脚本)。

如果你想在不使用 system() 的情况下加载 C 内核模块,你可以查看 insmod 工具的源代码。参见 libkmod/libkmod-module.c 文件,kmod_module_insert_module() 函数。你可以看到那些来源 here .

注意finit_module()函数调用。关于这个的一个很好的解释system call可以在手册页中找到:

$ man finit_module

这是一个如何使用 finit_module() 系统调用的例子:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <sys/stat.h>
#include <fcntl.h>

#define ROOT_UID    0
#define MOD_PATH    "/.../mymodule.ko"

static inline int finit_module(int fd, const char *uargs, int flags)
{
    return syscall(__NR_finit_module, fd, uargs, flags);
}

int main(void)
{
    uid_t uid;
    long res;
    int fd;

    /* Check if program being run by root */
    uid = getuid();
    if (uid != ROOT_UID) {
        fprintf(stderr, "Error: Please run this program as root\n");
        return EXIT_FAILURE;
    }

    /* Check if module file exists */
    if (access(MOD_PATH, F_OK) == -1) {
        fprintf(stderr, "Error: File \"%s\" doesn't exist\n", MOD_PATH);
        return EXIT_FAILURE;
    }

    /* Load module */
    fd = open(MOD_PATH, O_RDONLY | O_CLOEXEC);
    if (fd < 0) {
        perror("Unable to open module file");
        return EXIT_FAILURE;
    }
    res = finit_module(fd, "", 0);
    if (res != 0) {
        perror("Error when loading module");
        close(fd);
        return EXIT_FAILURE;
    }
    close(fd);

    printf("Module \"%s\" was successfully loaded\n", MOD_PATH);

    return EXIT_SUCCESS;
}

关于c - 如何从 shell 触发内核模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29183713/

相关文章:

java - 从 Java 中的 Android Activity 写入/proc 文件系统

c - 内核中是否有与 perror 等效的东西?

C——链表

c - 删除链表中的第一个节点

python - 从 python 运行 linux 命令

linux - Jenkins shell 字符串引用替换

linux - 哪个 Node 指向usr/bin/node,cd usr/bin/node不存在

linux-kernel - 我应该在Linux内核开发中使用哪种互斥锁变量?

c - 在结构体中填充 int 数组

C while 循环跳过输入