forkpty - 插座 [英] forkpty - socket

查看:504
本文介绍了forkpty - 插座的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个简单的远程登录/服务器守护进程必须运行一个新的套接字连接上的程序。
这部分工作的罚款。

I'm trying to develop a simple "telnet/server" daemon which have to run a program on a new socket connection. This part working fine.

但我有我的新工艺关联于pty,因为这个过程中有一些终端能力(如readline的)。

But I have to associate my new process to a pty, because this process have some terminal capabilities (like a readline).

在code我developped是(其中socketfd是新输入连接新的socket文件描述符):

The code I've developped is (where socketfd is the new socket file descriptor for the new input connection) :

int masterfd, pid;
const char *prgName = "...";
char *arguments[10] = ....;

if ((pid = forkpty(&masterfd, NULL, NULL, NULL)) < 0)
    perror("FORK");
else if (pid)
    return pid;
else
{
    close(STDOUT_FILENO);
    dup2(socketfd, STDOUT_FILENO);

    close(STDIN_FILENO);
    dup2(socketfd, STDIN_FILENO);

    close(STDERR_FILENO);
    dup2(socketfd, STDERR_FILENO);

    if (execvp(prgName, arguments) < 0)
    {
        perror("execvp");
        exit(2);
    }
}

使用了code,我的prgName,并因此,终端功能(使用ls -la的/ proc / PID / FD观察时)被关联到插座的标准输入/输出/标准错误的文件描述符这个过程中是行不通的。

With that code, the stdin / stdout / stderr file descriptor of my "prgName" are associated to the socket (when looking with ls -la /proc/PID/fd), and so, the terminal capabilities of this process doesn't work.

通过SSH / sshd的远程设备上(SSH连接下)的连接,并执行localY坐标的测试prgName,表明标准输入/输出/标准错误此过程中prgName的FD相关联于pty (因此该方法的终端性能被精细工作)。

A test with a connection via ssh/sshd on the remote device, and executing "localy" (under the ssh connection) prgName, show that the stdin/stdout/stderr fd of this process "prgName" are associated to a pty (and so the terminal capabilities of this process are working fine).

我做错了吗?
如何我socketfd与PTY(由forkpty创建)相关联?

What I am doing wrong? How to associate my socketfd with the pty (created by forkpty) ?

感谢

亚历

推荐答案

您必须写一些code从插座传输数据到主pty,反之亦然。它通常是一个父进程的工作。注意,数据传输必须是双向的。有很多选择:一个选择() - 驱动的周期来跟踪这两个masterfd和socketfd

You must write some code to transfer data from the socket to the master pty and vice versa. It's usually a parent process' job. Note that the data transfer must be bidirectional. There are many options: a select()-driven cycle to track both the masterfd and the socketfd

(正如提示,非常糟糕code,不生产!缺失错误和EOF检查!)

(just as hint, very bad code, not for production!!! Missing error and eof checks!!!)

for (;;) {
  FD_ZERO(&set);
  FD_SET(masterfd,&set);
  FD_SET(socketfd,&set);
  select(...,&set,...);
  if (FD_ISSET(masterfd,&set)) {
       read(masterfd,&c,1);
       write(socketfd,&c,1);
  }
  if (FD_ISSET(sockerfd,&set)) {
       read(sochetfd,&c,1);
       write(masterfd,&c,1);
  }

或一对线程,一个用于socketfd-> masterfd和一个用于masterfd-> sockefd转移

or a pair of threads, one for socketfd->masterfd and one for masterfd->sockefd transfers.

(正如提示,非常糟糕code,不生产!)

(just as hint, very bad code, not for production!!!)

/*thread 1 */
      while (read(masterfd,&c,1) > 0)
           write(socketfd,&c,1);


/*thread 2 */
      while (read(socketfd,&c,1) > 0)
           write(masterfdfd,&c,1);

反正你必须在分公司的父方添加一些code。

Anyway you must add some code in the parent side of the branch.

问候

---编辑---
当然,你不能重定向FD 0,1和2中的子进程socketfd。

---EDIT--- Of course, you must not redirect fd 0,1 and 2 to socketfd in the child process.

这篇关于forkpty - 插座的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆