From ce1b4011d409997b10ca205f459cedcb6b292bd7 Mon Sep 17 00:00:00 2001 From: salmon Date: Sun, 9 Aug 2026 23:53:47 +0200 Subject: Make daemon socket/PID paths environment-independent; fix rc service HOME --- rsplayer.rc | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) (limited to 'rsplayer.rc') diff --git a/rsplayer.rc b/rsplayer.rc index 33a46b9..706387b 100755 --- a/rsplayer.rc +++ b/rsplayer.rc @@ -14,11 +14,10 @@ # rsplayer_group="wheel" # group of that user # # rsplayer_user must be a regular user: the daemon socket and PID file live -# in rsplayer_runtime_dir (default /tmp/rsplayer, matching the daemon's -# fallback when XDG_RUNTIME_DIR is unset) and the `rsp` CLI connects to that -# socket, so the daemon and the CLI must run as the same user. -# -# Optional: rsplayer_runtime_dir="/tmp/rsplayer" +# in that user's XDG state directory (~/.local/state/rsplayer) and the `rsp` +# CLI connects to that socket, so the daemon and the CLI must run as the +# same user. This also means the socket path no longer depends on the +# environment (XDG_RUNTIME_DIR), so the service and manual runs agree. # PROVIDE: rsplayer # REQUIRE: LOGIN dbus @@ -33,11 +32,21 @@ load_rc_config $name : ${rsplayer_user:="root"} : ${rsplayer_group:="wheel"} -: ${rsplayer_runtime_dir:="/tmp/rsplayer"} + +# daemon(8) does not set HOME when dropping privileges, and rsp derives its +# config and runtime paths from it, so resolve the user's home explicitly. +rsplayer_home=$(pw usershow "${rsplayer_user}" 2>/dev/null | awk -F: '{print $9}') +if [ -z "${rsplayer_home}" ]; then + echo "Cannot determine home directory for user '${rsplayer_user}'." + echo "Set rsplayer_user in /etc/rc.conf." + exit 1 +fi command="/usr/local/bin/rsp" procname="${command}" -pidfile="${rsplayer_runtime_dir}/rsplayer.pid" +state_dir="${rsplayer_home}/.local/state/rsplayer" +pidfile="${state_dir}/rsplayer.pid" +socket="${state_dir}/daemon.sock" start_cmd="rsplayer_start" stop_cmd="rsplayer_stop" @@ -54,19 +63,23 @@ rsplayer_start() echo " connect to the daemon socket. Set rsplayer_user in rc.conf." fi - install -d -o "${rsplayer_user}" -g "${rsplayer_group}" -m 700 "${rsplayer_runtime_dir}" - echo "Starting rsplayer as ${rsplayer_user}." - /usr/sbin/daemon -u "${rsplayer_user}" -o "${rsplayer_runtime_dir}/rsplayer.log" \ + echo "Starting rsplayer as ${rsplayer_user} (HOME=${rsplayer_home})." + /usr/bin/env HOME="${rsplayer_home}" \ + /usr/sbin/daemon -u "${rsplayer_user}" -o "${state_dir}/rsplayer.log" \ "${command}" start-daemon } rsplayer_stop() { echo "Stopping rsplayer." - if ! su -m "${rsplayer_user}" -c "${command} stop-daemon"; then - echo "rsplayer is not running; cleaning up stale files." - rm -f "${pidfile}" "${rsplayer_runtime_dir}/daemon.sock" + if ! su -m "${rsplayer_user}" -c "env HOME='${rsplayer_home}' ${command} stop-daemon"; then + # stop-daemon could not reach the daemon (stale/missing PID file or + # socket, e.g. a daemon started under an older runtime dir): kill any + # leftover rsp start-daemon processes for this user. + echo "Graceful stop failed; killing leftover daemons." + pkill -u "${rsplayer_user}" -f "${command} start-daemon" 2>/dev/null || true fi + rm -f "${pidfile}" "${socket}" } run_rc_command "$1" -- cgit v1.2.3