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