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
23 # stdout: boolean whether to redirect commands stdout to syslog (default: 0)
24 # stderr: boolean whether to redirect commands stderr to syslog (default: 0)
25 # facility: syslog facility used when logging to syslog (default: daemon)
27 # No space separation is done for arrays/tables - use one function argument per command line argument
29 # procd_close_instance():
30 # Complete the instance being prepared
32 # procd_kill(service, [instance]):
33 # Kill a service instance (or all instances)
35 # procd_send_signal(service, [instance], [signal])
36 # Send a signal to a service instance (or all instances)
39 .
"$IPKG_INSTROOT/usr/share/libubox/jshn.sh"
41 PROCD_RELOAD_DELAY
=1000
45 local basescript
=$
(readlink
"$initscript")
46 local service_name
="$(basename ${basescript:-$initscript})"
48 flock
-n 1000 &> /dev
/null
49 if [ "$?" != "0" ]; then
50 exec 1000>"$IPKG_INSTROOT/var/lock/procd_${service_name}.lock"
52 if [ "$?" != "0" ]; then
53 logger
"warning: procd flock for $service_name failed"
61 json_set_namespace procd old_cb
63 json_set_namespace
$old_cb
69 eval "$1() { _procd_call _$1 \"\$@\"; }"
77 [ -n "$PROCD_DEBUG" ] && json_dump
>&2
78 ubus call service
"$cmd" "$(json_dump)"
82 _procd_open_service
() {
86 _PROCD_SERVICE
="$name"
90 json_add_string name
"$name"
91 [ -n "$script" ] && json_add_string
script "$script"
92 json_add_object instances
95 _procd_close_service
() {
103 _procd_ubus_call
${1:-set}
106 _procd_add_array_data
() {
107 while [ "$#" -gt 0 ]; do
108 json_add_string
"" "$1"
116 _procd_add_array_data
"$@"
120 _procd_add_table_data
() {
121 while [ -n "$1" ]; do
124 [ "$1" = "$val" ] && val
=
125 json_add_string
"$var" "$val"
133 _procd_add_table_data
"$@"
137 _procd_open_instance
() {
138 local name
="$1"; shift
140 _PROCD_INSTANCE_SEQ
="$(($_PROCD_INSTANCE_SEQ + 1))"
141 name
="${name:-instance$_PROCD_INSTANCE_SEQ}"
142 json_add_object
"$name"
143 [ -n "$TRACE_SYSCALLS" ] && json_add_boolean trace
"1"
146 _procd_open_trigger
() {
147 let '_procd_trigger_open = _procd_trigger_open + 1'
148 [ "$_procd_trigger_open" -gt 1 ] && return
149 json_add_array
"triggers"
152 _procd_close_trigger
() {
153 let '_procd_trigger_open = _procd_trigger_open - 1'
154 [ "$_procd_trigger_open" -lt 1 ] ||
return
159 let '_procd_data_open = _procd_data_open + 1'
160 [ "$_procd_data_open" -gt 1 ] && return
161 json_add_object
"data"
164 _procd_close_data
() {
165 let '_procd_data_open = _procd_data_open - 1'
166 [ "$_procd_data_open" -lt 1 ] ||
return
170 _procd_open_validate
() {
172 json_add_array
"validate"
175 _procd_close_validate
() {
181 json_add_object
"jail"
182 json_add_string name
"$1"
188 log
) json_add_boolean
"log" "1";;
189 ubus
) json_add_boolean
"ubus" "1";;
190 procfs
) json_add_boolean
"procfs" "1";;
191 sysfs
) json_add_boolean
"sysfs" "1";;
192 ronly
) json_add_boolean
"ronly" "1";;
195 json_add_object
"mount"
200 _procd_add_jail_mount
() {
201 local _json_no_warning
=1
211 json_add_string
"$a" "0"
217 _procd_add_jail_mount_rw
() {
218 local _json_no_warning
=1
228 json_add_string
"$a" "1"
235 local type="$1"; shift
239 _procd_add_table
"$type" "$@"
241 command|netdev|
file|respawn|watch
)
242 _procd_add_array
"$type" "$@"
245 json_add_array
"$type"
246 json_add_string
"" "$@"
250 json_add_int
"$type" "$1"
253 json_add_int
"$type" $
(kill -l "$1")
255 pidfile|user|seccomp|capabilities|facility
)
256 json_add_string
"$type" "$1"
258 stdout|stderr|no_new_privs
)
259 json_add_boolean
"$type" "$1"
264 _procd_add_timeout
() {
265 [ "$PROCD_RELOAD_DELAY" -gt 0 ] && json_add_int
"" "$PROCD_RELOAD_DELAY"
269 _procd_add_interface_trigger
() {
271 _procd_add_array_data
"$1"
275 _procd_add_array_data
"if"
278 _procd_add_array_data
"eq" "interface" "$1"
283 _procd_add_array_data
"run_script" "$@"
291 _procd_add_reload_interface_trigger
() {
292 local script=$
(readlink
"$initscript")
293 local name
=$
(basename ${script:-$initscript})
296 _procd_add_interface_trigger
"interface.*" $1 /etc
/init.d
/$name reload
300 _procd_add_config_trigger
() {
302 _procd_add_array_data
"$1"
306 _procd_add_array_data
"if"
309 _procd_add_array_data
"eq" "package" "$1"
314 _procd_add_array_data
"run_script" "$@"
322 _procd_add_raw_trigger
() {
324 _procd_add_array_data
"$1"
331 _procd_add_array_data
"run_script" "$@"
335 json_add_int
"" "$timeout"
340 _procd_add_reload_trigger
() {
341 local script=$
(readlink
"$initscript")
342 local name
=$
(basename ${script:-$initscript})
347 _procd_add_config_trigger
"config.change" "$file" /etc
/init.d
/$name reload
352 _procd_add_validation
() {
355 _procd_close_validate
358 _procd_append_param
() {
359 local type="$1"; shift
360 local _json_no_warning
=1
364 _procd_set_param
"$type" "$@"
369 _procd_add_table_data
"$@"
371 command|netdev|
file|respawn|watch
)
372 _procd_add_array_data
"$@"
375 json_add_string
"" "$@"
381 _procd_close_instance
() {
384 if json_select respawn
; then
385 json_get_values respawn_vals
386 if [ -z "$respawn_vals" ]; then
387 local respawn_threshold
=$
(uci_get system.@service
[0].respawn_threshold
)
388 local respawn_timeout
=$
(uci_get system.@service
[0].respawn_timeout
)
389 local respawn_retry
=$
(uci_get system.@service
[0].respawn_retry
)
390 _procd_add_array_data
${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
398 _procd_add_instance
() {
400 _procd_set_param
command "$@"
401 _procd_close_instance
409 [ -n "$service" ] && json_add_string name
"$service"
410 [ -n "$instance" ] && json_add_string instance
"$instance"
411 _procd_ubus_call delete
414 _procd_send_signal
() {
420 [A-Z
]*) signal
="$(kill -l "$signal" 2>/dev/null)" ||
return 1;;
424 json_add_string name
"$service"
425 [ -n "$instance" -a "$instance" != "*" ] && json_add_string instance
"$instance"
426 [ -n "$signal" ] && json_add_int signal
"$signal"
427 _procd_ubus_call signal
432 json_set_namespace procd __procd_old_cb
438 json_set_namespace
$__procd_old_cb
441 _procd_set_config_changed
() {
445 json_add_string
type config.change
447 json_add_string package
"$package"
450 ubus call service event
"$(json_dump)"
453 procd_add_mdns_service
() {
454 local service proto port
458 json_add_object
"${service}_$port"
459 json_add_string
"service" "_$service._$proto.local"
460 json_add_int port
"$port"
463 for txt
in "$@"; do json_add_string
"" "$txt"; done
471 json_add_object
"mdns"
472 procd_add_mdns_service
"$@"
477 uci_validate_section
()
485 _result
=`/sbin/validate_data "$_package" "$_type" "$_name" "$@" 2> /dev/null`
488 [ "$_error" = "0" ] ||
`/sbin/validate_data "$_package" "$_type" "$_name" "$@" 1> /dev/null`
492 uci_load_validate
() {
499 shift; shift; shift; shift
500 for _option
in "$@"; do
501 eval "local ${_option%%:*}"
503 uci_validate_section
"$_package" "$_type" "$_name" "$@"
505 [ -n "$_function" ] ||
return $_result
506 eval "$_function \"\$_name\" \"\$_result\""
511 procd_close_service \
513 procd_add_raw_trigger \
514 procd_add_config_trigger \
515 procd_add_interface_trigger \
516 procd_add_reload_trigger \
517 procd_add_reload_interface_trigger \
519 procd_close_trigger \
520 procd_open_instance \
521 procd_close_instance \
522 procd_open_validate \
523 procd_close_validate \
525 procd_add_jail_mount \
526 procd_add_jail_mount_rw \
529 procd_add_validation \
530 procd_set_config_changed \