| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- #!/bin/sh
- # Install radio-ui as a system service (systemd, sysvinit or OpenRC).
- #
- # sudo tools/install-service.sh [OPTIONS] [-- RADIO-UI ARGS...]
- #
- # The service runs ./radio-ui from this checkout (not a copy), as USER, with
- # the given arguments (default: drm). Build the library first, as yourself:
- # make lib.
- #
- # OPTIONS:
- # -i INIT systemd, sysvinit or openrc (default: the running one)
- # -u USER user the service runs as (default: $SUDO_USER; root refused
- # unless named here). Needs the video, input and i2c groups.
- # -n NAME service name (default: radio-ui)
- # -N do not enable or start the service, only write its file
- # -p print the service file and exit; nothing is installed
- # -r remove the service: stop, disable, delete its file
- # -h this help
- #
- # Examples:
- # sudo tools/install-service.sh
- # sudo tools/install-service.sh -- drm --host radio.lo
- # sudo tools/install-service.sh -- i2c --bus 1
- # sudo tools/install-service.sh -r
- set -eu
- dir=$(cd "$(dirname "$0")/.." && pwd -P)
- prog=$dir/radio-ui
- init= user=${SUDO_USER:-} name=radio-ui activate=1 print=0 remove=0
- while getopts i:u:n:Nprh opt; do
- case $opt in
- i) init=$OPTARG ;;
- u) user=$OPTARG ;;
- n) name=$OPTARG ;;
- N) activate=0 ;;
- p) print=1 ;;
- r) remove=1 ;;
- *) sed -n '2,24s/^# \{0,1\}//p' "$0"; [ "$opt" = h ] && exit 0; exit 2 ;;
- esac
- done
- shift $((OPTIND - 1))
- [ $# -gt 0 ] || set -- drm
- die() { echo "$0: $*" >&2; exit 1; }
- # The arguments go into ExecStart= or a shell script unquoted: keep them plain.
- safe() {
- case $1 in
- ''|*[!A-Za-z0-9._/:=,@+-]*) return 1 ;;
- esac
- }
- for a in "$name" "$dir" "$@"; do
- safe "$a" || die "'$a': only letters, digits and ._/:=,@+- are allowed"
- done
- args=$*
- if [ -z "$init" ]; then
- if [ -d /run/systemd/system ]; then init=systemd
- elif [ -d /run/openrc ] || command -v openrc-run >/dev/null 2>&1; then init=openrc
- else init=sysvinit
- fi
- fi
- case $init in
- systemd) file=/etc/systemd/system/$name.service ;;
- sysvinit|openrc) file=/etc/init.d/$name ;;
- *) die "unknown init system '$init' (systemd, sysvinit, openrc)" ;;
- esac
- log=/var/log/$name.log
- # ---- the service files -------------------------------------------------------
- unit_systemd() {
- cat <<EOF
- [Unit]
- Description=Radio front panel (radio-ui)
- After=network.target sound.target mpd.service
- Wants=mpd.service
- [Service]
- Type=simple
- User=$user
- WorkingDirectory=$dir
- Environment=PYTHONUNBUFFERED=1
- ExecStart=$prog $args
- Restart=on-failure
- RestartSec=5
- [Install]
- WantedBy=multi-user.target
- EOF
- }
- unit_openrc() {
- cat <<EOF
- #!/sbin/openrc-run
- # Radio front panel (radio-ui); written by tools/install-service.sh
- description="Radio front panel (radio-ui)"
- command="$prog"
- command_args="$args"
- command_user="$user"
- command_background=yes
- pidfile="/run/$name.pid"
- directory="$dir"
- output_log="$log"
- error_log="$log"
- export PYTHONUNBUFFERED=1
- depend() {
- need localmount
- after net mpd
- use mpd
- }
- start_pre() {
- checkpath -f -m 0644 -o "$user" "$log"
- }
- EOF
- }
- unit_sysvinit() {
- cat <<EOF
- #!/bin/sh
- ### BEGIN INIT INFO
- # Provides: $name
- # Required-Start: \$local_fs \$remote_fs \$network
- # Required-Stop: \$local_fs \$remote_fs \$network
- # Should-Start: mpd
- # Should-Stop: mpd
- # Default-Start: 2 3 4 5
- # Default-Stop: 0 1 6
- # Short-Description: Radio front panel (radio-ui)
- ### END INIT INFO
- # Written by tools/install-service.sh
- NAME=$name
- DAEMON=$prog
- ARGS="$args"
- USER=$user
- DIR=$dir
- PIDFILE=/run/\$NAME.pid
- LOG=$log
- export PYTHONUNBUFFERED=1
- running() {
- [ -f "\$PIDFILE" ] && kill -0 "\$(cat "\$PIDFILE")" 2>/dev/null
- }
- do_start() {
- if running; then echo "\$NAME already running"; return 0; fi
- touch "\$LOG" && chown "\$USER" "\$LOG"
- start-stop-daemon --start --background --make-pidfile --pidfile "\$PIDFILE" \\
- --chuid "\$USER" --chdir "\$DIR" --startas /bin/sh -- \\
- -c "exec \$DAEMON \$ARGS >>\$LOG 2>&1"
- }
- do_stop() {
- start-stop-daemon --stop --retry TERM/10/KILL/5 --pidfile "\$PIDFILE" --oknodo
- rm -f "\$PIDFILE"
- }
- case "\$1" in
- start) echo "Starting \$NAME"; do_start ;;
- stop) echo "Stopping \$NAME"; do_stop ;;
- restart|force-reload) do_stop; do_start ;;
- status)
- if running; then echo "\$NAME is running"; exit 0
- else echo "\$NAME is not running"; exit 3
- fi ;;
- *) echo "Usage: \$0 {start|stop|restart|force-reload|status}" >&2; exit 2 ;;
- esac
- EOF
- }
- # ---- enable, start, stop, disable -------------------------------------------
- svc() { # start, stop, restart, enable, disable
- case $init in
- systemd)
- case $1 in
- enable) systemctl daemon-reload; systemctl enable "$name" ;;
- disable) systemctl disable "$name" || true; systemctl daemon-reload ;;
- *) systemctl "$1" "$name" ;;
- esac ;;
- openrc)
- case $1 in
- enable) rc-update add "$name" default ;;
- disable) rc-update del "$name" default || true ;;
- *) rc-service "$name" "$1" ;;
- esac ;;
- sysvinit)
- case $1 in
- enable|disable)
- if command -v update-rc.d >/dev/null 2>&1; then
- [ "$1" = enable ] && update-rc.d "$name" defaults ||
- update-rc.d -f "$name" remove
- elif command -v chkconfig >/dev/null 2>&1; then
- [ "$1" = enable ] && chkconfig --add "$name" ||
- chkconfig --del "$name"
- else
- echo "no update-rc.d or chkconfig: $1 $file in the runlevels yourself" >&2
- fi ;;
- *) "$file" "$1" ;;
- esac ;;
- esac
- }
- # ---- main --------------------------------------------------------------------
- [ $print = 1 ] || [ "$(id -u)" = 0 ] || die "needs root: run it with sudo"
- [ -n "$user" ] || [ $print = 0 ] || user=$(id -un)
- if [ $remove = 0 ]; then
- [ -n "$user" ] || die "no user: pass -u USER (root is not used by default)"
- safe "$user" || die "'$user': not a plain user name"
- fi
- if [ $print = 1 ]; then
- "unit_$init"
- exit 0
- fi
- if [ $remove = 1 ]; then
- [ -e "$file" ] || die "$file does not exist"
- svc stop || true
- svc disable
- rm -f "$file"
- [ "$init" != systemd ] || systemctl daemon-reload
- echo "removed $file ($log, if any, is kept)"
- exit 0
- fi
- id "$user" >/dev/null 2>&1 || die "no such user: $user"
- [ -x "$prog" ] || die "$prog is not executable"
- [ -n "${RADIO_PANEL_LIB:-}" ] || [ -e "$dir/build/libradiopanel.so" ] ||
- echo "warning: $dir/build/libradiopanel.so missing; run 'make lib' as $user" >&2
- if [ "$user" != root ]; then
- for g in video input i2c; do
- getent group "$g" >/dev/null 2>&1 || continue
- id -nG "$user" | tr ' ' '\n' | grep -qx "$g" ||
- echo "warning: $user is not in group $g (usermod -aG $g $user)" >&2
- done
- fi
- tmp=$(mktemp "$file.XXXXXX")
- trap 'rm -f "$tmp"' EXIT
- "unit_$init" >"$tmp"
- if [ "$init" = systemd ]; then chmod 0644 "$tmp"; else chmod 0755 "$tmp"; fi
- mv "$tmp" "$file"
- trap - EXIT
- echo "wrote $file ($init): $prog $args, as $user"
- if [ $activate = 1 ]; then
- svc enable
- svc restart
- fi
|