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

相关文章

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter

MySQL中比较运算符的具体使用

《MySQL中比较运算符的具体使用》本文介绍了SQL中常用的符号类型和非符号类型运算符,符号类型运算符包括等于(=)、安全等于(=)、不等于(/!=)、大小比较(,=,,=)等,感兴趣的可以了解一下... 目录符号类型运算符1. 等于运算符=2. 安全等于运算符<=>3. 不等于运算符<>或!=4. 小于运

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

Python 字典 (Dictionary)使用详解

《Python字典(Dictionary)使用详解》字典是python中最重要,最常用的数据结构之一,它提供了高效的键值对存储和查找能力,:本文主要介绍Python字典(Dictionary)... 目录字典1.基本特性2.创建字典3.访问元素4.修改字典5.删除元素6.字典遍历7.字典的高级特性默认字典

使用Python构建一个高效的日志处理系统

《使用Python构建一个高效的日志处理系统》这篇文章主要为大家详细讲解了如何使用Python开发一个专业的日志分析工具,能够自动化处理、分析和可视化各类日志文件,大幅提升运维效率,需要的可以了解下... 目录环境准备工具功能概述完整代码实现代码深度解析1. 类设计与初始化2. 日志解析核心逻辑3. 文件处

一文详解如何使用Java获取PDF页面信息

《一文详解如何使用Java获取PDF页面信息》了解PDF页面属性是我们在处理文档、内容提取、打印设置或页面重组等任务时不可或缺的一环,下面我们就来看看如何使用Java语言获取这些信息吧... 目录引言一、安装和引入PDF处理库引入依赖二、获取 PDF 页数三、获取页面尺寸(宽高)四、获取页面旋转角度五、判断

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Linux进程CPU绑定优化与实践过程

《Linux进程CPU绑定优化与实践过程》Linux支持进程绑定至特定CPU核心,通过sched_setaffinity系统调用和taskset工具实现,优化缓存效率与上下文切换,提升多核计算性能,适... 目录1. 多核处理器及并行计算概念1.1 多核处理器架构概述1.2 并行计算的含义及重要性1.3 并

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.