PTY / TTY -Unix:无法打开Slave FD

时间:2016-06-02 03:09:21

标签: c terminal posix master-slave pty

我正在尝试创建一个单独的PTY终端。我正在尝试使用以下代码。但奴隶FD总是变成-1。有人可以指出我在哪里做错了。

/* Open available PTY */
master_fd = posix_openpt(O_RDWR | O_NOCTTY);       
if (master_fd == -1) {
    return -1;
}

/* Give Access to Slave */
if (grantpt(master_fd) == -1) {            
    perror("Unable to Provice Access to Slave");
    goto error;
}

/* Unlock Slave PTY */
if (unlockpt(master_fd) == -1) {
    perror("Unable to lock Slave PTY");
    goto error;
}

/* Get Slave Device Name */
slave_device_name = ptsname(master_fd);                      
if (slave_device_name == NULL) {
    perror("Unable to get Name for Slave Device")
    goto error;
}


slave_fd = open(slave_device_name, O_RDWR|O_NOCTTY);
if (slave_fd == -1) {
    perror("Unable to retrieve FD for slave");
    goto error;
}

error:
close(master_fd);
return -1;

0 个答案:

没有答案