From e5501a315dfcf78f173ce1d208a6e68955af09db Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Sun, 19 Dec 2021 15:12:59 +0900 Subject: [PATCH] main: work around "XDG_RUNTIME_DIR" of a different user by "su" --- ble.pp | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/ble.pp b/ble.pp index cbbc075c..46346285 100644 --- a/ble.pp +++ b/ble.pp @@ -350,15 +350,29 @@ function ble/base/initialize-base-directory { function ble/base/initialize-runtime-directory/.xdg { [[ $_ble_base != */out ]] || return - local runtime_dir=${XDG_RUNTIME_DIR:-/run/user/$UID} - if [[ ! -d $runtime_dir ]]; then - [[ $XDG_RUNTIME_DIR ]] && - echo "ble.sh: XDG_RUNTIME_DIR='$XDG_RUNTIME_DIR' is not a directory." >&2 - return 1 + local runtime_dir= + if [[ $XDG_RUNTIME_DIR ]]; then + if [[ ! -d $XDG_RUNTIME_DIR ]]; then + ble/util/print "ble.sh: XDG_RUNTIME_DIR='$XDG_RUNTIME_DIR' is not a directory." >&2 + return 1 + elif [[ -O $XDG_RUNTIME_DIR ]]; then + runtime_dir=$XDG_RUNTIME_DIR + else + # When XDG_RUNTIME_DIR is not owned by the current user, maybe "su" is + # used to enter this session keeping the environment variables of the + # original user. We just ignore XDG_RUNTIME_DIR (without issueing + # warnings) for such a case. + false + fi + fi + if [[ ! $runtime_dir ]]; then + runtime_dir=/run/user/$UID + [[ -d $runtime_dir && -O $runtime_dir ]] || return 1 fi + if ! [[ -r $runtime_dir && -w $runtime_dir && -x $runtime_dir ]]; then - [[ $XDG_RUNTIME_DIR ]] && - echo "ble.sh: XDG_RUNTIME_DIR='$XDG_RUNTIME_DIR' doesn't have a proper permission." >&2 + [[ $runtime_dir == "$XDG_RUNTIME_DIR" ]] && + ble/util/print "ble.sh: XDG_RUNTIME_DIR='$XDG_RUNTIME_DIR' doesn't have a proper permission." >&2 return 1 fi