1# SPDX-License-Identifier: GPL-2.0 2 3import errno 4import json as _json 5import os 6import random 7import re 8import select 9import socket 10import subprocess 11import time 12 13 14class CmdExitFailure(Exception): 15 def __init__(self, msg, cmd_obj): 16 super().__init__(msg) 17 self.cmd = cmd_obj 18 19 20def fd_read_timeout(fd, timeout): 21 rlist, _, _ = select.select([fd], [], [], timeout) 22 if rlist: 23 return os.read(fd, 1024) 24 else: 25 raise TimeoutError("Timeout waiting for fd read") 26 27 28class cmd: 29 """ 30 Execute a command on local or remote host. 31 32 Use bkg() instead to run a command in the background. 33 """ 34 def __init__(self, comm, shell=True, fail=True, ns=None, background=False, 35 host=None, timeout=5, ksft_wait=None): 36 if ns: 37 comm = f'ip netns exec {ns} ' + comm 38 39 self.stdout = None 40 self.stderr = None 41 self.ret = None 42 self.ksft_term_fd = None 43 44 self.comm = comm 45 if host: 46 self.proc = host.cmd(comm) 47 else: 48 # ksft_wait lets us wait for the background process to fully start, 49 # we pass an FD to the child process, and wait for it to write back. 50 # Similarly term_fd tells child it's time to exit. 51 pass_fds = () 52 env = os.environ.copy() 53 if ksft_wait is not None: 54 rfd, ready_fd = os.pipe() 55 wait_fd, self.ksft_term_fd = os.pipe() 56 pass_fds = (ready_fd, wait_fd, ) 57 env["KSFT_READY_FD"] = str(ready_fd) 58 env["KSFT_WAIT_FD"] = str(wait_fd) 59 60 self.proc = subprocess.Popen(comm, shell=shell, stdout=subprocess.PIPE, 61 stderr=subprocess.PIPE, pass_fds=pass_fds, 62 env=env) 63 if ksft_wait is not None: 64 os.close(ready_fd) 65 os.close(wait_fd) 66 msg = fd_read_timeout(rfd, ksft_wait) 67 os.close(rfd) 68 if not msg: 69 raise Exception("Did not receive ready message") 70 if not background: 71 self.process(terminate=False, fail=fail, timeout=timeout) 72 73 def process(self, terminate=True, fail=None, timeout=5): 74 if fail is None: 75 fail = not terminate 76 77 if self.ksft_term_fd: 78 os.write(self.ksft_term_fd, b"1") 79 if terminate: 80 self.proc.terminate() 81 stdout, stderr = self.proc.communicate(timeout) 82 self.stdout = stdout.decode("utf-8") 83 self.stderr = stderr.decode("utf-8") 84 self.proc.stdout.close() 85 self.proc.stderr.close() 86 self.ret = self.proc.returncode 87 88 if self.proc.returncode != 0 and fail: 89 if len(stderr) > 0 and stderr[-1] == "\n": 90 stderr = stderr[:-1] 91 raise CmdExitFailure("Command failed: %s\nSTDOUT: %s\nSTDERR: %s" % 92 (self.proc.args, stdout, stderr), self) 93 94 95class bkg(cmd): 96 """ 97 Run a command in the background. 98 99 Examples usage: 100 101 Run a command on remote host, and wait for it to finish. 102 This is usually paired with wait_port_listen() to make sure 103 the command has initialized: 104 105 with bkg("socat ...", exit_wait=True, host=cfg.remote) as nc: 106 ... 107 108 Run a command and expect it to let us know that it's ready 109 by writing to a special file descriptor passed via KSFT_READY_FD. 110 Command will be terminated when we exit the context manager: 111 112 with bkg("my_binary", ksft_wait=5): 113 """ 114 def __init__(self, comm, shell=True, fail=None, ns=None, host=None, 115 exit_wait=False, ksft_wait=None): 116 super().__init__(comm, background=True, 117 shell=shell, fail=fail, ns=ns, host=host, 118 ksft_wait=ksft_wait) 119 self.terminate = not exit_wait and not ksft_wait 120 self.check_fail = fail 121 122 if shell and self.terminate: 123 print("# Warning: combining shell and terminate is risky!") 124 print("# SIGTERM may not reach the child on zsh/ksh!") 125 126 def __enter__(self): 127 return self 128 129 def __exit__(self, ex_type, ex_value, ex_tb): 130 return self.process(terminate=self.terminate, fail=self.check_fail) 131 132 133global_defer_queue = [] 134 135 136class defer: 137 def __init__(self, func, *args, **kwargs): 138 global global_defer_queue 139 140 if not callable(func): 141 raise Exception("defer created with un-callable object, did you call the function instead of passing its name?") 142 143 self.func = func 144 self.args = args 145 self.kwargs = kwargs 146 147 self._queue = global_defer_queue 148 self._queue.append(self) 149 150 def __enter__(self): 151 return self 152 153 def __exit__(self, ex_type, ex_value, ex_tb): 154 return self.exec() 155 156 def exec_only(self): 157 self.func(*self.args, **self.kwargs) 158 159 def cancel(self): 160 self._queue.remove(self) 161 162 def exec(self): 163 self.cancel() 164 self.exec_only() 165 166 167def tool(name, args, json=None, ns=None, host=None): 168 cmd_str = name + ' ' 169 if json: 170 cmd_str += '--json ' 171 cmd_str += args 172 cmd_obj = cmd(cmd_str, ns=ns, host=host) 173 if json: 174 return _json.loads(cmd_obj.stdout) 175 return cmd_obj 176 177 178def bpftool(args, json=None, ns=None, host=None): 179 return tool('bpftool', args, json=json, ns=ns, host=host) 180 181 182def ip(args, json=None, ns=None, host=None): 183 if ns: 184 args = f'-netns {ns} ' + args 185 return tool('ip', args, json=json, host=host) 186 187 188def ethtool(args, json=None, ns=None, host=None): 189 return tool('ethtool', args, json=json, ns=ns, host=host) 190 191 192def bpftrace(expr, json=None, ns=None, host=None, timeout=None): 193 """ 194 Run bpftrace and return map data (if json=True). 195 The output of bpftrace is inconvenient, so the helper converts 196 to a dict indexed by map name, e.g.: 197 { 198 "@": { ... }, 199 "@map2": { ... }, 200 } 201 """ 202 cmd_arr = ['bpftrace'] 203 # Throw in --quiet if json, otherwise the output has two objects 204 if json: 205 cmd_arr += ['-f', 'json', '-q'] 206 if timeout: 207 expr += ' interval:s:' + str(timeout) + ' { exit(); }' 208 cmd_arr += ['-e', expr] 209 cmd_obj = cmd(cmd_arr, ns=ns, host=host, shell=False) 210 if json: 211 # bpftrace prints objects as lines 212 ret = {} 213 for l in cmd_obj.stdout.split('\n'): 214 if not l.strip(): 215 continue 216 one = _json.loads(l) 217 if one.get('type') != 'map': 218 continue 219 for k, v in one["data"].items(): 220 if k.startswith('@'): 221 k = k.lstrip('@') 222 ret[k] = v 223 return ret 224 return cmd_obj 225 226 227def rand_port(type=socket.SOCK_STREAM): 228 """ 229 Get a random unprivileged port. 230 """ 231 with socket.socket(socket.AF_INET6, type) as s: 232 s.bind(("", 0)) 233 return s.getsockname()[1] 234 235 236def wait_port_listen(port, proto="tcp", ns=None, host=None, sleep=0.005, deadline=5): 237 end = time.monotonic() + deadline 238 239 pattern = f":{port:04X} .* " 240 if proto == "tcp": # for tcp protocol additionally check the socket state 241 pattern += "0A" 242 pattern = re.compile(pattern) 243 244 while True: 245 data = cmd(f'cat /proc/net/{proto}*', ns=ns, host=host, shell=True).stdout 246 for row in data.split("\n"): 247 if pattern.search(row): 248 return 249 if time.monotonic() > end: 250 raise Exception("Waiting for port listen timed out") 251 time.sleep(sleep) 252