1*9c5db199SXin Li#!/usr/bin/python3 2*9c5db199SXin Li 3*9c5db199SXin Liimport common 4*9c5db199SXin Liimport sys, os, subprocess, fcntl 5*9c5db199SXin Li 6*9c5db199SXin Li 7*9c5db199SXin Libindir = os.path.dirname(__file__) 8*9c5db199SXin Liautotest = os.path.join(bindir, 'autotest') 9*9c5db199SXin Li 10*9c5db199SXin Lilogdir = sys.argv[1] 11*9c5db199SXin Li 12*9c5db199SXin Li 13*9c5db199SXin Li# We want to simulate the behaviour of autotest_client, where fd3 would be 14*9c5db199SXin Li# routed to stderr and fd1 & fd2 to stdout 15*9c5db199SXin Li 16*9c5db199SXin Li# HACK: grab fd3 for now 17*9c5db199SXin Lios.dup2(2, 3) 18*9c5db199SXin Li 19*9c5db199SXin Li# open up log files to use for std* 20*9c5db199SXin Listdout = open(os.path.join(logdir, 'stdout'), 'a', buffering=2) 21*9c5db199SXin Listderr = open(os.path.join(logdir, 'stderr'), 'a', buffering=2) 22*9c5db199SXin Li 23*9c5db199SXin Li# set up the file descriptors now, simulating the old behaviour 24*9c5db199SXin Lios.dup2(stdout.fileno(), 1) 25*9c5db199SXin Lios.dup2(stdout.fileno(), 2) 26*9c5db199SXin Lios.dup2(stderr.fileno(), 3) 27*9c5db199SXin Li 28*9c5db199SXin Li# we don't need the file objects any more 29*9c5db199SXin Listdout.close() 30*9c5db199SXin Listderr.close() 31*9c5db199SXin Li 32*9c5db199SXin Li 33*9c5db199SXin Liargs = [autotest] + sys.argv[2:] 34*9c5db199SXin Liif '-H' not in args: 35*9c5db199SXin Li args[1:1] = ['-H', 'autoserv'] 36*9c5db199SXin Licmd = ' '.join(args) 37*9c5db199SXin Li 38*9c5db199SXin Li# open up a log file for saving off the exit code 39*9c5db199SXin Liexit_file = open(os.path.join(logdir, 'exit_code'), 'wb', buffering=0) 40*9c5db199SXin Lifcntl.flock(exit_file, fcntl.LOCK_EX) 41*9c5db199SXin Li 42*9c5db199SXin Li# touch a 'started' file to indicate we've been initialized 43*9c5db199SXin Liopen(os.path.join(logdir, 'started'), 'w').close() 44*9c5db199SXin Li 45*9c5db199SXin Li# run the actual autotest client and write the exit code into the log file 46*9c5db199SXin Li# close_fds must be False to support python 2 and 3. In 3 the default changes 47*9c5db199SXin Li# to True, and will break fd writing used elsewhere (e.g. harness_autoserv) 48*9c5db199SXin Liexit_code = subprocess.call("{} {}".format(sys.executable, cmd), 49*9c5db199SXin Li shell=True, 50*9c5db199SXin Li close_fds=False) 51*9c5db199SXin Liexit_file.write(b'%+04d' % exit_code) 52*9c5db199SXin Liexit_file.flush() 53*9c5db199SXin Lifcntl.flock(exit_file, fcntl.LOCK_UN) 54*9c5db199SXin Liexit_file.close() 55