procd: improve status reporting for partially running services
authorJo-Philipp Wich <jo@mein.io>
Thu, 27 Jul 2023 11:31:18 +0000 (13:31 +0200)
committerJo-Philipp Wich <jo@mein.io>
Thu, 27 Jul 2023 11:31:18 +0000 (13:31 +0200)
The existing implementation incorrectly reported `running` for services
without any instances or with all instances stopped/terminated.

Improve the default implementation of `/etc/init.d/* status` to properly
report services with not running instances. In case a service exists,
but without running instance, the status call will now report
"not running" with exit code 5. In case some instances are running and
some are stopped/terminated, the call will report "running (X/Y)" where
`X` denoted the amount of running instances and `Y` the amount of total
registered ones.

Ref: https://forum.openwrt.org/t/x/159443
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
package/system/procd/Makefile
package/system/procd/files/procd.sh

index 9e829a21591ef1b4279fd41a98110dd761a576e0..d0576c12595dee044f00c1089af43caffd4c2d44 100644 (file)
@@ -8,7 +8,7 @@
 include $(TOPDIR)/rules.mk
 
 PKG_NAME:=procd
-PKG_RELEASE:=1
+PKG_RELEASE:=2
 
 PKG_SOURCE_PROTO:=git
 PKG_SOURCE_URL=$(PROJECT_GIT)/project/procd.git
index 5148b2f03c3511ee5c3de1d5d631ecd5a26c730a..8ee25f4f08b4715dd214623e3edba6d1db1379f9 100644 (file)
@@ -524,7 +524,10 @@ _procd_send_signal() {
 _procd_status() {
        local service="$1"
        local instance="$2"
-       local data
+       local data state
+       local n_running=0
+       local n_stopped=0
+       local n_total=0
 
        json_init
        [ -n "$service" ] && json_add_string name "$service"
@@ -539,10 +542,29 @@ _procd_status() {
        fi
 
        [ -n "$instance" ] && instance="\"$instance\"" || instance='*'
-       if [ -z "$(echo "$data" | jsonfilter -e '$['"$instance"']')" ]; then
-               echo "unknown instance $instance"; return 4
+
+       for state in $(jsonfilter -s "$data" -e '$['"$instance"'].running'); do
+               n_total=$((n_total + 1))
+               case "$state" in
+               false) n_stopped=$((n_stopped + 1)) ;;
+               true)  n_running=$((n_running + 1)) ;;
+               esac
+       done
+
+       if [ $n_total -gt 0 ]; then
+               if [ $n_running -gt 0 ] && [ $n_stopped -eq 0 ]; then
+                       echo "running"
+                       return 0
+               elif [ $n_running -gt 0 ]; then
+                       echo "running ($n_running/$n_total)"
+                       return 0
+               else
+                       echo "not running"
+                       return 5
+               fi
        else
-               echo "running"; return 0
+               echo "unknown instance $instance"
+               return 4
        fi
 }