我目前正在学习 C 中的伪终端功能。我能够创建 pty 的主、从端,在 fork 之后,我已将 stdin、stdout、stderr 设置为 fd Slave ,在 exec 后我的孩子仍然提示来自终端的密码,而不是从 stdin(fds) 读取的密码。当复制导致此行为的进程时ssh
,其他程序将按预期工作。
在我找不到原因setsid(); ioctl(0, TIOCSCTTY, 1);
之前,可以在子进程中使用时修复此问题,这是预期的行为吗?execvp
#define _XOPEN_SOURCE 600
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#define __USE_BSD
#include <termios.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <string.h>
int main(int ac, char *av[])
{
int fdm, fds;
int rc;
char input[1024], output[150];
// Check arguments
if (ac <= 1)
{
fprintf(stderr, "Usage: %s program_name [parameters]\n", av[0]);
exit(1);
}
fdm = posix_openpt(O_RDWR);
if (fdm < 0)
{
fprintf(stderr, "Error %d on posix_openpt()\n", errno);
return 1;
}
rc = grantpt(fdm);
if (rc != 0)
{
fprintf(stderr, "Error %d on grantpt()\n", errno);
return 1;
}
rc = unlockpt(fdm);
if (rc != 0)
{
fprintf(stderr, "Error %d on unlockpt()\n", errno);
return 1;
}
// Open the slave side ot the PTY
fds = open(ptsname(fdm), O_RDWR);
// Create the child process
if (fork())
{
fd_set fd_in;
// FATHER
// Close the slave side of the PTY
close(fds);
int pass_entered = 0;
while (1)
{
FD_ZERO(&fd_in);
FD_SET(fdm, &fd_in);
rc = select(fdm + 1, &fd_in, NULL, NULL, NULL);
// If data on master side of PTY
if (FD_ISSET(fdm, &fd_in))
{
rc = read(fdm, output, sizeof(input));
if (rc > 0)
{
// Send data on standard output
if (!pass_entered)
{
write(fdm, "password\n", 10);
pass_entered = 1;
}
write(2, "<<", 2);
write(2, output, rc);
write(2, ">>", 2);
int n = write(fdm, "id\n", 3);
}
}
}
}
else
{
// CHILD
// Close the master side of the PTY
close(fdm);
// The slave side of the PTY becomes the standard input and outputs of the child process
close(0); // Close standard input (current terminal)
close(1); // Close standard output (current terminal)
close(2); // Close standard error (current terminal)
dup2(fds, 0); // PTY becomes standard input (0)
dup2(fds, 1); // PTY becomes standard output (1)
dup2(fds, 2); // PTY becomes standard error (2)
close(fds);
// setsid();
// ioctl(0, TIOCSCTTY, 1);
{
char **child_av;
int i;
// Build the command line
child_av = (char **)malloc(ac * sizeof(char *));
for (i = 1; i < ac; i++)
{
child_av[i - 1] = strdup(av[i]);
}
child_av[i - 1] = NULL;
rc = execvp(child_av[0], child_av);
}
return 1;
}
return 0;
} // main
// gcc test_pty.c && ./a.out python3 /tty_test.py //works
// gcc test_pty.c && ./a.out su root //works
// gcc test_pty.c && ./a.out ssh user@localhost //fails
答案1
”Unix 环境中的高级编程“ (APUE) 显示了用于设置 pty 的不同代码。特别是,子进程总是执行 a
setsid(2)
创建一个新会话。此外,FreeBSD(可能还有其他 *BSD 系统)被指出需要TIOCSCTTY
调用来建立控制终端:
// from http://www.apuebook.com/src.3e.tar.gz -- lib/ptyfork.c
if ((pid = fork()) < 0) {
return(-1);
} else if (pid == 0) { /* child */
if (setsid() < 0)
err_sys("setsid error");
/*
* System V acquires controlling terminal on open().
*/
if ((fds = ptys_open(pts_name)) < 0)
err_sys("can't open slave pty");
close(fdm); /* all done with master in child */
#if defined(BSD)
/*
* TIOCSCTTY is the BSD way to acquire a controlling terminal.
*/
if (ioctl(fds, TIOCSCTTY, (char *)0) < 0)
err_sys("TIOCSCTTY error");
#endif
除了示例代码之外,您还需要深入挖掘以找到ptym_open
和代码,以准确了解 APUE 代码何时执行哪些操作。ptys_open
pty/main.c
您的子执行代码似乎不必要地复杂;
{
char **child_av;
int i;
// Build the command line
child_av = (char **)malloc(ac * sizeof(char *));
for (i = 1; i < ac; i++)
{
child_av[i - 1] = strdup(av[i]);
}
child_av[i - 1] = NULL;
rc = execvp(child_av[0], child_av);
}
可能可以替换为
av++;
execvp(av[0], av);
它将直接使用现有的参数而不是重复它们。作为替代方案,APUE 显示
execvp(argv[optind], &argv[optind]);
因为他们已经处理了选项getopt
。