diff --git a/etc/rc.conf.in b/etc/rc.conf.in index 546c36e..10f4919 100644 --- a/etc/rc.conf.in +++ b/etc/rc.conf.in @@ -73,7 +73,13 @@ #rc_crashed_stop=NO #rc_crashed_start=YES -# Set rc_nocolor to yes if you do not want colors displayed in OpenRC +# Set rc_supervisor to use a program to monitor your daemons and restart +# them when they crash. +# Leaving this undefined uses start-stop-daemon, which is OpenRC's +# default. +#rc_supervisor="" + +# Set rc_nocolor to yes if you do not want colors displayed in OpenRc # output. #rc_nocolor=NO diff --git a/man/openrc-run.8 b/man/openrc-run.8 index c330e8c..375aa99 100644 --- a/man/openrc-run.8 +++ b/man/openrc-run.8 @@ -115,11 +115,24 @@ Daemon to start or stop via if no start or stop function is defined by the service. .It Ar command_args List of arguments to pass to the daemon when starting. +.It Ar command_args_background +List of arguments, if the deamon has them, that tells the daemon to +background itself. This should be used if the daemon's default mode is +to run in the foreground, but it has an option to put it in the +background. This should not be used along with command_background below. +.It Ar command_args_foreground +List of arguments, if the deamon has them, that tells the daemon to +run in the foreground. This should be used to force a daemon that +normally runs in the background to stay in the foreground. .It Ar command_background -Set this to "true", "yes" or "1" (case-insensitive) to force the daemon into -the background. This implies the "--make-pidfile" and "--pidfile" option of +Set this to "true", "yes" or "1" (case-insensitive) if you want +.Xr start-stop-daemon 8 +to force the daemon into the background. This implies the "--make-pidfile" +and "--pidfile" option of .Xr start-stop-daemon 8 so the pidfile variable must be set. +.It Ar command_user +The user and group privileges to switch to before running the daemon. .It Ar pidfile Pidfile to use for the above defined command. .It Ar name diff --git a/sh/Makefile b/sh/Makefile index c1953f3..104dc8d 100644 --- a/sh/Makefile +++ b/sh/Makefile @@ -1,7 +1,8 @@ DIR= ${LIBEXECDIR}/sh SRCS= init.sh.in functions.sh.in gendepends.sh.in \ rc-functions.sh.in runscript.sh.in tmpfiles.sh.in ${SRCS-${OS}} -INC= rc-mount.sh functions.sh rc-functions.sh +INC= rc-mount.sh functions.sh rc-functions.sh runit.sh \ + start-stop-daemon.sh BIN= gendepends.sh init.sh runscript.sh tmpfiles.sh ${BIN-${OS}} INSTALLAFTER= _installafter diff --git a/sh/runit.sh b/sh/runit.sh new file mode 100644 index 0000000..af890c3 --- /dev/null +++ b/sh/runit.sh @@ -0,0 +1,48 @@ +# Copyright (c) 2014 Benda Xu +# Released under the 2-clause BSD license. + +sv_dir="${RC_SVCDIR}/sv/${RC_SVCNAME}" + +make_sv_dir() +{ + [ -d "${sv_dir}" ] || mkdir -p "${sv_dir}" + if [ ! -x "${sv_dir}/run" ]; then + [ -n "$command_user" ] && command_prefix="chpst -u $command_user" + cat > "${sv_dir}/run" << EOF +#!/bin/sh +exec 2>&1 +exec $command_prefix $command $command_args $command_args_foreground +EOF + chmod +x "${sv_dir}/run" + fi + + [ -d "${sv_dir}/log/main" ] || mkdir -p "${sv_dir}/log/main" + if [ ! -X $sv_dir/log/run ]; then + cat > "${sv_dir}/log/run" << EOF +#!/bin/sh +exec svlogd -tt ${sv_dir}/log/main +EOF + chmod +x "${sv_dir}/log/run" + fi +} + +start() +{ + make_sv_dir + ebegin "starting ${RC_SVCNAME} via runit" + ln -s "${sv_dir}" "${RC_SVCDIR}/runit/${RC_SVCNAME}" + # Some how we need to know that the service is active + eend $? +} + +stop() +{ + ebegin "stopping ${RC_SVCNAME} via runit" + rm "${RC_SVCDIR}/runit/${RC_SVCNAME}" + eend $? +} + +status() +{ + sv status "${RC_SVCDIR}/runit" +} diff --git a/sh/runscript.sh.in b/sh/runscript.sh.in index ceb9ab4..1fa21e9 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,15 @@ unset _conf_d # Load any system overrides sourcex -e "@SYSCONFDIR@/rc.conf" +# load a service supervisor +sourcex "@LIBEXECDIR@/sh/start-stop-daemon.sh" +if [ -n "$rc_supervisor" ]; then + if ! sourcex -e "@LIBEXECDIR@/sh/${rc_supervisor}.sh"; then + ewarn "$rc_supervisor is an invalid value for rc_supervisor" + ewarn "Using the default." + fi +fi + # Apply any ulimit defined [ -n "${rc_ulimit:-$RC_ULIMIT}" ] && ulimit ${rc_ulimit:-$RC_ULIMIT} diff --git a/sh/start-stop-daemon.sh b/sh/start-stop-daemon.sh new file mode 100644 index 0000000..aae6792 --- /dev/null +++ b/sh/start-stop-daemon.sh @@ -0,0 +1,71 @@ +# Default start / stop / status functions +# Copyright (c) 2007-2009 Roy Marples +# Released under the 2-clause BSD license. + +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 + if [ -n "${command_args_background}" ]; then + eend 1 "command_background used with command_args_background" + 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 \ + $_background $start_stop_daemon_args \ + -- $command_args $command_args_background + 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 +}