显示linux5.4源码中tcgetattr系统函数的源码
时间: 2023-12-14 07:38:13 浏览: 85
在 Linux 5.4 源码中,`tcgetattr()` 系统调用的源代码可以在 `drivers/tty/tty_io.c` 文件中找到。以下是该函数的源代码:
```
/**
* tcgetattr - get current terminal attributes
* @fd: file descriptor for the terminal
* @termiosp: pointer to output (current terminal attributes)
*
* Get the current terminal attributes in *termiosp.
*/
int tcgetattr(int fd, struct termios *termiosp)
{
struct tty_struct *tty;
int err = 0;
tty = get_current_tty();
if (!tty)
return -ENOTTY;
mutex_lock(&tty->termios_mutex);
if (tty->ldisc->ops->tiocmget &&
!try_module_get(tty->ldisc->owner)) {
struct serial_icounter_struct icount;
err = tty->ldisc->ops->tiocmget(tty, &icount);
if (!err) {
termios_lock();
*termiosp = tty->termios;
termios_unlock();
termiosp->c_ispeed = tty_get_baud_rate(tty);
termiosp->c_ospeed = termiosp->c_ispeed;
/* These values are not returned by the driver */
termiosp->c_cc[VINTR] = tty->termios.c_cc[VINTR];
termiosp->c_cc[VQUIT] = tty->termios.c_cc[VQUIT];
termiosp->c_cc[VMIN] = tty->termios.c_cc[VMIN];
termiosp->c_cc[VTIME] = tty->termios.c_cc[VTIME];
termiosp->c_cc[VEOF] = tty->termios.c_cc[VEOF];
termiosp->c_cc[VEOL] = tty->termios.c_cc[VEOL];
termiosp->c_cc[VEOL2] = tty->termios.c_cc[VEOL2];
termiosp->c_cc[VSWTC] = tty->termios.c_cc[VSWTC];
termiosp->c_cc[VSTART] = tty->termios.c_cc[VSTART];
termiosp->c_cc[VSTOP] = tty->termios.c_cc[VSTOP];
termiosp->c_cc[VSUSP] = tty->termios.c_cc[VSUSP];
if (tty->ops->get_icount)
memcpy(&termiosp->c_icount,
&tty->info->tty_icount,
sizeof(struct serial_icounter_struct));
else
memset(&termiosp->c_icount, 0,
sizeof(struct serial_icounter_struct));
}
module_put(tty->ldisc->owner);
} else {
termios_lock();
*termiosp = tty->termios;
termios_unlock();
termiosp->c_ispeed = tty_get_baud_rate(tty);
termiosp->c_ospeed = termiosp->c_ispeed;
}
mutex_unlock(&tty->termios_mutex);
return err;
}
```
该函数首先通过 `get_current_tty()` 函数获取当前进程的终端设备,然后获取该终端设备的属性,并将其存储在 `termiosp` 指针所指向的结构体中。函数返回值为 0 表示成功,否则表示失败。
阅读全文