procd: Add wrapper for uci_validate_section()
[openwrt/openwrt.git] / package / system / procd / files / procd.sh
index 91e2371d6231c9928886d820f7c3603d5f68b22e..72f25fe0c05bd284ee025d32c6aee09c1e4c074a 100644 (file)
 #   Send a signal to a service instance (or all instances)
 #
 
-. $IPKG_INSTROOT/usr/share/libubox/jshn.sh
+. "$IPKG_INSTROOT/usr/share/libubox/jshn.sh"
 
 PROCD_RELOAD_DELAY=1000
 _PROCD_SERVICE=
 
+procd_lock() {
+       local basescript=$(readlink "$initscript")
+       local service_name="$(basename ${basescript:-$initscript})"
+
+       flock -n 1000 &> /dev/null
+       if [ "$?" != "0" ]; then
+               exec 1000>"$IPKG_INSTROOT/var/lock/procd_${service_name}.lock"
+               flock 1000
+               if [ "$?" != "0" ]; then
+                       logger "warning: procd flock for $service_name failed"
+               fi
+       fi
+}
+
 _procd_call() {
        local old_cb
 
@@ -47,6 +61,7 @@ _procd_call() {
 }
 
 _procd_wrapper() {
+       procd_lock
        while [ -n "$1" ]; do
                eval "$1() { _procd_call _$1 \"\$@\"; }"
                shift
@@ -398,6 +413,10 @@ _procd_send_signal() {
        local instance="$2"
        local signal="$3"
 
+       case "$signal" in
+               [A-Z]*) signal="$(kill -l "$signal" 2>/dev/null)" || return 1;;
+       esac
+
        json_init
        json_add_string name "$service"
        [ -n "$instance" -a "$instance" != "*" ] && json_add_string instance "$instance"
@@ -467,6 +486,23 @@ uci_validate_section()
        return $_error
 }
 
+uci_load_validate() {
+       local _package="$1"
+       local _type="$2"
+       local _name="$3"
+       local _function="$4"
+       local _option
+       local _result
+       shift; shift; shift; shift
+       for _option in "$@"; do
+               eval "local ${_option%%:*}"
+       done
+       uci_validate_section "$_package" "$_type" "$_name" "$@"
+       _result=$?
+       [ -n "$_function" ] || return $_result
+       eval "$_function \"\$_name\" \"\$_result\""
+}
+
 _procd_wrapper \
        procd_open_service \
        procd_close_service \