From 31d9db7e745bfc9f1c8c36fd3396473c4571125a Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 10 Mar 2022 12:50:59 +0530 Subject: [PATCH] Use XDG_RUNTIME_DIR to store control master sockets On Linux this has the advantage that the dir is auto cleaned on reboot --- docs/glossary.rst | 6 ++++++ kittens/ssh/main.py | 7 +++++-- kitty/constants.py | 14 ++++++++++++++ kitty/main.py | 7 ++++--- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/docs/glossary.rst b/docs/glossary.rst index 9fc4e984a..05bd4e318 100644 --- a/docs/glossary.rst +++ b/docs/glossary.rst @@ -57,6 +57,12 @@ Variables that influence kitty behavior Controls where kitty stores cache files. Defaults to :file:`~/.cache/kitty` or :file:`~/Library/Caches/kitty` on macOS. +.. envvar:: KITTY_RUNTIME_DIRECTORY + + Controls where kitty stores runtime files like sockets. Defaults to + the :code:`XDG_RUNTIME_DIR` environment variable if that is defined + otherwise the run directory inside the kitty cache directory is used. + .. envvar:: VISUAL The terminal editor (such as ``vi`` or ``nano``) kitty uses, when, for diff --git a/kittens/ssh/main.py b/kittens/ssh/main.py index cf14e56bf..016a75fb1 100644 --- a/kittens/ssh/main.py +++ b/kittens/ssh/main.py @@ -23,7 +23,10 @@ from typing import ( Tuple, Union ) -from kitty.constants import cache_dir, shell_integration_dir, terminfo_dir +from kitty.constants import ( + cache_dir, runtime_dir, shell_integration_dir, ssh_control_master_template, + terminfo_dir +) from kitty.fast_data_types import get_options from kitty.shm import SharedMemory from kitty.utils import SSHConnectionData @@ -442,7 +445,7 @@ def get_remote_command( def connection_sharing_args(opts: SSHOptions, kitty_pid: int) -> List[str]: - cp = os.path.join(cache_dir(), 'ssh', f'{kitty_pid}-master-%C') + cp = os.path.join(runtime_dir(), ssh_control_master_template.format(kitty_pid=kitty_pid, ssh_placeholder='%C')) ans: List[str] = [ '-o', 'ControlMaster=auto', '-o', f'ControlPath={cp}', diff --git a/kitty/constants.py b/kitty/constants.py index 43a6a5377..b68c6e068 100644 --- a/kitty/constants.py +++ b/kitty/constants.py @@ -137,6 +137,19 @@ def cache_dir() -> str: return candidate +@run_once +def runtime_dir() -> str: + if 'KITTY_RUNTIME_DIRECTORY' in os.environ: + candidate = os.path.abspath(os.environ['KITTY_RUNTIME_DIRECTORY']) + elif 'XDG_RUNTIME_DIR' in os.environ: + candidate = os.path.abspath(os.environ['XDG_RUNTIME_DIR']) + else: + candidate = os.path.join(cache_dir(), 'run') + os.makedirs(candidate, exist_ok=True) + os.chmod(candidate, 0o700) + return candidate + + def wakeup() -> None: from .fast_data_types import get_boss b = get_boss() @@ -154,6 +167,7 @@ except KeyError: with suppress(Exception): print('Failed to read login shell via getpwuid() for current user, falling back to /bin/sh', file=sys.stderr) shell_path = '/bin/sh' +ssh_control_master_template = 'ssh-kitten-{kitty_pid}-master-{ssh_placeholder}' def glfw_path(module: str) -> str: diff --git a/kitty/main.py b/kitty/main.py index 7c77e045b..bf796b702 100644 --- a/kitty/main.py +++ b/kitty/main.py @@ -16,8 +16,9 @@ from .cli_stub import CLIOptions from .conf.utils import BadLine from .config import cached_values_for from .constants import ( - appname, beam_cursor_data_file, cache_dir, config_dir, glfw_path, is_macos, - is_wayland, kitty_exe, logo_png_file, running_in_kitty + appname, beam_cursor_data_file, config_dir, glfw_path, is_macos, + is_wayland, kitty_exe, logo_png_file, running_in_kitty, runtime_dir, + ssh_control_master_template ) from .fast_data_types import ( GLFW_IBEAM_CURSOR, GLFW_MOD_ALT, GLFW_MOD_SHIFT, create_os_window, @@ -349,7 +350,7 @@ def cleanup_ssh_control_masters() -> None: import glob import subprocess try: - files = glob.glob(os.path.join(cache_dir(), 'ssh', f'{os.getpid()}-master-*')) + files = glob.glob(os.path.join(runtime_dir(), ssh_control_master_template.format(kitty_pid=os.getpid(), ssh_placeholder='*'))) except OSError: return for x in files: