双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

内核模块无法卸载

zhoufanking zhoufanking 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
早上写了个模块加载到内核里,结果卸载的时候说设备忙?我用lsmod看了一下,我的模块used by那一栏里是0[permanent],之后我又加载了一个以前写的module,一切正常,lsmod 中 used by 就没有那个[permanent],这个是什么意思啊?如何会导致一个模块的used by里变成permanent?下面是module的代码和makefile
/**************module code********************/
include <linux/proc_fs.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/ip.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("ZHOUFAN");

#define proc_name "my_proc"


static struct proc_dir_entry *proc_file;
static struct nf_hook_ops nfinfo;

static int frame_count = 0;

int rd_proc(char *page, char **start, off_t off, int count, int *eof, void *data)
{
int ret;
printk(KERN_INFO"procfile_read(/proc/%s) called\n",proc_name);

if ( off > 0)
{
ret = 0;
}else {
ret = sprintf(page,"frame_count is %d \n",frame_count);
}

return ret;
}

unsigned int my_hook_fn( unsigned int hooknum,
struct sk_buff **skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn) ( struct sk_buff *))
{
frame_count++;

return NF_DROP;
}


int init_module()
{

nfinfo.hook = my_hook_fn;
nfinfo.pf = PF_INET;
nfinfo.hooknum = NF_IP_POST_ROUTING;
nfinfo.priority = NF_IP_PRI_FIRST;

nf_register_hook( &nfinfo );

proc_file = create_proc_entry(proc_name,0,NULL);

if( NULL == proc_file)

{
remove_proc_entry(proc_name,&proc_root);
printk(KERN_ALERT"Error:could not initialize /proc/%s\n",proc_name);
return -ENOMEM;
}

proc_file->read_proc= rd_proc;


printk(KERN_INFO"/proc/%s created\n", proc_name);
return 0;
}

void cleanup_moduel()
{
printk(KERN_INFO"entering cleanup module!\n");

nf_unregister_hook( &nfinfo );

printk(KERN_INFO"hook function unregistered\n");

remove_proc_entry(proc_name,&proc_root);

printk(KERN_INFO"/proc/%s removed\n", proc_name);
}

...全文

相关推荐

评论 5
收藏 已收藏
分享
返回顶部
zhoufanking
Linux_Kernel 4,469 成员 17,458 内容