proc文件
Posted soul.stone
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了proc文件相关的知识,希望对你有一定的参考价值。
参考代码:
#include <linux/init.h> #include <linux/version.h> #include <linux/module.h> #include <linux/sched.h> #include <linux/uaccess.h> #include <linux/proc_fs.h> #include <linux/fs.h> #include <linux/seq_file.h> #include <asm/uaccess.h> #define STRING_LEN 1024 char global_buffer[STRING_LEN] = {0}; static int my_proc_show(struct seq_file *seq, void *v) { printk("my_proc_show called\n"); seq_printf(seq, "current kernel time is %ld\n", jiffies); seq_printf(seq, "global_buffer: %s", global_buffer); return 0; } static int my_proc_open(struct inode *inode, struct file *file) { return single_open(file, my_proc_show, inode->i_private); } static ssize_t my_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos) { if (count > 0) { printk("my_proc_write called\n"); copy_from_user(global_buffer, buffer, count); } return count; } struct file_operations proc_fops = { .open = my_proc_open, .read = seq_read, .write = my_proc_write, .llseek = seq_lseek, .release = single_release, }; static struct proc_dir_entry *proc_dir = NULL; static struct proc_dir_entry *proc_file = NULL; static int __init proc_test_init(void) { proc_dir = proc_mkdir("my_proc", NULL); if (!proc_dir) { printk(KERN_DEBUG"proc_mkdir failed"); return 0; } proc_file = proc_create("buffer", 0666, proc_dir, &proc_fops); if (!proc_file) { printk(KERN_DEBUG"proc_create failed"); return 0; } strcpy(global_buffer, "hello"); return 0; } static void __exit proc_test_exit(void) { remove_proc_entry("buffer", proc_dir); remove_proc_entry("my_proc", NULL); } module_init(proc_test_init); module_exit(proc_test_exit); MODULE_AUTHOR("derek.yi"); MODULE_LICENSE("GPL"); /* echo "Hello from kernel" > /proc/my_proc/buffer cat /proc/my_proc/buffer */
测试:
[email protected]:~/share/ldd5$ cat /proc/my_proc/buffer current kernel time is 4295327951 global_buffer: hello
[email protected]:~/share/ldd5$ echo "Hello from kernel" > /proc/my_proc/buffer [email protected]:~/share/ldd5$ cat /proc/my_proc/buffer current kernel time is 4295330282 global_buffer: Hello from kernel
以上是关于proc文件的主要内容,如果未能解决你的问题,请参考以下文章