5148b2f03c3511ee5c3de1d5d631ecd5a26c730a
[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: $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 esac
202 done
203 json_add_object "mount"
204 json_close_object
205 json_close_object
206 }
207
208 _procd_add_jail_mount() {
209 local _json_no_warning=1
210
211 json_select "jail"
212 [ $? = 0 ] || return
213 json_select "mount"
214 [ $? = 0 ] || {
215 json_select ..
216 return
217 }
218 for a in $@; do
219 json_add_string "$a" "0"
220 done
221 json_select ..
222 json_select ..
223 }
224
225 _procd_add_jail_mount_rw() {
226 local _json_no_warning=1
227
228 json_select "jail"
229 [ $? = 0 ] || return
230 json_select "mount"
231 [ $? = 0 ] || {
232 json_select ..
233 return
234 }
235 for a in $@; do
236 json_add_string "$a" "1"
237 done
238 json_select ..
239 json_select ..
240 }
241
242 _procd_set_param() {
243 local type="$1"; shift
244
245 case "$type" in
246 env|data|limits)
247 _procd_add_table "$type" "$@"
248 ;;
249 command|netdev|file|respawn|watch|watchdog)
250 _procd_add_array "$type" "$@"
251 ;;
252 error)
253 json_add_array "$type"
254 json_add_string "" "$@"
255 json_close_array
256 ;;
257 nice|term_timeout)
258 json_add_int "$type" "$1"
259 ;;
260 reload_signal)
261 json_add_int "$type" $(kill -l "$1")
262 ;;
263 pidfile|user|group|seccomp|capabilities|facility|\
264 extroot|overlaydir|tmpoverlaysize)
265 json_add_string "$type" "$1"
266 ;;
267 stdout|stderr|no_new_privs)
268 json_add_boolean "$type" "$1"
269 ;;
270 esac
271 }
272
273 _procd_add_timeout() {
274 [ "$PROCD_RELOAD_DELAY" -gt 0 ] && json_add_int "" "$PROCD_RELOAD_DELAY"
275 return 0
276 }
277
278 _procd_add_interface_trigger() {
279 json_add_array
280 _procd_add_array_data "$1"
281 shift
282
283 json_add_array
284 _procd_add_array_data "if"
285
286 json_add_array
287 _procd_add_array_data "eq" "interface" "$1"
288 shift
289 json_close_array
290
291 json_add_array
292 _procd_add_array_data "run_script" "$@"
293 json_close_array
294
295 json_close_array
296 _procd_add_timeout
297 json_close_array
298 }
299
300 _procd_add_reload_interface_trigger() {
301 local script=$(readlink "$initscript")
302 local name=$(basename ${script:-$initscript})
303
304 _procd_open_trigger
305 _procd_add_interface_trigger "interface.*" $1 /etc/init.d/$name reload
306 _procd_close_trigger
307 }
308
309 _procd_add_config_trigger() {
310 json_add_array
311 _procd_add_array_data "$1"
312 shift
313
314 json_add_array
315 _procd_add_array_data "if"
316
317 json_add_array
318 _procd_add_array_data "eq" "package" "$1"
319 shift
320 json_close_array
321
322 json_add_array
323 _procd_add_array_data "run_script" "$@"
324 json_close_array
325
326 json_close_array
327 _procd_add_timeout
328 json_close_array
329 }
330
331 _procd_add_mount_trigger() {
332 json_add_array
333 _procd_add_array_data "$1"
334 local action="$2"
335 local multi=0
336 shift ; shift
337
338 json_add_array
339 _procd_add_array_data "if"
340
341 if [ "$2" ]; then
342 json_add_array
343 _procd_add_array_data "or"
344 multi=1
345 fi
346
347 while [ "$1" ]; do
348 json_add_array
349 _procd_add_array_data "eq" "target" "$1"
350 shift
351 json_close_array
352 done
353
354 [ $multi = 1 ] && json_close_array
355
356 json_add_array
357 _procd_add_array_data "run_script" /etc/init.d/$name $action
358 json_close_array
359
360 json_close_array
361 _procd_add_timeout
362 json_close_array
363 }
364
365 _procd_add_action_mount_trigger() {
366 local action="$1"
367 shift
368 local mountpoints="$(procd_get_mountpoints "$@")"
369 [ "${mountpoints//[[:space:]]}" ] || return 0
370 local script=$(readlink "$initscript")
371 local name=$(basename ${script:-$initscript})
372
373 _procd_open_trigger
374 _procd_add_mount_trigger mount.add $action "$mountpoints"
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 local mpath
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 _procd_add_action_mount_trigger restart "$@"
398 }
399
400 _procd_add_reload_mount_trigger() {
401 _procd_add_action_mount_trigger reload "$@"
402 }
403
404 _procd_add_raw_trigger() {
405 json_add_array
406 _procd_add_array_data "$1"
407 shift
408 local timeout=$1
409 shift
410
411 json_add_array
412 json_add_array
413 _procd_add_array_data "run_script" "$@"
414 json_close_array
415 json_close_array
416
417 json_add_int "" "$timeout"
418
419 json_close_array
420 }
421
422 _procd_add_reload_trigger() {
423 local script=$(readlink "$initscript")
424 local name=$(basename ${script:-$initscript})
425 local file
426
427 _procd_open_trigger
428 for file in "$@"; do
429 _procd_add_config_trigger "config.change" "$file" /etc/init.d/$name reload
430 done
431 _procd_close_trigger
432 }
433
434 _procd_add_validation() {
435 _procd_open_validate
436 $@
437 _procd_close_validate
438 }
439
440 _procd_append_param() {
441 local type="$1"; shift
442 local _json_no_warning=1
443
444 json_select "$type"
445 [ $? = 0 ] || {
446 _procd_set_param "$type" "$@"
447 return
448 }
449 case "$type" in
450 env|data|limits)
451 _procd_add_table_data "$@"
452 ;;
453 command|netdev|file|respawn|watch|watchdog)
454 _procd_add_array_data "$@"
455 ;;
456 error)
457 json_add_string "" "$@"
458 ;;
459 esac
460 json_select ..
461 }
462
463 _procd_close_instance() {
464 local respawn_vals
465 _json_no_warning=1
466 if json_select respawn ; then
467 json_get_values respawn_vals
468 if [ -z "$respawn_vals" ]; then
469 local respawn_threshold=$(uci_get system.@service[0].respawn_threshold)
470 local respawn_timeout=$(uci_get system.@service[0].respawn_timeout)
471 local respawn_retry=$(uci_get system.@service[0].respawn_retry)
472 _procd_add_array_data ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
473 fi
474 json_select ..
475 fi
476
477 json_close_object
478 }
479
480 _procd_add_instance() {
481 _procd_open_instance
482 _procd_set_param command "$@"
483 _procd_close_instance
484 }
485
486 procd_running() {
487 local service="$1"
488 local instance="${2:-*}"
489 [ "$instance" = "*" ] || instance="'$instance'"
490
491 json_init
492 json_add_string name "$service"
493 local running=$(_procd_ubus_call list | jsonfilter -l 1 -e "@['$service'].instances[$instance].running")
494
495 [ "$running" = "true" ]
496 }
497
498 _procd_kill() {
499 local service="$1"
500 local instance="$2"
501
502 json_init
503 [ -n "$service" ] && json_add_string name "$service"
504 [ -n "$instance" ] && json_add_string instance "$instance"
505 _procd_ubus_call delete
506 }
507
508 _procd_send_signal() {
509 local service="$1"
510 local instance="$2"
511 local signal="$3"
512
513 case "$signal" in
514 [A-Z]*) signal="$(kill -l "$signal" 2>/dev/null)" || return 1;;
515 esac
516
517 json_init
518 json_add_string name "$service"
519 [ -n "$instance" -a "$instance" != "*" ] && json_add_string instance "$instance"
520 [ -n "$signal" ] && json_add_int signal "$signal"
521 _procd_ubus_call signal
522 }
523
524 _procd_status() {
525 local service="$1"
526 local instance="$2"
527 local data
528
529 json_init
530 [ -n "$service" ] && json_add_string name "$service"
531
532 data=$(_procd_ubus_call list | jsonfilter -e '@["'"$service"'"]')
533 [ -z "$data" ] && { echo "inactive"; return 3; }
534
535 data=$(echo "$data" | jsonfilter -e '$.instances')
536 if [ -z "$data" ]; then
537 [ -z "$instance" ] && { echo "active with no instances"; return 0; }
538 data="[]"
539 fi
540
541 [ -n "$instance" ] && instance="\"$instance\"" || instance='*'
542 if [ -z "$(echo "$data" | jsonfilter -e '$['"$instance"']')" ]; then
543 echo "unknown instance $instance"; return 4
544 else
545 echo "running"; return 0
546 fi
547 }
548
549 procd_open_data() {
550 local name="$1"
551 json_set_namespace procd __procd_old_cb
552 json_add_object data
553 }
554
555 procd_close_data() {
556 json_close_object
557 json_set_namespace $__procd_old_cb
558 }
559
560 _procd_set_config_changed() {
561 local package="$1"
562
563 json_init
564 json_add_string type config.change
565 json_add_object data
566 json_add_string package "$package"
567 json_close_object
568
569 ubus call service event "$(json_dump)"
570 }
571
572 procd_add_mdns_service() {
573 local service proto port
574 service=$1; shift
575 proto=$1; shift
576 port=$1; shift
577 json_add_object "${service}_$port"
578 json_add_string "service" "_$service._$proto.local"
579 json_add_int port "$port"
580 [ -n "$1" ] && {
581 json_add_array txt
582 for txt in "$@"; do json_add_string "" "$txt"; done
583 json_select ..
584 }
585 json_select ..
586 }
587
588 procd_add_mdns() {
589 procd_open_data
590 json_add_object "mdns"
591 procd_add_mdns_service "$@"
592 json_close_object
593 procd_close_data
594 }
595
596 uci_validate_section()
597 {
598 local _package="$1"
599 local _type="$2"
600 local _name="$3"
601 local _result
602 local _error
603 shift; shift; shift
604 _result=$(/sbin/validate_data "$_package" "$_type" "$_name" "$@" 2> /dev/null)
605 _error=$?
606 eval "$_result"
607 [ "$_error" = "0" ] || $(/sbin/validate_data "$_package" "$_type" "$_name" "$@" 1> /dev/null)
608 return $_error
609 }
610
611 uci_load_validate() {
612 local _package="$1"
613 local _type="$2"
614 local _name="$3"
615 local _function="$4"
616 local _option
617 local _result
618 shift; shift; shift; shift
619 for _option in "$@"; do
620 eval "local ${_option%%:*}"
621 done
622 uci_validate_section "$_package" "$_type" "$_name" "$@"
623 _result=$?
624 [ -n "$_function" ] || return $_result
625 eval "$_function \"\$_name\" \"\$_result\""
626 }
627
628 _procd_wrapper \
629 procd_open_service \
630 procd_close_service \
631 procd_add_instance \
632 procd_add_raw_trigger \
633 procd_add_config_trigger \
634 procd_add_interface_trigger \
635 procd_add_mount_trigger \
636 procd_add_reload_trigger \
637 procd_add_reload_interface_trigger \
638 procd_add_action_mount_trigger \
639 procd_add_reload_mount_trigger \
640 procd_add_restart_mount_trigger \
641 procd_open_trigger \
642 procd_close_trigger \
643 procd_open_instance \
644 procd_close_instance \
645 procd_open_validate \
646 procd_close_validate \
647 procd_add_jail \
648 procd_add_jail_mount \
649 procd_add_jail_mount_rw \
650 procd_set_param \
651 procd_append_param \
652 procd_add_validation \
653 procd_set_config_changed \
654 procd_kill \
655 procd_send_signal