/*
 *    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);

3.10.17 procfs示例的更多相关文章

  1. 背水一战 Windows 10 (17) - 动画: ThemeTransition(过渡效果)

    [源码下载] 背水一战 Windows 10 (17) - 动画: ThemeTransition(过渡效果) 作者:webabcd 介绍背水一战 Windows 10 之 动画 ThemeTrans ...

  2. 发现struct proc_dir_entry内核3.10.17移到internal中去了,倒

    struct proc_dir_entry 原:2.6.38.8 在#include <linux/proc_fs.h> 现:3.10.17 在fs/proc/internal.h:str ...

  3. Windows 8.1 正式版微软官方原版镜像下载(新增10/17新版下载)

    中文版:中国区OEM预装版本,特定国家版,锁定语言,其它功能和核心版没有区别.简体中文单语言版:锁定语言,其它功能和核心版没有区别.专业版+核心版[零售版][推荐]:镜像内包含专业版(Professi ...

  4. Dell Technology Summit(2018.10.17)

    时间:2018.10.17地点:北京国家会议中心

  5. 10.17 NOIP模拟赛

    目录 2018.10.17 NOIP模拟赛 A 咒语curse B 神光light(二分 DP) C 迷宫maze(次短路) 考试代码 B 2018.10.17 NOIP模拟赛 时间:1h15min( ...

  6. WIN8.1 PRO RTM VOL.Enterprise.2013.10.17

    Windows 8.1 Pro VL (x64) - DVD (Chinese-Simplified)ISO|Chinese - Simplified|发布日期: 2013/10/17文件名: cn_ ...

  7. 2020.10.17 JZOJ 提高B组T2 导弹拦截

    2020.10.17 JZOJ 提高B组T2 导弹拦截 题目 Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统. 敌国的导弹形成了立体打击,每个导弹可以抽象成一个三维空间中的 ...

  8. Lucene 4.10.2开发示例

    这里面用的是比较新的Lucene4.10.2 做的一个实例.(lucene的索引不能太大,要不然效率会很低.大于1G的时候就必须考虑分布索引的问题) 先介绍一下Lucene的几个参数意义: Index ...

  9. 10.17 linux 文件权限

    文件权限模拟练习 [root@wen ~]# groupadd incahome[root@wen ~]# usersdd oldboy -g incahome-bash: usersdd: comm ...

随机推荐

  1. 编译安装php-amq扩展

    用途:这个扩展是用来操作rabbitmq服务端的 一.安装总括 1.编译安装librabbitmq库 这是一个开源c语言的库.用来与rabbitmq进行通信 而php的php-amqp扩展就是使用这个 ...

  2. JavaScript对象(Object)

         JavaScript的简单数据类型包括数字.字符串.布尔值.null值和undefined值,其他所有的值都是对象.数字.字符串.布尔值“貌似”对象,因为他们拥有方法,但是他们是不可变的. ...

  3. C++之面向对象的三个基本特征

    三大特性是:封装,继承,多态 所谓封装 就是把客观事物封装成抽象的类,并且类可以把自己的数据和方法只让可信的类或者对象操作,对不可信的进行信息隐藏.封装是面向对象的特征之一,是对象和类概念的主要特性. ...

  4. Asynchronous Jobs

    Because Play is a web application framework, most of the application logic is done by controllers re ...

  5. CSS3动画处理浏览器内核时候前缀(兼容性)

    Gecko内核   css前缀为"-moz-" 火狐浏览器 WebKit内核 css前缀为"-webkit-"    Comodo Drangon(科摩多龙), ...

  6. spring-hellow word

          在大三的时候开了一门JAVAEE SSH框架,属于软件方向选修课程,虽然本人是搞硬件的,但是也选了这么课程,因为我在想有一天物联网也会走上大门户的,所以果断去蹭课了,时至今日,重新拾起来, ...

  7. android Activity runOnUiThread() 方法使用

    在android 中我们一般用 Handler 做主线程 和 子线程 之间的通信 . 现在有了一种更为简洁的写法,就是 Activity 里面的 runOnUiThread( Runnable )方法 ...

  8. 【CoreData】表之间的关联

    这次是表之间怎么进行关联,要求如下: // 建立学生与班级表之间的联系 既然是表与表之间的关联,那肯定是要先创建表: // 1.创建模型文件 (相当于一个数据库里的表) // New File ——— ...

  9. 你真的了解UITableViewCell重用吗?

    一:首先查看一下关于UITableViewCell重用的定义 - (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentif ...

  10. 【代码笔记】iOS-竖状图

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...