Make creating fd for remote control a separate function

This commit is contained in:
Kovid Goyal
2024-09-28 11:00:56 +05:30
parent 76cd68760a
commit ec4f498daa

View File

@@ -7,6 +7,7 @@ import base64
import json import json
import os import os
import re import re
import socket
import sys import sys
from collections.abc import Container, Generator, Iterable, Iterator, Sequence from collections.abc import Container, Generator, Iterable, Iterator, Sequence
from contextlib import contextmanager, suppress from contextlib import contextmanager, suppress
@@ -2350,6 +2351,20 @@ class Boss:
overlay_for = w.id if w and as_overlay else None overlay_for = w.id if w and as_overlay else None
return SpecialWindow(cmd, input_data, cwd_from=cwd_from, overlay_for=overlay_for, env=env) return SpecialWindow(cmd, input_data, cwd_from=cwd_from, overlay_for=overlay_for, env=env)
def add_fd_based_remote_control(self, remote_control_passwords: Optional[dict[str, Sequence[str]]] = None) -> socket.socket:
local, remote = socket.socketpair()
os.set_inheritable(remote.fileno(), True)
lfd = os.dup(local.fileno())
local.close()
try:
peer_id = self.child_monitor.inject_peer(lfd)
except Exception:
os.close(lfd)
remote.close()
raise
self.peer_data_map[peer_id] = remote_control_passwords
return remote
def run_background_process( def run_background_process(
self, self,
cmd: list[str], cmd: list[str],
@@ -2383,20 +2398,9 @@ class Boss:
pass_fds: list[int] = [] pass_fds: list[int] = []
fds_to_close_on_launch_failure: list[int] = [] fds_to_close_on_launch_failure: list[int] = []
if allow_remote_control: if allow_remote_control:
import socket remote = self.add_fd_based_remote_control(remote_control_passwords)
local, remote = socket.socketpair()
os.set_inheritable(remote.fileno(), True)
lfd = os.dup(local.fileno())
local.close()
try:
peer_id = self.child_monitor.inject_peer(lfd)
except Exception:
os.close(lfd)
remote.close()
raise
pass_fds.append(remote.fileno()) pass_fds.append(remote.fileno())
add_env('KITTY_LISTEN_ON', f'fd:{remote.fileno()}') add_env('KITTY_LISTEN_ON', f'fd:{remote.fileno()}')
self.peer_data_map[peer_id] = remote_control_passwords
if activation_token: if activation_token:
add_env('XDG_ACTIVATION_TOKEN', activation_token) add_env('XDG_ACTIVATION_TOKEN', activation_token)
fds_to_close_on_launch_failure = list(pass_fds) fds_to_close_on_launch_failure = list(pass_fds)