base-files: protect stop and reload actions with procd_lock
[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 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 err=1
45 name="$(basename "${initscript}")"
46 [ "$START" ] && \
47 ln -sf "../init.d/$name" "$IPKG_INSTROOT/etc/rc.d/S${START}${name##S[0-9][0-9]}" && \
48 err=0
49 [ "$STOP" ] && \
50 ln -sf "../init.d/$name" "$IPKG_INSTROOT/etc/rc.d/K${STOP}${name##K[0-9][0-9]}" && \
51 err=0
52 return $err
53 }
54
55 enabled() {
56 name="$(basename "${initscript}")"
57 [ -x "$IPKG_INSTROOT/etc/rc.d/S${START}${name##S[0-9][0-9]}" ]
58 }
59
60 depends() {
61 return 0
62 }
63
64 help() {
65 cat <<EOF
66 Syntax: $initscript [command]
67
68 Available commands:
69 start Start the service
70 stop Stop the service
71 restart Restart the service
72 reload Reload configuration files (or restart if service does not implement reload)
73 enable Enable service autostart
74 disable Disable service autostart
75 $EXTRA_HELP
76 EOF
77 }
78
79 # for procd
80 start_service() {
81 return 0
82 }
83
84 stop_service() {
85 return 0
86 }
87
88 service_triggers() {
89 return 0
90 }
91
92 service_data() {
93 return 0
94 }
95
96 service_running() {
97 return 0
98 }
99
100 ${INIT_TRACE:+set -x}
101
102 . "$initscript"
103
104 [ -n "$USE_PROCD" ] && {
105 EXTRA_COMMANDS="${EXTRA_COMMANDS} running trace"
106
107 . $IPKG_INSTROOT/lib/functions/procd.sh
108 basescript=$(readlink "$initscript")
109 rc_procd() {
110 local method="set"
111 [ -n "$2" ] && method="add"
112 procd_open_service "$(basename ${basescript:-$initscript})" "$initscript"
113 "$@"
114 procd_close_service "$method"
115 }
116
117 start() {
118 rc_procd start_service "$@"
119 if eval "type service_started" 2>/dev/null >/dev/null; then
120 service_started
121 fi
122 }
123
124 trace() {
125 TRACE_SYSCALLS=1
126 start "$@"
127 }
128
129 stop() {
130 procd_lock
131 stop_service "$@"
132 procd_kill "$(basename ${basescript:-$initscript})" "$1"
133 }
134
135 reload() {
136 if eval "type reload_service" 2>/dev/null >/dev/null; then
137 procd_lock
138 reload_service "$@"
139 else
140 start
141 fi
142 }
143
144 running() {
145 service_running "$@"
146 }
147 }
148
149 ALL_COMMANDS="start stop reload restart boot shutdown enable disable enabled depends ${EXTRA_COMMANDS}"
150 list_contains ALL_COMMANDS "$action" || action=help
151 $action "$@"