对tty节点的操作tty_open

本文深入分析了tty_open函数,探讨了如何从设备节点操作到注册的驱动,重点在于tty->ops->open(tty, filp)的调用以及tty的初始化,包括线路规划的设置。通过tty_open,可以了解到tty驱动的关键信息,如tty->ops与driver->ops的关系以及线路规划ops的来源。" 131589200,7947617,华为OD机试题解:滑动窗口最大和,"['算法', '滑动窗口', '编程语言', '华为OD']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       接上一篇,我们已经注册了一个tty设备驱动并且在/dev生成相应的设备节点,我们对这个节点的操作open,read,write等对应的就是

static const struct file_operations tty_fops = {
	.llseek		= no_llseek,
	.read		= tty_read,
	.write		= tty_write,
	.poll		= tty_poll,
	.unlocked_ioctl	= tty_ioctl,
	.compat_ioctl	= tty_compat_ioctl,
	.open		= tty_open,
	.release	= tty_release,
	.fasync		= tty_fasync,
};

下面我们来一个个的分析:

 

tty_open

static int tty_open(struct inode *inode, struct file *filp)
{
    dev_t device = inode->i_rdev;
	noctty = filp->f_flags & O_NOCTTY;

	/* This is protected by the tty_mutex */
	tty = tty_open_current_tty(device, filp);
	if (IS_ERR(tty)) {
		goto err_unlock;
	} else if (!tty) {
		driver = tty_lookup_driver(device, filp, &noctty, &index);
		/* check whether we're reopening an existing tty */
		tty = tty_driver_lookup_tty(driver, inode, index);
	}

	if (tty) {	
		retval = tty_reopen(tty);
	} else	/* Returns with the tty_lock held for now */
		tty = tty_init_dev(driver, index);
		
	if (tty->ops->open)
		retval = tty->ops->open(tty, filp);
	else
		retval = -ENODEV;
	filp->f_flags = saved_flags;

	return 0;
}

删除很多不太重要的代码,剩下主干代码。这里最重要的功能是tty->ops->open(tty, filp); 调用tty的ops。我们要想办法知道这个ops的实现。同时tty = tty_init_dev(driver, index);这一句也是十分的重要,这里面对tty的初始化,启用线路规划功能,里面包含的信息十分的多。

static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp)
{
	struct tty_struct *tty;

	if (device != MKDEV(TTYAUX_MAJOR, 0))
		return NULL;

	tty = get_current_tty();
	if (!tty)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值