base-files: configure LED when added
[openwrt/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 restart
21 }
22
23 restart() {
24 trap '' TERM
25 stop "$@"
26 trap - TERM
27 start "$@"
28 }
29
30 boot() {
31 start "$@"
32 }
33
34 shutdown() {
35 stop
36 }
37
38 disable() {
39 name="$(basename "${initscript}")"
40 rm -f "$IPKG_INSTROOT"/etc/rc.d/S??$name
41 rm -f "$IPKG_INSTROOT"/etc/rc.d/K??$name
42 }
43
44 enable() {
45 err=1
46 name="$(basename "${initscript}")"
47 [ "$START" ] && \
48 ln -sf "../init.d/$name" "$IPKG_INSTROOT/etc/rc.d/S${START}${name##S[0-9][0-9]}" && \
49 err=0
50 [ "$STOP" ] && \
51 ln -sf "../init.d/$name" "$IPKG_INSTROOT/etc/rc.d/K${STOP}${name##K[0-9][0-9]}" && \
52 err=0
53 return $err
54 }
55
56 enabled() {
57 name="$(basename "${initscript}")"
58 name="${name##[SK][0-9][0-9]}"
59 {
60 [ -z "${START:-}" ] || [ -L "$IPKG_INSTROOT/etc/rc.d/S${START}$name" ]
61 } && {
62 [ -z "${STOP:-}" ] || [ -L "$IPKG_INSTROOT/etc/rc.d/K${STOP}$name" ]
63 }
64 }
65
66 depends() {
67 return 0
68 }
69
70 ALL_HELP=""
71 ALL_COMMANDS="boot shutdown depends"
72 extra_command() {
73 local cmd="$1"
74 local help="$2"
75
76 local extra="$(printf "%-16s%s" "${cmd}" "${help}")"
77 ALL_HELP="${ALL_HELP}\t${extra}\n"
78 ALL_COMMANDS="${ALL_COMMANDS} ${cmd}"
79 }
80
81 help() {
82 cat <<EOF
83 Syntax: $initscript [command]
84
85 Available commands:
86 EOF
87 echo -e "$ALL_HELP"
88 }
89
90 # for procd
91 start_service() {
92 return 0
93 }
94
95 stop_service() {
96 return 0
97 }
98
99 service_triggers() {
100 return 0
101 }
102
103 service_data() {
104 return 0
105 }
106
107 service_running() {
108 local instance="${1:-*}"
109
110 procd_running "$(basename $initscript)" "$instance"
111 }
112
113 ${INIT_TRACE:+set -x}
114
115 extra_command "start" "Start the service"
116 extra_command "stop" "Stop the service"
117 extra_command "restart" "Restart the service"
118 extra_command "reload" "Reload configuration files (or restart if service does not implement reload)"
119 extra_command "enable" "Enable service autostart"
120 extra_command "disable" "Disable service autostart"
121 extra_command "enabled" "Check if service is started on boot"
122
123 . "$initscript"
124
125 [ -n "$USE_PROCD" ] && {
126 extra_command "running" "Check if service is running"
127 extra_command "status" "Service status"
128 extra_command "trace" "Start with syscall trace"
129 extra_command "info" "Dump procd service info"
130
131 . $IPKG_INSTROOT/lib/functions/procd.sh
132 basescript=$(readlink "$initscript")
133 rc_procd() {
134 local method="set"
135 [ -n "$2" ] && method="add"
136 procd_open_service "$(basename ${basescript:-$initscript})" "$initscript"
137 "$@"
138 procd_close_service "$method"
139 }
140
141 start() {
142 rc_procd start_service "$@"
143 if eval "type service_started" 2>/dev/null >/dev/null; then
144 service_started
145 fi
146 }
147
148 trace() {
149 TRACE_SYSCALLS=1
150 start "$@"
151 }
152
153 info() {
154 json_init
155 json_add_string name "$(basename ${basescript:-$initscript})"
156 json_add_boolean verbose "1"
157 _procd_ubus_call list
158 }
159
160 stop() {
161 procd_lock
162 stop_service "$@"
163 procd_kill "$(basename ${basescript:-$initscript})" "$1"
164 if eval "type service_stopped" 2>/dev/null >/dev/null; then
165 service_stopped
166 fi
167 }
168
169 reload() {
170 if eval "type reload_service" 2>/dev/null >/dev/null; then
171 procd_lock
172 reload_service "$@"
173 else
174 start
175 fi
176 }
177
178 running() {
179 service_running "$@"
180 }
181
182 status() {
183 if eval "type status_service" 2>/dev/null >/dev/null; then
184 status_service "$@"
185 else
186 _procd_status "$(basename ${basescript:-$initscript})" "$1"
187 fi
188 }
189 }
190
191 ALL_COMMANDS="${ALL_COMMANDS} ${EXTRA_COMMANDS}"
192 ALL_HELP="${ALL_HELP}${EXTRA_HELP}"
193 list_contains ALL_COMMANDS "$action" || action=help
194 $action "$@"