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
44 json_set_namespace procd old_cb
46 json_set_namespace $old_cb
51 eval "$1() { _procd_call _$1 \"\$@\"; }"
59 [ -n "$PROCD_DEBUG" ] && json_dump >&2
60 ubus call service "$cmd" "$(json_dump)"
64 _procd_open_service() {
68 _PROCD_SERVICE="$name"
72 json_add_string name "$name"
73 [ -n "$script" ] && json_add_string script "$script"
74 json_add_object instances
77 _procd_close_service() {
82 _procd_ubus_call ${1:-set}
85 _procd_add_array_data() {
86 while [ "$#" -gt 0 ]; do
87 json_add_string "" "$1"
95 _procd_add_array_data "$@"
99 _procd_add_table_data() {
100 while [ -n "$1" ]; do
103 [ "$1" = "$val" ] && val=
104 json_add_string "$var" "$val"
112 _procd_add_table_data "$@"
116 _procd_open_instance() {
117 local name="$1"; shift
119 _PROCD_INSTANCE_SEQ="$(($_PROCD_INSTANCE_SEQ + 1))"
120 name="${name:-instance$_PROCD_INSTANCE_SEQ}"
121 json_add_object "$name"
122 [ -n "$TRACE_SYSCALLS" ] && json_add_boolean trace "1"
125 _procd_open_trigger() {
126 let '_procd_trigger_open = _procd_trigger_open + 1'
127 [ "$_procd_trigger_open" -gt 1 ] && return
128 json_add_array "triggers"
131 _procd_close_trigger() {
132 let '_procd_trigger_open = _procd_trigger_open - 1'
133 [ "$_procd_trigger_open" -lt 1 ] || return
137 _procd_open_validate() {
139 json_add_array "validate"
142 _procd_close_validate() {
148 json_add_object "jail"
149 json_add_string name "$1"
155 log) json_add_boolean "log" "1";;
156 ubus) json_add_boolean "ubus" "1";;
157 procfs) json_add_boolean "procfs" "1";;
158 sysfs) json_add_boolean "sysfs" "1";;
159 ronly) json_add_boolean "ronly" "1";;
162 json_add_object "mount"
167 _procd_add_jail_mount() {
168 local _json_no_warning=1
178 json_add_string "$a" "0"
184 _procd_add_jail_mount_rw() {
185 local _json_no_warning=1
195 json_add_string "$a" "1"
202 local type="$1"; shift
206 _procd_add_table "$type" "$@"
208 command|netdev|file|respawn|watch)
209 _procd_add_array "$type" "$@"
212 json_add_array "$type"
213 json_add_string "" "$@"
217 json_add_int "$type" "$1"
220 json_add_int "$type" $(kill -l "$1")
222 pidfile|user|seccomp|capabilities)
223 json_add_string "$type" "$1"
225 stdout|stderr|no_new_privs)
226 json_add_boolean "$type" "$1"
231 _procd_add_timeout() {
232 [ "$PROCD_RELOAD_DELAY" -gt 0 ] && json_add_int "" "$PROCD_RELOAD_DELAY"
236 _procd_add_interface_trigger() {
238 _procd_add_array_data "$1"
242 _procd_add_array_data "if"
245 _procd_add_array_data "eq" "interface" "$1"
250 _procd_add_array_data "run_script" "$@"
258 _procd_add_reload_interface_trigger() {
259 local script=$(readlink "$initscript")
260 local name=$(basename ${script:-$initscript})
263 _procd_add_interface_trigger "interface.*" $1 /etc/init.d/$name reload
267 _procd_add_config_trigger() {
269 _procd_add_array_data "$1"
273 _procd_add_array_data "if"
276 _procd_add_array_data "eq" "package" "$1"
281 _procd_add_array_data "run_script" "$@"
289 _procd_add_raw_trigger() {
291 _procd_add_array_data "$1"
298 _procd_add_array_data "run_script" "$@"
302 json_add_int "" "$timeout"
307 _procd_add_reload_trigger() {
308 local script=$(readlink "$initscript")
309 local name=$(basename ${script:-$initscript})
314 _procd_add_config_trigger "config.change" "$file" /etc/init.d/$name reload
319 _procd_add_validation() {
322 _procd_close_validate
325 _procd_append_param() {
326 local type="$1"; shift
327 local _json_no_warning=1
331 _procd_set_param "$type" "$@"
336 _procd_add_table_data "$@"
338 command|netdev|file|respawn|watch)
339 _procd_add_array_data "$@"
342 json_add_string "" "$@"
348 _procd_close_instance() {
351 if json_select respawn ; then
352 json_get_values respawn_vals
353 if [ -z "$respawn_vals" ]; then
354 local respawn_threshold=$(uci_get system.@service[0].respawn_threshold)
355 local respawn_timeout=$(uci_get system.@service[0].respawn_timeout)
356 local respawn_retry=$(uci_get system.@service[0].respawn_retry)
357 _procd_add_array_data ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
365 _procd_add_instance() {
367 _procd_set_param command "$@"
368 _procd_close_instance
376 [ -n "$service" ] && json_add_string name "$service"
377 [ -n "$instance" ] && json_add_string instance "$instance"
378 _procd_ubus_call delete
381 _procd_send_signal() {
387 json_add_string name "$service"
388 [ -n "$instance" -a "$instance" != "*" ] && json_add_string instance "$instance"
389 [ -n "$signal" ] && json_add_int signal "$signal"
390 _procd_ubus_call signal
395 json_set_namespace procd __procd_old_cb
401 json_set_namespace $__procd_old_cb
404 _procd_set_config_changed() {
408 json_add_string type config.change
410 json_add_string package "$package"
413 ubus call service event "$(json_dump)"
416 procd_add_mdns_service() {
417 local service proto port
421 json_add_object "${service}_$port"
422 json_add_string "service" "_$service._$proto.local"
423 json_add_int port "$port"
426 for txt in $@; do json_add_string "" $txt; done
434 json_add_object "mdns"
435 procd_add_mdns_service $@
440 uci_validate_section()
448 _result=`/sbin/validate_data "$_package" "$_type" "$_name" "$@" 2> /dev/null`
451 [ "$_error" = "0" ] || `/sbin/validate_data "$_package" "$_type" "$_name" "$@" 1> /dev/null`
457 procd_close_service \
459 procd_add_raw_trigger \
460 procd_add_config_trigger \
461 procd_add_interface_trigger \
462 procd_add_reload_trigger \
463 procd_add_reload_interface_trigger \
465 procd_close_trigger \
466 procd_open_instance \
467 procd_close_instance \
468 procd_open_validate \
469 procd_close_validate \
471 procd_add_jail_mount \
472 procd_add_jail_mount_rw \
475 procd_add_validation \
476 procd_set_config_changed \