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

相关文章

Java使用ANTLR4对Lua脚本语法校验详解

《Java使用ANTLR4对Lua脚本语法校验详解》ANTLR是一个强大的解析器生成器,用于读取、处理、执行或翻译结构化文本或二进制文件,下面就跟随小编一起看看Java如何使用ANTLR4对Lua脚本... 目录什么是ANTLR?第一个例子ANTLR4 的工作流程Lua脚本语法校验准备一个Lua Gramm

Java Optional的使用技巧与最佳实践

《JavaOptional的使用技巧与最佳实践》在Java中,Optional是用于优雅处理null的容器类,其核心目标是显式提醒开发者处理空值场景,避免NullPointerExce... 目录一、Optional 的核心用途二、使用技巧与最佳实践三、常见误区与反模式四、替代方案与扩展五、总结在 Java

Linux内核参数配置与验证详细指南

《Linux内核参数配置与验证详细指南》在Linux系统运维和性能优化中,内核参数(sysctl)的配置至关重要,本文主要来聊聊如何配置与验证这些Linux内核参数,希望对大家有一定的帮助... 目录1. 引言2. 内核参数的作用3. 如何设置内核参数3.1 临时设置(重启失效)3.2 永久设置(重启仍生效

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

使用Python构建一个Hexo博客发布工具

《使用Python构建一个Hexo博客发布工具》虽然Hexo的命令行工具非常强大,但对于日常的博客撰写和发布过程,我总觉得缺少一个直观的图形界面来简化操作,下面我们就来看看如何使用Python构建一个... 目录引言Hexo博客系统简介设计需求技术选择代码实现主框架界面设计核心功能实现1. 发布文章2. 加

如何将Python彻底卸载的三种方法

《如何将Python彻底卸载的三种方法》通常我们在一些软件的使用上有碰壁,第一反应就是卸载重装,所以有小伙伴就问我Python怎么卸载才能彻底卸载干净,今天这篇文章,小编就来教大家如何彻底卸载Pyth... 目录软件卸载①方法:②方法:③方法:清理相关文件夹软件卸载①方法:首先,在安装python时,下

kali linux 无法登录root的问题及解决方法

《kalilinux无法登录root的问题及解决方法》:本文主要介绍kalilinux无法登录root的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录kali linux 无法登录root1、问题描述1.1、本地登录root1.2、ssh远程登录root2、

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的