xref: /freebsd/contrib/ntp/scripts/ntp-wait/ntp-wait.in (revision f5f40dd63bc7acbb5312b26ac1ea1103c12352a6)
12b15cb3dSCy Schubert#! @PATH_PERL@
2*f5f40dd6SCy Schubert# @configure_input@
32b15cb3dSCy Schubert
42b15cb3dSCy Schubertpackage ntp_wait;
52b15cb3dSCy Schubertuse 5.006_000;
62b15cb3dSCy Schubertuse strict;
72b15cb3dSCy Schubertuse warnings;
82b15cb3dSCy Schubertuse lib "@PERLLIBDIR@";
92b15cb3dSCy Schubertuse NTP::Util qw(ntp_read_vars);
102b15cb3dSCy Schubert
112b15cb3dSCy Schubertexit run(@ARGV) unless caller;
122b15cb3dSCy Schubert
132b15cb3dSCy Schubertsub run {
142b15cb3dSCy Schubert    my $opts;
152b15cb3dSCy Schubert    if (!processOptions(\@_, $opts)) {
162b15cb3dSCy Schubert        usage(1);
172b15cb3dSCy Schubert    };
182b15cb3dSCy Schubert
192b15cb3dSCy Schubert    my $tries   = $opts->{tries};	# How many tries before we give up? (10 min+)
202b15cb3dSCy Schubert    my $sleep   = $opts->{sleep};	# Seconds to sleep between tries (6s = 10/min)
212b15cb3dSCy Schubert    my $verbose = $opts->{verbose};	# Be verbose?
222b15cb3dSCy Schubert
232b15cb3dSCy Schubert    # Autoflush stdout
242b15cb3dSCy Schubert    $| = 1;
252b15cb3dSCy Schubert
262b15cb3dSCy Schubert    print "Waiting for ntpd to synchronize...  " if $verbose;
272b15cb3dSCy Schubert
282b15cb3dSCy Schubert    for my $i (1 .. $tries) {
292b15cb3dSCy Schubert        my $info = ntp_read_vars(0, []);
302b15cb3dSCy Schubert
312b15cb3dSCy Schubert        if (!defined $info) {
322b15cb3dSCy Schubert            print "\bntpd is not running!\n" if $verbose;
332b15cb3dSCy Schubert            return 1;
342b15cb3dSCy Schubert        }
352b15cb3dSCy Schubert
362b15cb3dSCy Schubert        if (!exists $info->{status_line}{leap}) {
37e27abb66SXin LI            print "\bLeap status not available\n";
382b15cb3dSCy Schubert            return 1;
392b15cb3dSCy Schubert        }
402b15cb3dSCy Schubert
412b15cb3dSCy Schubert        my $leap   = $info->{status_line}{leap};
422b15cb3dSCy Schubert        my $sync   = $info->{status_line}{sync};
432b15cb3dSCy Schubert
442b15cb3dSCy Schubert        if ($leap =~ /(sync|leap)_alarm/) {
452b15cb3dSCy Schubert            print "\b".(substr "*+:.", $i % 4, 1) if $verbose;
462b15cb3dSCy Schubert            sleep $sleep if $i < $tries;
472b15cb3dSCy Schubert            next;
482b15cb3dSCy Schubert        }
492b15cb3dSCy Schubert
502b15cb3dSCy Schubert        if ($leap =~ /leap_(none|((add|del)_sec))/) {
512b15cb3dSCy Schubert            # We could check $sync here to make sure we like the source...
522b15cb3dSCy Schubert            print "\bOK!\n" if $verbose;
532b15cb3dSCy Schubert            return 0;
542b15cb3dSCy Schubert        }
552b15cb3dSCy Schubert
562b15cb3dSCy Schubert        print "\bUnexpected 'leap' status <$leap>\n";
572b15cb3dSCy Schubert        return 1;
582b15cb3dSCy Schubert    }
592b15cb3dSCy Schubert
602b15cb3dSCy Schubert    print "\bNo!\nntpd did not synchronize.\n" if $verbose;
612b15cb3dSCy Schubert    return 1;
622b15cb3dSCy Schubert}
632b15cb3dSCy Schubert
642b15cb3dSCy Schubert@ntp_wait_opts@
652b15cb3dSCy Schubert
662b15cb3dSCy Schubert1;
672b15cb3dSCy Schubert__END__
68