2012-12-22 18:56:06 +00:00
|
|
|
# procd API:
|
|
|
|
#
|
|
|
|
# procd_open_service(name, [script]):
|
|
|
|
# Initialize a new procd command message containing a service with one or more instances
|
|
|
|
#
|
|
|
|
# procd_close_service()
|
|
|
|
# Send the command message for the service
|
|
|
|
#
|
|
|
|
# procd_open_instance([name]):
|
|
|
|
# Add an instance to the service described by the previous procd_open_service call
|
|
|
|
#
|
|
|
|
# procd_set_param(type, [value...])
|
|
|
|
# Available types:
|
|
|
|
# command: command line (array).
|
2015-06-14 17:41:07 +00:00
|
|
|
# respawn info: array with 3 values $fail_threshold $restart_timeout $max_fail
|
2012-12-22 18:56:06 +00:00
|
|
|
# env: environment variable (passed to the process)
|
|
|
|
# data: arbitrary name/value pairs for detecting config changes (table)
|
|
|
|
# file: configuration files (array)
|
|
|
|
# netdev: bound network device (detects ifindex changes)
|
2013-12-09 17:29:34 +00:00
|
|
|
# limits: resource limits (passed to the process)
|
2014-09-11 12:28:00 +00:00
|
|
|
# user info: array with 1 values $username
|
2016-03-10 19:11:17 +00:00
|
|
|
# pidfile: file name to write pid into
|
2012-12-22 18:56:06 +00:00
|
|
|
#
|
|
|
|
# No space separation is done for arrays/tables - use one function argument per command line argument
|
|
|
|
#
|
|
|
|
# procd_close_instance():
|
|
|
|
# Complete the instance being prepared
|
|
|
|
#
|
|
|
|
# procd_kill(service, [instance]):
|
|
|
|
# Kill a service instance (or all instances)
|
|
|
|
#
|
|
|
|
|
|
|
|
. $IPKG_INSTROOT/usr/share/libubox/jshn.sh
|
|
|
|
|
2016-07-29 12:19:17 +00:00
|
|
|
PROCD_RELOAD_DELAY=1000
|
2012-12-22 18:56:06 +00:00
|
|
|
_PROCD_SERVICE=
|
|
|
|
|
|
|
|
_procd_call() {
|
|
|
|
local old_cb
|
|
|
|
|
|
|
|
json_set_namespace procd old_cb
|
|
|
|
"$@"
|
|
|
|
json_set_namespace $old_cb
|
|
|
|
}
|
|
|
|
|
|
|
|
_procd_wrapper() {
|
|
|
|
while [ -n "$1" ]; do
|
|
|
|
eval "$1() { _procd_call _$1 \"\$@\"; }"
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
_procd_ubus_call() {
|
|
|
|
local cmd="$1"
|
|
|
|
|
2014-03-21 15:54:18 +00:00
|
|
|
[ -n "$PROCD_DEBUG" ] && json_dump >&2
|
2012-12-22 18:56:06 +00:00
|
|
|
ubus call service "$cmd" "$(json_dump)"
|
|
|
|
json_cleanup
|
|
|
|
}
|
|
|
|
|
|
|
|
_procd_open_service() {
|
|
|
|
local name="$1"
|
|
|
|
local script="$2"
|
|
|
|
|
|
|
|
_PROCD_SERVICE="$name"
|
|
|
|
_PROCD_INSTANCE_SEQ=0
|
|
|
|
|
|
|
|
json_init
|
|
|
|
json_add_string name "$name"
|
|
|
|
[ -n "$script" ] && json_add_string script "$script"
|
|
|
|
json_add_object instances
|
|
|
|
}
|
|
|
|
|
|
|
|
_procd_close_service() {
|
|
|
|
json_close_object
|
2016-07-29 11:00:56 +00:00
|
|
|
_procd_open_trigger
|
2013-07-11 17:00:54 +00:00
|
|
|
service_triggers
|
2016-07-29 11:00:56 +00:00
|
|
|
_procd_close_trigger
|
2016-10-13 10:52:05 +00:00
|
|
|
_procd_ubus_call ${1:-set}
|
2012-12-22 18:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_procd_add_array_data() {
|
2014-09-10 12:56:19 +00:00
|
|
|
while [ "$#" -gt 0 ]; do
|
2012-12-22 18:56:06 +00:00
|
|
|
json_add_string "" "$1"
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
_procd_add_array() {
|
|
|
|
json_add_array "$1"
|
|
|
|
shift
|
|
|
|
_procd_add_array_data "$@"
|
|
|
|
json_close_array
|
|
|
|
}
|
|
|
|
|
|
|
|
_procd_add_table_data() {
|
|
|
|
while [ -n "$1" ]; do
|
|
|
|
local var="${1%%=*}"
|
|
|
|
local val="${1#*=}"
|
2014-10-14 12:21:11 +00:00
|
|
|
[ "$1" = "$val" ] && val=
|
2012-12-22 18:56:06 +00:00
|
|
|
json_add_string "$var" "$val"
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
_procd_add_table() {
|
|
|
|
json_add_object "$1"
|
|
|
|
shift
|
|
|
|
_procd_add_table_data "$@"
|
|
|
|
json_close_object
|
|
|
|
}
|
|
|
|
|
|
|
|
_procd_open_instance() {
|
|
|
|
local name="$1"; shift
|
|
|
|
|
|
|
|
_PROCD_INSTANCE_SEQ="$(($_PROCD_INSTANCE_SEQ + 1))"
|
|
|
|
name="${name:-instance$_PROCD_INSTANCE_SEQ}"
|
|
|
|
json_add_object "$name"
|
2015-03-26 10:58:25 +00:00
|
|
|
[ -n "$TRACE_SYSCALLS" ] && json_add_boolean trace "1"
|
2012-12-22 18:56:06 +00:00
|
|
|
}
|
|
|
|
|
2013-07-04 13:30:55 +00:00
|
|
|
_procd_open_trigger() {
|
2016-07-29 11:00:56 +00:00
|
|
|
let '_procd_trigger_open = _procd_trigger_open + 1'
|
|
|
|
[ "$_procd_trigger_open" -gt 1 ] && return
|
2013-07-04 13:30:55 +00:00
|
|
|
json_add_array "triggers"
|
|
|
|
}
|
|
|
|
|
2016-07-29 11:00:56 +00:00
|
|
|
_procd_close_trigger() {
|
|
|
|
let '_procd_trigger_open = _procd_trigger_open - 1'
|
|
|
|
[ "$_procd_trigger_open" -lt 1 ] || return
|
|
|
|
json_close_array
|
|
|
|
}
|
|
|
|
|
2013-11-13 10:49:25 +00:00
|
|
|
_procd_open_validate() {
|
2016-07-29 11:00:56 +00:00
|
|
|
json_select ..
|
2013-11-13 10:49:25 +00:00
|
|
|
json_add_array "validate"
|
|
|
|
}
|
|
|
|
|
2016-07-29 11:00:56 +00:00
|
|
|
_procd_close_validate() {
|
|
|
|
json_close_array
|
|
|
|
json_select triggers
|
|
|
|
}
|
|
|
|
|
2015-03-26 10:58:25 +00:00
|
|
|
_procd_add_jail() {
|
|
|
|
json_add_object "jail"
|
|
|
|
json_add_string name "$1"
|
|
|
|
|
|
|
|
shift
|
|
|
|
|
|
|
|
for a in $@; do
|
|
|
|
case $a in
|
|
|
|
log) json_add_boolean "log" "1";;
|
|
|
|
ubus) json_add_boolean "ubus" "1";;
|
|
|
|
procfs) json_add_boolean "procfs" "1";;
|
|
|
|
sysfs) json_add_boolean "sysfs" "1";;
|
2015-12-11 15:08:19 +00:00
|
|
|
ronly) json_add_boolean "ronly" "1";;
|
2015-03-26 10:58:25 +00:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
json_add_object "mount"
|
|
|
|
json_close_object
|
|
|
|
json_close_object
|
|
|
|
}
|
|
|
|
|
|
|
|
_procd_add_jail_mount() {
|
|
|
|
local _json_no_warning=1
|
|
|
|
|
|
|
|
json_select "jail"
|
|
|
|
[ $? = 0 ] || return
|
|
|
|
json_select "mount"
|
|
|
|
[ $? = 0 ] || {
|
|
|
|
json_select ..
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for a in $@; do
|
|
|
|
json_add_string "$a" "0"
|
|
|
|
done
|
|
|
|
json_select ..
|
|
|
|
json_select ..
|
|
|
|
}
|
|
|
|
|
|
|
|
_procd_add_jail_mount_rw() {
|
|
|
|
local _json_no_warning=1
|
|
|
|
|
|
|
|
json_select "jail"
|
|
|
|
[ $? = 0 ] || return
|
|
|
|
json_select "mount"
|
|
|
|
[ $? = 0 ] || {
|
|
|
|
json_select ..
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for a in $@; do
|
|
|
|
json_add_string "$a" "1"
|
|
|
|
done
|
|
|
|
json_select ..
|
|
|
|
json_select ..
|
|
|
|
}
|
|
|
|
|
2012-12-22 18:56:06 +00:00
|
|
|
_procd_set_param() {
|
|
|
|
local type="$1"; shift
|
|
|
|
|
|
|
|
case "$type" in
|
2013-12-09 17:29:34 +00:00
|
|
|
env|data|limits)
|
2012-12-22 18:56:06 +00:00
|
|
|
_procd_add_table "$type" "$@"
|
|
|
|
;;
|
2014-06-05 10:56:06 +00:00
|
|
|
command|netdev|file|respawn|watch)
|
2012-12-22 18:56:06 +00:00
|
|
|
_procd_add_array "$type" "$@"
|
|
|
|
;;
|
2014-06-06 09:34:30 +00:00
|
|
|
error)
|
|
|
|
json_add_array "$type"
|
|
|
|
json_add_string "" "$@"
|
|
|
|
json_close_array
|
|
|
|
;;
|
2012-12-22 18:56:06 +00:00
|
|
|
nice)
|
|
|
|
json_add_int "$type" "$1"
|
|
|
|
;;
|
2016-03-10 19:11:17 +00:00
|
|
|
pidfile|user|seccomp|capabilities)
|
2014-09-11 12:28:00 +00:00
|
|
|
json_add_string "$type" "$1"
|
|
|
|
;;
|
2015-12-11 15:08:19 +00:00
|
|
|
stdout|stderr|no_new_privs)
|
2015-02-26 13:42:47 +00:00
|
|
|
json_add_boolean "$type" "$1"
|
|
|
|
;;
|
2012-12-22 18:56:06 +00:00
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2016-07-29 12:19:17 +00:00
|
|
|
_procd_add_timeout() {
|
|
|
|
[ "$PROCD_RELOAD_DELAY" -gt 0 ] && json_add_int "" "$PROCD_RELOAD_DELAY"
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2014-06-05 10:56:06 +00:00
|
|
|
_procd_add_interface_trigger() {
|
|
|
|
json_add_array
|
|
|
|
_procd_add_array_data "$1"
|
|
|
|
shift
|
|
|
|
|
|
|
|
json_add_array
|
|
|
|
_procd_add_array_data "if"
|
|
|
|
|
|
|
|
json_add_array
|
|
|
|
_procd_add_array_data "eq" "interface" "$1"
|
|
|
|
shift
|
|
|
|
json_close_array
|
|
|
|
|
|
|
|
json_add_array
|
|
|
|
_procd_add_array_data "run_script" "$@"
|
|
|
|
json_close_array
|
|
|
|
|
|
|
|
json_close_array
|
|
|
|
json_close_array
|
2016-07-29 12:19:17 +00:00
|
|
|
|
|
|
|
_procd_add_timeout
|
2014-06-05 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
2014-06-16 18:55:05 +00:00
|
|
|
_procd_add_reload_interface_trigger() {
|
2014-06-05 10:56:06 +00:00
|
|
|
local script=$(readlink "$initscript")
|
|
|
|
local name=$(basename ${script:-$initscript})
|
|
|
|
|
|
|
|
_procd_open_trigger
|
|
|
|
_procd_add_interface_trigger "interface.*" $1 /etc/init.d/$name reload
|
|
|
|
_procd_close_trigger
|
|
|
|
}
|
|
|
|
|
2013-07-04 13:30:55 +00:00
|
|
|
_procd_add_config_trigger() {
|
|
|
|
json_add_array
|
2013-11-13 10:49:25 +00:00
|
|
|
_procd_add_array_data "$1"
|
|
|
|
shift
|
2013-07-04 13:30:55 +00:00
|
|
|
|
|
|
|
json_add_array
|
|
|
|
_procd_add_array_data "if"
|
|
|
|
|
|
|
|
json_add_array
|
|
|
|
_procd_add_array_data "eq" "package" "$1"
|
|
|
|
shift
|
|
|
|
json_close_array
|
|
|
|
|
|
|
|
json_add_array
|
|
|
|
_procd_add_array_data "run_script" "$@"
|
|
|
|
json_close_array
|
|
|
|
|
|
|
|
json_close_array
|
|
|
|
|
|
|
|
json_close_array
|
2016-07-29 12:19:17 +00:00
|
|
|
|
|
|
|
_procd_add_timeout
|
2013-07-04 13:30:55 +00:00
|
|
|
}
|
|
|
|
|
2014-08-29 18:16:24 +00:00
|
|
|
_procd_add_raw_trigger() {
|
|
|
|
json_add_array
|
|
|
|
_procd_add_array_data "$1"
|
|
|
|
shift
|
|
|
|
local timeout=$1
|
|
|
|
shift
|
|
|
|
|
|
|
|
json_add_array
|
|
|
|
json_add_array
|
|
|
|
_procd_add_array_data "run_script" "$@"
|
|
|
|
json_close_array
|
|
|
|
json_close_array
|
|
|
|
|
|
|
|
json_add_int "" "$timeout"
|
|
|
|
|
|
|
|
json_close_array
|
|
|
|
}
|
|
|
|
|
2013-09-17 21:45:24 +00:00
|
|
|
_procd_add_reload_trigger() {
|
|
|
|
local script=$(readlink "$initscript")
|
|
|
|
local name=$(basename ${script:-$initscript})
|
2014-06-11 17:05:46 +00:00
|
|
|
local file
|
2013-09-17 21:45:24 +00:00
|
|
|
|
2013-11-13 10:49:25 +00:00
|
|
|
_procd_open_trigger
|
2014-06-11 17:05:46 +00:00
|
|
|
for file in "$@"; do
|
|
|
|
_procd_add_config_trigger "config.change" "$file" /etc/init.d/$name reload
|
|
|
|
done
|
2013-11-13 10:49:25 +00:00
|
|
|
_procd_close_trigger
|
2013-09-17 21:45:24 +00:00
|
|
|
}
|
|
|
|
|
2013-11-13 10:49:25 +00:00
|
|
|
_procd_add_validation() {
|
|
|
|
_procd_open_validate
|
|
|
|
$@
|
|
|
|
_procd_close_validate
|
2013-09-17 21:45:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-22 18:56:06 +00:00
|
|
|
_procd_append_param() {
|
|
|
|
local type="$1"; shift
|
2014-06-06 09:34:30 +00:00
|
|
|
local _json_no_warning=1
|
2012-12-22 18:56:06 +00:00
|
|
|
|
|
|
|
json_select "$type"
|
2014-06-06 09:34:30 +00:00
|
|
|
[ $? = 0 ] || {
|
|
|
|
_procd_set_param "$type" "$@"
|
|
|
|
return
|
|
|
|
}
|
2012-12-22 18:56:06 +00:00
|
|
|
case "$type" in
|
2013-12-09 17:29:34 +00:00
|
|
|
env|data|limits)
|
2012-12-22 18:56:06 +00:00
|
|
|
_procd_add_table_data "$@"
|
|
|
|
;;
|
2014-06-05 10:56:06 +00:00
|
|
|
command|netdev|file|respawn|watch)
|
2012-12-22 18:56:06 +00:00
|
|
|
_procd_add_array_data "$@"
|
|
|
|
;;
|
2014-06-06 09:34:30 +00:00
|
|
|
error)
|
|
|
|
json_add_string "" "$@"
|
|
|
|
;;
|
2012-12-22 18:56:06 +00:00
|
|
|
esac
|
|
|
|
json_select ..
|
|
|
|
}
|
|
|
|
|
|
|
|
_procd_close_instance() {
|
2016-03-04 08:33:36 +00:00
|
|
|
local respawn_vals
|
2016-03-10 20:15:59 +00:00
|
|
|
_json_no_warning=1
|
2016-03-04 08:33:36 +00:00
|
|
|
if json_select respawn ; then
|
|
|
|
json_get_values respawn_vals
|
|
|
|
if [ -z "$respawn_vals" ]; then
|
|
|
|
local respawn_retry=$(uci_get system.@service[0].respawn_retry)
|
|
|
|
_procd_add_array_data 3600 5 ${respawn_retry:-5}
|
|
|
|
fi
|
|
|
|
json_select ..
|
|
|
|
fi
|
|
|
|
|
2012-12-22 18:56:06 +00:00
|
|
|
json_close_object
|
|
|
|
}
|
|
|
|
|
|
|
|
_procd_add_instance() {
|
|
|
|
_procd_open_instance
|
2013-10-09 11:07:23 +00:00
|
|
|
_procd_set_param command "$@"
|
2012-12-22 18:56:06 +00:00
|
|
|
_procd_close_instance
|
|
|
|
}
|
|
|
|
|
|
|
|
_procd_kill() {
|
|
|
|
local service="$1"
|
|
|
|
local instance="$2"
|
|
|
|
|
|
|
|
json_init
|
2013-07-11 17:00:49 +00:00
|
|
|
[ -n "$service" ] && json_add_string name "$service"
|
2012-12-22 18:56:06 +00:00
|
|
|
[ -n "$instance" ] && json_add_string instance "$instance"
|
|
|
|
_procd_ubus_call delete
|
|
|
|
}
|
|
|
|
|
2014-07-02 19:18:16 +00:00
|
|
|
procd_open_data() {
|
|
|
|
local name="$1"
|
|
|
|
json_set_namespace procd __procd_old_cb
|
|
|
|
json_add_object data
|
|
|
|
}
|
|
|
|
|
|
|
|
procd_close_data() {
|
|
|
|
json_close_object
|
|
|
|
json_set_namespace $__procd_old_cb
|
|
|
|
}
|
|
|
|
|
2014-07-02 19:18:20 +00:00
|
|
|
_procd_set_config_changed() {
|
|
|
|
local package="$1"
|
|
|
|
|
|
|
|
json_init
|
|
|
|
json_add_string type config.change
|
|
|
|
json_add_object data
|
|
|
|
json_add_string package "$package"
|
|
|
|
json_close_object
|
|
|
|
|
|
|
|
ubus call service event "$(json_dump)"
|
|
|
|
}
|
|
|
|
|
2014-08-29 18:16:24 +00:00
|
|
|
procd_add_mdns_service() {
|
|
|
|
local service proto port
|
|
|
|
service=$1; shift
|
|
|
|
proto=$1; shift
|
|
|
|
port=$1; shift
|
|
|
|
json_add_object "${service}_$port"
|
|
|
|
json_add_string "service" "_$service._$proto.local"
|
|
|
|
json_add_int port "$port"
|
|
|
|
[ -n "$1" ] && {
|
|
|
|
json_add_array txt
|
|
|
|
for txt in $@; do json_add_string "" $txt; done
|
|
|
|
json_select ..
|
|
|
|
}
|
|
|
|
json_select ..
|
|
|
|
}
|
|
|
|
|
2014-08-29 18:36:29 +00:00
|
|
|
procd_add_mdns() {
|
2014-08-29 18:16:24 +00:00
|
|
|
procd_open_data
|
|
|
|
json_add_object "mdns"
|
2014-08-29 18:36:29 +00:00
|
|
|
procd_add_mdns_service $@
|
2014-08-29 18:16:24 +00:00
|
|
|
json_close_object
|
|
|
|
procd_close_data
|
|
|
|
}
|
|
|
|
|
2013-11-13 10:49:25 +00:00
|
|
|
uci_validate_section()
|
|
|
|
{
|
2014-06-05 08:19:59 +00:00
|
|
|
local _package="$1"
|
|
|
|
local _type="$2"
|
|
|
|
local _name="$3"
|
2014-06-30 09:57:29 +00:00
|
|
|
local _result
|
2014-06-05 08:19:59 +00:00
|
|
|
local _error
|
2013-11-13 10:49:25 +00:00
|
|
|
shift; shift; shift
|
2014-06-30 09:57:29 +00:00
|
|
|
_result=`/sbin/validate_data "$_package" "$_type" "$_name" "$@" 2> /dev/null`
|
2014-06-05 08:19:59 +00:00
|
|
|
_error=$?
|
|
|
|
eval "$_result"
|
|
|
|
[ "$_error" = "0" ] || `/sbin/validate_data "$_package" "$_type" "$_name" "$@" 1> /dev/null`
|
|
|
|
return $_error
|
2013-11-13 10:49:25 +00:00
|
|
|
}
|
|
|
|
|
2012-12-22 18:56:06 +00:00
|
|
|
_procd_wrapper \
|
|
|
|
procd_open_service \
|
|
|
|
procd_close_service \
|
|
|
|
procd_add_instance \
|
2014-08-29 18:16:24 +00:00
|
|
|
procd_add_raw_trigger \
|
2013-07-04 13:30:55 +00:00
|
|
|
procd_add_config_trigger \
|
2014-06-05 10:56:06 +00:00
|
|
|
procd_add_interface_trigger \
|
2014-06-16 18:55:05 +00:00
|
|
|
procd_add_reload_trigger \
|
|
|
|
procd_add_reload_interface_trigger \
|
2013-07-04 13:30:55 +00:00
|
|
|
procd_open_trigger \
|
|
|
|
procd_close_trigger \
|
2012-12-22 18:56:06 +00:00
|
|
|
procd_open_instance \
|
|
|
|
procd_close_instance \
|
2014-02-18 13:33:29 +00:00
|
|
|
procd_open_validate \
|
|
|
|
procd_close_validate \
|
2015-03-26 10:58:25 +00:00
|
|
|
procd_add_jail \
|
|
|
|
procd_add_jail_mount \
|
|
|
|
procd_add_jail_mount_rw \
|
2012-12-22 18:56:06 +00:00
|
|
|
procd_set_param \
|
|
|
|
procd_append_param \
|
2013-11-13 10:49:25 +00:00
|
|
|
procd_add_validation \
|
2014-07-02 19:18:20 +00:00
|
|
|
procd_set_config_changed \
|
2012-12-22 18:56:06 +00:00
|
|
|
procd_kill
|