base-files,procd: add generic service status
authorLuiz Angelo Daros de Luca <luizluca@gmail.com>
Wed, 12 Sep 2018 01:35:09 +0000 (22:35 -0300)
committerPetr Štetiar <ynezz@true.cz>
Sun, 15 Sep 2019 20:53:01 +0000 (22:53 +0200)
Adds a default status action for init.d scripts.

procd "service status" will return:

 0) for loaded services (even if disabled by conf or dead)
 3) for inactive services
 4) when filtering a non-existing instance

Signed-off-by: Luiz Angelo Daros de Luca <luizluca@gmail.com>
[rebased, cleaned up]
Signed-off-by: Petr Štetiar <ynezz@true.cz>
package/base-files/files/etc/rc.common
package/system/procd/files/procd.sh

index 37adab0c54e1edcb7a5de32726ff1e42ca6c13c9..d3fa3be5058dcdd146bfaa5afa63b1fe77dabeff 100755 (executable)
@@ -105,9 +105,10 @@ ${INIT_TRACE:+set -x}
 . "$initscript"
 
 [ -n "$USE_PROCD" ] && {
-       EXTRA_COMMANDS="${EXTRA_COMMANDS} running trace"
+       EXTRA_COMMANDS="${EXTRA_COMMANDS} running status trace"
        EXTRA_HELP="\
        running Check if service is running
+       status  Service status
        "
 
        . $IPKG_INSTROOT/lib/functions/procd.sh
@@ -153,6 +154,14 @@ ${INIT_TRACE:+set -x}
        running() {
                service_running "$@"
        }
+
+       status() {
+               if eval "type status_service" 2>/dev/null >/dev/null; then
+                       status_service "$@"
+               else
+                       _procd_status "$(basename ${basescript:-$initscript})" "$1"
+               fi
+       }
 }
 
 ALL_COMMANDS="start stop reload restart boot shutdown enable disable enabled depends ${EXTRA_COMMANDS}"
index 8d6d4060125f7dc8c07f967ac323793fb7b32d3e..a9d2b341beab58658ceb245e62d426d3c49b1258 100644 (file)
@@ -443,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