Can you fool isatty AND log stdout and stderr separately?

前端 未结 4 1761
悲&欢浪女
悲&欢浪女 2020-12-28 12:55

Problem

So you want to log the stdout and stderr (separately) of a process or subprocess, without the output being different from what you\'d see in the terminal i

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-28 13:16

    Like this?

    % ./challenge.py >stdout 2>stderr
    % cat stdout 
    This is a real tty :)
    standard output data
    % cat stderr 
    standard error data
    

    Because I cheated a little bit. ;-)

    % echo $LD_PRELOAD
    /home/karol/preload.so
    

    Like so...

    % gcc preload.c -shared -o preload.so -fPIC
    

    I feel dirty now, but it was fun. :D

    % cat preload.c
    #include 
    
    int isatty(int fd) {
        if(fd == 2 || fd == 1) {
            return 1;
        }
        return 0;
    }
    
    char* ttyname(int fd) {
        static char* fake_name = "/dev/fake";
        if(fd == 2 || fd == 1) {
            return fake_name;
        }
        return NULL;
    }
    

提交回复
热议问题