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