linux内核源码分析之proc文件系统(三)
目录
一、数据结构分析
1、proc数据项的表示proc_dir_entry
2、proc inode
二、proc初始化
三、管理/proc数据项
1. 数据项的创建和注册
2. 查找proc数据项
三、读取和写入信息
四、进程相关的信息
1、创建目录inode
2、处理文件
一、数据结构分析
1、proc数据项的表示proc_dir_entry
struct proc_dir_entry {spinlock_t pde_unload_lock;struct completion *pde_unload_completion;const struct inode_operations *proc_iops;union {const struct proc_ops *proc_ops;const struct file_operations *proc_dir_ops;};const struct dentry_operations *proc_dops;proc_write_t write;
...
}
实例
struct proc_dir_entry proc_root = {.low_ino = PROC_ROOT_INO, .namelen = 5, .mode = S_IFDIR | S_IRUGO | S_IXUGO, .nlink = 2, .refcnt = REFCOUNT_INIT(1),.proc_iops = &proc_root_inode_operations, //对proc根目录几乎不能做什么.proc_dir_ops = &proc_root_operations,//proc目录是特别的,因为其中包含了<pid>目录,因而不能对该目录使用通用处理函数.parent = &proc_root,.subdir = RB_ROOT,.name = "/proc",
};
2、proc inode
//proc的数据与VFS层的inode数据关联起来,
struct proc_inode {struct pid *pid;unsigned int fd;union proc_op op;struct proc_dir_entry *pde;//pde是一个指针,指向关联到proc数据项的proc_dir_entry实例struct ctl_table_header *sysctl;struct ctl_table *sysctl_entry;struct hlist_node sysctl_inodes;const struct proc_ns_operations *ns_ops;struct inode vfs_inode;
} __randomize_layout;
两个结构体之间的关系
proc_inode中的成员pde是一个指针,指向关联到proc数据项的proc_dir_entry实例
二、proc初始化
初始化,装载文件系统
三、管理/proc数据项
1. 数据项的创建和注册
#define proc_create_seq_private(name, mode, parent, ops, size, data) ({NULL;})
#define proc_create_seq_data(name, mode, parent, ops, data) ({NULL;})
#define proc_create_seq(name, mode, parent, ops) ({NULL;})
#define proc_create_single(name, mode, parent, show) ({NULL;})
#define proc_create_single_data(name, mode, parent, show, data) ({NULL;})
#define proc_create(name, mode, parent, proc_ops) ({NULL;})
#define proc_create_data(name, mode, parent, proc_ops, data) ({NULL;})
struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,struct proc_dir_entry *parent,const struct proc_ops *proc_ops, void *data)
{struct proc_dir_entry *p;p = proc_create_reg(name, mode, &parent, data);if (!p)return NULL;p->proc_ops = proc_ops;return proc_register(parent, p);
}
2. 查找proc数据项
static const struct inode_operations proc_root_inode_operations = {.lookup = proc_root_lookup,.getattr = proc_root_getattr,
};static struct dentry *proc_root_lookup(struct inode * dir, struct dentry * dentry, unsigned int flags)
{if (!proc_pid_lookup(dentry, flags))//PIDreturn NULL;return proc_lookup(dir, dentry, flags);
}
三、读取和写入信息
使用proc_ops, 而不是file_operations
struct proc_ops {int (*proc_open)(struct inode *, struct file *);ssize_t (*proc_read)(struct file *, char __user *, size_t, loff_t *);ssize_t (*proc_write)(struct file *, const char __user *, size_t, loff_t *);loff_t (*proc_lseek)(struct file *, loff_t, int);int (*proc_release)(struct inode *, struct file *);__poll_t (*proc_poll)(struct file *, struct poll_table_struct *);long (*proc_ioctl)(struct file *, unsigned int, unsigned long);
#ifdef CONFIG_COMPATlong (*proc_compat_ioctl)(struct file *, unsigned int, unsigned long);
#endifint (*proc_mmap)(struct file *, struct vm_area_struct *);unsigned long (*proc_get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
};
这里以读取CPU信息为例
extern const struct seq_operations cpuinfo_op;
static int cpuinfo_open(struct inode *inode, struct file *file)
{arch_freq_prepare_all();return seq_open(file, &cpuinfo_op);
}//读写相关的操作函数
static const struct proc_ops cpuinfo_proc_ops = {.proc_open = cpuinfo_open,.proc_read = seq_read,.proc_lseek = seq_lseek,.proc_release = seq_release,
};//创建cpuinfo文件
static int __init proc_cpuinfo_init(void)
{proc_create("cpuinfo", 0, NULL, &cpuinfo_proc_ops);return 0;
}
fs_initcall(proc_cpuinfo_init);
四、进程相关的信息
1、创建目录inode
//查看进程信息
struct dentry *proc_pid_lookup(struct dentry *dentry, unsigned int flags)
{struct task_struct *task;unsigned tgid;struct pid_namespace *ns;struct dentry *result = ERR_PTR(-ENOENT);tgid = name_to_int(&dentry->d_name);if (tgid == ~0U)goto out;ns = dentry->d_sb->s_fs_info;rcu_read_lock();task = find_task_by_pid_ns(tgid, ns);if (task)get_task_struct(task);rcu_read_unlock();if (!task)goto out;result = proc_pid_instantiate(dentry, task, NULL);put_task_struct(task);
out:return result;
}
2、处理文件
static const struct pid_entry tgid_base_stuff[] = {REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),ONE("stat", S_IRUGO, proc_tgid_stat),ONE("statm", S_IRUGO, proc_pid_statm),REG("maps", S_IRUGO, proc_pid_maps_operations),
...
}
linux内核源码分析之proc文件系统(三)相关推荐
- linux内核源码分析之proc文件系统(一)
目录 一.proc文件系统 1.proc的内容 2.优缺点 二.特定进程的数据 三.一般性系统信息 四.网络信息 五.系统控制信息 一.proc文件系统 是一种虚拟文件系统,其信息不能从块设备读取,只 ...
- linux内核源码分析之虚拟文件系统VFS(一)
目录 一.文件系统类型 二.VFS虚拟文件系统 三.VFS中主要的对象类型 四.结构体之间的关系 五.inode 节点 一.文件系统类型 基于磁盘的文件系统:在非易失介质上存储文件,在多次会话间保持文 ...
- linux内核源码分析之虚拟文件系统VFS(二)
read系统调用流程 read()->ksys_read() ssize_t ksys_read(unsigned int fd, char __user *buf, size_t count) ...
- 【技术分享篇】Linux内核——手把手带你实现一个Linux内核文件系统丨Linux内核源码分析
手把手带你实现一个Linux内核文件系统 1. 内核文件系统架构分析 2. 行行珠玑,代码实现 [技术分享篇]Linux内核--手把手带你实现一个Linux内核文件系统丨Linux内核源码分析 更多L ...
- Linux内核源码分析--内核启动之(3)Image内核启动(C语言部分)(Linux-3.0 ARMv7) 【转】...
原文地址:Linux内核源码分析--内核启动之(3)Image内核启动(C语言部分)(Linux-3.0 ARMv7) 作者:tekkamanninja 转自:http://blog.chinauni ...
- Linux内核源码分析《进程管理》
Linux内核源码分析<进程管理> 前言 1. Linux 内核源码分析架构 2. 进程原理分析 2.1 进程基础知识 2.2 Linux进程四要素 2.3 进程描述符 task_stru ...
- iostat IO统计原理linux内核源码分析----基于单通道SATA盘
iostat IO统计原理linux内核源码分析----基于单通道SATA盘 先上一个IO发送submit_bio流程图,本文基本就是围绕该流程讲解. 内核版本 3.10.96 详细的源码注释:htt ...
- Linux内核源码分析--内核启动之(4)Image内核启动(setup_arch函数)(Linux-3.0 ARMv7)【转】...
原文地址:Linux内核源码分析--内核启动之(4)Image内核启动(setup_arch函数)(Linux-3.0 ARMv7) 作者:tekkamanninja 转自:http://blog.c ...
- Linux内核源码分析—从用户空间复制数据到内核空间
Linux内核源码分析-从用户空间复制数据到内核空间 本文主要参考<深入理解Linux内核>,结合2.6.11.1版的内核代码,分析从用户空间复制数据到内核空间函数. 1.不描述内核同步. ...
- Linux内核源码分析之内存管理
本文站的角度更底层,基本都是从Linux内核出发,会更深入.所以当你都读完,然后再次审视这些功能的实现和设计时,我相信你会有种豁然开朗的感觉. 1.页 内核把物理页作为内存管理的基本单元. 尽管处理器 ...
最新文章
- mysql为什么每天0点就装东西_MySQL的详细安装教程
- Java基础与实践题库_Java程序设计基础与实践(题库版)
- 污水处理厂数字监控系统解决方案
- html鼠标点击之后隐藏,html中如何用js设置鼠标两秒不动,隐藏鼠标
- 一张图让你读懂鹅厂的物联网框架
- 机器学习算法基础3-sklearn数据集与估计器
- 服务器性能低下时正确的处理方法
- Exchange2010中文版安装教程
- NSSA区域和Totally NSSA区域
- DNS域名解析协议详解
- java根据日期计算农历_Java给定公历日期计算相应农历/阴历日期
- 8.敏捷软件开发框架 - 规模化敏捷框架SAFe
- N沟道和P沟道MOS管的四个不同点
- 用c语言编写6位计算器,用c语言编写易简计算器.doc
- FastReport 金额大小写转换自定义函数
- 什么是系统平均负载(Load average)
- MT-InSAR原理和发展_刘计洪
- mysql left join 索引失效
- php 编码安全,PHP编码安全之二: 代码执行漏洞
- CDH6.2.1CM6.2.1版本部署操作文档