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