Chinaunix首页 | 论坛 | 博客
  • 博客访问: 939240
  • 博文数量: 633
  • 博客积分: 30780
  • 博客等级: 大将
  • 技术积分: 7532
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-12 21:07
文章分类

全部博文(633)

文章存档

2011年(10)

2010年(500)

2009年(47)

2008年(76)

我的朋友

分类:

2008-07-23 17:18:02

简陋的类似ps的driver,在/proc/steven_process下面存放了当前系统的进程列表
/*scan_process.c*/

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/slab.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Steven");

int my_read_proc(char *page, char **start, off_t offset, int count,
         int *eof, void *data);

static int __init scan_process_init(void)
{
    create_proc_read_entry("steven_process", 0, NULL, my_read_proc, NULL);
    return 1;
}

static void __exit scan_process_exit(void)
{
    remove_proc_entry("steven_process", NULL);
    return;
}

int my_read_proc(char *page, char **start, off_t offset, int count, int *eof, void *data)
{
    int len = 0;
    int res;
    struct task_struct *p_struct;

    char *buf = kmalloc(sizeof(char) * 1024, GFP_KERNEL);
    if (NULL == buf)
    {
        printk("steven: kmalloc error!\n");
        return 0;
    }
    memset(buf, 0, sizeof(char) * 1024);
    for_each_process(p_struct)
    {
        len += sprintf(buf + len, "%d\t\t%d\t\t%s\n", p_struct->pid, p_struct->parent->pid, p_struct->comm);
    }
    res = strlen(buf);
    strncpy(page, buf, res);
    return res;
}

module_init(scan_process_init);
module_exit(scan_process_exit);



对应的Makefile

ifneq ($(KERNELRELEASE),)
    obj-m := scan_process.o
else
    KERNELDIR ?= /lib/modules/$(shell uname -r)/build
    PWD := $(shell pwd)
default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
install:
    /sbin/insmod ./scan_process.ko
uninstall:
    /sbin/rmmod scan_process.ko
clean:
    rm -rf *.o *.ko *.mod*
endif


非常简陋,只是用于测试proc文件系统的接口。
阅读(623) | 评论(0) | 转发(0) |
0

上一篇:街头艺人

下一篇:怎样添加sysfs?

给主人留下些什么吧!~~