在Linux中,父过程如何将PTY应用于子过程?

发布于 2025-02-02 23:32:10 字数 1299 浏览 3 评论 0原文

这是父过程的代码:

#include <stdlib.h>
#include <fcntl.h>
#include <pty.h>
#include <stdio.h>
#include <utmp.h>
#include <sys/ioctl.h>
#include <unistd.h>

int main() {
    int err = 0, len = 0;
    int master, aslave;
    char name[BUFSIZ], buffer[BUFSIZ];
    FILE *child;

    err = openpty(&master, &aslave, &name[0], NULL, NULL);
    if (err == -1) {
        perror("openpty faild.");
        return EXIT_FAILURE;
    }
    fprintf(stdout, "pty name: %s\n", name);

    child = popen("./child", "r");
    if (!child) {
        perror("popen faild");
        return EXIT_FAILURE;
    }

    len = read(master, &buffer[0], BUFSIZ);
    if (len == 0) {
        fprintf(stdout, "Unable to read output from subprocess.");
        return EXIT_FAILURE;
    }
    fprintf(stdout, "child message: %s\n", buffer);
    return 0;
}

以下是子过程的代码:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
    fprintf(stdout, "%s\n", isatty(fileno(stdout)) ? "true": "false");
    return EXIT_SUCCESS;
}

我知道,要在子过程中通过ISATTY检查,有必要通过PTY与子过程进行交互,但是需要是完毕?

尽管这可能没有应用程序方案,但我仍然想知道PTY的用法。

Here is the code for the parent process:

#include <stdlib.h>
#include <fcntl.h>
#include <pty.h>
#include <stdio.h>
#include <utmp.h>
#include <sys/ioctl.h>
#include <unistd.h>

int main() {
    int err = 0, len = 0;
    int master, aslave;
    char name[BUFSIZ], buffer[BUFSIZ];
    FILE *child;

    err = openpty(&master, &aslave, &name[0], NULL, NULL);
    if (err == -1) {
        perror("openpty faild.");
        return EXIT_FAILURE;
    }
    fprintf(stdout, "pty name: %s\n", name);

    child = popen("./child", "r");
    if (!child) {
        perror("popen faild");
        return EXIT_FAILURE;
    }

    len = read(master, &buffer[0], BUFSIZ);
    if (len == 0) {
        fprintf(stdout, "Unable to read output from subprocess.");
        return EXIT_FAILURE;
    }
    fprintf(stdout, "child message: %s\n", buffer);
    return 0;
}

Here is the code for the child process:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
    fprintf(stdout, "%s\n", isatty(fileno(stdout)) ? "true": "false");
    return EXIT_SUCCESS;
}

I know that in order to pass the isatty check in the child process, it is necessary to interact with the child process through pty, but what needs to be done?

While this may not have an application scenario, I would still like to know the usage of pty.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九局 2025-02-09 23:32:10

可能您可以使用posix_openpt + fork在主和从属过程之间进行互动
以下是一个例子

#include <stdlib.h>
#include <fcntl.h>
#include <pty.h>
#include <stdio.h>
#include <utmp.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <unistd.h>

int main(int argc, char* argv[]) {

  int fdMaster = 0;
  int fdSlave = 0;
  int err = 0;
  int pid = 0;


  fdMaster = posix_openpt(O_RDWR);
  if (fdMaster < 0) {
    exit(1);
  }

  err = grantpt(fdMaster);
  if (err != 0) {
    exit(1);
  }

  err = unlockpt(fdMaster);
  if (err != 0) {
    exit(1);
  }

  fdSlave = open(ptsname(fdMaster), O_RDWR);
  if (fdSlave < 0) {
    exit(1);
  }

  pid = fork();

  if (pid < 0) {
    close(fdMaster);
    exit(1);
  } else if (pid == 0) {
    // child
    close(fdMaster);
    // dup stdin, stdout & stderr
    if (dup2(fdSlave, STDIN_FILENO) != STDIN_FILENO) {
      exit(1);
    }
    if (dup2(fdSlave, STDOUT_FILENO) != STDOUT_FILENO) {
      exit(1);
    }
    if (dup2(fdSlave, STDERR_FILENO) != STDERR_FILENO) {
      exit(1);
    }
    if (-1 == execvp("./child", NULL)) {
     exit(1);
    }
  } else {
    // parent
    close(fdSlave);
    int status = 1;
    int len = 0;
    char buffer[BUFSIZ];
    wait(&status);
    len = read(fdMaster, buffer, BUFSIZ);
    if (len <= 0) {
        fprintf(stdout, "Unable to read output from subprocess.");
        return EXIT_FAILURE;
    }
    fprintf(stdout, "child message: %s\n", buffer);
    return 0;
  }
}

Probably you could use posix_openpt + fork to have interaction between master and slave processes
Following is an example

#include <stdlib.h>
#include <fcntl.h>
#include <pty.h>
#include <stdio.h>
#include <utmp.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <unistd.h>

int main(int argc, char* argv[]) {

  int fdMaster = 0;
  int fdSlave = 0;
  int err = 0;
  int pid = 0;


  fdMaster = posix_openpt(O_RDWR);
  if (fdMaster < 0) {
    exit(1);
  }

  err = grantpt(fdMaster);
  if (err != 0) {
    exit(1);
  }

  err = unlockpt(fdMaster);
  if (err != 0) {
    exit(1);
  }

  fdSlave = open(ptsname(fdMaster), O_RDWR);
  if (fdSlave < 0) {
    exit(1);
  }

  pid = fork();

  if (pid < 0) {
    close(fdMaster);
    exit(1);
  } else if (pid == 0) {
    // child
    close(fdMaster);
    // dup stdin, stdout & stderr
    if (dup2(fdSlave, STDIN_FILENO) != STDIN_FILENO) {
      exit(1);
    }
    if (dup2(fdSlave, STDOUT_FILENO) != STDOUT_FILENO) {
      exit(1);
    }
    if (dup2(fdSlave, STDERR_FILENO) != STDERR_FILENO) {
      exit(1);
    }
    if (-1 == execvp("./child", NULL)) {
     exit(1);
    }
  } else {
    // parent
    close(fdSlave);
    int status = 1;
    int len = 0;
    char buffer[BUFSIZ];
    wait(&status);
    len = read(fdMaster, buffer, BUFSIZ);
    if (len <= 0) {
        fprintf(stdout, "Unable to read output from subprocess.");
        return EXIT_FAILURE;
    }
    fprintf(stdout, "child message: %s\n", buffer);
    return 0;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文