xref: /freebsd/contrib/ntp/scripts/ntp-wait/ntp-wait.in (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1#! @PATH_PERL@
2# @configure_input@
3
4package ntp_wait;
5use 5.006_000;
6use strict;
7use warnings;
8use lib "@PERLLIBDIR@";
9use NTP::Util qw(ntp_read_vars);
10
11exit run(@ARGV) unless caller;
12
13sub run {
14    my $opts;
15    if (!processOptions(\@_, $opts)) {
16        usage(1);
17    };
18
19    my $tries   = $opts->{tries};	# How many tries before we give up? (10 min+)
20    my $sleep   = $opts->{sleep};	# Seconds to sleep between tries (6s = 10/min)
21    my $verbose = $opts->{verbose};	# Be verbose?
22
23    # Autoflush stdout
24    $| = 1;
25
26    print "Waiting for ntpd to synchronize...  " if $verbose;
27
28    for my $i (1 .. $tries) {
29        my $info = ntp_read_vars(0, []);
30
31        if (!defined $info) {
32            print "\bntpd is not running!\n" if $verbose;
33            return 1;
34        }
35
36        if (!exists $info->{status_line}{leap}) {
37            print "\bLeap status not available\n";
38            return 1;
39        }
40
41        my $leap   = $info->{status_line}{leap};
42        my $sync   = $info->{status_line}{sync};
43
44        if ($leap =~ /(sync|leap)_alarm/) {
45            print "\b".(substr "*+:.", $i % 4, 1) if $verbose;
46            sleep $sleep if $i < $tries;
47            next;
48        }
49
50        if ($leap =~ /leap_(none|((add|del)_sec))/) {
51            # We could check $sync here to make sure we like the source...
52            print "\bOK!\n" if $verbose;
53            return 0;
54        }
55
56        print "\bUnexpected 'leap' status <$leap>\n";
57        return 1;
58    }
59
60    print "\bNo!\nntpd did not synchronize.\n" if $verbose;
61    return 1;
62}
63
64@ntp_wait_opts@
65
661;
67__END__
68