blob: a94c9668cb609dc69b2dde0a3c7a51dd092d9e68 [file] [log] [blame]
import common
import sys, os, signal, time, subprocess, fcntl
logdir = sys.argv[1]
stdout_start = int(sys.argv[2]) # number of bytes we can skip on stdout
stderr_start = int(sys.argv[3]) # nubmer of bytes we can skip on stderr
# TODO ( sbasi: Remove extra logging.
stderr = open(os.path.join(logdir, 'stderr'), 'a', 0)
print >> stderr, 'Entered autotestd_monitor.'
# if any of our tail processes die, the monitor should die too
def kill_self(signum, frame):
os.kill(os.getpid(), signal.SIGTERM)
signal.signal(signal.SIGCHLD, kill_self)
devnull = open(os.devnull, 'w')
# launch some tail processes to pump the std* streams
def launch_tail(filename, outstream, start):
path = os.path.join(logdir, filename)
argv = ['tail', '--retry', '--follow=name', '--bytes=+%d' % start, path]
# stdout=sys.stdout fails on pre-2.5 python (bug in subprocess module)
if outstream != subprocess.PIPE and outstream.fileno() == 1:
return subprocess.Popen(argv, stderr=devnull)
return subprocess.Popen(argv, stdout=outstream, stderr=devnull)
stdout_pump = launch_tail('stdout', sys.stdout, stdout_start)
stderr_pump = launch_tail('stderr', sys.stderr, stderr_start)
print >> stderr, 'Finished launching tail subprocesses.'
# wait for logdir/started to exist to be sure autotestd is started
start_time = time.time()
started_file_path = os.path.join(logdir, 'started')
while not os.path.exists(started_file_path):
if time.time() - start_time >= 30:
raise Exception("autotestd failed to start in %s" % logdir)
print >> stderr, 'Finished waiting on autotestd to start.'
# watch the exit code file for an exit
exit_code_file = open(os.path.join(logdir, 'exit_code'))
fcntl.flock(exit_code_file, fcntl.LOCK_EX)
print >> stderr, 'Got lock of exit_code_file.'
exit_code =
if len(exit_code) != 4:
exit_code = -signal.SIGKILL # autotestd was nuked
exit_code = int(exit_code)
fcntl.flock(exit_code_file, fcntl.LOCK_UN)
print >> stderr, 'Released lock of exit_code_file and closed it.'
# tail runs in 1s polling loop, so give them a chance to finish
print >> stderr, 'Killing child processes.'
# clear the SIGCHLD handler so that killing the tails doesn't kill us
signal.signal(signal.SIGCHLD, signal.SIG_DFL)
os.kill(, signal.SIGTERM)
os.kill(, signal.SIGTERM)
# exit (with the same code as autotestd)