From 923310993291ebb4347982eb0dc998d94b458d0b Mon Sep 17 00:00:00 2001
From: Seungmin Kim <8457324+ehfd@users.noreply.github.com>
Date: Tue, 2 Jul 2024 19:50:31 +0900
Subject: [PATCH] Edit possible race condition

---
 entrypoint.sh                   | 6 +++---
 kasmvnc-entrypoint.sh           | 3 +++
 selkies-gstreamer-entrypoint.sh | 3 +++
 3 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/entrypoint.sh b/entrypoint.sh
index 3e607cc..95a0407 100755
--- a/entrypoint.sh
+++ b/entrypoint.sh
@@ -9,9 +9,9 @@ set -e
 trap "echo TRAPed signal" HUP INT QUIT TERM
 
 # Create and modify permissions of XDG_RUNTIME_DIR
-mkdir -pm700 /tmp/runtime-ubuntu
-chown -f ubuntu:ubuntu /tmp/runtime-ubuntu
-chmod -f 700 /tmp/runtime-ubuntu
+mkdir -pm700 "${XDG_RUNTIME_DIR}"
+chown -f ubuntu:ubuntu "${XDG_RUNTIME_DIR}"
+chmod -f 700 "${XDG_RUNTIME_DIR}"
 # Make user directory owned by the default ubuntu user
 chown ubuntu:ubuntu /home/ubuntu || sudo-root chown ubuntu:ubuntu /home/ubuntu || chown ubuntu:ubuntu /home/ubuntu/* || sudo-root chown ubuntu:ubuntu /home/ubuntu/* || echo 'Failed to change user directory permissions, there may be permission issues'
 # Change operating system password to environment variable
diff --git a/kasmvnc-entrypoint.sh b/kasmvnc-entrypoint.sh
index b9b6356..932d435 100644
--- a/kasmvnc-entrypoint.sh
+++ b/kasmvnc-entrypoint.sh
@@ -15,6 +15,9 @@ export PIPEWIRE_RUNTIME_DIR="${PIPEWIRE_RUNTIME_DIR:-${XDG_RUNTIME_DIR:-/tmp}}"
 export PULSE_RUNTIME_PATH="${PULSE_RUNTIME_PATH:-${XDG_RUNTIME_DIR:-/tmp}/pulse}"
 export PULSE_SERVER="${PULSE_SERVER:-unix:${PULSE_RUNTIME_PATH:-${XDG_RUNTIME_DIR:-/tmp}/pulse}/native}"
 
+# Wait for XDG_RUNTIME_DIR
+until [ -d "${XDG_RUNTIME_DIR}" ]; do sleep 0.5; done
+
 # Configure NGINX
 if [ "$(echo ${SELKIES_ENABLE_BASIC_AUTH} | tr '[:upper:]' '[:lower:]')" != "false" ]; then htpasswd -bcm "${XDG_RUNTIME_DIR}/.htpasswd" "${SELKIES_BASIC_AUTH_USER:-${USER}}" "${SELKIES_BASIC_AUTH_PASSWORD:-${PASSWD}}"; fi
 echo "# Selkies KasmVNC NGINX Configuration
diff --git a/selkies-gstreamer-entrypoint.sh b/selkies-gstreamer-entrypoint.sh
index 438f4fe..26e59f6 100755
--- a/selkies-gstreamer-entrypoint.sh
+++ b/selkies-gstreamer-entrypoint.sh
@@ -15,6 +15,9 @@ export PIPEWIRE_RUNTIME_DIR="${PIPEWIRE_RUNTIME_DIR:-${XDG_RUNTIME_DIR:-/tmp}}"
 export PULSE_RUNTIME_PATH="${PULSE_RUNTIME_PATH:-${XDG_RUNTIME_DIR:-/tmp}/pulse}"
 export PULSE_SERVER="${PULSE_SERVER:-unix:${PULSE_RUNTIME_PATH:-${XDG_RUNTIME_DIR:-/tmp}/pulse}/native}"
 
+# Wait for XDG_RUNTIME_DIR
+until [ -d "${XDG_RUNTIME_DIR}" ]; do sleep 0.5; done
+
 # Configure NGINX
 if [ "$(echo ${SELKIES_ENABLE_BASIC_AUTH} | tr '[:upper:]' '[:lower:]')" != "false" ]; then htpasswd -bcm "${XDG_RUNTIME_DIR}/.htpasswd" "${SELKIES_BASIC_AUTH_USER:-${USER}}" "${SELKIES_BASIC_AUTH_PASSWORD:-${PASSWD}}"; fi
 echo "# Selkies-GStreamer NGINX Configuration