ntpstart 3.61 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2002 University of Utah and the Flux Group.
# All rights reserved.
#
use English;

#
# Fire up ntp from /etc/rc. This is a strict wrapper so it should be
# invoked from /etc/rc.conf as such:
#
#	xntpd_program="/usr/local/etc/emulab/ntpstart"
#	xntpd_flags="/usr/sbin/ntpd -p /var/run/ntpd.pid"
#
# which is to say that this program passes it entire argument list to
# off to the shell once it sets up the config file. We fire off ntp
# no matter what happens though.
#

#
# Untaint path
#
$ENV{'PATH'} = '/bin:/sbin:/usr/bin:/usr/local/bin:/etc/testbed';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Turn off line buffering on output
#
$| = 1;

#
# Load the OS independent support library. It will load the OS dependent
# library and initialize itself. 
# 
use lib "/usr/local/etc/emulab";
use libsetup;

# Locals
my @ntpinfo	= ();
my $useold	= 1;
my $newfile     = "/tmp/ntp.conf.new";
my $ntpfile     = "/etc/ntp.conf";
my $driftfile   = "/etc/ntp.drift";
my $pidfile	= "/var/run/ntpd.pid";
46
my $debug       = 0;
47
48
49
50
51

#
# Since this is a wrapper, we have to try to start ntp no matter what.
#
sub ntpstart () {
52
53
54
55
56
57
    if ($debug) {
	print "@ARGV\n";
	return 0;
    }
    system("@ARGV");
    return ($? >> 8);
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
}

#
# Ask for setup. If none then we are done. If provided with a drift value
# but no servers/peers, then need to leave the config alone and just
# replace the drift file.
# 
my $TM = OPENTMCC("ntpinfo");
while (<$TM>) {
    chomp();
    if ($_ =~ /^PEER=.*$/ ||
	$_ =~ /^SERVER=.*$/) {
	$useold = 0;
    }
    push(@ntpinfo, $_);
}
CLOSETMCC($TM);
if (! @ntpinfo) {
    exit(ntpstart());
}

#
# We are going to copy the old file to a new file, munging it as we go.
# Note that if the server did not provide any servers or peers, we want
# to use whatever is in the original file, but we still have to read it
# to find the name of the driftfile. So, just make a copy and throw it
# away later if it turns out we do not need a new version.
#
open(NEW, "> $newfile")
    or fatal("Could not open $newfile: $!");
open(NTP, "< $ntpfile")
    or fatal("Could not open $ntpfile: $!");

while (<NTP>) {
    chomp();
    SWITCH1: {
	/^peer.*$/ && do {
	    last SWITCH1;
	};
97
98
99
100
101
	# Leave refclock lines alone
	/^server.*127\.127.*$/ && do {
	    print NEW "$_\n";
	    last SWITCH1;
	};
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
	/^server.*$/ && do {
	    last SWITCH1;
	};
	/^driftfile[\s]*(\/.*)$/ && do {
	    $driftfile = $1;
	};
	print NEW "$_\n";
    }
}

#
# Okay, now tack on the servers and peers to the new file. The drift
# goes into the driftfile. 
# 
foreach my $line (@ntpinfo) {
    $_ = $line;

    SWITCH1: {
	/^PEER=(.*)$/ && do {
121
	    print NEW "peer $1\n";
122
123
124
	    last SWITCH1;
	};
	/^SERVER=(.*)$/ && do {
125
	    print NEW "server $1\n";
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
	    last SWITCH1;
	};
	/^DRIFT=(.*)$/ && do {
	    open(DRIFT, "> $driftfile");
	    print DRIFT "$1\n";
	    close(DRIFT);
	    last SWITCH1;
	};
    }
}

close(NTP)
    or fatal("Could not close $ntpfile: $!");
close(NEW)
    or fatal("Could not close $newfile: $!");

#
# If it turns out we want to use the old file (no servers/peers provided)
# then start ntp and exit. The new file is thrown away ...
#
if ($useold) {
    exit(ntpstart());
}

#
# Okay, back up the old file and replace it with the new file!
#
system("cp -fp $ntpfile $ntpfile.old");
if ($?) {
    fatal("Could not backup $ntpfile to $ntpfile.old\n");
}
system("cp -fp $newfile $ntpfile");
if ($?) {
    fatal("Could not replace $ntpfile with $newfile\n");
}
exit(ntpstart());

#
# Print error and exit, but must start ntp anyway!
#
sub fatal($)
{
    my ($msg) = @_;

    print STDERR "*** $0:\n" .
	         "    $msg\n";
    exit(ntpstart());
}