base-files: add service 'running' support for sysv

This PR adds support for sysv-style initscripts, so they can be shown
as 'running' in the '/sbin/service' list. Not all initscripts use procd
or have an active process.

Changes:

package/base-files/files/sbin/service:
* moved 'package/system/procd/files/service' to base-files
* added function rc_d to detect 'enabled' (much faster)
* moved procd ubus call into separate function proc_d
* added function sysv to detect 'running()' function

package/base-files/files/etc/rc.common:
* added default 'running()' function for sysv-scripts
* moved extra_command "running"

package/system/procd/Makefile:
* removed 'sbin/service' from Package/procd/install

Signed-off-by: John Kirk <johnskirk@proton.me>
This commit is contained in:
John Kirk 2024-11-17 15:27:56 +00:00 committed by bigsmile74
parent 98f26346cb
commit f28c3b4b03
4 changed files with 57 additions and 32 deletions

View File

@ -67,6 +67,10 @@ depends() {
return 0 return 0
} }
running() {
return 1
}
ALL_HELP="" ALL_HELP=""
ALL_COMMANDS="boot shutdown depends" ALL_COMMANDS="boot shutdown depends"
extra_command() { extra_command() {
@ -119,11 +123,11 @@ extra_command "reload" "Reload configuration files (or restart if service does n
extra_command "enable" "Enable service autostart" extra_command "enable" "Enable service autostart"
extra_command "disable" "Disable service autostart" extra_command "disable" "Disable service autostart"
extra_command "enabled" "Check if service is started on boot" extra_command "enabled" "Check if service is started on boot"
extra_command "running" "Check if service is running"
. "$initscript" . "$initscript"
[ -n "$USE_PROCD" ] && { [ -n "$USE_PROCD" ] && {
extra_command "running" "Check if service is running"
extra_command "status" "Service status" extra_command "status" "Service status"
extra_command "trace" "Start with syscall trace" extra_command "trace" "Start with syscall trace"
extra_command "info" "Dump procd service info" extra_command "info" "Dump procd service info"

View File

@ -0,0 +1,52 @@
#!/bin/sh
rc_d() {
local $(grep -oE '^(START|STOP)=[0-9][0-9]' /etc/init.d/"$1")
[ -n "$START$STOP" ] && {
[ -z "$START" ] || [ -L "/etc/rc.d/S$START$1" ]
} && {
[ -z "$STOP" ] || [ -L "/etc/rc.d/K$STOP$1" ]
}
}
proc_d() {
[ "$(ubus call service list "{'verbose':true,'name':'$1'}" \
| jsonfilter -e '@[*].instances[*].running' | uniq )" = "true" ]
}
sys_v() {
[ -n "${sysv+x}" ] || sysv="$(grep -l '^\s*running\s*()' /etc/init.d/*)"
[ "${sysv/$1}" != "$sysv" ] && /etc/init.d/"$1" running
}
main() {
local boot status service="$1"
shift
if [ -f "/etc/init.d/$service" ]; then
/etc/init.d/"$service" "$@"
exit $?
fi
if [ -n "$service" ]; then
echo "Service \"$service\" not found:"
exit 1
fi
echo "Usage: ${0##*/} <service> [command]"
ls /etc/init.d | while read service; do
if rc_d "$service"; then
boot="enabled"; else boot="disabled"
fi
if proc_d "$service" || sys_v "$service"; then
status="running"; else status="stopped"
fi
printf "%-30s\\t%10s\\t%10s\\n" \
"/etc/init.d/$service" "$boot" "$status"
done
}
main "$@"

View File

@ -118,7 +118,6 @@ define Package/procd/install
$(INSTALL_BIN) ./files/reload_config $(1)/sbin/ $(INSTALL_BIN) ./files/reload_config $(1)/sbin/
$(INSTALL_CONF) ./files/hotplug*.json $(1)/etc/ $(INSTALL_CONF) ./files/hotplug*.json $(1)/etc/
$(INSTALL_DATA) ./files/procd.sh $(1)/lib/functions/ $(INSTALL_DATA) ./files/procd.sh $(1)/lib/functions/
$(INSTALL_BIN) ./files/service $(1)/sbin/service
endef endef
Package/procd-selinux/install = $(Package/procd/install) Package/procd-selinux/install = $(Package/procd/install)

View File

@ -1,30 +0,0 @@
#!/bin/sh
main() {
local service="$1"
shift
local boot status
if [ -f "/etc/init.d/${service}" ]; then
/etc/init.d/"${service}" "$@"
exit "$?"
fi
if [ -n "$service" ]; then
echo "Service \"$service\" not found:"
exit 1
fi
echo "Usage: $(basename "$0") <service> [command]"
for service in /etc/init.d/* ; do
boot="$($service enabled && echo "enabled" || echo "disabled" )"
status="$( [ "$(ubus call service list "{ 'verbose': true, 'name': '$(basename "$service")' }" \
| jsonfilter -q -e "@['$(basename "$service")'].instances[*].running" | uniq)" = "true" ] \
&& echo "running" || echo "stopped" )"
printf "%-30s\\t%10s\\t%10s\\n" "$service" "$boot" "$status"
done
}
main "$@"