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)
33 . $IPKG_INSTROOT/usr/share/libubox/jshn.sh
40 json_set_namespace procd old_cb
42 json_set_namespace $old_cb
47 eval "$1() { _procd_call _$1 \"\$@\"; }"
55 [ -n "$PROCD_DEBUG" ] && json_dump >&2
56 ubus call service "$cmd" "$(json_dump)"
60 _procd_open_service() {
64 _PROCD_SERVICE="$name"
68 json_add_string name "$name"
69 [ -n "$script" ] && json_add_string script "$script"
70 json_add_object instances
73 _procd_close_service() {
81 _procd_add_array_data() {
82 while [ "$#" -gt 0 ]; do
83 json_add_string "" "$1"
91 _procd_add_array_data "$@"
95 _procd_add_table_data() {
99 [ "$1" = "$val" ] && val=
100 json_add_string "$var" "$val"
108 _procd_add_table_data "$@"
112 _procd_open_instance() {
113 local name="$1"; shift
115 _PROCD_INSTANCE_SEQ="$(($_PROCD_INSTANCE_SEQ + 1))"
116 name="${name:-instance$_PROCD_INSTANCE_SEQ}"
117 json_add_object "$name"
118 [ -n "$TRACE_SYSCALLS" ] && json_add_boolean trace "1"
121 _procd_open_trigger() {
122 let '_procd_trigger_open = _procd_trigger_open + 1'
123 [ "$_procd_trigger_open" -gt 1 ] && return
124 json_add_array "triggers"
127 _procd_close_trigger() {
128 let '_procd_trigger_open = _procd_trigger_open - 1'
129 [ "$_procd_trigger_open" -lt 1 ] || return
133 _procd_open_validate() {
135 json_add_array "validate"
138 _procd_close_validate() {
144 json_add_object "jail"
145 json_add_string name "$1"
151 log) json_add_boolean "log" "1";;
152 ubus) json_add_boolean "ubus" "1";;
153 procfs) json_add_boolean "procfs" "1";;
154 sysfs) json_add_boolean "sysfs" "1";;
155 ronly) json_add_boolean "ronly" "1";;
158 json_add_object "mount"
163 _procd_add_jail_mount() {
164 local _json_no_warning=1
174 json_add_string "$a" "0"
180 _procd_add_jail_mount_rw() {
181 local _json_no_warning=1
191 json_add_string "$a" "1"
198 local type="$1"; shift
202 _procd_add_table "$type" "$@"
204 command|netdev|file|respawn|watch)
205 _procd_add_array "$type" "$@"
208 json_add_array "$type"
209 json_add_string "" "$@"
213 json_add_int "$type" "$1"
215 pidfile|user|seccomp|capabilities)
216 json_add_string "$type" "$1"
218 stdout|stderr|no_new_privs)
219 json_add_boolean "$type" "$1"
224 _procd_add_interface_trigger() {
226 _procd_add_array_data "$1"
230 _procd_add_array_data "if"
233 _procd_add_array_data "eq" "interface" "$1"
238 _procd_add_array_data "run_script" "$@"
245 _procd_add_reload_interface_trigger() {
246 local script=$(readlink "$initscript")
247 local name=$(basename ${script:-$initscript})
250 _procd_add_interface_trigger "interface.*" $1 /etc/init.d/$name reload
254 _procd_add_config_trigger() {
256 _procd_add_array_data "$1"
260 _procd_add_array_data "if"
263 _procd_add_array_data "eq" "package" "$1"
268 _procd_add_array_data "run_script" "$@"
276 _procd_add_raw_trigger() {
278 _procd_add_array_data "$1"
285 _procd_add_array_data "run_script" "$@"
289 json_add_int "" "$timeout"
294 _procd_add_reload_trigger() {
295 local script=$(readlink "$initscript")
296 local name=$(basename ${script:-$initscript})
301 _procd_add_config_trigger "config.change" "$file" /etc/init.d/$name reload
306 _procd_add_validation() {
309 _procd_close_validate
312 _procd_append_param() {
313 local type="$1"; shift
314 local _json_no_warning=1
318 _procd_set_param "$type" "$@"
323 _procd_add_table_data "$@"
325 command|netdev|file|respawn|watch)
326 _procd_add_array_data "$@"
329 json_add_string "" "$@"
335 _procd_close_instance() {
338 if json_select respawn ; then
339 json_get_values respawn_vals
340 if [ -z "$respawn_vals" ]; then
341 local respawn_retry=$(uci_get system.@service[0].respawn_retry)
342 _procd_add_array_data 3600 5 ${respawn_retry:-5}
350 _procd_add_instance() {
352 _procd_set_param command "$@"
353 _procd_close_instance
361 [ -n "$service" ] && json_add_string name "$service"
362 [ -n "$instance" ] && json_add_string instance "$instance"
363 _procd_ubus_call delete
368 json_set_namespace procd __procd_old_cb
374 json_set_namespace $__procd_old_cb
377 _procd_set_config_changed() {
381 json_add_string type config.change
383 json_add_string package "$package"
386 ubus call service event "$(json_dump)"
389 procd_add_mdns_service() {
390 local service proto port
394 json_add_object "${service}_$port"
395 json_add_string "service" "_$service._$proto.local"
396 json_add_int port "$port"
399 for txt in $@; do json_add_string "" $txt; done
407 json_add_object "mdns"
408 procd_add_mdns_service $@
413 uci_validate_section()
421 _result=`/sbin/validate_data "$_package" "$_type" "$_name" "$@" 2> /dev/null`
424 [ "$_error" = "0" ] || `/sbin/validate_data "$_package" "$_type" "$_name" "$@" 1> /dev/null`
430 procd_close_service \
432 procd_add_raw_trigger \
433 procd_add_config_trigger \
434 procd_add_interface_trigger \
435 procd_add_reload_trigger \
436 procd_add_reload_interface_trigger \
438 procd_close_trigger \
439 procd_open_instance \
440 procd_close_instance \
441 procd_open_validate \
442 procd_close_validate \
444 procd_add_jail_mount \
445 procd_add_jail_mount_rw \
448 procd_add_validation \
449 procd_set_config_changed \