procd: service_data: Support data within the service itself
[openwrt/openwrt.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 $fail_threshold $restart_timeout $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 # user info: array with 1 values $username
22 # pidfile: file name to write pid into
23 #
24 # No space separation is done for arrays/tables - use one function argument per command line argument
25 #
26 # procd_close_instance():
27 # Complete the instance being prepared
28 #
29 # procd_kill(service, [instance]):
30 # Kill a service instance (or all instances)
31 #
32 # procd_send_signal(service, [instance], [signal])
33 # Send a signal to a service instance (or all instances)
34 #
35
36 . $IPKG_INSTROOT/usr/share/libubox/jshn.sh
37
38 PROCD_RELOAD_DELAY=1000
39 _PROCD_SERVICE=
40
41 _procd_call() {
42 local old_cb
43
44 json_set_namespace procd old_cb
45 "$@"
46 json_set_namespace $old_cb
47 }
48
49 _procd_wrapper() {
50 while [ -n "$1" ]; do
51 eval "$1() { _procd_call _$1 \"\$@\"; }"
52 shift
53 done
54 }
55
56 _procd_ubus_call() {
57 local cmd="$1"
58
59 [ -n "$PROCD_DEBUG" ] && json_dump >&2
60 ubus call service "$cmd" "$(json_dump)"
61 json_cleanup
62 }
63
64 _procd_open_service() {
65 local name="$1"
66 local script="$2"
67
68 _PROCD_SERVICE="$name"
69 _PROCD_INSTANCE_SEQ=0
70
71 json_init
72 json_add_string name "$name"
73 [ -n "$script" ] && json_add_string script "$script"
74 json_add_object instances
75 }
76
77 _procd_close_service() {
78 json_close_object
79 _procd_open_trigger
80 service_triggers
81 _procd_close_trigger
82 _procd_open_data
83 service_data
84 _procd_close_data
85 _procd_ubus_call ${1:-set}
86 }
87
88 _procd_add_array_data() {
89 while [ "$#" -gt 0 ]; do
90 json_add_string "" "$1"
91 shift
92 done
93 }
94
95 _procd_add_array() {
96 json_add_array "$1"
97 shift
98 _procd_add_array_data "$@"
99 json_close_array
100 }
101
102 _procd_add_table_data() {
103 while [ -n "$1" ]; do
104 local var="${1%%=*}"
105 local val="${1#*=}"
106 [ "$1" = "$val" ] && val=
107 json_add_string "$var" "$val"
108 shift
109 done
110 }
111
112 _procd_add_table() {
113 json_add_object "$1"
114 shift
115 _procd_add_table_data "$@"
116 json_close_object
117 }
118
119 _procd_open_instance() {
120 local name="$1"; shift
121
122 _PROCD_INSTANCE_SEQ="$(($_PROCD_INSTANCE_SEQ + 1))"
123 name="${name:-instance$_PROCD_INSTANCE_SEQ}"
124 json_add_object "$name"
125 [ -n "$TRACE_SYSCALLS" ] && json_add_boolean trace "1"
126 }
127
128 _procd_open_trigger() {
129 let '_procd_trigger_open = _procd_trigger_open + 1'
130 [ "$_procd_trigger_open" -gt 1 ] && return
131 json_add_array "triggers"
132 }
133
134 _procd_close_trigger() {
135 let '_procd_trigger_open = _procd_trigger_open - 1'
136 [ "$_procd_trigger_open" -lt 1 ] || return
137 json_close_array
138 }
139
140 _procd_open_data() {
141 let '_procd_data_open = _procd_data_open + 1'
142 [ "$_procd_data_open" -gt 1 ] && return
143 json_add_object "data"
144 }
145
146 _procd_close_data() {
147 let '_procd_data_open = _procd_data_open - 1'
148 [ "$_procd_data_open" -lt 1 ] || return
149 json_close_object
150 }
151
152 _procd_open_validate() {
153 json_select ..
154 json_add_array "validate"
155 }
156
157 _procd_close_validate() {
158 json_close_array
159 json_select triggers
160 }
161
162 _procd_add_jail() {
163 json_add_object "jail"
164 json_add_string name "$1"
165
166 shift
167
168 for a in $@; do
169 case $a in
170 log) json_add_boolean "log" "1";;
171 ubus) json_add_boolean "ubus" "1";;
172 procfs) json_add_boolean "procfs" "1";;
173 sysfs) json_add_boolean "sysfs" "1";;
174 ronly) json_add_boolean "ronly" "1";;
175 esac
176 done
177 json_add_object "mount"
178 json_close_object
179 json_close_object
180 }
181
182 _procd_add_jail_mount() {
183 local _json_no_warning=1
184
185 json_select "jail"
186 [ $? = 0 ] || return
187 json_select "mount"
188 [ $? = 0 ] || {
189 json_select ..
190 return
191 }
192 for a in $@; do
193 json_add_string "$a" "0"
194 done
195 json_select ..
196 json_select ..
197 }
198
199 _procd_add_jail_mount_rw() {
200 local _json_no_warning=1
201
202 json_select "jail"
203 [ $? = 0 ] || return
204 json_select "mount"
205 [ $? = 0 ] || {
206 json_select ..
207 return
208 }
209 for a in $@; do
210 json_add_string "$a" "1"
211 done
212 json_select ..
213 json_select ..
214 }
215
216 _procd_set_param() {
217 local type="$1"; shift
218
219 case "$type" in
220 env|data|limits)
221 _procd_add_table "$type" "$@"
222 ;;
223 command|netdev|file|respawn|watch)
224 _procd_add_array "$type" "$@"
225 ;;
226 error)
227 json_add_array "$type"
228 json_add_string "" "$@"
229 json_close_array
230 ;;
231 nice|term_timeout)
232 json_add_int "$type" "$1"
233 ;;
234 reload_signal)
235 json_add_int "$type" $(kill -l "$1")
236 ;;
237 pidfile|user|seccomp|capabilities)
238 json_add_string "$type" "$1"
239 ;;
240 stdout|stderr|no_new_privs)
241 json_add_boolean "$type" "$1"
242 ;;
243 esac
244 }
245
246 _procd_add_timeout() {
247 [ "$PROCD_RELOAD_DELAY" -gt 0 ] && json_add_int "" "$PROCD_RELOAD_DELAY"
248 return 0
249 }
250
251 _procd_add_interface_trigger() {
252 json_add_array
253 _procd_add_array_data "$1"
254 shift
255
256 json_add_array
257 _procd_add_array_data "if"
258
259 json_add_array
260 _procd_add_array_data "eq" "interface" "$1"
261 shift
262 json_close_array
263
264 json_add_array
265 _procd_add_array_data "run_script" "$@"
266 json_close_array
267
268 json_close_array
269 _procd_add_timeout
270 json_close_array
271 }
272
273 _procd_add_reload_interface_trigger() {
274 local script=$(readlink "$initscript")
275 local name=$(basename ${script:-$initscript})
276
277 _procd_open_trigger
278 _procd_add_interface_trigger "interface.*" $1 /etc/init.d/$name reload
279 _procd_close_trigger
280 }
281
282 _procd_add_config_trigger() {
283 json_add_array
284 _procd_add_array_data "$1"
285 shift
286
287 json_add_array
288 _procd_add_array_data "if"
289
290 json_add_array
291 _procd_add_array_data "eq" "package" "$1"
292 shift
293 json_close_array
294
295 json_add_array
296 _procd_add_array_data "run_script" "$@"
297 json_close_array
298
299 json_close_array
300 _procd_add_timeout
301 json_close_array
302 }
303
304 _procd_add_raw_trigger() {
305 json_add_array
306 _procd_add_array_data "$1"
307 shift
308 local timeout=$1
309 shift
310
311 json_add_array
312 json_add_array
313 _procd_add_array_data "run_script" "$@"
314 json_close_array
315 json_close_array
316
317 json_add_int "" "$timeout"
318
319 json_close_array
320 }
321
322 _procd_add_reload_trigger() {
323 local script=$(readlink "$initscript")
324 local name=$(basename ${script:-$initscript})
325 local file
326
327 _procd_open_trigger
328 for file in "$@"; do
329 _procd_add_config_trigger "config.change" "$file" /etc/init.d/$name reload
330 done
331 _procd_close_trigger
332 }
333
334 _procd_add_validation() {
335 _procd_open_validate
336 $@
337 _procd_close_validate
338 }
339
340 _procd_append_param() {
341 local type="$1"; shift
342 local _json_no_warning=1
343
344 json_select "$type"
345 [ $? = 0 ] || {
346 _procd_set_param "$type" "$@"
347 return
348 }
349 case "$type" in
350 env|data|limits)
351 _procd_add_table_data "$@"
352 ;;
353 command|netdev|file|respawn|watch)
354 _procd_add_array_data "$@"
355 ;;
356 error)
357 json_add_string "" "$@"
358 ;;
359 esac
360 json_select ..
361 }
362
363 _procd_close_instance() {
364 local respawn_vals
365 _json_no_warning=1
366 if json_select respawn ; then
367 json_get_values respawn_vals
368 if [ -z "$respawn_vals" ]; then
369 local respawn_threshold=$(uci_get system.@service[0].respawn_threshold)
370 local respawn_timeout=$(uci_get system.@service[0].respawn_timeout)
371 local respawn_retry=$(uci_get system.@service[0].respawn_retry)
372 _procd_add_array_data ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
373 fi
374 json_select ..
375 fi
376
377 json_close_object
378 }
379
380 _procd_add_instance() {
381 _procd_open_instance
382 _procd_set_param command "$@"
383 _procd_close_instance
384 }
385
386 _procd_kill() {
387 local service="$1"
388 local instance="$2"
389
390 json_init
391 [ -n "$service" ] && json_add_string name "$service"
392 [ -n "$instance" ] && json_add_string instance "$instance"
393 _procd_ubus_call delete
394 }
395
396 _procd_send_signal() {
397 local service="$1"
398 local instance="$2"
399 local signal="$3"
400
401 json_init
402 json_add_string name "$service"
403 [ -n "$instance" -a "$instance" != "*" ] && json_add_string instance "$instance"
404 [ -n "$signal" ] && json_add_int signal "$signal"
405 _procd_ubus_call signal
406 }
407
408 procd_open_data() {
409 local name="$1"
410 json_set_namespace procd __procd_old_cb
411 json_add_object data
412 }
413
414 procd_close_data() {
415 json_close_object
416 json_set_namespace $__procd_old_cb
417 }
418
419 _procd_set_config_changed() {
420 local package="$1"
421
422 json_init
423 json_add_string type config.change
424 json_add_object data
425 json_add_string package "$package"
426 json_close_object
427
428 ubus call service event "$(json_dump)"
429 }
430
431 procd_add_mdns_service() {
432 local service proto port
433 service=$1; shift
434 proto=$1; shift
435 port=$1; shift
436 json_add_object "${service}_$port"
437 json_add_string "service" "_$service._$proto.local"
438 json_add_int port "$port"
439 [ -n "$1" ] && {
440 json_add_array txt
441 for txt in "$@"; do json_add_string "" "$txt"; done
442 json_select ..
443 }
444 json_select ..
445 }
446
447 procd_add_mdns() {
448 procd_open_data
449 json_add_object "mdns"
450 procd_add_mdns_service "$@"
451 json_close_object
452 procd_close_data
453 }
454
455 uci_validate_section()
456 {
457 local _package="$1"
458 local _type="$2"
459 local _name="$3"
460 local _result
461 local _error
462 shift; shift; shift
463 _result=`/sbin/validate_data "$_package" "$_type" "$_name" "$@" 2> /dev/null`
464 _error=$?
465 eval "$_result"
466 [ "$_error" = "0" ] || `/sbin/validate_data "$_package" "$_type" "$_name" "$@" 1> /dev/null`
467 return $_error
468 }
469
470 _procd_wrapper \
471 procd_open_service \
472 procd_close_service \
473 procd_add_instance \
474 procd_add_raw_trigger \
475 procd_add_config_trigger \
476 procd_add_interface_trigger \
477 procd_add_reload_trigger \
478 procd_add_reload_interface_trigger \
479 procd_open_trigger \
480 procd_close_trigger \
481 procd_open_instance \
482 procd_close_instance \
483 procd_open_validate \
484 procd_close_validate \
485 procd_add_jail \
486 procd_add_jail_mount \
487 procd_add_jail_mount_rw \
488 procd_set_param \
489 procd_append_param \
490 procd_add_validation \
491 procd_set_config_changed \
492 procd_kill \
493 procd_send_signal