diff options
author | P. J. McDermott <pj@pehjota.net> | 2014-06-02 20:01:51 (EDT) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2014-06-02 20:01:51 (EDT) |
commit | 171da615c4d8d51ed5f4f2e445d6e19fc15c6f9a (patch) | |
tree | fc9a00edbc2afaa4ead55d668f3ac0bf5230176f | |
parent | 79d474a5fdc0ea8b9e05f5b68567dbbe9086e4ca (diff) |
/etc/rc.common: New file.
-rw-r--r-- | changelog | 7 | ||||
-rw-r--r-- | src.etc/rc.common | 96 |
2 files changed, 103 insertions, 0 deletions
@@ -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" <pj@pehjota.net> 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 '[[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 +} + +main "${@}" |