blob: a3217af9c5ef664f832a843652aad314859e6ec5 [file] [log] [blame]
jadmanski6dadd832009-02-05 23:39:27 +00001#!/usr/bin/python
2
3import common
4import sys, os, signal, time, subprocess, fcntl
5
6logdir = sys.argv[1]
7stdout_start = int(sys.argv[2]) # number of bytes we can skip on stdout
8stderr_start = int(sys.argv[3]) # nubmer of bytes we can skip on stderr
9
jadmanskic641fd72009-04-03 21:50:11 +000010# if any of our tail processes die, the monitor should die too
11def kill_self(signum, frame):
12 os.kill(os.getpid(), signal.SIGTERM)
13signal.signal(signal.SIGCHLD, kill_self)
14
jadmanski6dadd832009-02-05 23:39:27 +000015devnull = open(os.devnull, 'w')
16
17# launch some tail processes to pump the std* streams
18def launch_tail(filename, outstream, start):
19 path = os.path.join(logdir, filename)
20 argv = ['tail', '--retry', '--follow=name', '--bytes=+%d' % start, path]
21 # stdout=sys.stdout fails on pre-2.5 python (bug in subprocess module)
22 if outstream != subprocess.PIPE and outstream.fileno() == 1:
23 return subprocess.Popen(argv, stderr=devnull)
24 else:
25 return subprocess.Popen(argv, stdout=outstream, stderr=devnull)
26stdout_pump = launch_tail('stdout', sys.stdout, stdout_start)
27stderr_pump = launch_tail('stderr', sys.stderr, stderr_start)
28
jadmanskif3fbce82009-02-18 18:54:33 +000029# wait for logdir/started to exist to be sure autotestd is started
30start_time = time.time()
31started_file_path = os.path.join(logdir, 'started')
32while not os.path.exists(started_file_path):
33 time.sleep(1)
34 if time.time() - start_time >= 30:
35 raise Exception("autotestd failed to start in %s" % logdir)
jadmanskif3fbce82009-02-18 18:54:33 +000036
jadmanski6dadd832009-02-05 23:39:27 +000037# watch the exit code file for an exit
38exit_code_file = open(os.path.join(logdir, 'exit_code'))
39fcntl.flock(exit_code_file, fcntl.LOCK_EX)
40try:
41 exit_code = exit_code_file.read()
42 if len(exit_code) != 4:
43 exit_code = -signal.SIGKILL # autotestd was nuked
44 else:
45 exit_code = int(exit_code)
46finally:
47 fcntl.flock(exit_code_file, fcntl.LOCK_UN)
48 exit_code_file.close()
49
jadmanski263313e2009-07-01 16:26:03 +000050# tail runs in 1s polling loop, so give them a chance to finish
51time.sleep(2)
52# clear the SIGCHLD handler so that killing the tails doesn't kill us
53signal.signal(signal.SIGCHLD, signal.SIG_DFL)
jadmanski6dadd832009-02-05 23:39:27 +000054os.kill(stdout_pump.pid, signal.SIGTERM)
55os.kill(stderr_pump.pid, signal.SIGTERM)
56
57# exit (with the same code as autotestd)
58sys.exit(exit_code)