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 info: array with 1 values $username
22 # pidfile: file name to write pid into
24 # No space separation is done for arrays/tables - use one function argument per command line argument
26 # procd_close_instance():
27 # Complete the instance being prepared
29 # procd_kill(service, [instance]):
30 # Kill a service instance (or all instances)
32 # procd_send_signal(service, [instance], [signal])
33 # Send a signal to a service instance (or all instances)
36 .
"$IPKG_INSTROOT/usr/share/libubox/jshn.sh"
38 PROCD_RELOAD_DELAY
=1000
42 local basescript
=$
(readlink
"$initscript")
43 local service_name
="$(basename ${basescript:-$initscript})"
45 flock
-n 1000 &> /dev
/null
46 if [ "$?" != "0" ]; then
47 exec 1000>"$IPKG_INSTROOT/var/lock/procd_${service_name}.lock"
49 if [ "$?" != "0" ]; then
50 logger
"warning: procd flock for $service_name failed"
58 json_set_namespace procd old_cb
60 json_set_namespace
$old_cb
66 eval "$1() { _procd_call _$1 \"\$@\"; }"
74 [ -n "$PROCD_DEBUG" ] && json_dump
>&2
75 ubus call service
"$cmd" "$(json_dump)"
79 _procd_open_service
() {
83 _PROCD_SERVICE
="$name"
87 json_add_string name
"$name"
88 [ -n "$script" ] && json_add_string
script "$script"
89 json_add_object instances
92 _procd_close_service
() {
100 _procd_ubus_call
${1:-set}
103 _procd_add_array_data
() {
104 while [ "$#" -gt 0 ]; do
105 json_add_string
"" "$1"
113 _procd_add_array_data
"$@"
117 _procd_add_table_data
() {
118 while [ -n "$1" ]; do
121 [ "$1" = "$val" ] && val
=
122 json_add_string
"$var" "$val"
130 _procd_add_table_data
"$@"
134 _procd_open_instance
() {
135 local name
="$1"; shift
137 _PROCD_INSTANCE_SEQ
="$(($_PROCD_INSTANCE_SEQ + 1))"
138 name
="${name:-instance$_PROCD_INSTANCE_SEQ}"
139 json_add_object
"$name"
140 [ -n "$TRACE_SYSCALLS" ] && json_add_boolean trace
"1"
143 _procd_open_trigger
() {
144 let '_procd_trigger_open = _procd_trigger_open + 1'
145 [ "$_procd_trigger_open" -gt 1 ] && return
146 json_add_array
"triggers"
149 _procd_close_trigger
() {
150 let '_procd_trigger_open = _procd_trigger_open - 1'
151 [ "$_procd_trigger_open" -lt 1 ] ||
return
156 let '_procd_data_open = _procd_data_open + 1'
157 [ "$_procd_data_open" -gt 1 ] && return
158 json_add_object
"data"
161 _procd_close_data
() {
162 let '_procd_data_open = _procd_data_open - 1'
163 [ "$_procd_data_open" -lt 1 ] ||
return
167 _procd_open_validate
() {
169 json_add_array
"validate"
172 _procd_close_validate
() {
178 json_add_object
"jail"
179 json_add_string name
"$1"
185 log
) json_add_boolean
"log" "1";;
186 ubus
) json_add_boolean
"ubus" "1";;
187 procfs
) json_add_boolean
"procfs" "1";;
188 sysfs
) json_add_boolean
"sysfs" "1";;
189 ronly
) json_add_boolean
"ronly" "1";;
192 json_add_object
"mount"
197 _procd_add_jail_mount
() {
198 local _json_no_warning
=1
208 json_add_string
"$a" "0"
214 _procd_add_jail_mount_rw
() {
215 local _json_no_warning
=1
225 json_add_string
"$a" "1"
232 local type="$1"; shift
236 _procd_add_table
"$type" "$@"
238 command|netdev|
file|respawn|watch
)
239 _procd_add_array
"$type" "$@"
242 json_add_array
"$type"
243 json_add_string
"" "$@"
247 json_add_int
"$type" "$1"
250 json_add_int
"$type" $
(kill -l "$1")
252 pidfile|user|seccomp|capabilities
)
253 json_add_string
"$type" "$1"
255 stdout|stderr|no_new_privs
)
256 json_add_boolean
"$type" "$1"
261 _procd_add_timeout
() {
262 [ "$PROCD_RELOAD_DELAY" -gt 0 ] && json_add_int
"" "$PROCD_RELOAD_DELAY"
266 _procd_add_interface_trigger
() {
268 _procd_add_array_data
"$1"
272 _procd_add_array_data
"if"
275 _procd_add_array_data
"eq" "interface" "$1"
280 _procd_add_array_data
"run_script" "$@"
288 _procd_add_reload_interface_trigger
() {
289 local script=$
(readlink
"$initscript")
290 local name
=$
(basename ${script:-$initscript})
293 _procd_add_interface_trigger
"interface.*" $1 /etc
/init.d
/$name reload
297 _procd_add_config_trigger
() {
299 _procd_add_array_data
"$1"
303 _procd_add_array_data
"if"
306 _procd_add_array_data
"eq" "package" "$1"
311 _procd_add_array_data
"run_script" "$@"
319 _procd_add_raw_trigger
() {
321 _procd_add_array_data
"$1"
328 _procd_add_array_data
"run_script" "$@"
332 json_add_int
"" "$timeout"
337 _procd_add_reload_trigger
() {
338 local script=$
(readlink
"$initscript")
339 local name
=$
(basename ${script:-$initscript})
344 _procd_add_config_trigger
"config.change" "$file" /etc
/init.d
/$name reload
349 _procd_add_validation
() {
352 _procd_close_validate
355 _procd_append_param
() {
356 local type="$1"; shift
357 local _json_no_warning
=1
361 _procd_set_param
"$type" "$@"
366 _procd_add_table_data
"$@"
368 command|netdev|
file|respawn|watch
)
369 _procd_add_array_data
"$@"
372 json_add_string
"" "$@"
378 _procd_close_instance
() {
381 if json_select respawn
; then
382 json_get_values respawn_vals
383 if [ -z "$respawn_vals" ]; then
384 local respawn_threshold
=$
(uci_get system.@service
[0].respawn_threshold
)
385 local respawn_timeout
=$
(uci_get system.@service
[0].respawn_timeout
)
386 local respawn_retry
=$
(uci_get system.@service
[0].respawn_retry
)
387 _procd_add_array_data
${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
395 _procd_add_instance
() {
397 _procd_set_param
command "$@"
398 _procd_close_instance
406 [ -n "$service" ] && json_add_string name
"$service"
407 [ -n "$instance" ] && json_add_string instance
"$instance"
408 _procd_ubus_call delete
411 _procd_send_signal
() {
417 [A-Z
]*) signal
="$(kill -l "$signal" 2>/dev/null)" ||
return 1;;
421 json_add_string name
"$service"
422 [ -n "$instance" -a "$instance" != "*" ] && json_add_string instance
"$instance"
423 [ -n "$signal" ] && json_add_int signal
"$signal"
424 _procd_ubus_call signal
429 json_set_namespace procd __procd_old_cb
435 json_set_namespace
$__procd_old_cb
438 _procd_set_config_changed
() {
442 json_add_string
type config.change
444 json_add_string package
"$package"
447 ubus call service event
"$(json_dump)"
450 procd_add_mdns_service
() {
451 local service proto port
455 json_add_object
"${service}_$port"
456 json_add_string
"service" "_$service._$proto.local"
457 json_add_int port
"$port"
460 for txt
in "$@"; do json_add_string
"" "$txt"; done
468 json_add_object
"mdns"
469 procd_add_mdns_service
"$@"
474 uci_validate_section
()
482 _result
=`/sbin/validate_data "$_package" "$_type" "$_name" "$@" 2> /dev/null`
485 [ "$_error" = "0" ] ||
`/sbin/validate_data "$_package" "$_type" "$_name" "$@" 1> /dev/null`
489 uci_load_validate
() {
496 shift; shift; shift; shift
497 for _option
in "$@"; do
498 eval "local ${_option%%:*}"
500 uci_validate_section
"$_package" "$_type" "$_name" "$@"
502 [ -n "$_function" ] ||
return $_result
503 eval "$_function \"\$_name\" \"\$_result\""
508 procd_close_service \
510 procd_add_raw_trigger \
511 procd_add_config_trigger \
512 procd_add_interface_trigger \
513 procd_add_reload_trigger \
514 procd_add_reload_interface_trigger \
516 procd_close_trigger \
517 procd_open_instance \
518 procd_close_instance \
519 procd_open_validate \
520 procd_close_validate \
522 procd_add_jail_mount \
523 procd_add_jail_mount_rw \
526 procd_add_validation \
527 procd_set_config_changed \