- 论坛徽章:
- 0
|
我在论坛上找到了一个进程遍历的模块代码,我在make的时候却发生以下错误:
make -C /lib/modules/uname -r /build M="pwd" modules
make[1]: Entering directory `/lib/modules/uname'
make[1]: *** 没有规则可以创建目标“/build”。 停止。
make[1]: Leaving directory `/lib/modules/uname'
make: *** [default] 错误 2
我查找了很多的资料但是却还是无法解决,将内核重新编译过后还是不行,希望各位大大可以帮帮我,到底怎么解决这个问题
以下是我找到的代码的地址,我用的是第一套的代码和makefile代码和用户代码
http://bbs.chinaunix.net/viewthread.php?tid=653764&extra=page%3D1%26filter%3Ddigest#
代码:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/init.h>
#include <linux/sched.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("mq110");
static int ps_read(char *page, char **start, off_t offset,int count, int *eof,
void *data)
{
static char buf[1024*8]={0};
char tmp[128];
struct task_struct *p;
if(offset >0)
return 0;
memset(buf,0,sizeof(buf));
read_lock(&tasklist_lock);
for_each_process(p) //遍例内核进程链表.
{
sprintf(tmp,"%d\t\t%d\t\t\t%s\n",p->pid,p->parent->pid,p->comm);
strcat(buf,tmp);
memset(tmp,0,sizeof(tmp));
}
read_unlock(&tasklist_lock);
*start=buf;
return strlen(buf);
}
static __init int ps_init(void)
{
struct proc_dir_entry *entry;
entry = create_proc_entry("_ps", 0444, &proc_root); //建立/proc/_ps文件.
if(entry == 0)
{
printk(KERN_ERR "create_proc_entry failed!\n");
return -1;
}
entry->mode = S_IFREG | 0444;
entry->size = 0;
entry->read_proc = ps_read;
return 0;
}
static __exit void ps_cleanup(void)
{
remove_proc_entry("_ps", &proc_root);
}
module_init(ps_init);
module_exit(ps_cleanup);
makefile:
TARGET = 006
obj-m := $(TARGET).o
KERNELDIR=/lib/modules/`uname -r`/build
PWD=`pwd`
default :
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
install :
insmod $(TARGET).ko
uninstall :
rmmod $(TARGET).ko
clean :
rm -rf *.o *.mod.c *.ko
用户态程序:
#include <stdio.h>
#include <error.h>
int main()
{
FILE *fp;
char buf[1024];
fp=fopen("/proc/_ps","r");
if(fp==NULL)
{
perror("fopen");
return -1;
}
printf("pid\t\tppid\t\t\tcommand\n");
while(!feof(fp))
{
if(fgets(buf,sizeof(buf),fp)!=NULL)
printf("%s",buf);
}
fclose(fp);
return 0;
}
希望各位大大可以帮忙... |
|