zoukankan      html  css  js  c++  java
  • 3.10.17 procfs示例


    /*
     *    wesley
     *    the linux module sample of procfs
     *    
     *    wesley for testing
     *    2013-12-23 14:50:14
     
     *    insmod sampleproc.ko     加载模块
     *    rmmod sampleproc             卸载模块

     *    dmesg 来查看显示内容
     *    lsmod 显示加载模块
     
     http://blog.chinaunix.net/uid-23390992-id-3026872.html
     http://blog.chinaunix.net/uid-24432676-id-2607766.html
     
    */

    #include <linux/init.h>
    #include <linux/module.h>
    #include <linux/types.h>
    #include <linux/slab.h>
    #include <linux/fs.h>
    #include <linux/proc_fs.h>
    #include <linux/seq_file.h>
    #include <net/net_namespace.h>
    #include <linux/mm.h>

    MODULE_LICENSE("XJLI/GPL");

    struct proc_data_t{
        int a;
        int b;
    } ;


    struct _DATA_INFO{
        int data1;
        int data2;
    };
    static struct _DATA_INFO data_info[2];


    static void *sampleproc_seq_start(struct seq_file *s, loff_t *pos)
    {
        static unsigned long counter = 0;
        if ( *pos == 0 )
        {
            return &counter;
        }
        else
        {
            *pos = 0;
            return NULL;
        }
    }
    static void *sampleproc_seq_next(struct seq_file *s, void *v, loff_t *pos)
    {
        return NULL;
    }
    static void sampleproc_seq_stop(struct seq_file *s, void *v)
    {
    }

    static int sampleproc_seq_show(struct seq_file *s, void *v)
    {
        struct inode *i = s->private;
        struct _DATA_INFO *info = PDE_DATA(i);
        
        seq_printf(s, "%d----%d", info->data1, info->data2);
        
        return 0;
    }

    struct proc_data_t data;

    static struct seq_operations sampleproc_seq_ops = {
        .start = sampleproc_seq_start,
        .next = sampleproc_seq_next,
        .stop = sampleproc_seq_stop,
        .show = sampleproc_seq_show
    };

    static int sampleproc_proc_open(struct inode *inode, struct file *file)
    {
        int ret = seq_open(file, &sampleproc_seq_ops);
        if (!ret) {
            struct seq_file *sf = file->private_data;
            sf->private = inode;
        }
        
        return ret;
    }

    static const struct file_operations sampleproc_file_ops = {
        .owner   = THIS_MODULE,
        .open    = sampleproc_proc_open,
        .read    = seq_read,
        .llseek  = seq_lseek,
        .release = seq_release,
    };

    static int sampleproc_init(void)
    {
        //struct proc_dir_entry *proc;
        
        data.a = 10;
        data.b = 20;
        
        printk(KERN_INFO "sampleproc_init");
        
        data_info[0].data1=1;
        data_info[0].data2=2;
        proc_create_data("proc_test1", 0, NULL, &sampleproc_file_ops, &data_info[0]);
        data_info[1].data1=3;
        data_info[1].data2=4;
        proc_create_data("proc_test2", 0, NULL, &sampleproc_file_ops, &data_info[1]);
        
        
        /*第三个参数设置为NULL,默认在/proc/下建立名字为test_proc的文件*/
        //proc = proc_create("sampleproc", 0, NULL, &sampleproc_file_ops);
        //if (!proc)
        //    goto -1;
        
        return 0;
    }

    static void sampleproc_exit(void)
    {
        printk(KERN_INFO "sampleproc_exit");

        remove_proc_entry("proc_test1", NULL);
        remove_proc_entry("proc_test2", NULL);
    }

    module_init(sampleproc_init);
    module_exit(sampleproc_exit);

  • 相关阅读:
    Linux_C smsh1
    ACM&排序问题,操作符重载
    ACM&找双亲,并查集
    struct dirent/DIR
    关于win8如何查找出当前的密钥
    php之留言板
    php之include&require
    工作中的问题 和 所用到的知识点
    jQuery.extend 和 jQuery.fn.extend
    JavaScript 字符串函数 之查找字符方法(一)
  • 原文地址:https://www.cnblogs.com/mull/p/4477796.html
Copyright © 2011-2022 走看看