#!/usr/bin/python3

import subprocess
import sys

"""
Wrapper for xscreensaver binaries.

This script spawns a process, then monitors stdin of its own parent
and terminates the child when an EOF is received from the parent.

This will happen when safechild's parent exits and its
pipes are closed, regardless of *how* it got terminated.

This script must be spawned by its parent with a STDIN pipe.  The pipe
never actually needs to be used or access (in fact it's probably safer
if it *isn't* accessed, since unless you're careful, garbage collection
may cause an EOF to be sent, causing early termination of this script
and its child.)

"""

proc = None

try:
    proc = subprocess.Popen(sys.argv[1:])

    while True:
        line = sys.stdin.readline()
        if line == "":
            proc.kill()
            proc.wait()
            break
except Exception as e:
    print("safechild exception: ", e)
