Linux TTY基本概念之ttys*、tty*、ttyS*、console理解

echo "abc" > /dev/console 输出到系统控制台(console=ttyS0,表示系统控制台是串口)
echo "abc" > /dev/tty     和下面的相等
echo "abc" > `tty`        输出到本控制台  (tty命令就是输出本控制台)

在Linux中, tty可分为如下几类:

- 串行端口终端(serial port terminal): 指使用计算机串行端口连接的终端设备, /dev/ttySn
- 伪终端(pseudo terminal): 通常是通过ssh登陆的终端, /dev/pts/*
- 控制终端(controlling terminal): 代表当前tty设备 /dev/tty
- 控制台终端(console):  指计算机的输出设备, 通常是printk信息输出的设备, /dev/ttyn、/dev/console 

详细定义如下:

/* tty driver types */
#define TTY_DRIVER_TYPE_SYSTEM         0x0001
#define TTY_DRIVER_TYPE_CONSOLE        0x0002
#define TTY_DRIVER_TYPE_SERIAL         0x0003
#define TTY_DRIVER_TYPE_PTY            0x0004
#define TTY_DRIVER_TYPE_SCC            0x0005    
/* scc driver */
#define TTY_DRIVER_TYPE_SYSCONS        0x0006

1.echo abc > /dev/ttyS0

tty_cdev_add 给 tty driver 分配字符设备,设置字符设备的文件操作函数指针指向 tty_fops ,后者的写函数为 tty_write

static int tty_cdev_add(struct tty_driver *driver, dev_t dev,
		unsigned int index, unsigned int count)
{
    ...
   	driver->cdevs[index]->ops = &tty_fops;
    ...
}

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,
	.show_fdinfo	= tty_show_fdinfo,
};

tty_write 只是做一些检查,保证 line discipline 的写函数需要的组件可用,真正的操作由 do_tty_write 调用 ld->ops->write 完成。

static ssize_t tty_write(struct file *file, const char __user *buf,
						size_t count, loff_t *ppos)
{
	struct tty_struct *tty = file_tty(file);
 	struct tty_ldisc *ld;
	ssize_t ret;

	if (tty_paranoia_check(tty, file_inode(file), "tty_write"))
		return -EIO;

    // tty->ops->write 在 n_tty_write 中使用
	if (!tty || !tty->ops->write ||	tty_io_error(tty))
			return -EIO;
	/* Short term debug to catch buggy drivers */

    // tty->ops->write_room 在 n_tty_write-> process_output_block 中使用
	if (tty->ops->write_room == NULL)
		tty_err(tty, "missing write_room method\n");
	ld = tty_ldisc_ref_wait(tty);
	if (!ld)
		return hung_up_tty_write(file, buf, count, ppos);
	if (!ld->ops->write)
		ret = -EIO;
	else
		ret = do_tty_write(ld->ops->write, tty, file, buf, count);
	tty_ldisc_deref(ld);
	return ret;
}

2. echo abc > /dev/tty

根据 tty_init 函数, /dev/tty 的文件操作函数指针和 /dev/ttyS0 相同,因此写 /dev/tty 的操作执行的函数和写 /dev/ttyS0 相同。

3.cho abc > /dev/tty1

/dev/ttyn 设备的文件操作指针也是 tty_fops ,因此向其写入数据时,同样会调用 tty_write 函数。
tty_write 调用 line discipline 的写函数完成输出操作,最终调用 con_write 输出字符串。

4. echo abc > /dev/console

/dev/console 使用的文件操作函数指针为:

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

因此写操作调用的函数为 redirected_tty_write :函数首先获取变量 struct file *redirect ,如果文件指针存在,调用 vfs_write 将 buf 中内容输出到 redirect 文件;否则调用 tty_write 完成输出操作。

ssize_t redirected_tty_write(struct file *file, const char __user *buf,
						size_t count, loff_t *ppos)
{
	struct file *p = NULL;

	spin_lock(&redirect_lock);
	if (redirect)
		p = get_file(redirect);
	spin_unlock(&redirect_lock);

	if (p) {
		ssize_t res;
		res = vfs_write(p, buf, count, &p->f_pos);
		fput(p);
		return res;
	}
	return tty_write(file, buf, count, ppos);
}

4.1. 控制台重定向 redirect

redirect 定义在 drivers/tty/tty_io.c 中,修改 direct 的函数只有 tioccons ,用于相应 tty ioctl 的 TIOCCONS 命令。

根据 man tty_ioctl 的输出,这个命令的功能为:

重定向控制台输出,将原本要输出到 /dev/console 或者 /dev/tty0 的内容重定向到给定的终端。如果终端是一个伪终端的主设备,将其发送到从设备。

static struct file *redirect;

static int tioccons(struct file *file)
{
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

    // 传入的文件指针是 /dev/console 或者 /dev/tty0 ,结束重定向
	if (file->f_op->write == redirected_tty_write) {
		struct file *f;
		spin_lock(&redirect_lock);
		f = redirect;
		redirect = NULL;
		spin_unlock(&redirect_lock);
		if (f)
			fput(f);
		return 0;
	}
	spin_lock(&redirect_lock);

    // 如果已经设置了重定向,返回 EBUSY
	if (redirect) {
		spin_unlock(&redirect_lock);
		return -EBUSY;
	}

    // 设置重定向
	redirect = get_file(file);
	spin_unlock(&redirect_lock);
	return 0;
}

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值