【问题标题】:How do *nix pseudo-terminals work ? What's the master/slave channel?*nix 伪终端如何工作?什么是主/从通道?
【发布时间】:2010-10-03 08:16:53
【问题描述】:

我想在 Linux 系统上用 C 语言编写一个简单、愚蠢的 X 终端仿真器。

起初,我只是想我必须打开一个 shell 并显示它的输出。 我检查了xterm和rxvt的代码,看起来有点复杂。

首先,我必须用 openpty 打开一个伪终端。所以我查看了手册页,发现 openpty 填充了 2 个文件描述符,即主文件和从属文件。 xterm 和 rxvt 代码都很混乱,因为这些特殊文件依赖于系统。

我理解 termios 的东西:它只是一堆关于终端转义码的信息。 我真正不明白的是:我想用主/从文件描述符做什么?

一个打开终端、登录、在 shell 上执行“ls”的示例程序会很棒。

(英语不是我的母语,请原谅我最终的错误)

编辑: 这是我想出的示例代码:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <pty.h>
#include <utmp.h>
#include <ctype.h>

void
safe_print (char* s)
{
    while(*s) { 
        if(*s == '\n')
            putchar("\n");
        else if(iscntrl(*s))
            printf("\\e(%d)", *s);
        else
            putchar(*s);
        s++;
    }
}


int
main (int argc, char** argv)
{
    char buf[BUFSIZ] = {0};
    int master;
    int ret = forkpty(&master, NULL, NULL, NULL);

    if(ret == -1)
        puts("no fork"), exit(0);

    if(!ret) { 
        execl("/bin/sh", "sh", NULL);
        exit(0);
    }

    sleep(1); /* let the shell run */


    if(argc >= 2) {
        write(master, argv[1], strlen(argv[1]));
        write(master, "\n", 1);
    } else {
        write(master, "date\n", sizeof "date\n");
    }


    while(1) {
        switch(ret = read(master, buf, BUFSIZ)) {
        case -1:
            puts("error!"); 
            exit(1);
            break;
        case 0:
            puts("nothing.."), sleep(1);
            break;
        default:
            buf[ret] = '\0';
            safe_print(buf);

        }
    }

    close(master);

    return 0;
}    

【问题讨论】:

  • 我认为名为“screen”的命令行程序使用了这个。有了它,您可以在主机上登录控制台,如果您被甩了,您可以重新登录并重新连接该会话,然后继续。这就是 pty 的本质。它有一个与主机系统交互的通道,以及一个“反向通道”,您在外部告诉它要做什么(并查看结果)。我也没有任何实际的实施经验;我在“Linux 应用程序开发”中读到了它们。在X下,我想还有更多的门面,但基本原则应该是

标签: c linux tty


【解决方案1】:

关于您问题的主/从部分,来自pty(4) 手册页(引用自我系统上的 openpty(3) 手册页):

伪终端是一对 字符设备,主设备和 从设备。从设备 为进程提供接口 与 tty(4) 中描述的相同。 然而,而所有其他设备 提供所描述的接口 在 tty(4) 中有一个硬件设备 在他们身后的某种东西,奴隶 相反,设备有另一个进程 通过主人操纵它 伪终端的一半。 也就是说, 任何写在主设备上的东西 被提供给从设备作为输入 以及写在奴隶身上的任何东西 设备被呈现为上的输入 主设备。

手册页是你的朋友。

【讨论】:

    【解决方案2】:

    我刚刚尝试了在 this tutorial 上找到的示例,它们对我来说工作得很好,我认为它们是解决问题的一个有趣的起点。

    编辑: 本教程简要解释了伪终端功能。讲解是一步一步做的,后面是例子。

    下面的例子展示了如何创建一个新的伪终端,fork这个过程分为两部分,一个写在伪终端的master端,来自伪终端的 slave 端的另一个读数。

    #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> 
    
    
    int main(void) 
    { 
    int fdm, fds, rc; 
    char input[150]; 
    
    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 PTY
    fds = open(ptsname(fdm), O_RDWR); 
    printf("Virtual interface configured\n");
    printf("The master side is named : %s\n", ptsname(fdm));
    
    // Creation of a child process
    if (fork()) 
    { 
      // Father
     
      // Close the slave side of the PTY 
      close(fds); 
      while (1) 
      { 
        // Operator's entry (standard input = terminal) 
        write(1, "Input : ", sizeof("Input : ")); 
        rc = read(0, input, sizeof(input)); 
        if (rc > 0) 
        {
          // Send the input to the child process through the PTY 
          write(fdm, input, rc); 
    
          // Get the child's answer through the PTY 
          rc = read(fdm, input, sizeof(input) - 1); 
          if (rc > 0) 
          { 
            // Make the answer NUL terminated to display it as a string
            input[rc] = '\0'; 
    
            fprintf(stderr, "%s", input); 
          } 
          else 
          { 
            break; 
          } 
        } 
        else 
        { 
          break; 
        } 
      } // End while 
    } 
    else 
    { 
    struct termios slave_orig_term_settings; // Saved terminal settings 
    struct termios new_term_settings; // Current terminal settings 
    
      // Child
    
      // Close the master side of the PTY 
      close(fdm); 
    
      // Save the default parameters of the slave side of the PTY 
      rc = tcgetattr(fds, &slave_orig_term_settings); 
    
      // Set raw mode on the slave side of the PTY
      new_term_settings = slave_orig_term_settings; 
      cfmakeraw (&new_term_settings); 
      tcsetattr (fds, TCSANOW, &new_term_settings); 
    
      // 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) 
    
      dup(fds); // PTY becomes standard input (0) 
      dup(fds); // PTY becomes standard output (1) 
      dup(fds); // PTY becomes standard error (2) 
    
      while (1) 
      { 
        rc = read(fds, input, sizeof(input) - 1); 
    
        if (rc > 0) 
        { 
          // Replace the terminating \n by a NUL to display it as a string
          input[rc - 1] = '\0'; 
    
          printf("Child received : '%s'\n", input); 
        } 
        else 
        { 
          break; 
        } 
      } // End while 
    } 
    
    return 0; 
    } // main
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-21
      • 1970-01-01
      • 1970-01-01
      • 2016-04-14
      • 1970-01-01
      相关资源
      最近更新 更多