From: Aaron LI Date: Thu, 13 Dec 2018 05:58:27 +0000 (+0800) Subject: rc.d/sysctl: Rewrite to be more robust and clean X-Git-Tag: v5.7.0~670 X-Git-Url: https://gitweb.dragonflybsd.org/dragonfly.git/commitdiff_plain/87206e7c403a8c39917ab3389bb5c3259b68b501 rc.d/sysctl: Rewrite to be more robust and clean * Check the validity of the sysctl config and warn about the invalid syntax. A common mistake is that users set 'sysctl =' in /etc/sysctl.conf, which generate huge amount of junk/mysterious messages on the console. Now this will be warned. * Reorganize the logic to be much cleaner. * Do not bother to check whether the new value is different from the old one. * Rename the function for consistency. --- diff --git a/etc/rc.d/sysctl b/etc/rc.d/sysctl index 74e83a2dd7..f53d2f482b 100644 --- a/etc/rc.d/sysctl +++ b/etc/rc.d/sysctl @@ -11,44 +11,45 @@ . /etc/rc.subr name="sysctl" +required_files="/etc/sysctl.conf" stop_cmd=":" -start_cmd="DragonFly_start" +start_cmd="sysctl_start" +reload_cmd="sysctl_start" +lastload_cmd="sysctl_start last" extra_commands="reload lastload" -reload_cmd="DragonFly_start" -lastload_cmd="DragonFly_start last" -DragonFly_start() +sysctl_start() { - # - # Read in /etc/sysctl.conf and set things accordingly - # - if [ -f /etc/sysctl.conf ]; then - while read var comments - do - case ${var} in - \#*|'') - ;; - *) - mib=${var%=*} - val=${var#*=} - - if current_value=`${SYSCTL_N} -q ${mib}`; then - case ${current_value} in - ${val}) - ;; - *) - ${SYSCTL_W} ${var} - ;; - esac - elif [ "$1" = "last" ]; then - warn "sysctl ${mib} does not exist." - fi - ;; - esac - done < /etc/sysctl.conf + local config extra mib val + + if [ ! -f "/etc/sysctl.conf" ]; then + return fi -} + # NOTE: Do not miss the last line when it does not end with a LF. + while read config extra || [ -n "${config}" ]; do + case ${config} in + \#*|'') + continue + ;; + *[^=]=[^=]*) + mib=${config%=*} + val=${config#*=} + ;; + *) + warn "invalid syntax: ${config}" + continue + ;; + esac + + if ${SYSCTL_N} -q ${mib}; then + debug "${SYSCTL_W} ${mib}=${val}" + ${SYSCTL_W} ${mib}=${val} + elif [ "$1" = "last" ]; then + warn "sysctl '${mib}' does not exist." + fi + done < /etc/sysctl.conf +} load_rc_config $name run_rc_command "$1"