From b968741db7c1cdc6e3d4489f0991c0c51278aad9 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Sun, 26 May 2024 15:11:45 +1000 Subject: [bash] ensure xdg_runtime_dir https://unix.stackexchange.com/questions/476963/how-to-create-delete-xdg-runtime-dir-at-login-logoff --- misc/.bashrc | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/misc/.bashrc b/misc/.bashrc index 115dab5..95079eb 100644 --- a/misc/.bashrc +++ b/misc/.bashrc @@ -208,6 +208,26 @@ export QT_IM_MODULE=ibus #fix small vlc interface export QT_AUTO_SCREEN_SCALE_FACTOR=0 +# ensure $XDG_RUNTIME_DIR is set +if [ -z "$XDG_RUNTIME_DIR" ]; then # It's not already set + XDG_RUNTIME_DIR=/run/user/$UID # Try systemd created path + if [ ! -d "$XDG_RUNTIME_DIR" ]; then + # systemd-created directory doesn't exist + XDG_RUNTIME_DIR=/tmp/$USER-runtime + if [ ! -d "$XDG_RUNTIME_DIR" ]; then # Doesn't already exist + mkdir -m 0700 "$XDG_RUNTIME_DIR" + fi + fi +fi +# Check dir has got the correct type, ownership, and permissions +if ! [[ -d "$XDG_RUNTIME_DIR" && -O "$XDG_RUNTIME_DIR" && + "$(stat -c '%a' "$XDG_RUNTIME_DIR")" = 700 ]]; then + echo "\$XDG_RUNTIME_DIR: permissions problem with $XDG_RUNTIME_DIR:" >&2 + ls -ld "$XDG_RUNTIME_DIR" >&2 + XDG_RUNTIME_DIR=$(mktemp -d /tmp/"$USER"-runtime-XXXXXX) + echo "Set \$XDG_RUNTIME_DIR=$XDG_RUNTIME_DIR" >&2 +fi + # start the agent automatically and make sure that only one # ssh-agent process runs at a time if ! pgrep -u "$USER" ssh-agent > /dev/null; then -- cgit v1.2.3