Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 1 | # osmo_gsm_tester: process management |
| 2 | # |
| 3 | # Copyright (C) 2016-2017 by sysmocom - s.f.m.c. GmbH |
| 4 | # |
| 5 | # Author: Neels Hofmeyr <neels@hofmeyr.de> |
| 6 | # |
| 7 | # This program is free software: you can redistribute it and/or modify |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 8 | # it under the terms of the GNU General Public License as |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 9 | # published by the Free Software Foundation, either version 3 of the |
| 10 | # License, or (at your option) any later version. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 15 | # GNU General Public License for more details. |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 16 | # |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 17 | # You should have received a copy of the GNU General Public License |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 20 | import os |
| 21 | import time |
| 22 | import subprocess |
| 23 | import signal |
Holger Hans Peter Freyther | 20b52c1 | 2019-02-27 02:31:50 +0000 | [diff] [blame] | 24 | from abc import ABCMeta, abstractmethod |
Pau Espin Pedrol | 0d8deec | 2017-06-23 11:43:38 +0200 | [diff] [blame] | 25 | from datetime import datetime |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 26 | |
Pau Espin Pedrol | 9a4631c | 2018-03-28 19:17:34 +0200 | [diff] [blame] | 27 | from . import log |
| 28 | from .event_loop import MainLoop |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 29 | from .util import Dir |
| 30 | |
Holger Hans Peter Freyther | 20b52c1 | 2019-02-27 02:31:50 +0000 | [diff] [blame] | 31 | class TerminationStrategy(log.Origin, metaclass=ABCMeta): |
| 32 | """A baseclass for terminating a collection of processes.""" |
| 33 | |
| 34 | def __init__(self): |
| 35 | self._processes = [] |
| 36 | |
| 37 | def add_process(self, process): |
| 38 | """Remembers a process that needs to be terminated.""" |
| 39 | self._processes.append(process) |
| 40 | |
| 41 | @abstractmethod |
| 42 | def terminate_all(self): |
| 43 | "Terminates all scheduled processes and waits for the termination.""" |
| 44 | pass |
| 45 | |
| 46 | |
| 47 | class ParallelTerminationStrategy(TerminationStrategy): |
| 48 | """Processes will be terminated in parallel.""" |
| 49 | |
Holger Hans Peter Freyther | 54b4fa9 | 2019-02-27 13:00:33 +0000 | [diff] [blame] | 50 | def _prune_dead_processes(self, poll_first): |
| 51 | """Removes all dead processes from the list.""" |
| 52 | # Remove all processes that terminated! |
| 53 | self._processes = list(filter(lambda proc: proc.is_running(poll_first), self._processes)) |
| 54 | |
| 55 | def _build_process_map(self): |
| 56 | """Builds a mapping from pid to process.""" |
| 57 | self._process_map = {} |
Holger Hans Peter Freyther | 20b52c1 | 2019-02-27 02:31:50 +0000 | [diff] [blame] | 58 | for process in self._processes: |
Holger Hans Peter Freyther | 54b4fa9 | 2019-02-27 13:00:33 +0000 | [diff] [blame] | 59 | pid = process.pid() |
| 60 | if pid is None: |
| 61 | continue |
| 62 | self._process_map[pid] = process |
| 63 | |
| 64 | def _poll_once(self): |
| 65 | """Polls for to be collected children once.""" |
| 66 | pid, result = os.waitpid(0, os.WNOHANG) |
| 67 | # Did some other process die? |
| 68 | if pid == 0: |
| 69 | return False |
| 70 | proc = self._process_map.get(pid) |
| 71 | if proc is None: |
| 72 | self.dbg("Unknown process with pid(%d) died." % pid) |
| 73 | return False |
| 74 | # Update the process state and forget about it |
| 75 | self.log("PID %d died..." % pid) |
| 76 | proc.result = result |
| 77 | proc.cleanup() |
| 78 | self._processes.remove(proc) |
| 79 | del self._process_map[pid] |
| 80 | return True |
| 81 | |
| 82 | def _poll_for_termination(self, time_to_wait_for_term=5): |
| 83 | """Waits for the termination of processes until timeout|all ended.""" |
| 84 | |
| 85 | wait_step = 0.001 |
| 86 | waited_time = 0 |
| 87 | while len(self._processes) > 0: |
| 88 | # Collect processes until there are none to be collected. |
| 89 | while True: |
| 90 | try: |
| 91 | if not self._poll_once(): |
| 92 | break |
| 93 | except ChildProcessError: |
| 94 | break |
| 95 | |
| 96 | # All processes died and we can return before sleeping |
| 97 | if len(self._processes) == 0: |
| 98 | break |
| 99 | waited_time += wait_step |
| 100 | # make wait_step approach 1.0 |
| 101 | wait_step = (1. + 5. * wait_step) / 6. |
| 102 | if waited_time >= time_to_wait_for_term: |
| 103 | break |
| 104 | time.sleep(wait_step) |
| 105 | |
| 106 | def terminate_all(self): |
Pau Espin Pedrol | 0409655 | 2019-04-04 16:43:12 +0200 | [diff] [blame] | 107 | num_processes = len(self._processes) |
| 108 | self.dbg("Scheduled to terminate %d processes." % num_processes) |
| 109 | if num_processes == 0: |
| 110 | return |
Holger Hans Peter Freyther | 54b4fa9 | 2019-02-27 13:00:33 +0000 | [diff] [blame] | 111 | self._prune_dead_processes(True) |
| 112 | self._build_process_map() |
| 113 | |
| 114 | # Iterate through all signals. |
| 115 | for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGKILL]: |
| 116 | self.dbg("Starting to kill with %s" % sig.name) |
| 117 | for process in self._processes: |
| 118 | process.kill(sig) |
| 119 | if sig == signal.SIGKILL: |
| 120 | continue |
| 121 | self._poll_for_termination() |
Pau Espin Pedrol | 0409655 | 2019-04-04 16:43:12 +0200 | [diff] [blame] | 122 | if len(self._processes) == 0: |
| 123 | return |
Holger Hans Peter Freyther | 20b52c1 | 2019-02-27 02:31:50 +0000 | [diff] [blame] | 124 | |
| 125 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 126 | class Process(log.Origin): |
| 127 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 128 | def __init__(self, name, run_dir, popen_args, **popen_kwargs): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 129 | super().__init__(log.C_RUN, name) |
Pau Espin Pedrol | 5860367 | 2018-08-09 13:45:55 +0200 | [diff] [blame] | 130 | self.process_obj = None |
| 131 | self.result = None |
| 132 | self.killed = None |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 133 | self.name_str = name |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 134 | self.run_dir = run_dir |
| 135 | self.popen_args = popen_args |
| 136 | self.popen_kwargs = popen_kwargs |
| 137 | self.outputs = {} |
| 138 | if not isinstance(self.run_dir, Dir): |
| 139 | self.run_dir = Dir(os.path.abspath(str(self.run_dir))) |
| 140 | |
| 141 | def set_env(self, key, value): |
| 142 | env = self.popen_kwargs.get('env') or {} |
| 143 | env[key] = value |
| 144 | self.popen_kwargs['env'] = env |
| 145 | |
| 146 | def make_output_log(self, name): |
| 147 | ''' |
| 148 | create a non-existing log output file in run_dir to pipe stdout and |
| 149 | stderr from this process to. |
| 150 | ''' |
| 151 | path = self.run_dir.new_child(name) |
| 152 | f = open(path, 'w') |
| 153 | self.dbg(path) |
Pau Espin Pedrol | 0d8deec | 2017-06-23 11:43:38 +0200 | [diff] [blame] | 154 | f.write('(launched: %s)\n' % datetime.now().strftime(log.LONG_DATEFMT)) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 155 | f.flush() |
| 156 | self.outputs[name] = (path, f) |
| 157 | return f |
| 158 | |
| 159 | def launch(self): |
Pau Espin Pedrol | 3a479c2 | 2019-04-05 19:47:40 +0200 | [diff] [blame] | 160 | preexec_fn = None |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 161 | log.dbg('cd %r; %s %s' % ( |
| 162 | os.path.abspath(str(self.run_dir)), |
| 163 | ' '.join(['%s=%r'%(k,v) for k,v in self.popen_kwargs.get('env', {}).items()]), |
| 164 | ' '.join(self.popen_args))) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 165 | |
Pau Espin Pedrol | 3a479c2 | 2019-04-05 19:47:40 +0200 | [diff] [blame] | 166 | if self.popen_args[0] == "sudo": |
| 167 | # sudo drops forwarding of signals sent by processes of the same |
| 168 | # process group, which means by default will drop signals from |
| 169 | # parent and children processes. By moving it to another group, we |
| 170 | # will later be able to kill it. |
| 171 | # Note: sudo documentation is wrong, since it states it only drops |
| 172 | # signals from children. |
| 173 | preexec_fn = os.setpgrp |
| 174 | |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 175 | self.process_obj = subprocess.Popen( |
| 176 | self.popen_args, |
| 177 | stdout=self.make_output_log('stdout'), |
| 178 | stderr=self.make_output_log('stderr'), |
| 179 | stdin=subprocess.PIPE, |
Pau Espin Pedrol | 3a479c2 | 2019-04-05 19:47:40 +0200 | [diff] [blame] | 180 | preexec_fn=preexec_fn, |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 181 | shell=False, |
| 182 | cwd=self.run_dir.path, |
| 183 | **self.popen_kwargs) |
| 184 | self.set_name(self.name_str, pid=self.process_obj.pid) |
| 185 | self.log('Launched') |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 186 | |
Pau Espin Pedrol | 78087be | 2018-11-12 18:20:52 +0100 | [diff] [blame] | 187 | def launch_sync(self, raise_nonsuccess=True): |
Pau Espin Pedrol | 79df739 | 2018-11-12 18:15:30 +0100 | [diff] [blame] | 188 | ''' |
| 189 | calls launch() method and block waiting for it to finish, serving the |
| 190 | mainloop meanwhile. |
| 191 | ''' |
| 192 | try: |
| 193 | self.launch() |
| 194 | self.wait() |
| 195 | except Exception as e: |
| 196 | self.terminate() |
| 197 | raise e |
Pau Espin Pedrol | 78087be | 2018-11-12 18:20:52 +0100 | [diff] [blame] | 198 | if raise_nonsuccess and self.result != 0: |
Pau Espin Pedrol | 79df739 | 2018-11-12 18:15:30 +0100 | [diff] [blame] | 199 | log.ctx(self) |
Pau Espin Pedrol | 78087be | 2018-11-12 18:20:52 +0100 | [diff] [blame] | 200 | raise log.Error('Exited in error %d' % self.result) |
| 201 | return self.result |
Pau Espin Pedrol | 79df739 | 2018-11-12 18:15:30 +0100 | [diff] [blame] | 202 | |
Pau Espin Pedrol | b1526b9 | 2018-05-22 20:32:30 +0200 | [diff] [blame] | 203 | def respawn(self): |
| 204 | self.dbg('respawn') |
| 205 | assert not self.is_running() |
| 206 | self.result = None |
| 207 | self.killed = None |
Pau Espin Pedrol | 922ce5a | 2019-09-10 13:44:20 +0200 | [diff] [blame] | 208 | return self.launch() |
Pau Espin Pedrol | b1526b9 | 2018-05-22 20:32:30 +0200 | [diff] [blame] | 209 | |
Pau Espin Pedrol | dd7bb2c | 2019-09-10 13:44:39 +0200 | [diff] [blame] | 210 | def respawn_sync(self, raise_nonsuccess=True): |
| 211 | self.dbg('respawn_sync') |
| 212 | assert not self.is_running() |
| 213 | self.result = None |
| 214 | self.killed = None |
| 215 | return self.launch_sync(raise_nonsuccess) |
| 216 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 217 | def _poll_termination(self, time_to_wait_for_term=5): |
| 218 | wait_step = 0.001 |
| 219 | waited_time = 0 |
| 220 | while True: |
| 221 | # poll returns None if proc is still running |
| 222 | self.result = self.process_obj.poll() |
| 223 | if self.result is not None: |
| 224 | return True |
| 225 | waited_time += wait_step |
| 226 | # make wait_step approach 1.0 |
| 227 | wait_step = (1. + 5. * wait_step) / 6. |
| 228 | if waited_time >= time_to_wait_for_term: |
| 229 | break |
| 230 | time.sleep(wait_step) |
| 231 | return False |
| 232 | |
Pau Espin Pedrol | fd4c144 | 2018-10-25 17:37:23 +0200 | [diff] [blame] | 233 | def send_signal(self, sig): |
| 234 | os.kill(self.process_obj.pid, sig) |
| 235 | |
Holger Hans Peter Freyther | 54b4fa9 | 2019-02-27 13:00:33 +0000 | [diff] [blame] | 236 | def pid(self): |
| 237 | if self.process_obj is None: |
| 238 | return None |
| 239 | return self.process_obj.pid |
| 240 | |
Holger Hans Peter Freyther | 0d714c9 | 2019-02-27 09:50:52 +0000 | [diff] [blame] | 241 | def kill(self, sig): |
| 242 | """Kills the process with the given signal and remembers it.""" |
| 243 | self.log('Terminating (%s)' % sig.name) |
| 244 | self.send_signal(sig) |
| 245 | self.killed = sig |
| 246 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 247 | def terminate(self): |
| 248 | if self.process_obj is None: |
| 249 | return |
| 250 | if self.result is not None: |
| 251 | return |
| 252 | |
| 253 | while True: |
| 254 | # first try SIGINT to allow stdout+stderr flushing |
Holger Hans Peter Freyther | 0d714c9 | 2019-02-27 09:50:52 +0000 | [diff] [blame] | 255 | self.kill(signal.SIGINT) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 256 | if self._poll_termination(): |
| 257 | break |
| 258 | |
| 259 | # SIGTERM maybe? |
Holger Hans Peter Freyther | 0d714c9 | 2019-02-27 09:50:52 +0000 | [diff] [blame] | 260 | self.kill(signal.SIGTERM) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 261 | if self._poll_termination(): |
| 262 | break |
| 263 | |
| 264 | # out of patience |
Holger Hans Peter Freyther | 0d714c9 | 2019-02-27 09:50:52 +0000 | [diff] [blame] | 265 | self.kill(signal.SIGKILL) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 266 | break; |
| 267 | |
| 268 | self.process_obj.wait() |
| 269 | self.cleanup() |
| 270 | |
| 271 | def cleanup(self): |
Pau Espin Pedrol | 06ada45 | 2018-05-22 19:20:41 +0200 | [diff] [blame] | 272 | self.dbg('Cleanup') |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 273 | self.close_output_logs() |
| 274 | if self.result == 0: |
| 275 | self.log('Terminated: ok', rc=self.result) |
| 276 | elif self.killed: |
| 277 | self.log('Terminated', rc=self.result) |
| 278 | else: |
| 279 | self.err('Terminated: ERROR', rc=self.result) |
Neels Hofmeyr | 85eb324 | 2017-04-09 22:01:16 +0200 | [diff] [blame] | 280 | #self.log_stdout_tail() |
| 281 | self.log_stderr_tail() |
| 282 | |
| 283 | def log_stdout_tail(self): |
| 284 | m = self.get_stdout_tail(prefix='| ') |
| 285 | if not m: |
| 286 | return |
| 287 | self.log('stdout:\n', m, '\n') |
| 288 | |
| 289 | def log_stderr_tail(self): |
| 290 | m = self.get_stderr_tail(prefix='| ') |
| 291 | if not m: |
| 292 | return |
| 293 | self.log('stderr:\n', m, '\n') |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 294 | |
| 295 | def close_output_logs(self): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 296 | for k, v in self.outputs.items(): |
| 297 | path, f = v |
| 298 | if f: |
| 299 | f.flush() |
| 300 | f.close() |
| 301 | self.outputs[k] = (path, None) |
| 302 | |
| 303 | def poll(self): |
| 304 | if self.process_obj is None: |
| 305 | return |
| 306 | if self.result is not None: |
| 307 | return |
| 308 | self.result = self.process_obj.poll() |
| 309 | if self.result is not None: |
| 310 | self.cleanup() |
| 311 | |
Neels Hofmeyr | 5356d0a | 2017-04-10 03:45:30 +0200 | [diff] [blame] | 312 | def is_running(self, poll_first=True): |
| 313 | if poll_first: |
| 314 | self.poll() |
Neels Hofmeyr | 85eb324 | 2017-04-09 22:01:16 +0200 | [diff] [blame] | 315 | return self.process_obj is not None and self.result is None |
| 316 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 317 | def get_output(self, which): |
| 318 | v = self.outputs.get(which) |
| 319 | if not v: |
| 320 | return None |
| 321 | path, f = v |
| 322 | with open(path, 'r') as f2: |
| 323 | return f2.read() |
| 324 | |
| 325 | def get_output_tail(self, which, tail=10, prefix=''): |
Neels Hofmeyr | 5356d0a | 2017-04-10 03:45:30 +0200 | [diff] [blame] | 326 | out = self.get_output(which) |
| 327 | if not out: |
| 328 | return None |
| 329 | out = out.splitlines() |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 330 | tail = min(len(out), tail) |
Neels Hofmeyr | 5356d0a | 2017-04-10 03:45:30 +0200 | [diff] [blame] | 331 | return prefix + ('\n' + prefix).join(out[-tail:]) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 332 | |
| 333 | def get_stdout(self): |
| 334 | return self.get_output('stdout') |
| 335 | |
| 336 | def get_stderr(self): |
| 337 | return self.get_output('stderr') |
| 338 | |
| 339 | def get_stdout_tail(self, tail=10, prefix=''): |
| 340 | return self.get_output_tail('stdout', tail, prefix) |
| 341 | |
| 342 | def get_stderr_tail(self, tail=10, prefix=''): |
| 343 | return self.get_output_tail('stderr', tail, prefix) |
| 344 | |
Neels Hofmeyr | 5356d0a | 2017-04-10 03:45:30 +0200 | [diff] [blame] | 345 | def terminated(self, poll_first=True): |
| 346 | if poll_first: |
| 347 | self.poll() |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 348 | return self.result is not None |
| 349 | |
Neels Hofmeyr | 5356d0a | 2017-04-10 03:45:30 +0200 | [diff] [blame] | 350 | def wait(self, timeout=300): |
Pau Espin Pedrol | 9a4631c | 2018-03-28 19:17:34 +0200 | [diff] [blame] | 351 | MainLoop.wait(self, self.terminated, timeout=timeout) |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 352 | |
| 353 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 354 | class RemoteProcess(Process): |
| 355 | |
Pau Espin Pedrol | f6d45ad | 2020-02-11 14:39:15 +0100 | [diff] [blame] | 356 | def __init__(self, name, run_dir, remote_user, remote_host, remote_cwd, popen_args, remote_env={}, **popen_kwargs): |
Neels Hofmeyr | 5356d0a | 2017-04-10 03:45:30 +0200 | [diff] [blame] | 357 | super().__init__(name, run_dir, popen_args, **popen_kwargs) |
Pau Espin Pedrol | 3895fec | 2017-04-28 16:13:03 +0200 | [diff] [blame] | 358 | self.remote_user = remote_user |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 359 | self.remote_host = remote_host |
| 360 | self.remote_cwd = remote_cwd |
Pau Espin Pedrol | f6d45ad | 2020-02-11 14:39:15 +0100 | [diff] [blame] | 361 | self.remote_env = remote_env |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 362 | |
| 363 | # hacky: instead of just prepending ssh, i.e. piping stdout and stderr |
| 364 | # over the ssh link, we should probably run on the remote side, |
| 365 | # monitoring the process remotely. |
Neels Hofmeyr | 5356d0a | 2017-04-10 03:45:30 +0200 | [diff] [blame] | 366 | if self.remote_cwd: |
Pau Espin Pedrol | f6d45ad | 2020-02-11 14:39:15 +0100 | [diff] [blame] | 367 | cd = 'cd "%s";' % self.remote_cwd |
Neels Hofmeyr | 5356d0a | 2017-04-10 03:45:30 +0200 | [diff] [blame] | 368 | else: |
| 369 | cd = '' |
Pau Espin Pedrol | 302c756 | 2018-10-02 13:08:02 +0200 | [diff] [blame] | 370 | # We need double -t to force tty and be able to forward signals to |
| 371 | # processes (SIGHUP) when we close ssh on the local side. As a result, |
| 372 | # stderr seems to be merged into stdout in ssh client. |
| 373 | self.popen_args = ['ssh', '-t', '-t', self.remote_user+'@'+self.remote_host, |
Pau Espin Pedrol | f6d45ad | 2020-02-11 14:39:15 +0100 | [diff] [blame] | 374 | '%s %s %s' % (cd, |
| 375 | ' '.join(['%s=%r'%(k,v) for k,v in self.remote_env.items()]), |
| 376 | ' '.join(self.popen_args))] |
| 377 | self.dbg(self.popen_args, dir=self.run_dir, conf=self.popen_kwargs, remote_env=self.remote_env) |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 378 | |
Pau Espin Pedrol | fd4c144 | 2018-10-25 17:37:23 +0200 | [diff] [blame] | 379 | class NetNSProcess(Process): |
| 380 | NETNS_EXEC_BIN = 'osmo-gsm-tester_netns_exec.sh' |
| 381 | def __init__(self, name, run_dir, netns, popen_args, **popen_kwargs): |
| 382 | super().__init__(name, run_dir, popen_args, **popen_kwargs) |
| 383 | self.netns = netns |
| 384 | |
| 385 | self.popen_args = ['sudo', self.NETNS_EXEC_BIN, self.netns] + list(popen_args) |
| 386 | self.dbg(self.popen_args, dir=self.run_dir, conf=self.popen_kwargs) |
| 387 | |
| 388 | # HACK: Since we run under sudo, only way to kill root-owned process is to kill as root... |
| 389 | # This function is overwritten from Process. |
| 390 | def send_signal(self, sig): |
Pau Espin Pedrol | e159cd2 | 2019-04-03 17:53:54 +0200 | [diff] [blame] | 391 | if sig == signal.SIGKILL: |
| 392 | # if we kill sudo, its children (bash running NETNS_EXEC_BIN + |
| 393 | # tcpdump under it) are kept alive. Let's instead tell the script to |
| 394 | # kill tcpdump: |
| 395 | sig = signal.SIGUSR1 |
Pau Espin Pedrol | fd4c144 | 2018-10-25 17:37:23 +0200 | [diff] [blame] | 396 | kill_cmd = ('kill', '-%d' % int(sig), str(self.process_obj.pid)) |
Pau Espin Pedrol | 17a4ed9 | 2019-04-03 17:10:31 +0200 | [diff] [blame] | 397 | run_local_netns_sync(self.run_dir, self.name()+"-kill"+str(sig), self.netns, kill_cmd) |
Pau Espin Pedrol | fd4c144 | 2018-10-25 17:37:23 +0200 | [diff] [blame] | 398 | |
Pau Espin Pedrol | 14022d3 | 2020-02-11 14:20:00 +0100 | [diff] [blame] | 399 | class RemoteNetNSProcess(RemoteProcess): |
| 400 | NETNS_EXEC_BIN = 'osmo-gsm-tester_netns_exec.sh' |
| 401 | def __init__(self, name, run_dir, remote_user, remote_host, remote_cwd, netns, popen_args, **popen_kwargs): |
Pau Espin Pedrol | 4983eb5 | 2020-02-11 19:16:06 +0100 | [diff] [blame] | 402 | self.netns = netns |
Pau Espin Pedrol | 14022d3 | 2020-02-11 14:20:00 +0100 | [diff] [blame] | 403 | args = ['sudo', self.NETNS_EXEC_BIN, self.netns] + list(popen_args) |
| 404 | super().__init__(name, run_dir, remote_user, remote_host, remote_cwd, args, **popen_kwargs) |
Pau Espin Pedrol | fd4c144 | 2018-10-25 17:37:23 +0200 | [diff] [blame] | 405 | |
Pau Espin Pedrol | e4358a9 | 2018-10-01 11:27:55 +0200 | [diff] [blame] | 406 | def run_local_sync(run_dir, name, popen_args): |
| 407 | run_dir =run_dir.new_dir(name) |
| 408 | proc = Process(name, run_dir, popen_args) |
Pau Espin Pedrol | 79df739 | 2018-11-12 18:15:30 +0100 | [diff] [blame] | 409 | proc.launch_sync() |
Pau Espin Pedrol | 12c5ea4 | 2019-11-26 14:25:33 +0100 | [diff] [blame] | 410 | return proc |
Pau Espin Pedrol | e4358a9 | 2018-10-01 11:27:55 +0200 | [diff] [blame] | 411 | |
Pau Espin Pedrol | fd4c144 | 2018-10-25 17:37:23 +0200 | [diff] [blame] | 412 | def run_local_netns_sync(run_dir, name, netns, popen_args): |
| 413 | run_dir =run_dir.new_dir(name) |
| 414 | proc = NetNSProcess(name, run_dir, netns, popen_args) |
Pau Espin Pedrol | 79df739 | 2018-11-12 18:15:30 +0100 | [diff] [blame] | 415 | proc.launch_sync() |
Pau Espin Pedrol | 12c5ea4 | 2019-11-26 14:25:33 +0100 | [diff] [blame] | 416 | return proc |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 417 | # vim: expandtab tabstop=4 shiftwidth=4 |