内核提供的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接口函数例子
1 #include <linux/module.h> 2 #include <linux/kernel.h> 3 #include <linux/proc_fs.h> 4 #include <linux/sched.h> 5 #include <asm/uaccess.h> 6 7 #ifndef LINUX_VERSION_CODE 8 #define LINUX_VERSION_CODE 199163 /* kernel version 3.10 */ 9 #endif 10 11 #ifndef KERNEL_VERSION(a, b, c) 12 #define KERNEL_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c)) 13 #endif 14 15 #define STRINGLEN 1024 16 char global_buffer[STRINGLEN] = "hello"; 17 struct proc_dir_entry *example_dir, *data_file; 18 19 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 0, 0) 20 int proc_read_hello(char *page, char **start, off_t off, int count, 21 int *eof, void *data) 22 { 23 int len; 24 len = sprintf(page, global_buffer); //把global_buffer的内容显示给访问者 25 26 return len; 27 } 28 29 int proc_write_hello(struct file *file, const char *buffer, 30 unsigned long count, void *data) 31 { 32 int len; 33 34 if (count >= STRINGLEN) 35 len = STRINGLEN - 1; 36 else 37 len = count; 38 39 /* 40 * copy_from_user函数将数据从用户空间拷贝到内核空间 41 * 此处将用户写入的数据存入global_buffer 42 */ 43 copy_from_user(global_buffer, buffer, len); 44 global_buffer[len] = '\0'; 45 46 return len; 47 } 48 #else 49 static ssize_t proc_read(struct file *file, char __user *buf, 50 size_t count, loff_t *ppos) 51 { 52 int len; 53 54 if (*ppos > 0) 55 return 0; 56 57 len = strlen(global_buffer) + 1; 58 if (copy_to_user(buf, global_buffer, len)) 59 return -EFAULT; 60 61 *ppos += len; 62 63 return len; 64 } 65 66 static ssize_t proc_write(struct file *file, const char __user *buf, 67 size_t count, loff_t *ppos) 68 { 69 int len; 70 71 if (count >= STRINGLEN) 72 len = STRINGLEN - 1; 73 else 74 len = count; 75 76 if (copy_from_user(global_buffer, buf, len)) 77 return -EFAULT; 78 79 global_buffer[len] = '\0'; 80 81 return len; 82 } 83 84 static const struct file_operations proc_fops = { 85 .owner = THIS_MODULE, 86 .read = proc_read, 87 .write = proc_write, 88 }; 89 #endif 90 91 static int __init proc_test_init(void) 92 { 93 example_dir = proc_mkdir("proc_test", NULL); 94 95 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 0, 0) 96 data_file = create_proc_entry("data", S_IRUGO, example_dir); 97 data_file->read_proc = proc_read_hello; 98 data_file->write_proc = proc_write_hello; 99 #else 100 data_file = proc_create("data", S_IRUGO, example_dir, &proc_fops); 101 #endif 102 return 0; 103 } 104 105 static void __exit proc_test_exit(void) 106 { 107 remove_proc_entry("data", example_dir); 108 remove_proc_entry("proc_test", NULL); 109 } 110 111 module_init(proc_test_init); 112 module_exit(proc_test_exit);
参考文章
参考资源
转载:https://blog.csdn.net/luckywang1103/article/details/50629186