Last active
June 14, 2020 15:04
-
-
Save ksurent/9f331ed546b4dabe90c5afae6230ad24 to your computer and use it in GitHub Desktop.
Simple test case for Linux's PR_SET_PDEATHSIG
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import sys | |
import time | |
import prctl | |
import signal | |
def do_the_thing(): | |
pid = os.fork() | |
if pid == 0: | |
# PR_SET_PDEATHSIG is preserved across exec(2) but gets cleared after fork(2). | |
# Modern bash, it appears, translates "echo" directly into write(2), | |
# i.e. doesn't fork&exec /bin/echo. | |
# This is not true for sleep, though. So the sleep invocation below may become | |
# a zombie? | |
prctl.set_pdeathsig(signal.SIGTERM) | |
os.execv('/bin/sh', ['/bin/sh', '-c', 'while :; do echo "still running: $$"; sleep 1; done']) | |
else: | |
time.sleep(5) | |
print('killing the parent') | |
os.kill(os.getpid(), signal.SIGKILL) | |
do_the_thing() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment