jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import common |
| 4 | import sys, os, signal, time, subprocess, fcntl |
| 5 | |
| 6 | logdir = sys.argv[1] |
| 7 | stdout_start = int(sys.argv[2]) # number of bytes we can skip on stdout |
| 8 | stderr_start = int(sys.argv[3]) # nubmer of bytes we can skip on stderr |
| 9 | |
jadmanski | c641fd7 | 2009-04-03 21:50:11 +0000 | [diff] [blame] | 10 | # if any of our tail processes die, the monitor should die too |
| 11 | def kill_self(signum, frame): |
| 12 | os.kill(os.getpid(), signal.SIGTERM) |
| 13 | signal.signal(signal.SIGCHLD, kill_self) |
| 14 | |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 15 | devnull = open(os.devnull, 'w') |
| 16 | |
| 17 | # launch some tail processes to pump the std* streams |
| 18 | def 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) |
| 26 | stdout_pump = launch_tail('stdout', sys.stdout, stdout_start) |
| 27 | stderr_pump = launch_tail('stderr', sys.stderr, stderr_start) |
| 28 | |
jadmanski | f3fbce8 | 2009-02-18 18:54:33 +0000 | [diff] [blame] | 29 | # wait for logdir/started to exist to be sure autotestd is started |
| 30 | start_time = time.time() |
| 31 | started_file_path = os.path.join(logdir, 'started') |
| 32 | while 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) |
jadmanski | f3fbce8 | 2009-02-18 18:54:33 +0000 | [diff] [blame] | 36 | |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 37 | # watch the exit code file for an exit |
| 38 | exit_code_file = open(os.path.join(logdir, 'exit_code')) |
| 39 | fcntl.flock(exit_code_file, fcntl.LOCK_EX) |
| 40 | try: |
| 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) |
| 46 | finally: |
| 47 | fcntl.flock(exit_code_file, fcntl.LOCK_UN) |
| 48 | exit_code_file.close() |
| 49 | |
jadmanski | 263313e | 2009-07-01 16:26:03 +0000 | [diff] [blame] | 50 | # tail runs in 1s polling loop, so give them a chance to finish |
| 51 | time.sleep(2) |
| 52 | # clear the SIGCHLD handler so that killing the tails doesn't kill us |
| 53 | signal.signal(signal.SIGCHLD, signal.SIG_DFL) |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 54 | os.kill(stdout_pump.pid, signal.SIGTERM) |
| 55 | os.kill(stderr_pump.pid, signal.SIGTERM) |
| 56 | |
| 57 | # exit (with the same code as autotestd) |
| 58 | sys.exit(exit_code) |