显示linux5.4源码中tcgetattr系统函数的源码
时间: 2023-12-14 11:39:05 浏览: 28
以下是 Linux 5.4 源码中 tcgetattr 系统函数的源码:
```c
int tcgetattr(int fd, struct termios *termios_p)
{
struct tty_struct *tty;
int ret = 0;
unsigned long flags;
mutex_lock(&termios_mutex);
tty = __tty_lookup(fd);
if (!tty) {
ret = -ENOTTY;
goto out_unlock;
}
spin_lock_irqsave(&tty->termios_rwsem.lock, flags);
if (tty->termios) {
memcpy(termios_p, tty->termios, sizeof(*termios_p));
ret = 0;
} else
ret = -EIO;
spin_unlock_irqrestore(&tty->termios_rwsem.lock, flags);
tty_kref_put(tty);
out_unlock:
mutex_unlock(&termios_mutex);
return ret;
}
```
这个函数的作用是获取一个终端的属性设置。函数首先通过文件描述符 `fd` 查找对应的 tty 设备,然后通过 `termios_p` 指向的结构体返回终端的属性设置。如果成功,返回 0;否则返回一个负数错误码。