From: Roy Marples Date: Tue, 9 Aug 2022 16:26:37 +0000 (+0100) Subject: hooks: Support timesyncd X-Git-Tag: v10.0.0~56 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2f070c7d2003a4f08ba0eed49c2fb8f5507eab07;p=thirdparty%2Fdhcpcd.git hooks: Support timesyncd Fixes #106 --- diff --git a/configure b/configure index 9b8e4403..8ff66a1a 100755 --- a/configure +++ b/configure @@ -1707,6 +1707,21 @@ if ! $HOOKSET; then echo "NTP will default to $NTPD" fi + printf "Checking for timesyncd ... " + TIMESYNCD= + for x in /usr/lib/systemd/systemd-timesyncd; do + if [ -x "$x" ]; then + TIMESYNCD=$x + break + fi + done + if [ -n "$TIMESYNCD" ]; then + echo "$TIMESYNCD" + HOOKS="$HOOKS${HOOKS:+ }50-timesyncd.conf" + else + echo "not found" + fi + printf "Checking for ypbind ... " YPBIND=$(_which ypbind) if [ -n "$YPBIND" ]; then diff --git a/hooks/50-timesyncd.conf b/hooks/50-timesyncd.conf new file mode 100644 index 00000000..064e0c03 --- /dev/null +++ b/hooks/50-timesyncd.conf @@ -0,0 +1,39 @@ +: ${timesyncd_conf_d:=/run/systemd/timesyncd.conf.d} +timesyncd_conf="${timesyncd_conf_d}/dhcpcd-$ifname.conf" +timesyncd_tmp_d="$state_dir/timesyncd" +timesyncd_tmp="$timesyncd_tmp_d/$ifname" + +NL=" +" + +add_timesyncd_conf() +{ + [ -n "$new_ntp_servers" ] || return + + mkdir -p "$timesyncd_tmp_d" "$timesyncd_conf_d" + + conf="$signature$NL" + conf="${conf}[Time]$NL" + conf="${conf}NTP=$new_ntp_servers$NL" + + printf %s "$conf" > "$timesyncd_tmp" + if change_file "$timesyncd_conf" "$timesyncd_tmp"; then + systemctl try-reload-or-restart systemd-timesyncd + fi +} + +remove_timesyncd_conf() +{ + if [ -e "$timesyncd_conf" ]; then + rm "$timesyncd_conf" + systemctl try-reload-or-restart systemd-timesyncd + fi +} + +if $if_configured; then + if $if_up; then + add_timesyncd_conf + elif $if_down; then + remove_timesyncd_conf + fi +fi