e4e75edb2e13174afa178d12f08bf92576653aac
[openwrt/staging/yousong.git] / package / system / procd / files / procd.sh
1 # procd API:
2 #
3 # procd_open_service(name, [script]):
4 # Initialize a new procd command message containing a service with one or more instances
5 #
6 # procd_close_service()
7 # Send the command message for the service
8 #
9 # procd_open_instance([name]):
10 # Add an instance to the service described by the previous procd_open_service call
11 #
12 # procd_set_param(type, [value...])
13 # Available types:
14 # command: command line (array).
15 # respawn info: array with 3 values $restart_timeout $fail_hreshold $max_fail
16 # env: environment variable (passed to the process)
17 # data: arbitrary name/value pairs for detecting config changes (table)
18 # file: configuration files (array)
19 # netdev: bound network device (detects ifindex changes)
20 # limits: resource limits (passed to the process)
21 #
22 # No space separation is done for arrays/tables - use one function argument per command line argument
23 #
24 # procd_close_instance():
25 # Complete the instance being prepared
26 #
27 # procd_kill(service, [instance]):
28 # Kill a service instance (or all instances)
29 #
30
31 . $IPKG_INSTROOT/usr/share/libubox/jshn.sh
32
33 _PROCD_SERVICE=
34
35 _procd_call() {
36 local old_cb
37
38 json_set_namespace procd old_cb
39 "$@"
40 json_set_namespace $old_cb
41 }
42
43 _procd_wrapper() {
44 while [ -n "$1" ]; do
45 eval "$1() { _procd_call _$1 \"\$@\"; }"
46 shift
47 done
48 }
49
50 _procd_ubus_call() {
51 local cmd="$1"
52
53 [ -n "$PROCD_DEBUG" ] && json_dump >&2
54 ubus call service "$cmd" "$(json_dump)"
55 json_cleanup
56 }
57
58 _procd_open_service() {
59 local name="$1"
60 local script="$2"
61
62 _PROCD_SERVICE="$name"
63 _PROCD_INSTANCE_SEQ=0
64
65 json_init
66 json_add_string name "$name"
67 [ -n "$script" ] && json_add_string script "$script"
68 json_add_object instances
69 }
70
71 _procd_close_service() {
72 json_close_object
73 service_triggers
74 _procd_ubus_call set
75 }
76
77 _procd_add_array_data() {
78 while [ "$#" -gt 0 ]; do
79 json_add_string "" "$1"
80 shift
81 done
82 }
83
84 _procd_add_array() {
85 json_add_array "$1"
86 shift
87 _procd_add_array_data "$@"
88 json_close_array
89 }
90
91 _procd_add_table_data() {
92 while [ -n "$1" ]; do
93 local var="${1%%=*}"
94 local val="${1#*=}"
95 [[ "$1" == "$val" ]] && val=
96 json_add_string "$var" "$val"
97 shift
98 done
99 }
100
101 _procd_add_table() {
102 json_add_object "$1"
103 shift
104 _procd_add_table_data "$@"
105 json_close_object
106 }
107
108 _procd_open_instance() {
109 local name="$1"; shift
110
111 _PROCD_INSTANCE_SEQ="$(($_PROCD_INSTANCE_SEQ + 1))"
112 name="${name:-instance$_PROCD_INSTANCE_SEQ}"
113 json_add_object "$name"
114 }
115
116 _procd_open_trigger() {
117 json_add_array "triggers"
118 }
119
120 _procd_open_validate() {
121 json_add_array "validate"
122 }
123
124 _procd_set_param() {
125 local type="$1"; shift
126
127 case "$type" in
128 env|data|limits)
129 _procd_add_table "$type" "$@"
130 ;;
131 command|netdev|file|respawn|watch)
132 _procd_add_array "$type" "$@"
133 ;;
134 error)
135 json_add_array "$type"
136 json_add_string "" "$@"
137 json_close_array
138 ;;
139 nice)
140 json_add_int "$type" "$1"
141 ;;
142 esac
143 }
144
145 _procd_add_interface_trigger() {
146 json_add_array
147 _procd_add_array_data "$1"
148 shift
149
150 json_add_array
151 _procd_add_array_data "if"
152
153 json_add_array
154 _procd_add_array_data "eq" "interface" "$1"
155 shift
156 json_close_array
157
158 json_add_array
159 _procd_add_array_data "run_script" "$@"
160 json_close_array
161
162 json_close_array
163
164 json_close_array
165 }
166
167 _procd_add_reload_interface_trigger() {
168 local script=$(readlink "$initscript")
169 local name=$(basename ${script:-$initscript})
170
171 _procd_open_trigger
172 _procd_add_interface_trigger "interface.*" $1 /etc/init.d/$name reload
173 _procd_close_trigger
174 }
175
176 _procd_add_config_trigger() {
177 json_add_array
178 _procd_add_array_data "$1"
179 shift
180
181 json_add_array
182 _procd_add_array_data "if"
183
184 json_add_array
185 _procd_add_array_data "eq" "package" "$1"
186 shift
187 json_close_array
188
189 json_add_array
190 _procd_add_array_data "run_script" "$@"
191 json_close_array
192
193 json_close_array
194
195 json_close_array
196 }
197
198 _procd_add_raw_trigger() {
199 json_add_array
200 _procd_add_array_data "$1"
201 shift
202 local timeout=$1
203 shift
204
205 json_add_array
206 json_add_array
207 _procd_add_array_data "run_script" "$@"
208 json_close_array
209 json_close_array
210
211 json_add_int "" "$timeout"
212
213 json_close_array
214 }
215
216 _procd_add_reload_trigger() {
217 local script=$(readlink "$initscript")
218 local name=$(basename ${script:-$initscript})
219 local file
220
221 _procd_open_trigger
222 for file in "$@"; do
223 _procd_add_config_trigger "config.change" "$file" /etc/init.d/$name reload
224 done
225 _procd_close_trigger
226 }
227
228 _procd_add_validation() {
229 _procd_open_validate
230 $@
231 _procd_close_validate
232 }
233
234 _procd_append_param() {
235 local type="$1"; shift
236 local _json_no_warning=1
237
238 json_select "$type"
239 [ $? = 0 ] || {
240 _procd_set_param "$type" "$@"
241 return
242 }
243 case "$type" in
244 env|data|limits)
245 _procd_add_table_data "$@"
246 ;;
247 command|netdev|file|respawn|watch)
248 _procd_add_array_data "$@"
249 ;;
250 error)
251 json_add_string "" "$@"
252 ;;
253 esac
254 json_select ..
255 }
256
257 _procd_close_instance() {
258 json_close_object
259 }
260
261 _procd_close_trigger() {
262 json_close_array
263 }
264
265 _procd_close_validate() {
266 json_close_array
267 }
268
269 _procd_add_instance() {
270 _procd_open_instance
271 _procd_set_param command "$@"
272 _procd_close_instance
273 }
274
275 _procd_kill() {
276 local service="$1"
277 local instance="$2"
278
279 json_init
280 [ -n "$service" ] && json_add_string name "$service"
281 [ -n "$instance" ] && json_add_string instance "$instance"
282 _procd_ubus_call delete
283 }
284
285 procd_open_data() {
286 local name="$1"
287 json_set_namespace procd __procd_old_cb
288 json_add_object data
289 }
290
291 procd_close_data() {
292 json_close_object
293 json_set_namespace $__procd_old_cb
294 }
295
296 _procd_set_config_changed() {
297 local package="$1"
298
299 json_init
300 json_add_string type config.change
301 json_add_object data
302 json_add_string package "$package"
303 json_close_object
304
305 ubus call service event "$(json_dump)"
306 }
307
308 procd_add_mdns_service() {
309 local service proto port
310 service=$1; shift
311 proto=$1; shift
312 port=$1; shift
313 json_add_object "${service}_$port"
314 json_add_string "service" "_$service._$proto.local"
315 json_add_int port "$port"
316 [ -n "$1" ] && {
317 json_add_array txt
318 for txt in $@; do json_add_string "" $txt; done
319 json_select ..
320 }
321 json_select ..
322 }
323
324 procd_add_mdns() {
325 procd_open_data
326 json_add_object "mdns"
327 procd_add_mdns_service $@
328 json_close_object
329 procd_close_data
330 }
331
332 uci_validate_section()
333 {
334 local _package="$1"
335 local _type="$2"
336 local _name="$3"
337 local _result
338 local _error
339 shift; shift; shift
340 _result=`/sbin/validate_data "$_package" "$_type" "$_name" "$@" 2> /dev/null`
341 _error=$?
342 eval "$_result"
343 [ "$_error" = "0" ] || `/sbin/validate_data "$_package" "$_type" "$_name" "$@" 1> /dev/null`
344 return $_error
345 }
346
347 _procd_wrapper \
348 procd_open_service \
349 procd_close_service \
350 procd_add_instance \
351 procd_add_raw_trigger \
352 procd_add_config_trigger \
353 procd_add_interface_trigger \
354 procd_add_reload_trigger \
355 procd_add_reload_interface_trigger \
356 procd_add_interface_reload \
357 procd_open_trigger \
358 procd_close_trigger \
359 procd_open_instance \
360 procd_close_instance \
361 procd_open_validate \
362 procd_close_validate \
363 procd_set_param \
364 procd_append_param \
365 procd_add_validation \
366 procd_set_config_changed \
367 procd_kill