diff options
Diffstat (limited to 'src.etc/rc.common')
-rw-r--r-- | src.etc/rc.common | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/src.etc/rc.common b/src.etc/rc.common new file mode 100644 index 0000000..7ddc27e --- /dev/null +++ b/src.etc/rc.common @@ -0,0 +1,128 @@ +#!/bin/sh +# +# /etc/rc.common +# Framework for service initialization scripts. +# +# Copyright (C) 2014 Patrick "P. J." McDermott +# +# This file may be reproduced, distributed, modified, and otherwise dealt in +# under the terms of the Expat License. + +set -u + +SCRIPT='' +ALL_CMDS='' +LOG_MSG='' +EXTRA_COMMANDS='' +START='' +STOP='' + +main() +{ + local action= + local es= + + SCRIPT="${1}" + + . "${SCRIPT}" + + ALL_CMDS=" start stop restart ${EXTRA_COMMANDS} " + if [ "x${START:+set}" = 'xset' ] || [ "x${STOP:+set}" = 'xset' ]; then + ALL_CMDS="${ALL_CMDS} enable disable " + fi + + if [ ${#} -ne 2 ]; then + usage + return 1 + fi + action="${2:-<unknown>}" + if [ "x${ALL_CMDS#* ${action} }" != "x${ALL_CMDS}" ]; then + if [ -f /etc/rc.policy ]; then + [ "x$(cat /etc/rc.policy)" = 'xdisabled' ] && return 0 + fi + ${action} + es=${?} + log_end ${es} + return ${es} + else + usage + return 1 + fi +} + +tty_printf() +{ + local tty= + + tty="$(tty)" && printf "${@}" >"${tty}" +} + +usage() { + local cmds= + + cmds="$(printf '%s|' ${ALL_CMDS})" + tty_printf 'Usage: %s {%s}\n' "${SCRIPT}" "${cmds%|}" +} + +log() +{ + local fmt="${1}" + shift 1 + + LOG_MSG="$(printf "${fmt}" "${@}")" + tty_printf '[ ] %s...\r' "${LOG_MSG}" +} + +log_end() +{ + local es="${1}" + + [ "x${LOG_MSG:+set}" = 'xset' ] || return 0 + + case ${es} in + 0) + tty_printf '[[32m ok [39m] %s... done.\n' \ + "${LOG_MSG}" + ;; + 255) + tty_printf '[[33mwarn[39m] %s... done.\n' \ + "${LOG_MSG}" + ;; + *) + tty_printf '[[31mfail[39m] %s... failed.\n' \ + "${LOG_MSG}" + ;; + esac +} + +# Default function definitions + +stop() +{ + : +} + +restart() +{ + stop + start +} + +enable() +{ + local name="${SCRIPT##*/}" + name="${name#[SK][0-9][0-9]}" + [ "x${START:+set}" = 'xset' ] && \ + ln -sf "../init.d/${name}" "/etc/rc.d/S${START}${name}" + [ "x${STOP:+set}" = 'xset' ] && \ + ln -sf "../init.d/${name}" "/etc/rc.d/K${STOP}${name}" +} + +disable() +{ + local name="${SCRIPT##*/}" + name="${name#[SK][0-9][0-9]}" + rm -f /etc/rc.d/[SK][0-9][0-9]"${name}" +} + +main "${@}" |