Chinaunix首页 | 论坛 | 博客
  • 博客访问: 233519
  • 博文数量: 69
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 15
  • 用 户 组: 普通用户
  • 注册时间: 2013-02-23 13:55
文章分类

全部博文(69)

文章存档

2016年(11)

2013年(58)

我的朋友

分类: LINUX

2016-05-09 12:17:33

原文地址:proc相关操作 作者:leonwang202

一、数据结构

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;
    get_info_t *get_info;        //
    struct module *owner;
    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 */
    struct completion *pde_unload_completion;
    shadow_proc_t *shadow_proc; 
};

这个结构比较重要的就是3个钩子函数,分别用于 read write info操作

1、read操作:
typedef int(read_proc_t)(char *page,char **start,off_t off, int count, int *eof, void *data);
page:指示用来写入数据的缓冲区;
off和count:与read函数对应的参数相同;
start和eof:用于读取大于1个page数据时实现。

2、write操作:
typedef int (write_proc_t)(struct file *file, const char __user *buffer, unsigned long count, void *data);
filp 参数实际上是一个打开文件结构(可以忽略这个参数)。
buffer 参数是用户空间要写入的数据。缓冲区地址实际上是一个用户空间的缓冲区,不能直接读取它。
len 参数定义了在 buffer 中有多少数据要被写入。
data 参数是一个指向私有数据的指针。

3、info操作

typedef int (get_info_t)(char *, char **, off_t, int);

二、proc API

创建目录
struct proc_dir_entry *proc_mkdir(const char *name, struct proc_dir_entry *parent)

name: 文件夹的名字
parent: 确定文件所在目录,如果置NULL,则位置为/proc下。

创建文件
struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode, struct proc_dir_entry *parent);

name: 要创建的文件名称;
mode: 该文件的保护掩码;
parent: 确定文件所在目录,如果置NULL,则位置为/proc下。

create_proc_entry还有很多衍生 API

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)
{
    struct proc_dir_entry *res=create_proc_entry(name,mode,base);
    if (res) {
        res->read_proc=read_proc;
        res->data=data;
    }
    return res;
}

struct proc_dir_entry *create_proc_info_entry(const char *name,
    mode_t mode, struct proc_dir_entry *base, get_info_t *get_info)
{
    struct proc_dir_entry *res=create_proc_entry(name,mode,base);
    if (res) res->get_info=get_info;
    return res;
}

删除文件或目录
void remove_proc_entry(const char *name, struct proc_dir_entry *parent)

name: 文件夹或文件的名字
parent: 确定文件所在目录,如果置NULL,则位置为/proc下。

##################################################################################

常用proc文件大集合:

一、 /proc/devices        显示所有已分配的主设备号

-bash-3.2# cat /proc/devices
Character devices:
  1 mem
  2 pty
  3 ttyp
  5 /dev/tty
....略

二、/proc/sys/kernel/printk    显示和修改控制台的日志级别
-bash-3.2# cat /proc/sys/kernel/printk
6       4       1       7

四个整数分别是:
    当前日志级别
    未明确指定日志级别时默认消息级别
    最小允许的日志级别
    引导时的默认日志级别

若让所有控制消息显示在控制台,则
echo "0 0 0 0" > /proc/sys/kernel/printk

三、/proc/iomem     显示mem地址分布


-bash-3.2# cat /proc/iomem
80000000-9fffffff : PCI1 host bridge
  a2000800-a2000807 : pata_85xx_cf.0
  a200080e-a200080e : pata_85xx_cf.0
  9ff90000-9ff9ffff : 0000:00:0c.1
    9ff90000-9ff9ffff : e1000
  9ffa0000-9ffbffff : 0000:00:0c.1
    9ffa0000-9ffbffff : e1000
。。。略


四、/proc/ioports 显示IO资源分布
-bash-3.2# cat /proc/ioports
00000000-00ffffff : PCI1 host bridge
  00ffff80-00ffffbf : 0000:00:0c.1
    00ffff80-00ffffbf : e1000
  00ffffc0-00ffffff : 0000:00:0c.0
    00ffffc0-00ffffff : e1000

五、/proc/interrupts  显示外部中断信息
-bash-3.2# cat /proc/interrupts
           CPU0
 41:         35   CPM2 SIU  Level     cpm_uart
 77:        132   OpenPIC   Level     enet_tx
 78:     487854   OpenPIC   Level     enet_rx
 82:          0   OpenPIC   Level     enet_error
 91:       1417   OpenPIC   Level     i2c-mpc
 94:         35   OpenPIC   Level     cpm2_cascade
BAD:          0

六、/proc/uptime    显示系统运行时间

the uptime of the system(seconds),
and the amount of time spent in idle process(seconds).   

-bash-3.2# cat /proc/uptime
5603.90 5587.39

七、/proc/net/dev    显示网络设备收发包统计信息

-bash-3.2# cat /proc/net/dev
Inter-|   Receive                                                |  Transmit
 face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
    lo:    6924      29    0    0    0     0          0         0     6924      29    0    0    0     0       0          0
  eth0:22376982  352073    0    0    0     0          0         0 493743767 19521668    0    0   26     0       0          0
  eth1:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
  eth2:68372765 1016385    0    0    0     0          0       263   668734    5119    0    0    0     0       0          0
  eth3:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
 bond0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
dummy0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0


八、/proc/kallsyms  显示当前内核的符号表

九、/proc/cmdline   显示bootloader引导linux时,所用的启动参数
阅读(2546) | 评论(0) | 转发(0) |
0

上一篇:socket编程备忘录

下一篇:ELF文件和BIN文件

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