When EOF is seen on stdin, the child process is killed.
When the child process exits, this helper exits too.
+
+usage: daemon-helper <signal> [--kill-group] [nostdin] command...
"""
import fcntl
cmd_start = 2
+group = False
+if sys.argv[cmd_start] == "--kill-group":
+ group = True
+ cmd_start += 1
+
nostdin = False
if sys.argv[cmd_start] == "nostdin":
nostdin = True
proc = subprocess.Popen(
args=sys.argv[cmd_start:],
stdin=devnull,
+ preexec_fn=os.setsid,
)
flags = fcntl.fcntl(0, fcntl.F_GETFL)
data = os.read(0, 1)
if not data:
saw_eof = True
- proc.send_signal(end_signal)
+ if not group:
+ proc.send_signal(end_signal)
+ else:
+ os.killpg(proc.pid, end_signal)
break
else:
sig, = struct.unpack('!b', data)
- proc.send_signal(sig)
+ if not group:
+ proc.send_signal(sig)
+ else:
+ os.killpg(proc.pid, end_signal)
+
if proc.poll() is not None:
# child exited