diff options
| author | salmon <salmonik@tuta.io> | 2026-08-09 23:53:47 +0200 |
|---|---|---|
| committer | salmon <salmonik@tuta.io> | 2026-08-09 23:53:47 +0200 |
| commit | ce1b4011d409997b10ca205f459cedcb6b292bd7 (patch) | |
| tree | bbfd1572f0d6d3a30478bbfb613a09df0022940b /rsplayer.rc | |
| parent | Make install.sh POSIX sh so it runs without bash (diff) | |
| download | rsplayer-ce1b4011d409997b10ca205f459cedcb6b292bd7.tar.gz rsplayer-ce1b4011d409997b10ca205f459cedcb6b292bd7.zip | |
Make daemon socket/PID paths environment-independent; fix rc service HOME
Diffstat (limited to '')
| -rwxr-xr-x | rsplayer.rc | 39 |
1 files changed, 26 insertions, 13 deletions
diff --git a/rsplayer.rc b/rsplayer.rc index 33a46b9..706387b 100755 --- a/rsplayer.rc +++ b/rsplayer.rc | |||
| @@ -14,11 +14,10 @@ | |||
| 14 | # rsplayer_group="wheel" # group of that user | 14 | # rsplayer_group="wheel" # group of that user |
| 15 | # | 15 | # |
| 16 | # rsplayer_user must be a regular user: the daemon socket and PID file live | 16 | # rsplayer_user must be a regular user: the daemon socket and PID file live |
| 17 | # in rsplayer_runtime_dir (default /tmp/rsplayer, matching the daemon's | 17 | # in that user's XDG state directory (~/.local/state/rsplayer) and the `rsp` |
| 18 | # fallback when XDG_RUNTIME_DIR is unset) and the `rsp` CLI connects to that | 18 | # CLI connects to that socket, so the daemon and the CLI must run as the |
| 19 | # socket, so the daemon and the CLI must run as the same user. | 19 | # same user. This also means the socket path no longer depends on the |
| 20 | # | 20 | # environment (XDG_RUNTIME_DIR), so the service and manual runs agree. |
| 21 | # Optional: rsplayer_runtime_dir="/tmp/rsplayer" | ||
| 22 | 21 | ||
| 23 | # PROVIDE: rsplayer | 22 | # PROVIDE: rsplayer |
| 24 | # REQUIRE: LOGIN dbus | 23 | # REQUIRE: LOGIN dbus |
| @@ -33,11 +32,21 @@ load_rc_config $name | |||
| 33 | 32 | ||
| 34 | : ${rsplayer_user:="root"} | 33 | : ${rsplayer_user:="root"} |
| 35 | : ${rsplayer_group:="wheel"} | 34 | : ${rsplayer_group:="wheel"} |
| 36 | : ${rsplayer_runtime_dir:="/tmp/rsplayer"} | 35 | |
| 36 | # daemon(8) does not set HOME when dropping privileges, and rsp derives its | ||
| 37 | # config and runtime paths from it, so resolve the user's home explicitly. | ||
| 38 | rsplayer_home=$(pw usershow "${rsplayer_user}" 2>/dev/null | awk -F: '{print $9}') | ||
| 39 | if [ -z "${rsplayer_home}" ]; then | ||
| 40 | echo "Cannot determine home directory for user '${rsplayer_user}'." | ||
| 41 | echo "Set rsplayer_user in /etc/rc.conf." | ||
| 42 | exit 1 | ||
| 43 | fi | ||
| 37 | 44 | ||
| 38 | command="/usr/local/bin/rsp" | 45 | command="/usr/local/bin/rsp" |
| 39 | procname="${command}" | 46 | procname="${command}" |
| 40 | pidfile="${rsplayer_runtime_dir}/rsplayer.pid" | 47 | state_dir="${rsplayer_home}/.local/state/rsplayer" |
| 48 | pidfile="${state_dir}/rsplayer.pid" | ||
| 49 | socket="${state_dir}/daemon.sock" | ||
| 41 | 50 | ||
| 42 | start_cmd="rsplayer_start" | 51 | start_cmd="rsplayer_start" |
| 43 | stop_cmd="rsplayer_stop" | 52 | stop_cmd="rsplayer_stop" |
| @@ -54,19 +63,23 @@ rsplayer_start() | |||
| 54 | echo " connect to the daemon socket. Set rsplayer_user in rc.conf." | 63 | echo " connect to the daemon socket. Set rsplayer_user in rc.conf." |
| 55 | fi | 64 | fi |
| 56 | 65 | ||
| 57 | install -d -o "${rsplayer_user}" -g "${rsplayer_group}" -m 700 "${rsplayer_runtime_dir}" | 66 | echo "Starting rsplayer as ${rsplayer_user} (HOME=${rsplayer_home})." |
| 58 | echo "Starting rsplayer as ${rsplayer_user}." | 67 | /usr/bin/env HOME="${rsplayer_home}" \ |
| 59 | /usr/sbin/daemon -u "${rsplayer_user}" -o "${rsplayer_runtime_dir}/rsplayer.log" \ | 68 | /usr/sbin/daemon -u "${rsplayer_user}" -o "${state_dir}/rsplayer.log" \ |
| 60 | "${command}" start-daemon | 69 | "${command}" start-daemon |
| 61 | } | 70 | } |
| 62 | 71 | ||
| 63 | rsplayer_stop() | 72 | rsplayer_stop() |
| 64 | { | 73 | { |
| 65 | echo "Stopping rsplayer." | 74 | echo "Stopping rsplayer." |
| 66 | if ! su -m "${rsplayer_user}" -c "${command} stop-daemon"; then | 75 | if ! su -m "${rsplayer_user}" -c "env HOME='${rsplayer_home}' ${command} stop-daemon"; then |
| 67 | echo "rsplayer is not running; cleaning up stale files." | 76 | # stop-daemon could not reach the daemon (stale/missing PID file or |
| 68 | rm -f "${pidfile}" "${rsplayer_runtime_dir}/daemon.sock" | 77 | # socket, e.g. a daemon started under an older runtime dir): kill any |
| 78 | # leftover rsp start-daemon processes for this user. | ||
| 79 | echo "Graceful stop failed; killing leftover daemons." | ||
| 80 | pkill -u "${rsplayer_user}" -f "${command} start-daemon" 2>/dev/null || true | ||
| 69 | fi | 81 | fi |
| 82 | rm -f "${pidfile}" "${socket}" | ||
| 70 | } | 83 | } |
| 71 | 84 | ||
| 72 | run_rc_command "$1" | 85 | run_rc_command "$1" |
