blob: a5f4a8e91baed1aa8c9f3dce7c0072fc28ca13da [file] [log] [blame]
Stephen Warrend2015062016-01-15 11:15:24 -07001# Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
2#
3# SPDX-License-Identifier: GPL-2.0
4
5# Logic to spawn a sub-process and interact with its stdio.
6
7import os
8import re
9import pty
10import signal
11import select
12import time
13
14class Timeout(Exception):
Stephen Warrene8debf32016-01-26 13:41:30 -070015 """An exception sub-class that indicates that a timeout occurred."""
Stephen Warrend2015062016-01-15 11:15:24 -070016 pass
17
18class Spawn(object):
Stephen Warrene8debf32016-01-26 13:41:30 -070019 """Represents the stdio of a freshly created sub-process. Commands may be
Stephen Warrend2015062016-01-15 11:15:24 -070020 sent to the process, and responses waited for.
Stephen Warrene8debf32016-01-26 13:41:30 -070021 """
Stephen Warrend2015062016-01-15 11:15:24 -070022
Stephen Warrend27f2fc2016-01-27 23:57:53 -070023 def __init__(self, args, cwd=None):
Stephen Warrene8debf32016-01-26 13:41:30 -070024 """Spawn (fork/exec) the sub-process.
Stephen Warrend2015062016-01-15 11:15:24 -070025
26 Args:
Stephen Warrend27f2fc2016-01-27 23:57:53 -070027 args: array of processs arguments. argv[0] is the command to
28 execute.
29 cwd: the directory to run the process in, or None for no change.
Stephen Warrend2015062016-01-15 11:15:24 -070030
31 Returns:
32 Nothing.
Stephen Warrene8debf32016-01-26 13:41:30 -070033 """
Stephen Warrend2015062016-01-15 11:15:24 -070034
35 self.waited = False
36 self.buf = ''
37 self.logfile_read = None
38 self.before = ''
39 self.after = ''
40 self.timeout = None
41
42 (self.pid, self.fd) = pty.fork()
43 if self.pid == 0:
44 try:
45 # For some reason, SIGHUP is set to SIG_IGN at this point when
46 # run under "go" (www.go.cd). Perhaps this happens under any
47 # background (non-interactive) system?
48 signal.signal(signal.SIGHUP, signal.SIG_DFL)
Stephen Warrend27f2fc2016-01-27 23:57:53 -070049 if cwd:
50 os.chdir(cwd)
Stephen Warrend2015062016-01-15 11:15:24 -070051 os.execvp(args[0], args)
52 except:
53 print 'CHILD EXECEPTION:'
54 import traceback
55 traceback.print_exc()
56 finally:
57 os._exit(255)
58
Stephen Warren93134e12016-02-10 16:54:37 -070059 try:
60 self.poll = select.poll()
61 self.poll.register(self.fd, select.POLLIN | select.POLLPRI | select.POLLERR | select.POLLHUP | select.POLLNVAL)
62 except:
63 self.close()
64 raise
Stephen Warrend2015062016-01-15 11:15:24 -070065
66 def kill(self, sig):
Stephen Warrene8debf32016-01-26 13:41:30 -070067 """Send unix signal "sig" to the child process.
Stephen Warrend2015062016-01-15 11:15:24 -070068
69 Args:
70 sig: The signal number to send.
71
72 Returns:
73 Nothing.
Stephen Warrene8debf32016-01-26 13:41:30 -070074 """
Stephen Warrend2015062016-01-15 11:15:24 -070075
76 os.kill(self.pid, sig)
77
78 def isalive(self):
Stephen Warrene8debf32016-01-26 13:41:30 -070079 """Determine whether the child process is still running.
Stephen Warrend2015062016-01-15 11:15:24 -070080
81 Args:
82 None.
83
84 Returns:
85 Boolean indicating whether process is alive.
Stephen Warrene8debf32016-01-26 13:41:30 -070086 """
Stephen Warrend2015062016-01-15 11:15:24 -070087
88 if self.waited:
89 return False
90
91 w = os.waitpid(self.pid, os.WNOHANG)
92 if w[0] == 0:
93 return True
94
95 self.waited = True
96 return False
97
98 def send(self, data):
Stephen Warrene8debf32016-01-26 13:41:30 -070099 """Send data to the sub-process's stdin.
Stephen Warrend2015062016-01-15 11:15:24 -0700100
101 Args:
102 data: The data to send to the process.
103
104 Returns:
105 Nothing.
Stephen Warrene8debf32016-01-26 13:41:30 -0700106 """
Stephen Warrend2015062016-01-15 11:15:24 -0700107
108 os.write(self.fd, data)
109
110 def expect(self, patterns):
Stephen Warrene8debf32016-01-26 13:41:30 -0700111 """Wait for the sub-process to emit specific data.
Stephen Warrend2015062016-01-15 11:15:24 -0700112
113 This function waits for the process to emit one pattern from the
114 supplied list of patterns, or for a timeout to occur.
115
116 Args:
117 patterns: A list of strings or regex objects that we expect to
118 see in the sub-process' stdout.
119
120 Returns:
121 The index within the patterns array of the pattern the process
122 emitted.
123
124 Notable exceptions:
125 Timeout, if the process did not emit any of the patterns within
126 the expected time.
Stephen Warrene8debf32016-01-26 13:41:30 -0700127 """
Stephen Warrend2015062016-01-15 11:15:24 -0700128
129 for pi in xrange(len(patterns)):
130 if type(patterns[pi]) == type(''):
131 patterns[pi] = re.compile(patterns[pi])
132
Stephen Warrend314e242016-01-22 12:30:07 -0700133 tstart_s = time.time()
Stephen Warrend2015062016-01-15 11:15:24 -0700134 try:
135 while True:
136 earliest_m = None
137 earliest_pi = None
138 for pi in xrange(len(patterns)):
139 pattern = patterns[pi]
140 m = pattern.search(self.buf)
141 if not m:
142 continue
Stephen Warren44ac7622016-01-27 23:57:47 -0700143 if earliest_m and m.start() >= earliest_m.start():
Stephen Warrend2015062016-01-15 11:15:24 -0700144 continue
145 earliest_m = m
146 earliest_pi = pi
147 if earliest_m:
148 pos = earliest_m.start()
Stephen Warrend8926812016-02-05 18:04:42 -0700149 posafter = earliest_m.end()
Stephen Warrend2015062016-01-15 11:15:24 -0700150 self.before = self.buf[:pos]
151 self.after = self.buf[pos:posafter]
152 self.buf = self.buf[posafter:]
153 return earliest_pi
Stephen Warrend314e242016-01-22 12:30:07 -0700154 tnow_s = time.time()
Stephen Warren89ab8412016-02-04 16:11:50 -0700155 if self.timeout:
156 tdelta_ms = (tnow_s - tstart_s) * 1000
157 poll_maxwait = self.timeout - tdelta_ms
158 if tdelta_ms > self.timeout:
159 raise Timeout()
160 else:
161 poll_maxwait = None
162 events = self.poll.poll(poll_maxwait)
Stephen Warrend2015062016-01-15 11:15:24 -0700163 if not events:
164 raise Timeout()
165 c = os.read(self.fd, 1024)
166 if not c:
167 raise EOFError()
168 if self.logfile_read:
169 self.logfile_read.write(c)
170 self.buf += c
171 finally:
172 if self.logfile_read:
173 self.logfile_read.flush()
174
175 def close(self):
Stephen Warrene8debf32016-01-26 13:41:30 -0700176 """Close the stdio connection to the sub-process.
Stephen Warrend2015062016-01-15 11:15:24 -0700177
178 This also waits a reasonable time for the sub-process to stop running.
179
180 Args:
181 None.
182
183 Returns:
184 Nothing.
Stephen Warrene8debf32016-01-26 13:41:30 -0700185 """
Stephen Warrend2015062016-01-15 11:15:24 -0700186
187 os.close(self.fd)
188 for i in xrange(100):
189 if not self.isalive():
190 break
191 time.sleep(0.1)