tty的Linux ioctl提供了EPERM

时间:2019-05-08 11:51:03

标签: c linux tty ioctl

我尝试实现一个getty程序(该程序获取特定的tty,将其设置为stdin,stdout和stderr并正常执行登录过程;典型的实现为agetty)。

我的问题是,在进行ioctl调用时,它总是给我一个EPERM错误,这应该更改控制终端。

关于ioctl_tty(2)的联机帮助页上说:

   TIOCSCTTY int arg
          Make the given terminal the controlling terminal of the calling process.  The calling  process  must  be  a  session
          leader and not have a controlling terminal already.  For this case, arg should be specified as zero.

          If  this terminal is already the controlling terminal of a different session group, then the ioctl fails with EPERM,
          unless the caller has the CAP_SYS_ADMIN capability and arg equals 1, in which case the terminal is stolen,  and  all
          processes that had it as controlling terminal lose it.

现在,根据手册capabilities(7),如果我仅出于测试目的使用超级用户特权(即GID和UID = 0)运行它,则理论上内核应该仅跳过功能检查。

以root身份运行时,以下代码有什么问题?

#include <termios.h>
#include <stropts.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>

int main (int argc, char **argv) {
    char path[50] = "/dev/";
    if (argc > 1) strcat (path, argv[1]);
            /* First argument is tty device name */
    else return 1;

    int fd = open (path, O_RDWR, 0);
    if (fd == -1) return 1;

    ioctl (fd, TIOCSCTTY, 1); /* Here is the error */
}

0 个答案:

没有答案