介绍个特殊的字符设备,主设备号为1的设备
1 char Memory devices
1 = /dev/mem Physical memory access
2 = /dev/kmem Kernel virtual memory access
3 = /dev/null Null device
4 = /dev/port I/O port access
5 = /dev/zero Null byte source
6 = /dev/core OBSOLETE - replaced by /proc/kcore
7 = /dev/full Returns ENOSPC on write
8 = /dev/random Nondeterministic random number gen.
9 = /dev/urandom Faster, less secure random number gen.
10 = /dev/aio Asynchronous I/O notification interface
11 = /dev/kmsg Writes to this come out as printk's, reads
export the buffered printk records.
12 = /dev/oldmem OBSOLETE - replaced by /proc/vmcore
主设备号为1 的设备严格上都不是真正的字符设备,都是和mem有关系的 MEM_MAJOR 1
例如:/dev/null 内核中有些信息不适合在屏幕上显示,又不适合重定向到磁盘中,这时候
/dev/null起到直接把数据消失的作用。
实现代码原理如下:
最终执行是在file_operations中的,所以从文件操作开始:主设备号为1的如下:
static const struct file_operations memory_fops = {
.open = memory_open,
.llseek = noop_llseek,
};
static int memory_open(struct inode *inode, struct file *filp)
{
int minor;
const struct memdev *dev;
minor = iminor(inode); //获取次设备号
if (minor >= ARRAY_SIZE(devlist)) //主设备号为1的devlist设备列表 ,判断是否在范围内
return -ENXIO;
dev = &devlist[minor]; //从列表中获取设备指针
if (!dev->fops) //判断文件操作是否有效
return -ENXIO;
filp->f_op = dev->fops; //赋值文件操作指针
filp->f_mode |= dev->fmode;
if (dev->fops->open) //如果非空,执行自己的open操作 /dev/null为空
return dev->fops->open(inode, filp);
return 0;
}
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,
};
上面filp->f_op = dev->fops; 后就可进行读写操作了。
读写操作如下:
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;
}
没错,什么也没做直接返回了。从而掩盖事实。做了啊,实际什么也没做。
起到一些消息不得不管理,统一空处理的作用。