base-files: run readlink on initscript name before opening services
[openwrt.git] / package / base-files / files / etc / rc.common
index 7ca1078..aeec90c 100755 (executable)
@@ -1,9 +1,12 @@
 #!/bin/sh
-# Copyright (C) 2006 OpenWrt.org
+# Copyright (C) 2006-2012 OpenWrt.org
 
-. $IPKG_INSTROOT/etc/functions.sh
+. $IPKG_INSTROOT/lib/functions.sh
+. $IPKG_INSTROOT/lib/functions/service.sh
 
-START=50
+initscript=$1
+action=${2:-help}
+shift 2
 
 start() {
        return 0
@@ -19,28 +22,33 @@ reload() {
 
 restart() {
        trap '' TERM
-       stop
-       start
+       stop "$@"
+       start "$@"
 }
 
 boot() {
-       start
+       start "$@"
 }
 
 shutdown() {
-       return 0
+       stop
 }
 
 disable() {
        name="$(basename "${initscript}")"
        rm -f "$IPKG_INSTROOT"/etc/rc.d/S??$name
+       rm -f "$IPKG_INSTROOT"/etc/rc.d/K??$name
 }
 
 enable() {
        name="$(basename "${initscript}")"
        disable
-       ln -s "/etc/init.d/$name" "$IPKG_INSTROOT/etc/rc.d/S${START}${name##S[0-9][0-9]}"
-       [ "$STOP" ] && ln -s "/etc/init.d/$name" "$IPKG_INSTROOT/etc/rc.d/K${START}${name##K[0-9][0-9]}"
+       [ -n "$START" -o -n "$STOP" ] || {
+               echo "/etc/init.d/$name does not have a START or STOP value"
+               return 1
+       }
+       [ "$START" ] && ln -s "../init.d/$name" "$IPKG_INSTROOT/etc/rc.d/S${START}${name##S[0-9][0-9]}"
+       [ "$STOP"  ] && ln -s "../init.d/$name" "$IPKG_INSTROOT/etc/rc.d/K${STOP}${name##K[0-9][0-9]}"
 }
 
 enabled() {
@@ -67,25 +75,42 @@ $EXTRA_HELP
 EOF
 }
 
-initscript="$1"
-action="$2"
+# for procd
+start_service() {
+       return 0
+}
+
+stop_service() {
+       return 0
+}
+
+${INIT_TRACE:+set -x}
 
 . "$initscript"
 
-cmds=
-for cmd in $EXTRA_COMMANDS; do
-       cmds="${cmds:+$cmds$N}$cmd) $cmd;;"
-done
-eval "case \"\$action\" in
-       start) start;;
-       stop) stop;;
-       reload) reload || restart;;
-       restart) restart;;
-       boot) boot;;
-       shutdown) shutdown;;
-       enable) enable;;
-       enabled) enabled;;
-       disable) disable;;
-       $cmds
-       *) help;;
-esac"
+[ -n "$USE_PROCD" ] && {
+       . $IPKG_INSTROOT/lib/functions/procd.sh
+       basescript=$(readlink "$initscript")
+       rc_procd() {
+               procd_open_service "$(basename ${basescript:-$initscript})" "$initscript"
+               "$@"
+               procd_close_service
+       }
+
+       start() {
+               rc_procd start_service "$@"
+       }
+
+       stop() {
+               procd_kill "$(basename ${basescript:-$initscript})" "$1"
+       }
+
+       reload() {
+               start
+       }
+}
+
+ALL_COMMANDS="start stop reload restart boot shutdown enable disable enabled depends ${EXTRA_COMMANDS}"
+list_contains ALL_COMMANDS "$action" || action=help
+[ "$action" = "reload" ] && action='eval reload "$@" || restart "$@" && :'
+$action "$@"