tomerfiliba / plumbum

Plumbum: Shell Combinators
https://plumbum.readthedocs.io
MIT License
2.79k stars 182 forks source link

Set PR_SET_PDEATHSIG on for all commands #597

Open willir opened 2 years ago

willir commented 2 years ago

Hi, thanks for the great library. I have a question - is it possible to set PR_SET_PDEATHSIG on Linux for all running commands?

When I use subprocess myself, I do it like:

SIGHUP = 1
PR_SET_PDEATHSIG = 1
libc = ctypes.CDLL("/lib64/libc.so.6")

def set_pdeathsig_cb():
    ppid = os.getpid()

    def cb():
        res = libc.prctl(PR_SET_PDEATHSIG, SIGHUP)
        if res != 0:
            print(f"Fail to set PR_SET_PDEATHSIG. Return value: {res}", file=sys.stderr)
            sys.exit(1)
        if os.getppid() != ppid:
            print("Parent process has already died", file=sys.stderr)
            sys.exit(1)

    return cb

with subprocess.Popen(cmd, preexec_fn=set_pdeathsig_cb()) as p:
    ...

Thank you.