Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set PR_SET_PDEATHSIG on for all commands #597

Open
willir opened this issue Mar 31, 2022 · 0 comments
Open

Set PR_SET_PDEATHSIG on for all commands #597

willir opened this issue Mar 31, 2022 · 0 comments

Comments

@willir
Copy link

willir commented Mar 31, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant