blob: 0393d79e01f5fccead07cc5c61f38d05ff4015b5 [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
10devnull = open(os.devnull, 'w')
11
12# launch some tail processes to pump the std* streams
13def launch_tail(filename, outstream, start):
14 path = os.path.join(logdir, filename)
15 argv = ['tail', '--retry', '--follow=name', '--bytes=+%d' % start, path]
16 # stdout=sys.stdout fails on pre-2.5 python (bug in subprocess module)
17 if outstream != subprocess.PIPE and outstream.fileno() == 1:
18 return subprocess.Popen(argv, stderr=devnull)
19 else:
20 return subprocess.Popen(argv, stdout=outstream, stderr=devnull)
21stdout_pump = launch_tail('stdout', sys.stdout, stdout_start)
22stderr_pump = launch_tail('stderr', sys.stderr, stderr_start)
23
24# watch the exit code file for an exit
25exit_code_file = open(os.path.join(logdir, 'exit_code'))
26fcntl.flock(exit_code_file, fcntl.LOCK_EX)
27try:
28 exit_code = exit_code_file.read()
29 if len(exit_code) != 4:
30 exit_code = -signal.SIGKILL # autotestd was nuked
31 else:
32 exit_code = int(exit_code)
33finally:
34 fcntl.flock(exit_code_file, fcntl.LOCK_UN)
35 exit_code_file.close()
36
37time.sleep(2) # tail runs in 1s polling loop, so give them a chance to finish
38os.kill(stdout_pump.pid, signal.SIGTERM)
39os.kill(stderr_pump.pid, signal.SIGTERM)
40
41# exit (with the same code as autotestd)
42sys.exit(exit_code)