From 171da615c4d8d51ed5f4f2e445d6e19fc15c6f9a Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Mon, 02 Jun 2014 20:01:51 -0400 Subject: /etc/rc.common: New file. --- diff --git a/changelog b/changelog index f10ff05..6b3611b 100644 --- a/changelog +++ b/changelog @@ -1,3 +1,10 @@ +busybox (1.21.1-5) trunk + + * /etc/rc.common: New framework for service initialization scripts. + * Convert service scripts to use the new framework. + + -- "P. J. McDermott" Mon, 02 Jun 2014 19:58:21 -0400 + busybox (1.21.1-4) trunk * /etc/init.d/httpd: Add missing space in start message. diff --git a/src.etc/rc.common b/src.etc/rc.common new file mode 100644 index 0000000..344e3e7 --- /dev/null +++ b/src.etc/rc.common @@ -0,0 +1,96 @@ +#!/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='' +LOG_MSG='' +EXTRA_COMMANDS='' + +main() +{ + local action="${2}" + local all_cmds= + local es= + + SCRIPT="${1}" + + . "${SCRIPT}" + + all_cmds=" start stop restart ${EXTRA_COMMANDS} " + if [ "x${all_cmds#* ${action} }" != "x${all_cmds}" ]; then + ${action} + es=${?} + log_end ${es} + return ${es} + else + usage + return 1 + fi +} + +tty_printf() +{ + local tty="$(tty)" && printf "${@}" >"${tty}" +} + +usage() { + local cmd= + + tty_printf 'Usage: %s {start|stop|restart' "${SCRIPT}" + if [ "x${EXTRA_COMMANDS:+set}" = 'xset' ]; then + tty_printf '|%s' ${EXTRA_COMMANDS} + fi + tty_printf '}\n' +} + +log() +{ + local fmt="${1}" + shift 1 + + LOG_MSG="$(printf "${fmt}" "${@}")" + tty_printf '[ ] %s...\r' "${LOG_MSG}" +} + +log_end() +{ + local es="${1}" + + case ${es} in + 0) + tty_printf '[ ok ] %s... done.\n' \ + "${LOG_MSG}" + ;; + 255) + tty_printf '[warn] %s... done.\n' \ + "${LOG_MSG}" + ;; + *) + tty_printf '[fail] %s... failed.\n' \ + "${LOG_MSG}" + ;; + esac +} + +# Default function definitions + +stop() +{ + : +} + +restart() +{ + stop + start +} + +main "${@}" -- cgit v0.9.1