|
7 | 7 | import json |
8 | 8 | import os |
9 | 9 | import re |
| 10 | +import socket |
10 | 11 | import sys |
11 | 12 | from collections.abc import Container, Generator, Iterable, Iterator, Sequence |
12 | 13 | from contextlib import contextmanager, suppress |
@@ -2350,6 +2351,20 @@ def special_window_for_cmd( |
2350 | 2351 | overlay_for = w.id if w and as_overlay else None |
2351 | 2352 | return SpecialWindow(cmd, input_data, cwd_from=cwd_from, overlay_for=overlay_for, env=env) |
2352 | 2353 |
|
| 2354 | + def add_fd_based_remote_control(self, remote_control_passwords: Optional[dict[str, Sequence[str]]] = None) -> socket.socket: |
| 2355 | + local, remote = socket.socketpair() |
| 2356 | + os.set_inheritable(remote.fileno(), True) |
| 2357 | + lfd = os.dup(local.fileno()) |
| 2358 | + local.close() |
| 2359 | + try: |
| 2360 | + peer_id = self.child_monitor.inject_peer(lfd) |
| 2361 | + except Exception: |
| 2362 | + os.close(lfd) |
| 2363 | + remote.close() |
| 2364 | + raise |
| 2365 | + self.peer_data_map[peer_id] = remote_control_passwords |
| 2366 | + return remote |
| 2367 | + |
2353 | 2368 | def run_background_process( |
2354 | 2369 | self, |
2355 | 2370 | cmd: list[str], |
@@ -2383,20 +2398,9 @@ def doit(activation_token: str = '') -> None: |
2383 | 2398 | pass_fds: list[int] = [] |
2384 | 2399 | fds_to_close_on_launch_failure: list[int] = [] |
2385 | 2400 | if allow_remote_control: |
2386 | | - import socket |
2387 | | - local, remote = socket.socketpair() |
2388 | | - os.set_inheritable(remote.fileno(), True) |
2389 | | - lfd = os.dup(local.fileno()) |
2390 | | - local.close() |
2391 | | - try: |
2392 | | - peer_id = self.child_monitor.inject_peer(lfd) |
2393 | | - except Exception: |
2394 | | - os.close(lfd) |
2395 | | - remote.close() |
2396 | | - raise |
| 2401 | + remote = self.add_fd_based_remote_control(remote_control_passwords) |
2397 | 2402 | pass_fds.append(remote.fileno()) |
2398 | 2403 | add_env('KITTY_LISTEN_ON', f'fd:{remote.fileno()}') |
2399 | | - self.peer_data_map[peer_id] = remote_control_passwords |
2400 | 2404 | if activation_token: |
2401 | 2405 | add_env('XDG_ACTIVATION_TOKEN', activation_token) |
2402 | 2406 | fds_to_close_on_launch_failure = list(pass_fds) |
|
0 commit comments