3 # procd_open_service(name, [script]):
4 # Initialize a new procd command message containing a service with one or more instances
6 # procd_close_service()
7 # Send the command message for the service
9 # procd_open_instance([name]):
10 # Add an instance to the service described by the previous procd_open_service call
12 # procd_set_param(type, [value...])
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)
28 # No space separation is done for arrays/tables - use one function argument per command line argument
30 # procd_close_instance():
31 # Complete the instance being prepared
33 # procd_running(service, [instance]):
34 # Checks if service/instance is currently running
36 # procd_kill(service, [instance]):
37 # Kill a service instance (or all instances)
39 # procd_send_signal(service, [instance], [signal])
40 # Send a signal to a service instance (or all instances)
43 .
"$IPKG_INSTROOT/usr/share/libubox/jshn.sh"
45 PROCD_RELOAD_DELAY
=1000
49 local basescript
=$
(readlink
"$initscript")
50 local service_name
="$(basename ${basescript:-$initscript})"
52 flock
-n 1000 &> /dev
/null
53 if [ "$?" != "0" ]; then
54 exec 1000>"$IPKG_INSTROOT/var/lock/procd_${service_name}.lock"
56 if [ "$?" != "0" ]; then
57 logger
"warning: procd flock for $service_name failed"
65 json_set_namespace procd old_cb
67 json_set_namespace
$old_cb
73 eval "$1() { _procd_call _$1 \"\$@\"; }"
81 [ -n "$PROCD_DEBUG" ] && json_dump
>&2
82 ubus call service
"$cmd" "$(json_dump)"
86 _procd_open_service
() {
90 _PROCD_SERVICE
="$name"
94 json_add_string name
"$name"
95 [ -n "$script" ] && json_add_string
script "$script"
96 json_add_object instances
99 _procd_close_service
() {
107 _procd_ubus_call
${1:-set}
110 _procd_add_array_data
() {
111 while [ "$#" -gt 0 ]; do
112 json_add_string
"" "$1"
120 _procd_add_array_data
"$@"
124 _procd_add_table_data
() {
125 while [ -n "$1" ]; do
128 [ "$1" = "$val" ] && val
=
129 json_add_string
"$var" "$val"
137 _procd_add_table_data
"$@"
141 _procd_open_instance
() {
142 local name
="$1"; shift
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"
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"
156 _procd_close_trigger
() {
157 let '_procd_trigger_open = _procd_trigger_open - 1'
158 [ "$_procd_trigger_open" -lt 1 ] ||
return
163 let '_procd_data_open = _procd_data_open + 1'
164 [ "$_procd_data_open" -gt 1 ] && return
165 json_add_object
"data"
168 _procd_close_data
() {
169 let '_procd_data_open = _procd_data_open - 1'
170 [ "$_procd_data_open" -lt 1 ] ||
return
174 _procd_open_validate
() {
176 json_add_array
"validate"
179 _procd_close_validate
() {
185 json_add_object
"jail"
186 json_add_string name
"$1"
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";;
203 json_add_object
"mount"
208 _procd_add_jail_mount
() {
209 local _json_no_warning
=1
219 json_add_string
"$a" "0"
225 _procd_add_jail_mount_rw
() {
226 local _json_no_warning
=1
236 json_add_string
"$a" "1"
243 local type="$1"; shift
247 _procd_add_table
"$type" "$@"
249 command|netdev|
file|respawn|watch|watchdog
)
250 _procd_add_array
"$type" "$@"
253 json_add_array
"$type"
254 json_add_string
"" "$@"
258 json_add_int
"$type" "$1"
261 json_add_int
"$type" $
(kill -l "$1")
263 pidfile|user|group|seccomp|capabilities|facility|\
264 extroot|overlaydir|tmpoverlaysize
)
265 json_add_string
"$type" "$1"
267 stdout|stderr|no_new_privs
)
268 json_add_boolean
"$type" "$1"
273 _procd_add_timeout
() {
274 [ "$PROCD_RELOAD_DELAY" -gt 0 ] && json_add_int
"" "$PROCD_RELOAD_DELAY"
278 _procd_add_interface_trigger
() {
280 _procd_add_array_data
"$1"
284 _procd_add_array_data
"if"
287 _procd_add_array_data
"eq" "interface" "$1"
292 _procd_add_array_data
"run_script" "$@"
300 _procd_add_reload_interface_trigger
() {
301 local script=$
(readlink
"$initscript")
302 local name
=$
(basename ${script:-$initscript})
305 _procd_add_interface_trigger
"interface.*" $1 /etc
/init.d
/$name reload
309 _procd_add_config_trigger
() {
311 _procd_add_array_data
"$1"
315 _procd_add_array_data
"if"
318 _procd_add_array_data
"eq" "package" "$1"
323 _procd_add_array_data
"run_script" "$@"
331 _procd_add_mount_trigger
() {
333 _procd_add_array_data
"$1"
339 _procd_add_array_data
"if"
343 _procd_add_array_data
"or"
349 _procd_add_array_data
"eq" "target" "$1"
354 [ $multi = 1 ] && json_close_array
357 _procd_add_array_data
"run_script" /etc
/init.d
/$name $action
365 _procd_add_action_mount_trigger
() {
368 local mountpoints
="$(procd_get_mountpoints "$@
")"
369 [ "${mountpoints//[[:space:]]}" ] ||
return 0
370 local script=$
(readlink
"$initscript")
371 local name
=$
(basename ${script:-$initscript})
374 _procd_add_mount_trigger mount.add
$action "$mountpoints"
378 procd_get_mountpoints
() {
380 __procd_check_mount
() {
382 local path
="${2%%/}/"
384 config_get target
"$cfg" target
385 target
="${target%%/}/"
386 [ "$path" != "${path##$target}" ] && echo "${target%%/}"
390 for mpath
in "$@"; do
391 config_foreach __procd_check_mount mount
"$mpath"
396 _procd_add_restart_mount_trigger
() {
397 _procd_add_action_mount_trigger restart
"$@"
400 _procd_add_reload_mount_trigger
() {
401 _procd_add_action_mount_trigger reload
"$@"
404 _procd_add_raw_trigger
() {
406 _procd_add_array_data
"$1"
413 _procd_add_array_data
"run_script" "$@"
417 json_add_int
"" "$timeout"
422 _procd_add_reload_trigger
() {
423 local script=$
(readlink
"$initscript")
424 local name
=$
(basename ${script:-$initscript})
429 _procd_add_config_trigger
"config.change" "$file" /etc
/init.d
/$name reload
434 _procd_add_validation
() {
437 _procd_close_validate
440 _procd_append_param
() {
441 local type="$1"; shift
442 local _json_no_warning
=1
446 _procd_set_param
"$type" "$@"
451 _procd_add_table_data
"$@"
453 command|netdev|
file|respawn|watch|watchdog
)
454 _procd_add_array_data
"$@"
457 json_add_string
"" "$@"
463 _procd_close_instance
() {
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}
480 _procd_add_instance
() {
482 _procd_set_param
command "$@"
483 _procd_close_instance
488 local instance
="${2:-*}"
489 [ "$instance" = "*" ] || instance
="'$instance'"
492 json_add_string name
"$service"
493 local running
=$
(_procd_ubus_call list | jsonfilter
-l 1 -e "@['$service'].instances[$instance].running")
495 [ "$running" = "true" ]
503 [ -n "$service" ] && json_add_string name
"$service"
504 [ -n "$instance" ] && json_add_string instance
"$instance"
505 _procd_ubus_call delete
508 _procd_send_signal
() {
514 [A-Z
]*) signal
="$(kill -l "$signal" 2>/dev/null)" ||
return 1;;
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
530 [ -n "$service" ] && json_add_string name
"$service"
532 data
=$
(_procd_ubus_call list | jsonfilter
-e '@["'"$service"'"]')
533 [ -z "$data" ] && { echo "inactive"; return 3; }
535 data
=$
(echo "$data" | jsonfilter
-e '$.instances')
536 if [ -z "$data" ]; then
537 [ -z "$instance" ] && { echo "active with no instances"; return 0; }
541 [ -n "$instance" ] && instance
="\"$instance\"" || instance
='*'
542 if [ -z "$(echo "$data" | jsonfilter -e '$['"$instance"']')" ]; then
543 echo "unknown instance $instance"; return 4
545 echo "running"; return 0
551 json_set_namespace procd __procd_old_cb
557 json_set_namespace
$__procd_old_cb
560 _procd_set_config_changed
() {
564 json_add_string
type config.change
566 json_add_string package
"$package"
569 ubus call service event
"$(json_dump)"
572 procd_add_mdns_service
() {
573 local service proto port txt_count
=0
577 json_add_object
"${service}_$port"
578 json_add_string
"service" "_$service._$proto.local"
579 json_add_int port
"$port"
581 [ -z "$txt" ] && continue
582 txt_count
=$
((txt_count
+1))
583 [ $txt_count -eq 1 ] && json_add_array txt
584 json_add_string
"" "$txt"
586 [ $txt_count -gt 0 ] && json_select ..
593 json_add_object
"mdns"
594 procd_add_mdns_service
"$@"
599 uci_validate_section
()
607 _result
=$
(/sbin
/validate_data
"$_package" "$_type" "$_name" "$@" 2> /dev
/null
)
610 [ "$_error" = "0" ] || $
(/sbin
/validate_data
"$_package" "$_type" "$_name" "$@" 1> /dev
/null
)
614 uci_load_validate
() {
621 shift; shift; shift; shift
622 for _option
in "$@"; do
623 eval "local ${_option%%:*}"
625 uci_validate_section
"$_package" "$_type" "$_name" "$@"
627 [ -n "$_function" ] ||
return $_result
628 eval "$_function \"\$_name\" \"\$_result\""
633 procd_close_service \
635 procd_add_raw_trigger \
636 procd_add_config_trigger \
637 procd_add_interface_trigger \
638 procd_add_mount_trigger \
639 procd_add_reload_trigger \
640 procd_add_reload_interface_trigger \
641 procd_add_action_mount_trigger \
642 procd_add_reload_mount_trigger \
643 procd_add_restart_mount_trigger \
645 procd_close_trigger \
646 procd_open_instance \
647 procd_close_instance \
648 procd_open_validate \
649 procd_close_validate \
651 procd_add_jail_mount \
652 procd_add_jail_mount_rw \
655 procd_add_validation \
656 procd_set_config_changed \