欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

linux内核/dev/null的实现

时间:2023-07-13

static const struct file_operations null_fops = {.llseek= null_lseek,.read= read_null,.write= write_null,.read_iter= read_iter_null,.write_iter= write_iter_null,.splice_write= splice_write_null,};static const struct memdev {const char *name;umode_t mode;const struct file_operations *fops;fmode_t fmode;} devlist[] = {#ifdef CONFIG_DEVMEM [1] = { "mem", 0, &mem_fops, FMODE_UNSIGNED_OFFSET },#endif#ifdef CONFIG_DEVKMEM [2] = { "kmem", 0, &kmem_fops, FMODE_UNSIGNED_OFFSET },#endif [3] = { "null", 0666, &null_fops, 0 },#ifdef CONFIG_DEVPORT [4] = { "port", 0, &port_fops, 0 },#endif [5] = { "zero", 0666, &zero_fops, 0 }, [7] = { "full", 0666, &full_fops, 0 }, [8] = { "random", 0666, &random_fops, 0 }, [9] = { "urandom", 0666, &urandom_fops, 0 },#ifdef CONFIG_PRINTK[11] = { "kmsg", 0644, &kmsg_fops, 0 },#endif};

外部进行读取的时候,直接返回0

写入时返回写入的count,buf是直接不管的。

static ssize_t read_null(struct file *file, char __user *buf, size_t count, loff_t *ppos){return 0;}static ssize_t write_null(struct file *file, const char __user *buf, size_t count, loff_t *ppos){return count;}

代码见:

mem.c - drivers/char/mem.c - Linux source code (v4.4) - Bootlin

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。