#!/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 </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