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).
|
2013-09-17 21:45:24 +00:00
|
|
|
# respawn info: array with 3 values $restart_timeout $fail_hreshold $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)
|
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
|
|
|
|
|
|
|
|
_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"
|
|
|
|
|
|
|
|
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
|
2013-07-11 17:00:54 +00:00
|
|
|
service_triggers
|
2012-12-22 18:56:06 +00:00
|
|
|
_procd_ubus_call set
|
|
|
|
}
|
|
|
|
|
|
|
|
_procd_add_array_data() {
|
|
|
|
while [ -n "$1" ]; do
|
|
|
|
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#*=}"
|
|
|
|
[[ "$1" == "$val" ]] && val=
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2013-07-04 13:30:55 +00:00
|
|
|
_procd_open_trigger() {
|
|
|
|
json_add_array "triggers"
|
|
|
|
}
|
|
|
|
|
2013-11-13 10:49:25 +00:00
|
|
|
_procd_open_validate() {
|
|
|
|
json_add_array "validate"
|
|
|
|
}
|
|
|
|
|
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" "$@"
|
|
|
|
;;
|
2013-09-17 21:45:24 +00:00
|
|
|
command|netdev|file|respawn)
|
2012-12-22 18:56:06 +00:00
|
|
|
_procd_add_array "$type" "$@"
|
|
|
|
;;
|
|
|
|
nice)
|
|
|
|
json_add_int "$type" "$1"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-09-17 21:45:24 +00:00
|
|
|
_procd_add_reload_trigger() {
|
|
|
|
local script=$(readlink "$initscript")
|
|
|
|
local name=$(basename ${script:-$initscript})
|
|
|
|
|
2013-11-13 10:49:25 +00:00
|
|
|
_procd_open_trigger
|
|
|
|
_procd_add_config_trigger "config.change" $1 /etc/init.d/$name reload
|
|
|
|
_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
|
|
|
|
|
|
|
|
json_select "$type"
|
|
|
|
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 "$@"
|
|
|
|
;;
|
2013-09-17 21:45:24 +00:00
|
|
|
command|netdev|file|respawn)
|
2012-12-22 18:56:06 +00:00
|
|
|
_procd_add_array_data "$@"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
json_select ..
|
|
|
|
}
|
|
|
|
|
|
|
|
_procd_close_instance() {
|
|
|
|
json_close_object
|
|
|
|
}
|
|
|
|
|
2013-07-04 13:30:55 +00:00
|
|
|
_procd_close_trigger() {
|
|
|
|
json_close_array
|
|
|
|
}
|
|
|
|
|
2013-11-13 10:49:25 +00:00
|
|
|
_procd_close_validate() {
|
|
|
|
json_close_array
|
|
|
|
}
|
|
|
|
|
2012-12-22 18:56:06 +00:00
|
|
|
_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
|
|
|
|
}
|
|
|
|
|
2013-11-13 10:49:25 +00:00
|
|
|
uci_validate_section()
|
|
|
|
{
|
2013-11-25 08:34:47 +00:00
|
|
|
local package="$1"
|
|
|
|
local type="$2"
|
|
|
|
local name="$3"
|
|
|
|
local error
|
2013-11-13 10:49:25 +00:00
|
|
|
shift; shift; shift
|
2013-11-28 11:26:34 +00:00
|
|
|
local result=`/sbin/validate_data "$package" "$type" "$name" "$@" 2> /dev/null`
|
2013-11-25 08:34:47 +00:00
|
|
|
error=$?
|
|
|
|
eval "$result"
|
2013-11-28 11:26:34 +00:00
|
|
|
[ "$error" = "0" ] || `/sbin/validate_data "$package" "$type" "$name" "$@" 1> /dev/null`
|
2013-11-13 10:49:25 +00:00
|
|
|
return $error
|
|
|
|
}
|
|
|
|
|
2012-12-22 18:56:06 +00:00
|
|
|
_procd_wrapper \
|
|
|
|
procd_open_service \
|
|
|
|
procd_close_service \
|
|
|
|
procd_add_instance \
|
2013-07-04 13:30:55 +00:00
|
|
|
procd_add_config_trigger \
|
2013-09-17 21:45:24 +00:00
|
|
|
procd_add_reload_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 \
|
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 \
|
2012-12-22 18:56:06 +00:00
|
|
|
procd_kill
|