另一种简单的读写-simple_read_from_buffer

这篇博客介绍了内核编程中,除了使用copy_to_user和copy_from_user之外,如何利用simple_read_from_buffer和simple_write_to_buffer进行简单读写操作。文章以GPIO读取为例,阐述了这两个函数在内核与用户空间交互过程中的应用。
摘要由CSDN通过智能技术生成

之前在《简单的读和写》中提到过file_operations结构中的读/写方法中的buff参数是指向用户空间的缓冲区。而用户空间的指针,内核代码不能直接引用其中的内容。

所以用了copy_to_user和copy_from_user来完成最后的读写操作。

这里介绍另一对可以完成读写的函数:simple_read_from_buffer和simple_write_to_buffer。

这两个方法定义在:kernel-4.9/fs/libfs.c

/**
 * simple_read_from_buffer - copy data from the buffer to user space
 * @to: the user space buffer to read to
 * @count: the maximum number of bytes to read
 * @ppos: the current position in the buffer
 * @from: the buffer to read from
 * @available: the size of the buffer
 *
 * The simple_read_from_buffer() function reads up to @count bytes from the
 * buffer @from at offset @ppos into the user space address starting at @to.
 *
 * On success, the number of bytes read is returned and the offset @ppos is
 * advanced by this number, or negative value is returned on error.
 **/
ssize_t simple_read_from_buffer(void __user *to, size
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是 `procfs_example.c` 和 `Makefile` 的代码: ```c #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/proc_fs.h> #include <linux/uaccess.h> #define BUF_SIZE 100 static struct proc_dir_entry *example_dir, *foo_file, *bar_file, *jiffies_file, *jiffies_too_file; static char msg[BUF_SIZE]; static ssize_t foo_read(struct file *file, char __user *buf, size_t count, loff_t *pos) { return simple_read_from_buffer(buf, count, pos, msg, strlen(msg)); } static ssize_t foo_write(struct file *file, const char __user *buf, size_t count, loff_t *pos) { return simple_write_to_buffer(msg, BUF_SIZE, pos, buf, count); } static const struct file_operations foo_fops = { .owner = THIS_MODULE, .read = foo_read, .write = foo_write, }; static ssize_t bar_read(struct file *file, char __user *buf, size_t count, loff_t *pos) { return simple_read_from_buffer(buf, count, pos, msg, strlen(msg)); } static ssize_t bar_write(struct file *file, const char __user *buf, size_t count, loff_t *pos) { return simple_write_to_buffer(msg, BUF_SIZE, pos, buf, count); } static const struct file_operations bar_fops = { .owner = THIS_MODULE, .read = bar_read, .write = bar_write, }; static ssize_t jiffies_read(struct file *file, char __user *buf, size_t count, loff_t *pos) { unsigned long jiffies = get_jiffies_64(); snprintf(msg, BUF_SIZE, "jiffies: %lu\n", jiffies); return simple_read_from_buffer(buf, count, pos, msg, strlen(msg)); } static const struct file_operations jiffies_fops = { .owner = THIS_MODULE, .read = jiffies_read, }; static ssize_t jiffies_too_read(struct file *file, char __user *buf, size_t count, loff_t *pos) { return jiffies_read(file, buf, count, pos); } static const struct file_operations jiffies_too_fops = { .owner = THIS_MODULE, .read = jiffies_too_read, }; static int __init example_init(void) { example_dir = proc_mkdir("proc_example", NULL); if (!example_dir) { printk(KERN_ERR "Failed to create /proc/proc_example directory\n"); return -ENOMEM; } foo_file = proc_create("foo", 0666, example_dir, &foo_fops); if (!foo_file) { printk(KERN_ERR "Failed to create /proc/proc_example/foo file\n"); return -ENOMEM; } bar_file = proc_create("bar", 0666, example_dir, &bar_fops); if (!bar_file) { printk(KERN_ERR "Failed to create /proc/proc_example/bar file\n"); return -ENOMEM; } jiffies_file = proc_create("jiffies", 0444, example_dir, &jiffies_fops); if (!jiffies_file) { printk(KERN_ERR "Failed to create /proc/proc_example/jiffies file\n"); return -ENOMEM; } jiffies_too_file = proc_symlink("jiffies_too", example_dir, "jiffies"); if (!jiffies_too_file) { printk(KERN_ERR "Failed to create /proc/proc_example/jiffies_too symlink\n"); return -ENOMEM; } printk(KERN_INFO "Successfully created /proc/proc_example directory and files\n"); return 0; } static void __exit example_exit(void) { proc_remove(jiffies_too_file); proc_remove(jiffies_file); proc_remove(bar_file); proc_remove(foo_file); proc_remove(example_dir); printk(KERN_INFO "Removed /proc/proc_example directory and files\n"); } module_init(example_init); module_exit(example_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("A simple example Linux module."); ``` ```Makefile obj-m += procfs_example.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean ``` 这个模块会创建 `/proc/proc_example` 目录,以及其中的三个文件 `foo`、`bar` 和 `jiffies`,还有一个名为 `jiffies_too` 的符号链接。`foo` 和 `bar` 是可读写的文件,`jiffies` 是只读的,它会输出当前的 jiffies 值,`jiffies_too` 则是 `jiffies` 的一个符号链接,它与 `jiffies` 的内容相同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值