Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0 |
Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 2 | # Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 3 | |
Heinrich Schuchardt | 725bd31 | 2021-11-23 00:01:46 +0100 | [diff] [blame] | 4 | """ |
| 5 | Logic to spawn a sub-process and interact with its stdio. |
| 6 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 7 | |
| 8 | import os |
| 9 | import re |
| 10 | import pty |
| 11 | import signal |
| 12 | import select |
| 13 | import time |
Heinrich Schuchardt | 725bd31 | 2021-11-23 00:01:46 +0100 | [diff] [blame] | 14 | import traceback |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 15 | |
| 16 | class Timeout(Exception): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 17 | """An exception sub-class that indicates that a timeout occurred.""" |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 18 | |
Heinrich Schuchardt | 725bd31 | 2021-11-23 00:01:46 +0100 | [diff] [blame] | 19 | class Spawn: |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 20 | """Represents the stdio of a freshly created sub-process. Commands may be |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 21 | sent to the process, and responses waited for. |
Simon Glass | 9bc2083 | 2016-07-04 11:58:39 -0600 | [diff] [blame] | 22 | |
| 23 | Members: |
| 24 | output: accumulated output from expect() |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 25 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 26 | |
Simon Glass | 3f0bd0c | 2024-06-23 14:30:30 -0600 | [diff] [blame] | 27 | def __init__(self, args, cwd=None, decode_signal=False): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 28 | """Spawn (fork/exec) the sub-process. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 29 | |
| 30 | Args: |
Stephen Warren | a85fce9 | 2016-01-27 23:57:53 -0700 | [diff] [blame] | 31 | args: array of processs arguments. argv[0] is the command to |
| 32 | execute. |
| 33 | cwd: the directory to run the process in, or None for no change. |
Simon Glass | 3f0bd0c | 2024-06-23 14:30:30 -0600 | [diff] [blame] | 34 | decode_signal (bool): True to indicate the exception number when |
| 35 | something goes wrong |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 36 | |
| 37 | Returns: |
| 38 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 39 | """ |
Simon Glass | 3f0bd0c | 2024-06-23 14:30:30 -0600 | [diff] [blame] | 40 | self.decode_signal = decode_signal |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 41 | self.waited = False |
Simon Glass | afd0004 | 2021-10-08 09:15:23 -0600 | [diff] [blame] | 42 | self.exit_code = 0 |
| 43 | self.exit_info = '' |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 44 | self.buf = '' |
Simon Glass | 9bc2083 | 2016-07-04 11:58:39 -0600 | [diff] [blame] | 45 | self.output = '' |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 46 | self.logfile_read = None |
| 47 | self.before = '' |
| 48 | self.after = '' |
| 49 | self.timeout = None |
Stephen Warren | 8e7c3ec | 2016-07-06 10:34:30 -0600 | [diff] [blame] | 50 | # http://stackoverflow.com/questions/7857352/python-regex-to-match-vt100-escape-sequences |
Tom Rini | 649bdaf | 2019-10-24 11:59:28 -0400 | [diff] [blame] | 51 | self.re_vt100 = re.compile(r'(\x1b\[|\x9b)[^@-_]*[@-_]|\x1b[@-_]', re.I) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 52 | |
| 53 | (self.pid, self.fd) = pty.fork() |
| 54 | if self.pid == 0: |
| 55 | try: |
| 56 | # For some reason, SIGHUP is set to SIG_IGN at this point when |
| 57 | # run under "go" (www.go.cd). Perhaps this happens under any |
| 58 | # background (non-interactive) system? |
| 59 | signal.signal(signal.SIGHUP, signal.SIG_DFL) |
Stephen Warren | a85fce9 | 2016-01-27 23:57:53 -0700 | [diff] [blame] | 60 | if cwd: |
| 61 | os.chdir(cwd) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 62 | os.execvp(args[0], args) |
| 63 | except: |
Paul Burton | 00f2d20 | 2017-09-14 14:34:43 -0700 | [diff] [blame] | 64 | print('CHILD EXECEPTION:') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 65 | traceback.print_exc() |
| 66 | finally: |
| 67 | os._exit(255) |
| 68 | |
Stephen Warren | 65503db | 2016-02-10 16:54:37 -0700 | [diff] [blame] | 69 | try: |
| 70 | self.poll = select.poll() |
Heinrich Schuchardt | 725bd31 | 2021-11-23 00:01:46 +0100 | [diff] [blame] | 71 | self.poll.register(self.fd, select.POLLIN | select.POLLPRI | select.POLLERR | |
| 72 | select.POLLHUP | select.POLLNVAL) |
Stephen Warren | 65503db | 2016-02-10 16:54:37 -0700 | [diff] [blame] | 73 | except: |
| 74 | self.close() |
| 75 | raise |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 76 | |
| 77 | def kill(self, sig): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 78 | """Send unix signal "sig" to the child process. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 79 | |
| 80 | Args: |
| 81 | sig: The signal number to send. |
| 82 | |
| 83 | Returns: |
| 84 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 85 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 86 | |
| 87 | os.kill(self.pid, sig) |
| 88 | |
Simon Glass | afd0004 | 2021-10-08 09:15:23 -0600 | [diff] [blame] | 89 | def checkalive(self): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 90 | """Determine whether the child process is still running. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 91 | |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 92 | Returns: |
Simon Glass | afd0004 | 2021-10-08 09:15:23 -0600 | [diff] [blame] | 93 | tuple: |
| 94 | True if process is alive, else False |
| 95 | 0 if process is alive, else exit code of process |
| 96 | string describing what happened ('' or 'status/signal n') |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 97 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 98 | |
| 99 | if self.waited: |
Simon Glass | afd0004 | 2021-10-08 09:15:23 -0600 | [diff] [blame] | 100 | return False, self.exit_code, self.exit_info |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 101 | |
| 102 | w = os.waitpid(self.pid, os.WNOHANG) |
| 103 | if w[0] == 0: |
Simon Glass | afd0004 | 2021-10-08 09:15:23 -0600 | [diff] [blame] | 104 | return True, 0, 'running' |
| 105 | status = w[1] |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 106 | |
Simon Glass | afd0004 | 2021-10-08 09:15:23 -0600 | [diff] [blame] | 107 | if os.WIFEXITED(status): |
| 108 | self.exit_code = os.WEXITSTATUS(status) |
| 109 | self.exit_info = 'status %d' % self.exit_code |
| 110 | elif os.WIFSIGNALED(status): |
| 111 | signum = os.WTERMSIG(status) |
| 112 | self.exit_code = -signum |
Heinrich Schuchardt | 725bd31 | 2021-11-23 00:01:46 +0100 | [diff] [blame] | 113 | self.exit_info = 'signal %d (%s)' % (signum, signal.Signals(signum).name) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 114 | self.waited = True |
Simon Glass | afd0004 | 2021-10-08 09:15:23 -0600 | [diff] [blame] | 115 | return False, self.exit_code, self.exit_info |
| 116 | |
| 117 | def isalive(self): |
| 118 | """Determine whether the child process is still running. |
| 119 | |
| 120 | Args: |
| 121 | None. |
| 122 | |
| 123 | Returns: |
| 124 | Boolean indicating whether process is alive. |
| 125 | """ |
| 126 | return self.checkalive()[0] |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 127 | |
| 128 | def send(self, data): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 129 | """Send data to the sub-process's stdin. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 130 | |
| 131 | Args: |
| 132 | data: The data to send to the process. |
| 133 | |
| 134 | Returns: |
| 135 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 136 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 137 | |
Tom Rini | 6a99041 | 2019-10-24 11:59:21 -0400 | [diff] [blame] | 138 | os.write(self.fd, data.encode(errors='replace')) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 139 | |
| 140 | def expect(self, patterns): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 141 | """Wait for the sub-process to emit specific data. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 142 | |
| 143 | This function waits for the process to emit one pattern from the |
| 144 | supplied list of patterns, or for a timeout to occur. |
| 145 | |
| 146 | Args: |
| 147 | patterns: A list of strings or regex objects that we expect to |
| 148 | see in the sub-process' stdout. |
| 149 | |
| 150 | Returns: |
| 151 | The index within the patterns array of the pattern the process |
| 152 | emitted. |
| 153 | |
| 154 | Notable exceptions: |
| 155 | Timeout, if the process did not emit any of the patterns within |
| 156 | the expected time. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 157 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 158 | |
Paul Burton | d2849ed | 2017-09-14 14:34:44 -0700 | [diff] [blame] | 159 | for pi in range(len(patterns)): |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 160 | if type(patterns[pi]) == type(''): |
| 161 | patterns[pi] = re.compile(patterns[pi]) |
| 162 | |
Stephen Warren | 22eba12 | 2016-01-22 12:30:07 -0700 | [diff] [blame] | 163 | tstart_s = time.time() |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 164 | try: |
| 165 | while True: |
| 166 | earliest_m = None |
| 167 | earliest_pi = None |
Paul Burton | d2849ed | 2017-09-14 14:34:44 -0700 | [diff] [blame] | 168 | for pi in range(len(patterns)): |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 169 | pattern = patterns[pi] |
| 170 | m = pattern.search(self.buf) |
| 171 | if not m: |
| 172 | continue |
Stephen Warren | 3880dec | 2016-01-27 23:57:47 -0700 | [diff] [blame] | 173 | if earliest_m and m.start() >= earliest_m.start(): |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 174 | continue |
| 175 | earliest_m = m |
| 176 | earliest_pi = pi |
| 177 | if earliest_m: |
| 178 | pos = earliest_m.start() |
Stephen Warren | 4223a2f | 2016-02-05 18:04:42 -0700 | [diff] [blame] | 179 | posafter = earliest_m.end() |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 180 | self.before = self.buf[:pos] |
| 181 | self.after = self.buf[pos:posafter] |
Simon Glass | 9bc2083 | 2016-07-04 11:58:39 -0600 | [diff] [blame] | 182 | self.output += self.buf[:posafter] |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 183 | self.buf = self.buf[posafter:] |
| 184 | return earliest_pi |
Stephen Warren | 22eba12 | 2016-01-22 12:30:07 -0700 | [diff] [blame] | 185 | tnow_s = time.time() |
Stephen Warren | 33db1ee | 2016-02-04 16:11:50 -0700 | [diff] [blame] | 186 | if self.timeout: |
| 187 | tdelta_ms = (tnow_s - tstart_s) * 1000 |
| 188 | poll_maxwait = self.timeout - tdelta_ms |
| 189 | if tdelta_ms > self.timeout: |
| 190 | raise Timeout() |
| 191 | else: |
| 192 | poll_maxwait = None |
| 193 | events = self.poll.poll(poll_maxwait) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 194 | if not events: |
| 195 | raise Timeout() |
Simon Glass | afd0004 | 2021-10-08 09:15:23 -0600 | [diff] [blame] | 196 | try: |
| 197 | c = os.read(self.fd, 1024).decode(errors='replace') |
| 198 | except OSError as err: |
| 199 | # With sandbox, try to detect when U-Boot exits when it |
| 200 | # shouldn't and explain why. This is much more friendly than |
| 201 | # just dying with an I/O error |
Simon Glass | 3f0bd0c | 2024-06-23 14:30:30 -0600 | [diff] [blame] | 202 | if self.decode_signal and err.errno == 5: # I/O error |
Heinrich Schuchardt | 725bd31 | 2021-11-23 00:01:46 +0100 | [diff] [blame] | 203 | alive, _, info = self.checkalive() |
Simon Glass | afd0004 | 2021-10-08 09:15:23 -0600 | [diff] [blame] | 204 | if alive: |
Heinrich Schuchardt | 725bd31 | 2021-11-23 00:01:46 +0100 | [diff] [blame] | 205 | raise err |
| 206 | raise ValueError('U-Boot exited with %s' % info) |
Simon Glass | 3f0bd0c | 2024-06-23 14:30:30 -0600 | [diff] [blame] | 207 | raise |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 208 | if self.logfile_read: |
| 209 | self.logfile_read.write(c) |
| 210 | self.buf += c |
Stephen Warren | 8e7c3ec | 2016-07-06 10:34:30 -0600 | [diff] [blame] | 211 | # count=0 is supposed to be the default, which indicates |
| 212 | # unlimited substitutions, but in practice the version of |
| 213 | # Python in Ubuntu 14.04 appears to default to count=2! |
| 214 | self.buf = self.re_vt100.sub('', self.buf, count=1000000) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 215 | finally: |
| 216 | if self.logfile_read: |
| 217 | self.logfile_read.flush() |
| 218 | |
| 219 | def close(self): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 220 | """Close the stdio connection to the sub-process. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 221 | |
| 222 | This also waits a reasonable time for the sub-process to stop running. |
| 223 | |
| 224 | Args: |
| 225 | None. |
| 226 | |
| 227 | Returns: |
| 228 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 229 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 230 | |
| 231 | os.close(self.fd) |
Heinrich Schuchardt | 725bd31 | 2021-11-23 00:01:46 +0100 | [diff] [blame] | 232 | for _ in range(100): |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 233 | if not self.isalive(): |
| 234 | break |
| 235 | time.sleep(0.1) |
Simon Glass | 9bc2083 | 2016-07-04 11:58:39 -0600 | [diff] [blame] | 236 | |
| 237 | def get_expect_output(self): |
| 238 | """Return the output read by expect() |
| 239 | |
| 240 | Returns: |
| 241 | The output processed by expect(), as a string. |
| 242 | """ |
| 243 | return self.output |