Send a daemon (--admin-daemon) command 'cmd'. asok_path is the path to the admin socket; cmd is a list of strings; format may be set to one of the formatted forms to get output in that form (daemon commands don't support 'plain' output).
(asok_path: str,
cmd: List[str],
format: Optional[str] = '')
| 32 | |
| 33 | |
| 34 | def admin_socket(asok_path: str, |
| 35 | cmd: List[str], |
| 36 | format: Optional[str] = '') -> bytes: |
| 37 | """ |
| 38 | Send a daemon (--admin-daemon) command 'cmd'. asok_path is the |
| 39 | path to the admin socket; cmd is a list of strings; format may be |
| 40 | set to one of the formatted forms to get output in that form |
| 41 | (daemon commands don't support 'plain' output). |
| 42 | """ |
| 43 | |
| 44 | def do_sockio(path: str, cmd_bytes: bytes) -> bytes: |
| 45 | """ helper: do all the actual low-level stream I/O """ |
| 46 | sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 47 | sock.connect(path) |
| 48 | try: |
| 49 | sock.sendall(cmd_bytes + b'\0') |
| 50 | len_str = sock.recv(4) |
| 51 | if len(len_str) < 4: |
| 52 | raise RuntimeError("no data returned from admin socket") |
| 53 | l, = struct.unpack(">I", len_str) |
| 54 | sock_ret = b'' |
| 55 | |
| 56 | got = 0 |
| 57 | while got < l: |
| 58 | # recv() receives signed int, i.e max 2GB |
| 59 | # workaround by capping READ_CHUNK_SIZE per call. |
| 60 | want = min(l - got, READ_CHUNK_SIZE) |
| 61 | bit = sock.recv(want) |
| 62 | sock_ret += bit |
| 63 | got += len(bit) |
| 64 | |
| 65 | except Exception as sock_e: |
| 66 | raise RuntimeError('exception: ' + str(sock_e)) |
| 67 | return sock_ret |
| 68 | |
| 69 | try: |
| 70 | cmd_json = do_sockio(asok_path, |
| 71 | b'{"prefix": "get_command_descriptions"}') |
| 72 | except Exception as e: |
| 73 | raise RuntimeError('exception getting command descriptions: ' + str(e)) |
| 74 | |
| 75 | sigdict = parse_json_funcsigs(cmd_json.decode('utf-8'), 'cli') |
| 76 | valid_dict = validate_command(sigdict, cmd) |
| 77 | if not valid_dict: |
| 78 | raise RuntimeError('invalid command') |
| 79 | |
| 80 | if format: |
| 81 | valid_dict['format'] = format |
| 82 | |
| 83 | try: |
| 84 | ret = do_sockio(asok_path, json.dumps(valid_dict).encode('utf-8')) |
| 85 | except Exception as e: |
| 86 | raise RuntimeError('exception: ' + str(e)) |
| 87 | |
| 88 | return ret |
| 89 | |
| 90 | |
| 91 | class Termsize(object): |
no test coverage detected