xref: /llvm-project/lldb/test/API/driver/job_control/shell.py (revision 4bcadd66864bf4af929ac8753a51d7ad408cdef0)
1"""
2Launch a process (given through argv) similar to how a shell would do it.
3"""
4
5import signal
6import subprocess
7import sys
8import os
9
10
11def preexec_fn():
12    # Block SIGTTOU generated by the tcsetpgrp call
13    orig_mask = signal.pthread_sigmask(signal.SIG_BLOCK, [signal.SIGTTOU])
14
15    # Put us in a new process group.
16    os.setpgid(0, 0)
17
18    # And put it in the foreground.
19    fd = os.open("/dev/tty", os.O_RDONLY)
20    os.tcsetpgrp(fd, os.getpgid(0))
21    os.close(fd)
22
23    signal.pthread_sigmask(signal.SIG_SETMASK, orig_mask)
24
25
26if __name__ == "__main__":
27    child = subprocess.Popen(sys.argv[1:], preexec_fn=preexec_fn)
28    print("PID=%d" % child.pid)
29
30    _, status = os.waitpid(child.pid, os.WUNTRACED)
31    print("STATUS=%d" % status)
32
33    returncode = child.wait()
34    print("RETURNCODE=%d" % returncode)
35