[package] base-files: enable shell tracing in initscripts when INIT_TRACE is set
[openwrt.git] / package / base-files / files / etc / rc.common
1 #!/bin/sh
2 # Copyright (C) 2006-2012 OpenWrt.org
3
4 . $IPKG_INSTROOT/lib/functions.sh
5 . $IPKG_INSTROOT/lib/functions/service.sh
6
7 initscript=$1
8 action=${2:-help}
9 shift 2
10
11 start() {
12         return 0
13 }
14
15 stop() {
16         return 0
17 }
18
19 reload() {
20         return 1
21 }
22
23 restart() {
24         trap '' TERM
25         stop "$@"
26         start "$@"
27 }
28
29 boot() {
30         start "$@"
31 }
32
33 shutdown() {
34         stop
35 }
36
37 disable() {
38         name="$(basename "${initscript}")"
39         rm -f "$IPKG_INSTROOT"/etc/rc.d/S??$name
40         rm -f "$IPKG_INSTROOT"/etc/rc.d/K??$name
41 }
42
43 enable() {
44         name="$(basename "${initscript}")"
45         disable
46         [ -n "$START" -o -n "$STOP" ] || {
47                 echo "/etc/init.d/$name does not have a START or STOP value"
48                 return 1
49         }
50         [ "$START" ] && ln -s "../init.d/$name" "$IPKG_INSTROOT/etc/rc.d/S${START}${name##S[0-9][0-9]}"
51         [ "$STOP"  ] && ln -s "../init.d/$name" "$IPKG_INSTROOT/etc/rc.d/K${STOP}${name##K[0-9][0-9]}"
52 }
53
54 enabled() {
55         name="$(basename "${initscript}")"
56         [ -x "$IPKG_INSTROOT/etc/rc.d/S${START}${name##S[0-9][0-9]}" ]
57 }
58
59 depends() {
60         return 0
61 }
62
63 help() {
64         cat <<EOF
65 Syntax: $initscript [command]
66
67 Available commands:
68         start   Start the service
69         stop    Stop the service
70         restart Restart the service
71         reload  Reload configuration files (or restart if that fails)
72         enable  Enable service autostart
73         disable Disable service autostart
74 $EXTRA_HELP
75 EOF
76 }
77
78 ${INIT_TRACE:+set -x}
79
80 . "$initscript"
81
82 ALL_COMMANDS="start stop reload restart boot shutdown enable disable enabled depends ${EXTRA_COMMANDS}"
83 list_contains ALL_COMMANDS "$action" || action=help
84 [ "$action" = "reload" ] && action='eval reload "$@" || restart "$@" && :'
85 $action "$@"