Home > Enterprise >  How can I trick the isatty check of the child process?
How can I trick the isatty check of the child process?

Time:05-30

Here is the code for the parent process:

#include <stdio.h>

int main() {
    char buffer[BUFSIZ];
    FILE *child = popen("./child", "r");
    size_t len = fread((void *) &buffer, BUFSIZ, 1, child);
    fprintf(stdout, "child message: %s", 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;
}
$ gcc child.c -o child && gcc main.c -o main
$ ./main
child message: false

I guess I shouldn't start the child process via popen , it can't fool the isatty check of the child process.

How should I create a subprocess so that I can cheat the isatty check?

CodePudding user response:

You can allocate a pty for your process. This can be done conveniently with the script utility, available on many platforms by default. This usage is for the util-linux version:

#include <stdio.h>

int main() {
    char buffer[BUFSIZ];
    FILE *child = popen("script -qc ./child /dev/null", "r");
    size_t len = fread((void *) &buffer, BUFSIZ, 1, child);
    fprintf(stdout, "child message: %s", buffer);
    return 0;
}

macOS would instead use script -q /dev/null ./child

  •  Tags:  
  • c
  • Related