Skip to content

Commit ec4f498

Browse files
committed
Make creating fd for remote control a separate function
1 parent 76cd687 commit ec4f498

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

kitty/boss.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import json
88
import os
99
import re
10+
import socket
1011
import sys
1112
from collections.abc import Container, Generator, Iterable, Iterator, Sequence
1213
from contextlib import contextmanager, suppress
@@ -2350,6 +2351,20 @@ def special_window_for_cmd(
23502351
overlay_for = w.id if w and as_overlay else None
23512352
return SpecialWindow(cmd, input_data, cwd_from=cwd_from, overlay_for=overlay_for, env=env)
23522353

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+
23532368
def run_background_process(
23542369
self,
23552370
cmd: list[str],
@@ -2383,20 +2398,9 @@ def doit(activation_token: str = '') -> None:
23832398
pass_fds: list[int] = []
23842399
fds_to_close_on_launch_failure: list[int] = []
23852400
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)
23972402
pass_fds.append(remote.fileno())
23982403
add_env('KITTY_LISTEN_ON', f'fd:{remote.fileno()}')
2399-
self.peer_data_map[peer_id] = remote_control_passwords
24002404
if activation_token:
24012405
add_env('XDG_ACTIVATION_TOKEN', activation_token)
24022406
fds_to_close_on_launch_failure = list(pass_fds)

0 commit comments

Comments
 (0)