显示linux系统的tcgetattr函数源码及其文件路径
时间: 2023-11-28 10:03:39 浏览: 44
`tcgetattr()` 函数是用于获取终端属性的函数,其定义在 `termios.h` 头文件中。以下是 `tcgetattr()` 函数的源码:
```c
#include <termios.h>
int tcgetattr(int fd, struct termios *termios_p);
```
`tcgetattr()` 函数的文件路径通常在 `/usr/include/termios.h`。
需要注意的是,`tcgetattr()` 函数是一个系统调用,其实现是在内核中完成的,因此其完整的源代码并不在 `/usr/include/termios.h` 中。
相关问题
显示linux系统的tcgetattr函数源码
`tcgetattr` 是一个系统调用函数,其源码实现位于内核中。下面是 `tcgetattr` 函数的源码:
```c
int tcgetattr(int fd, struct termios *termios_p)
{
struct tty_struct *tty;
int ret = 0;
tty = get_current_tty();
if (!tty)
return -ENXIO;
mutex_lock(&tty->termios_mutex);
if (fd != tty->driver->ttynr) {
ret = -EINVAL;
goto out_unlock;
}
ret = tty_termios_copy_hw(tty, termios_p);
if (ret)
goto out_unlock;
ret = tty_termios_copy_ld(tty, termios_p);
if (ret)
goto out_unlock;
out_unlock:
mutex_unlock(&tty->termios_mutex);
return ret;
}
```
该函数接受两个参数:一个文件描述符 `fd` 和一个指向 `termios` 结构的指针 `termios_p`。
`tcgetattr` 在内部调用 `get_current_tty` 函数,获取当前进程所连接的 tty 设备,并使用 `tty->driver->ttynr` 检查传递的文件描述符是否与该 tty 设备的文件描述符相匹配。如果不匹配,则返回 `-EINVAL` 错误码。
如果文件描述符匹配,`tcgetattr` 将调用 `tty_termios_copy_hw` 和 `tty_termios_copy_ld` 函数来将 tty 的硬件和软件参数复制到 `termios_p` 结构中。如果复制失败,`tcgetattr` 将返回相应的错误码。
最后,`tcgetattr` 函数将释放 tty 设备的 termios 互斥锁,并返回执行结果。
显示linux5.4源码中tcgetattr系统函数的源码
`tcgetattr()` 函数是一个系统调用,其源码实现在 Linux 内核中。在 Linux 5.4 版本中,`tcgetattr()` 函数的实现代码如下:
```c
SYSCALL_DEFINE2(tcgetattr, unsigned int, fd, struct termios __user *, termios_p)
{
struct ktermios termios;
int err;
err = tty_termios_ioctl(fd, TCGETS, &termios);
if (!err)
err = copy_to_user(termios_p, &termios, sizeof(*termios_p)) ? -EFAULT : 0;
return err;
}
```
该函数实现定义在 `fs/ioctl.c` 中,是一个系统调用函数,使用了 `SYSCALL_DEFINE2()` 宏进行定义。
该函数的参数包括文件描述符 `fd` 和一个指向 `termios` 结构体的指针 `termios_p`。函数内部会调用 `tty_termios_ioctl()` 函数,该函数会通过 `TCGETS` 命令获取终端设备的属性,并将其保存在 `termios` 结构体中。
最后,函数会通过 `copy_to_user()` 函数将 `termios` 结构体的内容复制到用户空间的 `termios_p` 指向的位置,如果复制失败则返回 `-EFAULT` 错误码,否则返回 0 表示成功。