procd: add support for service signals
[openwrt/openwrt.git] / package / system / procd / files / procd.sh
index f3c7e48e50423807c571acc3a9e567a009735e24..d2c5b424a7f88e45568c81177078116128f39e7a 100644 (file)
 # procd_set_param(type, [value...])
 #   Available types:
 #     command: command line (array).
-#     respawn info: array with 3 values $restart_timeout $fail_hreshold $max_fail
+#     respawn info: array with 3 values $fail_threshold $restart_timeout $max_fail
 #     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)
 #     limits: resource limits (passed to the process)
 #     user info: array with 1 values $username
+#     pidfile: file name to write pid into
 #
 #   No space separation is done for arrays/tables - use one function argument per command line argument
 #
@@ -31,6 +32,7 @@
 
 . $IPKG_INSTROOT/usr/share/libubox/jshn.sh
 
+PROCD_RELOAD_DELAY=1000
 _PROCD_SERVICE=
 
 _procd_call() {
@@ -71,8 +73,10 @@ _procd_open_service() {
 
 _procd_close_service() {
        json_close_object
+       _procd_open_trigger
        service_triggers
-       _procd_ubus_call set
+       _procd_close_trigger
+       _procd_ubus_call ${1:-set}
 }
 
 _procd_add_array_data() {
@@ -116,17 +120,30 @@ _procd_open_instance() {
 }
 
 _procd_open_trigger() {
+       let '_procd_trigger_open = _procd_trigger_open + 1'
+       [ "$_procd_trigger_open" -gt 1 ] && return
        json_add_array "triggers"
 }
 
+_procd_close_trigger() {
+       let '_procd_trigger_open = _procd_trigger_open - 1'
+       [ "$_procd_trigger_open" -lt 1 ] || return
+       json_close_array
+}
+
 _procd_open_validate() {
+       json_select ..
        json_add_array "validate"
 }
 
+_procd_close_validate() {
+       json_close_array
+       json_select triggers
+}
+
 _procd_add_jail() {
        json_add_object "jail"
        json_add_string name "$1"
-       json_add_string root "/tmp/.jail/$1"
 
        shift
        
@@ -136,6 +153,7 @@ _procd_add_jail() {
                ubus)   json_add_boolean "ubus" "1";;
                procfs) json_add_boolean "procfs" "1";;
                sysfs)  json_add_boolean "sysfs" "1";;
+               ronly)  json_add_boolean "ronly" "1";;
                esac
        done
        json_add_object "mount"
@@ -192,18 +210,23 @@ _procd_set_param() {
                        json_add_string "" "$@"
                        json_close_array
                ;;
-               nice)
+               nice|reload_signal)
                        json_add_int "$type" "$1"
                ;;
-               user|seccomp)
+               pidfile|user|seccomp|capabilities)
                        json_add_string "$type" "$1"
                ;;
-               stdout|stderr)
+               stdout|stderr|no_new_privs)
                        json_add_boolean "$type" "$1"
                ;;
        esac
 }
 
+_procd_add_timeout() {
+       [ "$PROCD_RELOAD_DELAY" -gt 0 ] && json_add_int "" "$PROCD_RELOAD_DELAY"
+       return 0
+}
+
 _procd_add_interface_trigger() {
        json_add_array
        _procd_add_array_data "$1"
@@ -222,9 +245,9 @@ _procd_add_interface_trigger() {
        json_close_array
 
        json_close_array
-
-       _procd_add_array_data "10000"
        json_close_array
+
+       _procd_add_timeout
 }
 
 _procd_add_reload_interface_trigger() {
@@ -256,6 +279,8 @@ _procd_add_config_trigger() {
        json_close_array
 
        json_close_array
+
+       _procd_add_timeout
 }
 
 _procd_add_raw_trigger() {
@@ -318,15 +343,18 @@ _procd_append_param() {
 }
 
 _procd_close_instance() {
-       json_close_object
-}
-
-_procd_close_trigger() {
-       json_close_array
-}
+       local respawn_vals
+       _json_no_warning=1
+       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
 
-_procd_close_validate() {
-       json_close_array
+       json_close_object
 }
 
 _procd_add_instance() {