Stephen Warren | d201506 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 1 | # 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 | |
| 7 | import os |
| 8 | import re |
| 9 | import pty |
| 10 | import signal |
| 11 | import select |
| 12 | import time |
| 13 | |
| 14 | class Timeout(Exception): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 15 | """An exception sub-class that indicates that a timeout occurred.""" |
Stephen Warren | d201506 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 16 | pass |
| 17 | |
| 18 | class Spawn(object): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 19 | """Represents the stdio of a freshly created sub-process. Commands may be |
Stephen Warren | d201506 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 20 | sent to the process, and responses waited for. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 21 | """ |
Stephen Warren | d201506 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 22 | |
Stephen Warren | d27f2fc | 2016-01-27 23:57:53 -0700 | [diff] [blame] | 23 | def __init__(self, args, cwd=None): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 24 | """Spawn (fork/exec) the sub-process. |
Stephen Warren | d201506 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 25 | |
| 26 | Args: |
Stephen Warren | d27f2fc | 2016-01-27 23:57:53 -0700 | [diff] [blame] | 27 | 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 Warren | d201506 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 30 | |
| 31 | Returns: |
| 32 | Nothing. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 33 | """ |
Stephen Warren | d201506 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 34 | |
| 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 Warren | d27f2fc | 2016-01-27 23:57:53 -0700 | [diff] [blame] | 49 | if cwd: |
| 50 | os.chdir(cwd) |
Stephen Warren | d201506 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 51 | 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 Warren | 93134e1 | 2016-02-10 16:54:37 -0700 | [diff] [blame] | 59 | 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 Warren | d201506 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 65 | |
| 66 | def kill(self, sig): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 67 | """Send unix signal "sig" to the child process. |
Stephen Warren | d201506 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 68 | |
| 69 | Args: |
| 70 | sig: The signal number to send. |
| 71 | |
| 72 | Returns: |
| 73 | Nothing. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 74 | """ |
Stephen Warren | d201506 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 75 | |
| 76 | os.kill(self.pid, sig) |
| 77 | |
| 78 | def isalive(self): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 79 | """Determine whether the child process is still running. |
Stephen Warren | d201506 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 80 | |
| 81 | Args: |
| 82 | None. |
| 83 | |
| 84 | Returns: |
| 85 | Boolean indicating whether process is alive. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 86 | """ |
Stephen Warren | d201506 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 87 | |
| 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 Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 99 | """Send data to the sub-process's stdin. |
Stephen Warren | d201506 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 100 | |
| 101 | Args: |
| 102 | data: The data to send to the process. |
| 103 | |
| 104 | Returns: |
| 105 | Nothing. |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 106 | """ |
Stephen Warren | d201506 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 107 | |
| 108 | os.write(self.fd, data) |
| 109 | |
| 110 | def expect(self, patterns): |
Stephen Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 111 | """Wait for the sub-process to emit specific data. |
Stephen Warren | d201506 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 112 | |
| 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 Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 127 | """ |
Stephen Warren | d201506 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 128 | |
| 129 | for pi in xrange(len(patterns)): |
| 130 | if type(patterns[pi]) == type(''): |
| 131 | patterns[pi] = re.compile(patterns[pi]) |
| 132 | |
Stephen Warren | d314e24 | 2016-01-22 12:30:07 -0700 | [diff] [blame] | 133 | tstart_s = time.time() |
Stephen Warren | d201506 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 134 | 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 Warren | 44ac762 | 2016-01-27 23:57:47 -0700 | [diff] [blame] | 143 | if earliest_m and m.start() >= earliest_m.start(): |
Stephen Warren | d201506 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 144 | continue |
| 145 | earliest_m = m |
| 146 | earliest_pi = pi |
| 147 | if earliest_m: |
| 148 | pos = earliest_m.start() |
Stephen Warren | d892681 | 2016-02-05 18:04:42 -0700 | [diff] [blame] | 149 | posafter = earliest_m.end() |
Stephen Warren | d201506 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 150 | self.before = self.buf[:pos] |
| 151 | self.after = self.buf[pos:posafter] |
| 152 | self.buf = self.buf[posafter:] |
| 153 | return earliest_pi |
Stephen Warren | d314e24 | 2016-01-22 12:30:07 -0700 | [diff] [blame] | 154 | tnow_s = time.time() |
Stephen Warren | 89ab841 | 2016-02-04 16:11:50 -0700 | [diff] [blame] | 155 | 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 Warren | d201506 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 163 | 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 Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 176 | """Close the stdio connection to the sub-process. |
Stephen Warren | d201506 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 177 | |
| 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 Warren | e8debf3 | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 185 | """ |
Stephen Warren | d201506 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 186 | |
| 187 | os.close(self.fd) |
| 188 | for i in xrange(100): |
| 189 | if not self.isalive(): |
| 190 | break |
| 191 | time.sleep(0.1) |