proc文件系统接口
来源:互联网 发布:淘宝退货原因假冒品牌 编辑:程序博客网 时间:2024/05/19 18:42
内核提供的proc接口函数
头文件linux/proc_fs.h
struct proc_dir_entry *proc_mkdir
(const char *name, struct proc_dir_entry *parent);
在parent目录创建一个名为name的目录
struct proc_dir_entry *create_proc_entry
(const char *name, mode_t mode, struct proc_dir_entry *parent);
在parent目录创建一个名为name,权限为mode的文件
在create_proc_entry函数执行后返回struct proc_dir_entry可以自己指定read, write等函数
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);
如果只需要read函数,可以使用他,他其实是对create_proc_entry进行了封装,把create_proc_read_entry的传入值read_proc赋给了create_proc_entry返回值的read_proc成员。
struct proc_dir_entry *proc_create
(const char *name, umode_t mode, struct proc_dir_entry *parent, const struct file_operations *proc_fops)
新的内核版本中,至少在3.10内核中已经看不到create_proc_entry了,取而代之的是使用proc_create创建文件,proc_create中需要指定proc_fops中的read和write等方法,以方便用户空间的操作。
struct proc_dir_entry *proc_symlink
(const char *name, struct proc_dir_entry *parent, const char *dest);
在parent目录创建指定dest目录的名为name的符号链接。
void remove_proc_entry
( const char *name, struct proc_dir_entry *parent );
删除parent目录中名为name的文件或者目录。
使用proc接口函数例子
#include <linux/module.h>#include <linux/kernel.h>#include <linux/proc_fs.h>#include <linux/sched.h>#include <asm/uaccess.h>#ifndef LINUX_VERSION_CODE#define LINUX_VERSION_CODE 199163 /* kernel version 3.10 */#endif#ifndef KERNEL_VERSION(a, b, c)#define KERNEL_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))#endif#define STRINGLEN 1024char global_buffer[STRINGLEN] = "hello";struct proc_dir_entry *example_dir, *data_file;#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 0, 0)int proc_read_hello(char *page, char **start, off_t off, int count, int *eof, void *data){ int len; len = sprintf(page, global_buffer); //把global_buffer的内容显示给访问者 return len;}int proc_write_hello(struct file *file, const char *buffer, unsigned long count, void *data){ int len; if (count >= STRINGLEN) len = STRINGLEN - 1; else len = count; /* * copy_from_user函数将数据从用户空间拷贝到内核空间 * 此处将用户写入的数据存入global_buffer */ copy_from_user(global_buffer, buffer, len); global_buffer[len] = '\0'; return len;}#elsestatic ssize_t proc_read(struct file *file, char __user *buf, size_t count, loff_t *ppos){ int len; if (*ppos > 0) return 0; len = strlen(global_buffer) + 1; if (copy_to_user(buf, global_buffer, len)) return -EFAULT; *ppos += len; return len;}static ssize_t proc_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos){ int len; if (count >= STRINGLEN) len = STRINGLEN - 1; else len = count; if (copy_from_user(global_buffer, buf, len)) return -EFAULT; global_buffer[len] = '\0'; return len;}static const struct file_operations proc_fops = { .owner = THIS_MODULE, .read = proc_read, .write = proc_write,};#endifstatic int __init proc_test_init(void){ example_dir = proc_mkdir("proc_test", NULL);#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 0, 0) data_file = create_proc_entry("data", S_IRUGO, example_dir); data_file->read_proc = proc_read_hello; data_file->write_proc = proc_write_hello;#else data_file = proc_create("data", S_IRUGO, example_dir, &proc_fops);#endif return 0;}static void __exit proc_test_exit(void){ remove_proc_entry("data", example_dir); remove_proc_entry("proc_test", NULL);}module_init(proc_test_init);module_exit(proc_test_exit);
参考文章
使用 /proc 文件系统来访问 Linux 内核的内容
参考资源
内核proc文件系统的代码
- proc文件系统接口
- 设备驱动之一 - proc文件系统接口
- 内核proc文件系统 --和--seq接口
- 内核proc文件系统与seq接口
- 内核proc文件系统与seq接口----内核proc文件系统编程接口
- 内核proc文件系统与seq接口(2)---内核proc文件系统编程接口
- 内核proc文件系统与seq接口(2)---内核proc文件系统编程接口
- 内核proc文件系统与seq接口(5)---通用proc接口与seq_file接口实验
- 内核proc文件系统与seq接口(1)---内核proc文件系统简介
- 内核proc文件系统与seq接口(1)---内核proc文件系统简介
- proc文件系统
- proc文件系统
- /proc 文件系统
- proc 文件系统
- proc文件系统
- /proc文件系统
- /proc 文件系统
- proc文件系统
- 本地执行Spark程序示例
- tomcat会话之持久化会话管理器
- android 图标如何闪烁
- 非阻塞IO--Select
- TBB之concurrent_hash_map
- proc文件系统接口
- 关于delete obsolete 对于 RMAN中 archivelog deletion policy的影响
- Redis图形化监控(redis-stat)
- C#开机自启动实现(分析无法启动情况)
- Gamma校正及其实现
- HTML5初学者路线图
- 两种典型排序模板
- 代码块
- 小型企业组织建设改进路线图