How can I change a python script's STDIN to be the STDOUT of another process in the middle of the runtime of that script?
I'm writing a python application that runs as a non-root user and that launches a child process as root using subprocess
. Due to complexities in how MacOS handles privilege escalation, the child process's parent process is no longer the python script that created it, the child looses its tty, and the stdin/stdout communication gets broken between parent & child.
Since the child process is root, I can find the actual parent processes' PID and TTY using ps
result = os.popen( 'ps -eo "pid, tty, command" | grep -iE "[s]pawn_root.py"' ).read().splitlines().pop().split()
pid = result[0]
tty = result[1]
And I was successfully able to attach the stdin of the child process to the tty of the parent process as follows
sys.stdin = open( '/dev/' +str(tty), 'r' )
Unfortunately, it seems that attaching to the tty of the parent process is not the same as attaching to the STDOUT of the parent process. I guess it's a path to a different file descriptor, somehow.
If I have a python process A running as root on MacOS, how can I attach that processes' STDIN to the STDOUT of process B, given the PID or Command name of process B?