Linux 使用C语言来加载和卸载内核模块

2024-04-16 09:20

本文主要是介绍Linux 使用C语言来加载和卸载内核模块,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 一、insmod/rmmod
    • 1.1 insmod
    • 1.2 rmmod
  • 二、C语言示例
    • 2.1 syscall
    • 2.2 dmeo

一、insmod/rmmod

1.1 insmod

Linux 使用insmod来加载内核模块:

NAMEinsmod - Simple program to insert a module into the Linux Kernel

使用strace来追踪其过程:

[root@localhost helloworld]# strace insmod helloworld.ko
execve("/usr/sbin/insmod", ["insmod", "helloworld.ko"], 0x7fffff3cc6e8 /* 27 vars */) = 0
......
open("/root/module/helloworld/helloworld.ko", O_RDONLY|O_CLOEXEC) = 3
.....
finit_module(3, "", 0)                  = 0
......
close(3)                                = 0
[root@localhost helloworld]#

可以看到使用finit_module系统调用来加载内核模块:

// linux-5.15/kernel/module.cSYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
{......return load_module(&info, uargs, flags);
}

1.2 rmmod

Linux 使用shell命令rmmod来卸载内核模块:

NAMErmmod - Simple program to remove a module from the Linux Kernel

使用strace来追踪其过程:

[root@localhost helloworld]# strace rmmod helloworld
execve("/usr/sbin/rmmod", ["rmmod", "helloworld"], 0x7ffdc01ddef8 /* 27 vars */) = 0
......
delete_module("helloworld", O_NONBLOCK) = 0
// linux-5.15/kernel/module.cSYSCALL_DEFINE2(delete_module, const char __user *, name_user,unsigned int, flags)
{......
}

二、C语言示例

2.1 syscall

NAMEsyscall - indirect system callSYNOPSIS#define _GNU_SOURCE         /* See feature_test_macros(7) */#include <unistd.h>#include <sys/syscall.h>   /* For SYS_xxx definitions */int syscall(int number, ...);DESCRIPTIONsyscall()  is a small library function that invokes the system call whose assembly language interface has the specified number with the specified arguments.  Employ‐ing syscall() is useful, for example, when invoking a system call that has no wrapper function in the C library.syscall() saves CPU registers before making the system call, restores the registers upon return from the system call, and stores any error code returned by the  sys‐tem call in errno(3) if an error occurs.Symbolic constants for system call numbers can be found in the header file <sys/syscall.h>.

syscall()是一个小型的库函数,用于调用具有指定编号和指定参数的汇编语言接口的系统调用。使用syscall()函数非常有用,特别是在调用在C库中没有封装函数的系统调用时。

2.2 dmeo

(1)C语言示例:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <fcntl.h>int load_module(const char *modulePath) {int fd = open(modulePath, O_RDONLY);if (fd < 0) {perror("Failed to open module");return 1;}int result = syscall(__NR_finit_module, fd, "", 0);if (result < 0) {perror("Failed to load module");close(fd);return 1;}printf("Module loaded successfully\n");close(fd);return 0;
}int unload_module(const char *moduleName) {int result = syscall(__NR_delete_module, moduleName, O_NONBLOCK);if (result < 0) {perror("Failed to unload module");return 1;}printf("Module unloaded successfully\n");return 0;
}int main() {const char *modulePath = "./helloworld.ko";const char *moduleName = "helloworld";int loadResult = load_module(modulePath);if (loadResult != 0) {printf("Failed to load module\n");return 1;}int unloadResult = unload_module(moduleName);if (unloadResult != 0) {printf("Failed to unload module\n");return 1;}return 0;
}

(2)内核模块示例:

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>//内核模块初始化函数
static int __init lkm_init(void)
{printk("Hello World\n");return 0;
}//内核模块退出函数
static void __exit lkm_exit(void)
{printk("Goodbye\n");
}module_init(lkm_init);
module_exit(lkm_exit);MODULE_LICENSE("GPL");

结果:

[root@localhost helloworld]# ./a.out
Module loaded successfully
Module unloaded successfully[root@localhost helloworld]# dmesg -c
[368000.085692] Hello World
[368000.086536] Goodbye

这篇关于Linux 使用C语言来加载和卸载内核模块的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/908395

相关文章

一文详解SpringBoot中控制器的动态注册与卸载

《一文详解SpringBoot中控制器的动态注册与卸载》在项目开发中,通过动态注册和卸载控制器功能,可以根据业务场景和项目需要实现功能的动态增加、删除,提高系统的灵活性和可扩展性,下面我们就来看看Sp... 目录项目结构1. 创建 Spring Boot 启动类2. 创建一个测试控制器3. 创建动态控制器注

使用Docker构建Python Flask程序的详细教程

《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker

Python使用vllm处理多模态数据的预处理技巧

《Python使用vllm处理多模态数据的预处理技巧》本文深入探讨了在Python环境下使用vLLM处理多模态数据的预处理技巧,我们将从基础概念出发,详细讲解文本、图像、音频等多模态数据的预处理方法,... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

在Linux中改变echo输出颜色的实现方法

《在Linux中改变echo输出颜色的实现方法》在Linux系统的命令行环境下,为了使输出信息更加清晰、突出,便于用户快速识别和区分不同类型的信息,常常需要改变echo命令的输出颜色,所以本文给大家介... 目python录在linux中改变echo输出颜色的方法技术背景实现步骤使用ANSI转义码使用tpu

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文