From 2ca8fa688241e0648987c4e655b85f25c1d868db Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Fri, 10 Feb 2012 17:22:48 +0100 Subject: [PATCH] improve chrony-helper to keep track of servers added from DHCP (#787042) --- chrony.dhclient | 16 +++++++-------- chrony.helper | 52 +++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 51 insertions(+), 17 deletions(-) diff --git a/chrony.dhclient b/chrony.dhclient index a01138e..f779ac8 100644 --- a/chrony.dhclient +++ b/chrony.dhclient @@ -5,20 +5,18 @@ SERVERFILE=$SAVEDIR/chrony.servers.$interface chrony_config() { rm -f $SERVERFILE if [ "$PEERNTP" != "no" ]; then - /usr/libexec/chrony-helper command "$( - for server in $new_ntp_servers; do - echo "add server $server $NTPSERVERARGS" - echo "$server $NTPSERVERARGS" >> $SERVERFILE - done)" &> /dev/null || : + for server in $new_ntp_servers; do + echo "$server $NTPSERVERARGS" >> $SERVERFILE + done + /usr/libexec/chrony-helper is-running && + /usr/libexec/chrony-helper add-dhclient-servers || : fi } chrony_restore() { if [ -f $SERVERFILE ]; then - /usr/libexec/chrony-helper command "$( - while read server serverargs; do - echo "delete $server" - done < $SERVERFILE)" &> /dev/null rm -f $SERVERFILE + /usr/libexec/chrony-helper is-running && + /usr/libexec/chrony-helper remove-dhclient-servers || : fi } diff --git a/chrony.helper b/chrony.helper index 9158ae3..809794e 100644 --- a/chrony.helper +++ b/chrony.helper @@ -4,6 +4,7 @@ config=/etc/chrony.conf keyfile=/etc/chrony.keys chronyc=/usr/bin/chronyc dhclient_servers=/var/lib/dhclient/chrony.servers.* +dhclient_added_servers=/var/lib/dhclient/chrony.added_servers service_name=chronyd.service get_key() { @@ -36,14 +37,43 @@ generate_commandkey() { [ -n "$commandkey" ] && echo "$commandkeyid $commandkey" >> $keyfile } +update_dhclient_added_servers() { + new_servers=$(echo "$1" | sort -u) + old_servers=$(cat $dhclient_added_servers 2> /dev/null) + [ "$old_servers" = "$new_servers" ] && return 0 + [ -n "$new_servers" ] && echo "$new_servers" > $dhclient_added_servers || + rm -f $dhclient_added_servers +} + add_dhclient_servers() { - command=$(cat $dhclient_servers 2> /dev/null | - while read server serverargs; do - echo "add server $server $serverargs" - done) - if [ -n "$command" ]; then - chrony_command "$command" &> /dev/null - fi + shopt -s nullglob + servers_files=($dhclient_servers) + shopt -u nullglob + (( ${#servers_files[*]} )) || return 0 + + added_servers=$( + cat $dhclient_added_servers 2> /dev/null + cat ${servers_files[*]} | + while read server serverargs; do + chrony_command "add server $server $serverargs" &> /dev/null && + echo "$server" + done) + update_dhclient_added_servers "$added_servers" +} + +remove_dhclient_servers() { + [ -f $dhclient_added_servers ] || return 0 + all_servers=$( + cat $dhclient_servers 2> /dev/null | + while read server serverargs; do + echo "$server" + done | sort -u) + echo "$all_servers" | comm -23 $dhclient_added_servers - | + while read server; do + chrony_command "delete $server" &> /dev/null + done + added_servers=$(echo "$all_servers" | comm -12 $dhclient_added_servers -) + update_dhclient_added_servers "$added_servers" } is_running() { @@ -57,6 +87,12 @@ case "$1" in add-dhclient-servers) add_dhclient_servers ;; + remove-dhclient-servers) + remove_dhclient_servers + ;; + is-running) + is_running + ;; command) is_running && chrony_command "$2" ;; @@ -64,7 +100,7 @@ case "$1" in chrony_command "$2" ;; *) - echo $"Usage: $0 {generate-commandkey|add-dhclient-servers|command|forced-command}" + echo $"Usage: $0 {generate-commandkey|add-dhclient-servers|remove-dhclient-servers|is-running|command|forced-command}" exit 2 esac exit $?