commit 128428d912e2c91b7ae7f4d037f9e05cca771109 Author: Benda Xu Date: Thu Aug 9 17:00:37 2012 +0900 introduce a mechanism to start service via runit 1. separate background and foreground execution modes 2. split start-stop-daemon template into an addon, add runit along with it 3. init script can just set command{,_args}, arg_{foreground,background} and use the default start and stop defined in the addons. 4. command_foreground does not need to be set when $command starts foreground by default. Same applies to command_background. diff --git a/Makefile b/Makefile index 119a27e..1438353 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ include Makefile.inc -SUBDIR= conf.d etc init.d local.d man scripts sh src sysctl.d +SUBDIR= conf.d etc init.d local.d man scripts sh src sysctl.d addon # Build pkgconfig or not MKPKGCONFIG?= yes diff --git a/addon/Makefile b/addon/Makefile new file mode 100644 index 0000000..5ab8c49 --- /dev/null +++ b/addon/Makefile @@ -0,0 +1,9 @@ +DIR= ${LIBEXECDIR}/addons +INC= runit.sh ssdaemon.sh + +MK= ../mk + +include ${MK}/scripts.mk + +check test:: + ./runtests.sh diff --git a/addon/runit.sh b/addon/runit.sh new file mode 100644 index 0000000..a3a511f --- /dev/null +++ b/addon/runit.sh @@ -0,0 +1,59 @@ +# Copyright (c) 2014 Benda Xu +# Released under the 2-clause BSD license. + +RUNIT_DIR="${EPREFIX}/var/runit/${RC_SVCNAME}" + +status() +{ + eval sv status "${RUNIT_DIR}" +} + +reload() +{ + eval sv force-reload "${RUNIT_DIR}" +} + +stop() +{ + ebegin "stopping ${RC_SVCNAME} via runit" + eval sv stop "${RUNIT_DIR}" + eend $? +} + +start() +{ + ebegin "starting ${RC_SVCNAME} via runit" + + if [ ! -x "${RUNIT_DIR}/run" ]; then + [ -n "$arg_foreground" ] && command_args="$command_args $arg_foreground" + [ -d "${RUNIT_DIR}" ] || mkdir -p "${RUNIT_DIR}" + touch "${RUNIT_DIR}/down" + [ -d "${RUNIT_DIR}/log/main" ] || mkdir -p "${RUNIT_DIR}/log/main" + cat > "${RUNIT_DIR}/log/run" << EOF +#!/bin/sh +exec svlogd -tt ${RUNIT_DIR}/log/main +EOF + chmod +x "${RUNIT_DIR}/log/run" + + command_prefixed=$command + [ -n "$command_user" ] && command_prefixed="chpst -u $command_user $command_prefixed" + [ -n "$command_env" ] && command_prefixed="env $command_env $command_prefixed" + cat > "${RUNIT_DIR}/run" << EOF +#!/bin/sh +exec 2>&1 +exec $command_prefixed $command_args +EOF + chmod +x "${RUNIT_DIR}/run" + # from manpage of runsvdir: At least every + # five seconds runsvdir checks whether the + # time of last modification, the inode, or the + # device, of the services directory dir has + # changed. So we wait for 5 seconds here. It + # would be cooler if we can signal runsvdir + # such change. Better if we can control runsv + # via runsvdir. + ebegin "set up ${RUNIT_DIR}, wait for 5 seconds for runsvdir to pick it up and spawn runsv" + sleep 5 + eend $? + fi +} diff --git a/addon/runtests.sh b/addon/runtests.sh new file mode 100755 index 0000000..2a8f857 --- /dev/null +++ b/addon/runtests.sh @@ -0,0 +1,6 @@ +#!/bin/sh +for f in ./*.sh; do + [ $f = ./runtests.sh ] && continue + . $f || exit 1 +done +exit 0 diff --git a/addon/ssdaemon.sh b/addon/ssdaemon.sh new file mode 100644 index 0000000..5fdf0cb --- /dev/null +++ b/addon/ssdaemon.sh @@ -0,0 +1,67 @@ +# Template start / stop / status functions +start() +{ + [ -n "$command" ] || return 0 + [ -n "$arg_background" ] && command_args="$command_args $arg_background" + + local _background= + ebegin "Starting ${name:-$RC_SVCNAME}" + if yesno "${command_background}"; then + if [ -z "${pidfile}" ]; then + eend 1 "command_background option used but no pidfile specified" + return 1 + fi + _background="--background --make-pidfile" + fi + if yesno "$start_inactive"; then + local _inactive=false + service_inactive && _inactive=true + mark_service_inactive + fi + eval start-stop-daemon --start \ + --exec $command \ + ${procname:+--name} $procname \ + ${pidfile:+--pidfile} $pidfile \ + ${command_user+--user} $command_user \ + ${command_env+--env} "$command_env" \ + ${start_wait+--wait} "$start_wait" \ + $_background $start_stop_daemon_args \ + -- $command_args + if eend $? "Failed to start $RC_SVCNAME"; then + service_set_value "command" "${command}" + [ -n "${pidfile}" ] && service_set_value "pidfile" "${pidfile}" + [ -n "${procname}" ] && service_set_value "procname" "${procname}" + return 0 + fi + if yesno "$start_inactive"; then + if ! $_inactive; then + mark_service_stopped + fi + fi + return 1 +} + +stop() +{ + local startcommand="$(service_get_value "command")" + local startpidfile="$(service_get_value "pidfile")" + local startprocname="$(service_get_value "procname")" + command="${startcommand:-$command}" + pidfile="${startpidfile:-$pidfile}" + procname="${startprocname:-$procname}" + [ -n "$command" -o -n "$procname" -o -n "$pidfile" ] || return 0 + ebegin "Stopping ${name:-$RC_SVCNAME}" + start-stop-daemon --stop \ + ${retry:+--retry} $retry \ + ${command:+--exec} $command \ + ${procname:+--name} $procname \ + ${pidfile:+--pidfile} $pidfile \ + ${stopsig:+--signal} $stopsig + + eend $? "Failed to stop $RC_SVCNAME" +} + +status() +{ + _status +} diff --git a/sh/runscript.sh.in b/sh/runscript.sh.in index ceb9ab4..1bff553 100644 --- a/sh/runscript.sh.in +++ b/sh/runscript.sh.in @@ -122,68 +122,6 @@ _status() fi } -# Template start / stop / status functions -start() -{ - [ -n "$command" ] || return 0 - local _background= - ebegin "Starting ${name:-$RC_SVCNAME}" - if yesno "${command_background}"; then - if [ -z "${pidfile}" ]; then - eend 1 "command_background option used but no pidfile specified" - return 1 - fi - _background="--background --make-pidfile" - fi - if yesno "$start_inactive"; then - local _inactive=false - service_inactive && _inactive=true - mark_service_inactive - fi - eval start-stop-daemon --start \ - --exec $command \ - ${procname:+--name} $procname \ - ${pidfile:+--pidfile} $pidfile \ - $_background $start_stop_daemon_args \ - -- $command_args - if eend $? "Failed to start $RC_SVCNAME"; then - service_set_value "command" "${command}" - [ -n "${pidfile}" ] && service_set_value "pidfile" "${pidfile}" - [ -n "${procname}" ] && service_set_value "procname" "${procname}" - return 0 - fi - if yesno "$start_inactive"; then - if ! $_inactive; then - mark_service_stopped - fi - fi - return 1 -} - -stop() -{ - local startcommand="$(service_get_value "command")" - local startpidfile="$(service_get_value "pidfile")" - local startprocname="$(service_get_value "procname")" - command="${startcommand:-$command}" - pidfile="${startpidfile:-$pidfile}" - procname="${startprocname:-$procname}" - [ -n "$command" -o -n "$procname" -o -n "$pidfile" ] || return 0 - ebegin "Stopping ${name:-$RC_SVCNAME}" - start-stop-daemon --stop \ - ${retry:+--retry} $retry \ - ${command:+--exec} $command \ - ${procname:+--name} $procname \ - ${pidfile:+--pidfile} $pidfile \ - ${stopsig:+--signal} $stopsig - eend $? "Failed to stop $RC_SVCNAME" -} - -status() -{ - _status -} - yesno $RC_DEBUG && set -x _conf_d=${RC_SERVICE%/*}/../conf.d @@ -205,6 +143,13 @@ unset _conf_d # Load any system overrides sourcex -e "@SYSCONFDIR@/rc.conf" +# load a service controller +if [ -n $VIA ] && has_addon $VIA; then + import_addon $VIA +else + import_addon ssdaemon +fi + # Apply any ulimit defined [ -n "${rc_ulimit:-$RC_ULIMIT}" ] && ulimit ${rc_ulimit:-$RC_ULIMIT}