base-files,procd: add generic service status
[openwrt/openwrt.git] / package / system / procd / files / procd.sh
index 72f25fe0c05bd284ee025d32c6aee09c1e4c074a..a9d2b341beab58658ceb245e62d426d3c49b1258 100644 (file)
 #     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
+#     user: $username to run service as
+#     group: $groupname to run service as
 #     pidfile: file name to write pid into
+#     stdout: boolean whether to redirect commands stdout to syslog (default: 0)
+#     stderr: boolean whether to redirect commands stderr to syslog (default: 0)
+#     facility: syslog facility used when logging to syslog (default: daemon)
 #
 #   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_running(service, [instance]):
+#   Checks if service/instance is currently running
+#
 # procd_kill(service, [instance]):
 #   Kill a service instance (or all instances)
 #
@@ -249,7 +256,7 @@ _procd_set_param() {
                reload_signal)
                        json_add_int "$type" $(kill -l "$1")
                ;;
-               pidfile|user|seccomp|capabilities)
+               pidfile|user|group|seccomp|capabilities|facility)
                        json_add_string "$type" "$1"
                ;;
                stdout|stderr|no_new_privs)
@@ -398,6 +405,18 @@ _procd_add_instance() {
        _procd_close_instance
 }
 
+procd_running() {
+       local service="$1"
+       local instance="${2:-instance1}"
+       local running
+
+       json_init
+       json_add_string name "$service"
+       running=$(_procd_ubus_call list | jsonfilter -e "@.$service.instances.${instance}.running")
+
+       [ "$running" = "true" ]
+}
+
 _procd_kill() {
        local service="$1"
        local instance="$2"
@@ -424,6 +443,31 @@ _procd_send_signal() {
        _procd_ubus_call signal
 }
 
+_procd_status() {
+       local service="$1"
+       local instance="$2"
+       local data
+
+       json_init
+       [ -n "$service" ] && json_add_string name "$service"
+
+       data=$(_procd_ubus_call list | jsonfilter -e '@["'"$service"'"]')
+       [ -z "$data" ] && { echo "inactive"; return 3; }
+
+       data=$(echo "$data" | jsonfilter -e '$.instances')
+       if [ -z "$data" ]; then
+               [ -z "$instance" ] && { echo "active with no instances"; return 0; }
+               data="[]"
+       fi
+
+       [ -n "$instance" ] && instance="\"$instance\"" || instance='*'
+       if [ -z "$(echo "$data" | jsonfilter -e '$['"$instance"']')" ]; then
+               echo "unknown instance $instance"; return 4
+       else
+               echo "running"; return 0
+       fi
+}
+
 procd_open_data() {
        local name="$1"
        json_set_namespace procd __procd_old_cb