procd: add missing dependency and fix empty mount triggers
[openwrt/staging/dedeckeh.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: $username to run service as
22 # group: $groupname to run service as
23 # pidfile: file name to write pid into
24 # stdout: boolean whether to redirect commands stdout to syslog (default: 0)
25 # stderr: boolean whether to redirect commands stderr to syslog (default: 0)
26 # facility: syslog facility used when logging to syslog (default: daemon)
27 #
28 # No space separation is done for arrays/tables - use one function argument per command line argument
29 #
30 # procd_close_instance():
31 # Complete the instance being prepared
32 #
33 # procd_running(service, [instance]):
34 # Checks if service/instance is currently running
35 #
36 # procd_kill(service, [instance]):
37 # Kill a service instance (or all instances)
38 #
39 # procd_send_signal(service, [instance], [signal])
40 # Send a signal to a service instance (or all instances)
41 #
42
43 . "$IPKG_INSTROOT/usr/share/libubox/jshn.sh"
44
45 PROCD_RELOAD_DELAY=1000
46 _PROCD_SERVICE=
47
48 procd_lock() {
49 local basescript=$(readlink "$initscript")
50 local service_name="$(basename ${basescript:-$initscript})"
51
52 flock -n 1000 &> /dev/null
53 if [ "$?" != "0" ]; then
54 exec 1000>"$IPKG_INSTROOT/var/lock/procd_${service_name}.lock"
55 flock 1000
56 if [ "$?" != "0" ]; then
57 logger "warning: procd flock for $service_name failed"
58 fi
59 fi
60 }
61
62 _procd_call() {
63 local old_cb
64
65 json_set_namespace procd old_cb
66 "$@"
67 json_set_namespace $old_cb
68 }
69
70 _procd_wrapper() {
71 procd_lock
72 while [ -n "$1" ]; do
73 eval "$1() { _procd_call _$1 \"\$@\"; }"
74 shift
75 done
76 }
77
78 _procd_ubus_call() {
79 local cmd="$1"
80
81 [ -n "$PROCD_DEBUG" ] && json_dump >&2
82 ubus call service "$cmd" "$(json_dump)"
83 json_cleanup
84 }
85
86 _procd_open_service() {
87 local name="$1"
88 local script="$2"
89
90 _PROCD_SERVICE="$name"
91 _PROCD_INSTANCE_SEQ=0
92
93 json_init
94 json_add_string name "$name"
95 [ -n "$script" ] && json_add_string script "$script"
96 json_add_object instances
97 }
98
99 _procd_close_service() {
100 json_close_object
101 _procd_open_trigger
102 service_triggers
103 _procd_close_trigger
104 _procd_open_data
105 service_data
106 _procd_close_data
107 _procd_ubus_call ${1:-set}
108 }
109
110 _procd_add_array_data() {
111 while [ "$#" -gt 0 ]; do
112 json_add_string "" "$1"
113 shift
114 done
115 }
116
117 _procd_add_array() {
118 json_add_array "$1"
119 shift
120 _procd_add_array_data "$@"
121 json_close_array
122 }
123
124 _procd_add_table_data() {
125 while [ -n "$1" ]; do
126 local var="${1%%=*}"
127 local val="${1#*=}"
128 [ "$1" = "$val" ] && val=
129 json_add_string "$var" "$val"
130 shift
131 done
132 }
133
134 _procd_add_table() {
135 json_add_object "$1"
136 shift
137 _procd_add_table_data "$@"
138 json_close_object
139 }
140
141 _procd_open_instance() {
142 local name="$1"; shift
143
144 _PROCD_INSTANCE_SEQ="$(($_PROCD_INSTANCE_SEQ + 1))"
145 name="${name:-instance$_PROCD_INSTANCE_SEQ}"
146 json_add_object "$name"
147 [ -n "$TRACE_SYSCALLS" ] && json_add_boolean trace "1"
148 }
149
150 _procd_open_trigger() {
151 let '_procd_trigger_open = _procd_trigger_open + 1'
152 [ "$_procd_trigger_open" -gt 1 ] && return
153 json_add_array "triggers"
154 }
155
156 _procd_close_trigger() {
157 let '_procd_trigger_open = _procd_trigger_open - 1'
158 [ "$_procd_trigger_open" -lt 1 ] || return
159 json_close_array
160 }
161
162 _procd_open_data() {
163 let '_procd_data_open = _procd_data_open + 1'
164 [ "$_procd_data_open" -gt 1 ] && return
165 json_add_object "data"
166 }
167
168 _procd_close_data() {
169 let '_procd_data_open = _procd_data_open - 1'
170 [ "$_procd_data_open" -lt 1 ] || return
171 json_close_object
172 }
173
174 _procd_open_validate() {
175 json_select ..
176 json_add_array "validate"
177 }
178
179 _procd_close_validate() {
180 json_close_array
181 json_select triggers
182 }
183
184 _procd_add_jail() {
185 json_add_object "jail"
186 json_add_string name "$1"
187
188 shift
189
190 for a in $@; do
191 case $a in
192 log) json_add_boolean "log" "1";;
193 ubus) json_add_boolean "ubus" "1";;
194 procfs) json_add_boolean "procfs" "1";;
195 sysfs) json_add_boolean "sysfs" "1";;
196 ronly) json_add_boolean "ronly" "1";;
197 requirejail) json_add_boolean "requirejail" "1";;
198 netns) json_add_boolean "netns" "1";;
199 userns) json_add_boolean "userns" "1";;
200 cgroupsns) json_add_boolean "cgroupsns" "1";;
201 console) json_add_boolean "console" "1";;
202 esac
203 done
204 json_add_object "mount"
205 json_close_object
206 json_close_object
207 }
208
209 _procd_add_jail_mount() {
210 local _json_no_warning=1
211
212 json_select "jail"
213 [ $? = 0 ] || return
214 json_select "mount"
215 [ $? = 0 ] || {
216 json_select ..
217 return
218 }
219 for a in $@; do
220 json_add_string "$a" "0"
221 done
222 json_select ..
223 json_select ..
224 }
225
226 _procd_add_jail_mount_rw() {
227 local _json_no_warning=1
228
229 json_select "jail"
230 [ $? = 0 ] || return
231 json_select "mount"
232 [ $? = 0 ] || {
233 json_select ..
234 return
235 }
236 for a in $@; do
237 json_add_string "$a" "1"
238 done
239 json_select ..
240 json_select ..
241 }
242
243 _procd_set_param() {
244 local type="$1"; shift
245
246 case "$type" in
247 env|data|limits)
248 _procd_add_table "$type" "$@"
249 ;;
250 command|netdev|file|respawn|watch|watchdog)
251 _procd_add_array "$type" "$@"
252 ;;
253 error)
254 json_add_array "$type"
255 json_add_string "" "$@"
256 json_close_array
257 ;;
258 nice|term_timeout)
259 json_add_int "$type" "$1"
260 ;;
261 reload_signal)
262 json_add_int "$type" $(kill -l "$1")
263 ;;
264 pidfile|user|group|seccomp|capabilities|facility|\
265 extroot|overlaydir|tmpoverlaysize)
266 json_add_string "$type" "$1"
267 ;;
268 stdout|stderr|no_new_privs)
269 json_add_boolean "$type" "$1"
270 ;;
271 esac
272 }
273
274 _procd_add_timeout() {
275 [ "$PROCD_RELOAD_DELAY" -gt 0 ] && json_add_int "" "$PROCD_RELOAD_DELAY"
276 return 0
277 }
278
279 _procd_add_interface_trigger() {
280 json_add_array
281 _procd_add_array_data "$1"
282 shift
283
284 json_add_array
285 _procd_add_array_data "if"
286
287 json_add_array
288 _procd_add_array_data "eq" "interface" "$1"
289 shift
290 json_close_array
291
292 json_add_array
293 _procd_add_array_data "run_script" "$@"
294 json_close_array
295
296 json_close_array
297 _procd_add_timeout
298 json_close_array
299 }
300
301 _procd_add_reload_interface_trigger() {
302 local script=$(readlink "$initscript")
303 local name=$(basename ${script:-$initscript})
304
305 _procd_open_trigger
306 _procd_add_interface_trigger "interface.*" $1 /etc/init.d/$name reload
307 _procd_close_trigger
308 }
309
310 _procd_add_config_trigger() {
311 json_add_array
312 _procd_add_array_data "$1"
313 shift
314
315 json_add_array
316 _procd_add_array_data "if"
317
318 json_add_array
319 _procd_add_array_data "eq" "package" "$1"
320 shift
321 json_close_array
322
323 json_add_array
324 _procd_add_array_data "run_script" "$@"
325 json_close_array
326
327 json_close_array
328 _procd_add_timeout
329 json_close_array
330 }
331
332 _procd_add_mount_trigger() {
333 json_add_array
334 _procd_add_array_data "$1"
335 local action="$2"
336 local multi=0
337 shift ; shift
338
339 json_add_array
340 _procd_add_array_data "if"
341
342 if [ "$2" ]; then
343 json_add_array
344 _procd_add_array_data "or"
345 multi=1
346 fi
347
348 while [ "$1" ]; do
349 json_add_array
350 _procd_add_array_data "eq" "target" "$1"
351 shift
352 json_close_array
353 done
354
355 [ $multi = 1 ] && json_close_array
356
357 json_add_array
358 _procd_add_array_data "run_script" /etc/init.d/$name $action
359 json_close_array
360
361 json_close_array
362 _procd_add_timeout
363 json_close_array
364 }
365
366 _procd_add_action_mount_trigger() {
367 local script=$(readlink "$initscript")
368 local name=$(basename ${script:-$initscript})
369 local action="$1"
370 local mpath
371 shift
372
373 _procd_open_trigger
374 _procd_add_mount_trigger mount.add $action "$@"
375 _procd_close_trigger
376 }
377
378 procd_get_mountpoints() {
379 (
380 __procd_check_mount() {
381 local cfg="$1"
382 local path="${2%%/}/"
383 local target
384 config_get target "$cfg" target
385 target="${target%%/}/"
386 [ "$path" != "${path##$target}" ] && echo "${target%%/}"
387 }
388
389 config_load fstab
390 for mpath in "$@"; do
391 config_foreach __procd_check_mount mount "$mpath"
392 done
393 ) | sort -u
394 }
395
396 _procd_add_restart_mount_trigger() {
397 local mountpoints="$(procd_get_mountpoints "$@")"
398 [ "${mountpoints//[[:space:]]}" ] &&
399 _procd_add_action_mount_trigger restart $mountpoints
400 }
401
402 _procd_add_reload_mount_trigger() {
403 local mountpoints="$(procd_get_mountpoints "$@")"
404 [ "${mountpoints//[[:space:]]}" ] &&
405 _procd_add_action_mount_trigger reload $mountpoints
406 }
407
408 _procd_add_raw_trigger() {
409 json_add_array
410 _procd_add_array_data "$1"
411 shift
412 local timeout=$1
413 shift
414
415 json_add_array
416 json_add_array
417 _procd_add_array_data "run_script" "$@"
418 json_close_array
419 json_close_array
420
421 json_add_int "" "$timeout"
422
423 json_close_array
424 }
425
426 _procd_add_reload_trigger() {
427 local script=$(readlink "$initscript")
428 local name=$(basename ${script:-$initscript})
429 local file
430
431 _procd_open_trigger
432 for file in "$@"; do
433 _procd_add_config_trigger "config.change" "$file" /etc/init.d/$name reload
434 done
435 _procd_close_trigger
436 }
437
438 _procd_add_validation() {
439 _procd_open_validate
440 $@
441 _procd_close_validate
442 }
443
444 _procd_append_param() {
445 local type="$1"; shift
446 local _json_no_warning=1
447
448 json_select "$type"
449 [ $? = 0 ] || {
450 _procd_set_param "$type" "$@"
451 return
452 }
453 case "$type" in
454 env|data|limits)
455 _procd_add_table_data "$@"
456 ;;
457 command|netdev|file|respawn|watch|watchdog)
458 _procd_add_array_data "$@"
459 ;;
460 error)
461 json_add_string "" "$@"
462 ;;
463 esac
464 json_select ..
465 }
466
467 _procd_close_instance() {
468 local respawn_vals
469 _json_no_warning=1
470 if json_select respawn ; then
471 json_get_values respawn_vals
472 if [ -z "$respawn_vals" ]; then
473 local respawn_threshold=$(uci_get system.@service[0].respawn_threshold)
474 local respawn_timeout=$(uci_get system.@service[0].respawn_timeout)
475 local respawn_retry=$(uci_get system.@service[0].respawn_retry)
476 _procd_add_array_data ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
477 fi
478 json_select ..
479 fi
480
481 json_close_object
482 }
483
484 _procd_add_instance() {
485 _procd_open_instance
486 _procd_set_param command "$@"
487 _procd_close_instance
488 }
489
490 procd_running() {
491 local service="$1"
492 local instance="${2:-*}"
493 [ "$instance" = "*" ] || instance="'$instance'"
494
495 json_init
496 json_add_string name "$service"
497 local running=$(_procd_ubus_call list | jsonfilter -l 1 -e "@['$service'].instances[$instance].running")
498
499 [ "$running" = "true" ]
500 }
501
502 _procd_kill() {
503 local service="$1"
504 local instance="$2"
505
506 json_init
507 [ -n "$service" ] && json_add_string name "$service"
508 [ -n "$instance" ] && json_add_string instance "$instance"
509 _procd_ubus_call delete
510 }
511
512 _procd_send_signal() {
513 local service="$1"
514 local instance="$2"
515 local signal="$3"
516
517 case "$signal" in
518 [A-Z]*) signal="$(kill -l "$signal" 2>/dev/null)" || return 1;;
519 esac
520
521 json_init
522 json_add_string name "$service"
523 [ -n "$instance" -a "$instance" != "*" ] && json_add_string instance "$instance"
524 [ -n "$signal" ] && json_add_int signal "$signal"
525 _procd_ubus_call signal
526 }
527
528 _procd_status() {
529 local service="$1"
530 local instance="$2"
531 local data
532
533 json_init
534 [ -n "$service" ] && json_add_string name "$service"
535
536 data=$(_procd_ubus_call list | jsonfilter -e '@["'"$service"'"]')
537 [ -z "$data" ] && { echo "inactive"; return 3; }
538
539 data=$(echo "$data" | jsonfilter -e '$.instances')
540 if [ -z "$data" ]; then
541 [ -z "$instance" ] && { echo "active with no instances"; return 0; }
542 data="[]"
543 fi
544
545 [ -n "$instance" ] && instance="\"$instance\"" || instance='*'
546 if [ -z "$(echo "$data" | jsonfilter -e '$['"$instance"']')" ]; then
547 echo "unknown instance $instance"; return 4
548 else
549 echo "running"; return 0
550 fi
551 }
552
553 procd_open_data() {
554 local name="$1"
555 json_set_namespace procd __procd_old_cb
556 json_add_object data
557 }
558
559 procd_close_data() {
560 json_close_object
561 json_set_namespace $__procd_old_cb
562 }
563
564 _procd_set_config_changed() {
565 local package="$1"
566
567 json_init
568 json_add_string type config.change
569 json_add_object data
570 json_add_string package "$package"
571 json_close_object
572
573 ubus call service event "$(json_dump)"
574 }
575
576 procd_add_mdns_service() {
577 local service proto port
578 service=$1; shift
579 proto=$1; shift
580 port=$1; shift
581 json_add_object "${service}_$port"
582 json_add_string "service" "_$service._$proto.local"
583 json_add_int port "$port"
584 [ -n "$1" ] && {
585 json_add_array txt
586 for txt in "$@"; do json_add_string "" "$txt"; done
587 json_select ..
588 }
589 json_select ..
590 }
591
592 procd_add_mdns() {
593 procd_open_data
594 json_add_object "mdns"
595 procd_add_mdns_service "$@"
596 json_close_object
597 procd_close_data
598 }
599
600 uci_validate_section()
601 {
602 local _package="$1"
603 local _type="$2"
604 local _name="$3"
605 local _result
606 local _error
607 shift; shift; shift
608 _result=$(/sbin/validate_data "$_package" "$_type" "$_name" "$@" 2> /dev/null)
609 _error=$?
610 eval "$_result"
611 [ "$_error" = "0" ] || $(/sbin/validate_data "$_package" "$_type" "$_name" "$@" 1> /dev/null)
612 return $_error
613 }
614
615 uci_load_validate() {
616 local _package="$1"
617 local _type="$2"
618 local _name="$3"
619 local _function="$4"
620 local _option
621 local _result
622 shift; shift; shift; shift
623 for _option in "$@"; do
624 eval "local ${_option%%:*}"
625 done
626 uci_validate_section "$_package" "$_type" "$_name" "$@"
627 _result=$?
628 [ -n "$_function" ] || return $_result
629 eval "$_function \"\$_name\" \"\$_result\""
630 }
631
632 _procd_wrapper \
633 procd_open_service \
634 procd_close_service \
635 procd_add_instance \
636 procd_add_raw_trigger \
637 procd_add_config_trigger \
638 procd_add_interface_trigger \
639 procd_add_mount_trigger \
640 procd_add_reload_trigger \
641 procd_add_reload_interface_trigger \
642 procd_add_reload_mount_trigger \
643 procd_add_restart_mount_trigger \
644 procd_open_trigger \
645 procd_close_trigger \
646 procd_open_instance \
647 procd_close_instance \
648 procd_open_validate \
649 procd_close_validate \
650 procd_add_jail \
651 procd_add_jail_mount \
652 procd_add_jail_mount_rw \
653 procd_set_param \
654 procd_append_param \
655 procd_add_validation \
656 procd_set_config_changed \
657 procd_kill \
658 procd_send_signal