创建自己的/proc文件——processinfo

Cited from http://www.lupaworld.com/home-space-uid-401174-do-blog-id-149701.html

前面写过一个模块,创建内核进程之kernel_thread获取到了进程的一些信息。结合最近看得/proc文件系统,我写了个模块,将获取的信息读入到/proc文件中。
        主要思想是:利用proc_mkdir()创建一个mydir,再利用create_proc_read_entry()函数创建一个processinfo文件。我们从模块里面获取的信息都将写入到processinfo文件中。
        还是现看看具体的代码吧。
#include <linux/module.h>
#include <linux/list.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <linux/kthread.h>
#include <linux/proc_fs.h>
//#define tasklist_lock_address 0xc04f1980 
static struct proc_dir_entry *tempdir, *processinfo_file;
static int proc_read_processinfo(char *page, char **start, off_t offset,int count, int *eof, void *data)  
{
    struct task_struct *pos, *task;
    static char buf[1024*8] = {0};
    char tmp[128] = {0};
    task = &init_task; 
       if(offset  > 0){
            return 0;
    }
      memset(buf, 0, sizeof(buf));
//    read_lock(tasklist_lock_address);
        list_for_each_entry(pos, &task->tasks, tasks){
            sprintf(tmp,"%d\t\t%s\n",pos->pid, pos->comm);
        strcat(buf, tmp);
       memset(tmp, 0 sizeof(tmp));
    }
//    read_unlock(tasklist_lock_address);
        *start=buf;
        return strlen(buf);
}
static int __init hello_list_process(void)
{
    int rv = 0;
    printk("kernel,I am coming!...........\n");
    tempdir = proc_mkdir("mydir", NULL);
    if(tempdir == NULL){
        rv = -ENOMEM;
        return rv;
    }
    
    processinfo_file = create_proc_read_entry("processinfo", 0444, tempdir, proc_read_processinfo, NULL);
        if(processinfo_file == NULL){
                rv = -ENOMEM;
                remove_proc_entry("mydir", NULL);
        }
    printk("%s initialised\n", "mydir");
    printk("create processinfo success!\n");
    return 0;
}
static void __exit bye_list_process(void)
{    
    remove_proc_entry("processinfo", tempdir);
    remove_proc_entry("mydir", NULL);
    printk("bye kernel.......\n");
}
module_init(hello_list_process);
module_exit(bye_list_process);
MODULE_LICENSE("GPL");
        代码思路很简单。创建文件成功后,利用宏list_for_each_entry()获取进程信息,并全部导入到processinfo中。若创建失败则 remove_proc_entry()。
        首先看一下结构体proc_dir_entry
        在/include/linux/proc_fs.h中:
struct proc_dir_entry {
unsigned int low_ino;
        unsigned short namelen;
        const char *name;
        mode_t mode;
        nlink_t nlink;
        uid_t uid;
        gid_t gid;
        loff_t size;
        const struct inode_operations *proc_iops;
        /*
         * NULL ->proc_fops means "PDE is going away RSN" or
         * "PDE is just created". In either case, e.g. ->read_proc won't be
         * called because it's too late or too early, respectively.
         *
         * If you're allocating ->proc_fops dynamically, save a pointer
         * somewhere.
         */
        const struct file_operations *proc_fops;
        struct proc_dir_entry *next, *parent, *subdir;
        void *data;
        read_proc_t *read_proc;
        write_proc_t *write_proc;
        atomic_t count;         /* use count */
        int pde_users;  /* number of callers into module in progress */
        spinlock_t pde_unload_lock; /* proc_fops checks and pde_users bumps *

        常用字段: read_proc_t *read_proc;,write_proc_t *write_proc.proc文件的读函数,写函数。read_proc_t 类型将在后面介绍。
        具体的我们来看看下面几个函数。
        proc_mkdir()主要功能是创建一个目录.函数原型为:
        struct proc_dir_entry *proc_mkdir(const char *name,struct proc_dir_entry *parent);
        参数说明如下:name就是你定义的目录名称。parent就是你定义的这个目录的父目录。若为NULL,表示为/proc。
        create_proc_read_entry()这个函数主要功能是创建一个只读文件。关于使用这个函数,我是然了很久关于只读只写这个问题的。因为内核里面还有create_proc_entry()函数可以创建可读可写文件。所以没想明白这个文件的权限时,我没发确定用哪个函数。
        那么我们来看看这个文件究竟应该是怎么样的权限了?对于内核的话,对于这个文件就应该是可写的,可是对于用户应该对这个文件只是可读的,那这个文件究竟是可读还是可写还是可读又可写。最后我然清楚了两个概念,才算是明白了。用户态数据,内核态数据。其实这个文件应该是对用户可读的,其实这也即使说对内核是可写的。那么我们要创建的就是一个可读文件。用户读的过程其实就是内核写的过程。那么我们就可以用create_proc_read_entry()这个函数直接创建一个可读的文件即可。
        看看create_proc_read_entry()的原型。static inline struct proc_dir_entry *create_proc_read_entry(const char *name, mode_t mode, struct proc_dir_entry *base,  read_proc_t *read_proc, void * data)
        参数描述如下:name为定义的文件名,mode为对该proc节点的访问权限, base指定建立的proc节点所在的目录。如果为NULL则指/proc目录,read_proc是建立的文件的读函数指针,参数data为该文件的专用数据,它将保存在该文件对应的struct file结构的private_data字段中。
        在介绍我自己写的/proc文件读函数proc_read_processinfo()函数之前,先介绍一下:
        typedef int (read_proc_t)(char *page, char **start, off_t off,
                          int count, int *eof, void *data);
        page:把需要返回给信息写入page(最好不要超过PAGE_SIZE(通常4K),否则比较麻烦)
        start:顾名思义,就是开始的指针。不经常使用。当然个人觉得这也暗示某个关键时候要用它。当我们写的数据的大小大于一个页面的时候,需要*start指针控制写入page的起始地址。例如在本模块中,数据的大小是1024*8,是8K,就需要用到*start指针。否则会出现乱码。
        off:page的偏移量,表示page已有的数据。例如本模块,如果off > 0,page原来就有数据,我们则跳出read函数,即不再往page里面写数据。
        count:用户所要读取的字节数目。
        peof:当读到文件尾时,置peof为1。
        data:当这个函数被多个proc文件定义为“读”函数时,我们可以通过这个指针传递参数。
        proc_read_processinfo()函数就是这样类型的函数。
我们写的这个函数,是将buf数组里面的数据写入page中。里面的memset函数主要只是起一个清洁数组的作用。不让这次写入的数据残留到下次的数据。
        remove_proc_entry()函数就是删除节点。函数原型为: void remove_proc_entry(const char *name, struct proc_dir_entry *parent).参数描述如下:name为你要删除的文件或者目录,是字符串型的。parent是它的父目录,注意它是struct proc_dir_entry指针型。  
        关于程序中注释掉的部分//#define tasklist_lock_address 0xc04f1980 ,//    read_lock(tasklist_lock_address);//    read_unlock(tasklist_lock_address);刚开始使用tasklist_lock时,是考虑到读取的时候需要锁上,保护读的过程。tasklist_lock读写自旋锁,保护以init_task为头节点的进程链表。那么就使用它了,可是发现一个问题,就是加载模块后提示:Undefined Symbol。dmesg一下发现提示是:Unknown symbol tasklist_lock。打开/proc/kallsyms里面发现c04f1980 D tasklist_lock然后就这样给用上了。这样应该感觉不对,虽然没有了错误,但是提示有警告。
        整个程序下来运行结果是:
susu@corechen-desktop:/proc$ cd mydir/
susu@corechen-desktop:/proc/mydir$ ls
processinfo
susu@corechen-desktop:/proc/mydir$ cat processinfo 
1        init
2        kthreadd
3        migration/0
4        ksoftirqd/0
5        watchdog/0
6        migration/1
7        ksoftirqd/1
8        watchdog/1
9        events/0
10        events/1
11        cpuset
12        khelper
17        async/mgr
67        sync_supers
69        bdi-default
71        kblockd/0
72        kblockd/1
74        kacpid
75        kacpi_notify
76        kacpi_hotplug
162        kseriod
218        khungtaskd
219        kswapd0
264        aio/0
265        aio/1
268        crypto/0
269        crypto/1
1534        ksuspend_usbd
1542        khubd
1672        ata/0
1673        ata/1
1674        ata_aux
1685        scsi_eh_0
1687        scsi_eh_1
2663        kjournald
2848        udevd
3049        kpsmoused
4590        getty
4591        getty
4593        getty
4594        getty
4595        getty
4717        acpid
4741        kconservative/0
4742        kconservative/1
4756        kondemand/0
4757        kondemand/1
4826        syslogd
4884        dd
4886        klogd
4908        dbus-daemon
4924        NetworkManager
4938        NetworkManagerD
4951        system-tools-ba
4973        sshd
5003        avahi-daemon
5004        avahi-daemon
5035        flush-8:0
5056        mysqld_safe
5098        mysqld
5099        logger
5192        cupsd
5209        nginx
5210        nginx
5305        winbindd
5352        winbindd
5353        dhcdbd
5372        hald
5375        console-kit-dae
5376        hald-runner

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值