base-files,procd: add generic service status
[openwrt/openwrt.git] / package / system / procd / files / procd.sh
index 8d6d4060125f7dc8c07f967ac323793fb7b32d3e..a9d2b341beab58658ceb245e62d426d3c49b1258 100644 (file)
@@ -443,6 +443,31 @@ _procd_send_signal() {
        _procd_ubus_call 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
 procd_open_data() {
        local name="$1"
        json_set_namespace procd __procd_old_cb