Detecting when a child process is waiting for input

前端 未结 2 1665
北恋
北恋 2020-12-24 03:22

I\'m writing a Python program for running user-uploaded arbitrary (and thus, at the worst case, unsafe, erroneous and crashing) code on a Linux server. The security question

2条回答
  •  爱一瞬间的悲伤
    2020-12-24 03:59

    Instead of trying to detect when the child process is waiting for an input, you can use the linux script command. From the man page for script:

    The script utility makes a typescript of everything printed on your terminal.

    You can use it like this if you were using it on a terminal:

    $ script -q  
    

    So in Python you can try giving this command to the Popen routine instead of just .

    Edit: I made the following program:

    #include 
    int main() {
        int i;
        scanf("%d", &i);
        printf("i + 1 = %d\n", i+1);
    }
    

    and then ran it as follows:

    $ echo 9 > infile
    $ script -q output ./a.out < infile
    $ cat output
    9
    i + 1 = 10
    

    So I think it can be done in Python this way instead of using the stdout, stderr and stdin flags of Popen.

提交回复
热议问题