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

相关文章

使用Dify访问mysql数据库详细代码示例

《使用Dify访问mysql数据库详细代码示例》:本文主要介绍使用Dify访问mysql数据库的相关资料,并详细讲解了如何在本地搭建数据库访问服务,使用ngrok暴露到公网,并创建知识库、数据库访... 1、在本地搭建数据库访问的服务,并使用ngrok暴露到公网。#sql_tools.pyfrom

使用mvn deploy命令上传jar包的实现

《使用mvndeploy命令上传jar包的实现》本文介绍了使用mvndeploy:deploy-file命令将本地仓库中的JAR包重新发布到Maven私服,文中通过示例代码介绍的非常详细,对大家的学... 目录一、背景二、环境三、配置nexus上传账号四、执行deploy命令上传包1. 首先需要把本地仓中要

Spring Cloud之注册中心Nacos的使用详解

《SpringCloud之注册中心Nacos的使用详解》本文介绍SpringCloudAlibaba中的Nacos组件,对比了Nacos与Eureka的区别,展示了如何在项目中引入SpringClo... 目录Naacos服务注册/服务发现引⼊Spring Cloud Alibaba依赖引入Naco编程s依

Java springBoot初步使用websocket的代码示例

《JavaspringBoot初步使用websocket的代码示例》:本文主要介绍JavaspringBoot初步使用websocket的相关资料,WebSocket是一种实现实时双向通信的协... 目录一、什么是websocket二、依赖坐标地址1.springBoot父级依赖2.springBoot依赖

C语言中的浮点数存储详解

《C语言中的浮点数存储详解》:本文主要介绍C语言中的浮点数存储详解,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、首先明确一个概念2、接下来,讲解C语言中浮点型数存储的规则2.1、可以将上述公式分为两部分来看2.2、问:十进制小数0.5该如何存储?2.3 浮点

Linux下修改hostname的三种实现方式

《Linux下修改hostname的三种实现方式》:本文主要介绍Linux下修改hostname的三种实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下修改ho编程stname三种方式方法1:修改配置文件方法2:hFvEWEostnamectl命

Java使用Mail构建邮件功能的完整指南

《Java使用Mail构建邮件功能的完整指南》JavaMailAPI是一个功能强大的工具,它可以帮助开发者轻松实现邮件的发送与接收功能,本文将介绍如何使用JavaMail发送和接收邮件,希望对大家有所... 目录1、简述2、主要特点3、发送样例3.1 发送纯文本邮件3.2 发送 html 邮件3.3 发送带

Linux虚拟机不显示IP地址的解决方法(亲测有效)

《Linux虚拟机不显示IP地址的解决方法(亲测有效)》本文主要介绍了通过VMware新装的Linux系统没有IP地址的解决方法,主要步骤包括:关闭虚拟机、打开VM虚拟网络编辑器、还原VMnet8或修... 目录前言步骤0.问题情况1.关闭虚拟机2.China编程打开VM虚拟网络编辑器3.1 方法一:点击还原VM

Android WebView无法加载H5页面的常见问题和解决方法

《AndroidWebView无法加载H5页面的常见问题和解决方法》AndroidWebView是一种视图组件,使得Android应用能够显示网页内容,它基于Chromium,具备现代浏览器的许多功... 目录1. WebView 简介2. 常见问题3. 网络权限设置4. 启用 JavaScript5. D

使用DeepSeek搭建个人知识库(在笔记本电脑上)

《使用DeepSeek搭建个人知识库(在笔记本电脑上)》本文介绍了如何在笔记本电脑上使用DeepSeek和开源工具搭建个人知识库,通过安装DeepSeek和RAGFlow,并使用CherryStudi... 目录部署环境软件清单安装DeepSeek安装Cherry Studio安装RAGFlow设置知识库总