procd: Add wrapper for uci_validate_section()
authorJeffery To <jeffery.to@gmail.com>
Sat, 12 Jan 2019 18:14:22 +0000 (02:14 +0800)
committerJo-Philipp Wich <jo@mein.io>
Tue, 22 Jan 2019 08:05:59 +0000 (09:05 +0100)
This adds a wrapper (uci_load_validate) for uci_validate_section() that
allows callers (through a callback function) to access the values set by
uci_validate_section(), without having to manually declare a
(potentially long) list of local variables.

The callback function receives two arguments when called, the config
section name and the return value of uci_validate_section().

If no callback function is given, then the wrapper exits with the value
returned by uci_validate_section().

This also updates several init scripts to use the new wrapper function.

Signed-off-by: Jeffery To <jeffery.to@gmail.com>
package/base-files/files/etc/init.d/system
package/network/services/dropbear/files/dropbear.init
package/system/procd/files/procd.sh
package/system/ubox/files/log.init
package/utils/busybox/files/sysntpd

index 531aa8c..a98a972 100755 (executable)
@@ -6,7 +6,7 @@ USE_PROCD=1
 
 validate_system_section()
 {
-       uci_validate_section system system "${1}" \
+       uci_load_validate system system "$1" "$2" \
                'hostname:string:OpenWrt' \
                'conloglevel:uinteger' \
                'buffersize:uinteger' \
@@ -15,11 +15,7 @@ validate_system_section()
 }
 
 system_config() {
-       local cfg="$1"
-
-       local hostname conloglevel buffersize timezone zonename
-
-       validate_system_section "${1}" || {
+       [ "$2" = 0 ] || {
                echo "validation failed"
                return 1
        }
@@ -36,7 +32,7 @@ system_config() {
 
 reload_service() {
        config_load system
-       config_foreach system_config system
+       config_foreach validate_system_section system system_config
 }
 
 service_triggers()
index eef2643..2ea637e 100755 (executable)
@@ -29,7 +29,7 @@ append_ports()
 
 validate_section_dropbear()
 {
-       uci_validate_section dropbear dropbear "${1}" \
+       uci_load_validate dropbear dropbear "$1" "$2" \
                'PasswordAuth:bool:1' \
                'enable:bool:1' \
                'Interface:string' \
@@ -48,12 +48,9 @@ validate_section_dropbear()
 
 dropbear_instance()
 {
-       local PasswordAuth enable Interface GatewayPorts \
-               RootPasswordAuth RootLogin rsakeyfile \
-               BannerFile Port SSHKeepAlive IdleTimeout \
-               MaxAuthTries RecvWindowSize mdns ipaddrs
+       local ipaddrs
 
-       validate_section_dropbear "${1}" || {
+       [ "$2" = 0 ] || {
                echo "validation failed"
                return 1
        }
@@ -135,7 +132,7 @@ start_service()
        . /lib/functions/network.sh
 
        config_load "${NAME}"
-       config_foreach dropbear_instance dropbear
+       config_foreach validate_section_dropbear dropbear dropbear_instance
 }
 
 service_triggers()
index 6f16b74..72f25fe 100644 (file)
@@ -486,6 +486,23 @@ uci_validate_section()
        return $_error
 }
 
+uci_load_validate() {
+       local _package="$1"
+       local _type="$2"
+       local _name="$3"
+       local _function="$4"
+       local _option
+       local _result
+       shift; shift; shift; shift
+       for _option in "$@"; do
+               eval "local ${_option%%:*}"
+       done
+       uci_validate_section "$_package" "$_type" "$_name" "$@"
+       _result=$?
+       [ -n "$_function" ] || return $_result
+       eval "$_function \"\$_name\" \"\$_result\""
+}
+
 _procd_wrapper \
        procd_open_service \
        procd_close_service \
index ae5dd1f..250f805 100644 (file)
@@ -11,7 +11,7 @@ PROG=/sbin/logread
 
 validate_log_section()
 {
-       uci_validate_section system system "${1}" \
+       uci_load_validate system system "$1" "$2" \
                'log_file:string' \
                'log_size:uinteger' \
                'log_hostname:string' \
@@ -25,15 +25,13 @@ validate_log_section()
 
 validate_log_daemon()
 {
-       uci_validate_section system system "${1}" \
+       uci_load_validate system system "$1" "$2" \
                'log_size:uinteger:0' \
                'log_buffer_size:uinteger:0'
 }
 
 start_service_daemon()
 {
-       local log_buffer_size log_size
-       validate_log_daemon "${1}"
        [ $log_buffer_size -eq 0 -a $log_size -gt 0 ] && log_buffer_size=$log_size
        [ $log_buffer_size -eq 0 ] && log_buffer_size=64
        procd_open_instance
@@ -47,9 +45,8 @@ start_service_file()
 {
        PIDCOUNT="$(( ${PIDCOUNT} + 1))"
        local pid_file="/var/run/logread.${PIDCOUNT}.pid"
-       local log_file log_size
 
-       validate_log_section "${1}" || {
+       [ "$2" = 0 ] || {
                echo "validation failed"
                return 1
        }
@@ -67,9 +64,8 @@ start_service_remote()
 {
        PIDCOUNT="$(( ${PIDCOUNT} + 1))"
        local pid_file="/var/run/logread.${PIDCOUNT}.pid"
-       local log_ip log_port log_proto log_prefix log_remote log_trailer_null log_hostname
 
-       validate_log_section "${1}" || {
+       [ "$2" = 0 ] || {
                echo "validation failed"
                return 1
        }
@@ -96,7 +92,7 @@ service_triggers()
 start_service()
 {
        config_load system
-       config_foreach start_service_daemon system
-       config_foreach start_service_file system
-       config_foreach start_service_remote system
+       config_foreach validate_log_daemon system start_service_daemon
+       config_foreach validate_log_section system start_service_file
+       config_foreach validate_log_section system start_service_remote
 }
index e693e40..52866ba 100755 (executable)
@@ -29,14 +29,14 @@ get_dhcp_ntp_servers() {
 }
 
 validate_ntp_section() {
-       uci_validate_section system timeserver "${1}" \
+       uci_load_validate system timeserver "$1" "$2" \
                'server:list(host)' 'enabled:bool:1' 'enable_server:bool:0' 'use_dhcp:bool:1' 'dhcp_interface:list(string)'
 }
 
-start_service() {
-       local server enabled enable_server use_dhcp dhcp_interface peer
+start_ntpd_instance() {
+       local peer
 
-       validate_ntp_section ntp || {
+       [ "$2" = 0 ] || {
                echo "validation failed"
                return 1
        }
@@ -58,6 +58,10 @@ start_service() {
        procd_close_instance
 }
 
+start_service() {
+       validate_ntp_section ntp start_ntpd_instance
+}
+
 service_triggers() {
        local script name use_dhcp