~ubuntu-branches/ubuntu/trusty/system-tools-backends/trusty-proposed

« back to all changes in this revision

Viewing changes to .pc/03ubuntu_empty_ntp_conf.patch/Time/NTP.pm

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2010-12-06 11:56:01 UTC
  • Revision ID: james.westby@ubuntu.com-20101206115601-70b8tysecjq1sr2s
Tags: 2.10.1-0ubuntu2
03ubuntu_empty_ntp_conf.patch: Don't create empty NTP configuration
files (LP: #83604).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 
2
 
 
3
# NTP Configuration handling
 
4
#
 
5
# Copyright (C) 2000-2001 Ximian, Inc.
 
6
#
 
7
# Authors: Hans Petter Jansson <hpj@ximian.com>
 
8
#          Carlos Garnacho     <carlosg@gnome.org>
 
9
#
 
10
# This program is free software; you can redistribute it and/or modify
 
11
# it under the terms of the GNU Library General Public License as published
 
12
# by the Free Software Foundation; either version 2 of the License, or
 
13
# (at your option) any later version.
 
14
#
 
15
# This program is distributed in the hope that it will be useful,
 
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
# GNU Library General Public License for more details.
 
19
#
 
20
# You should have received a copy of the GNU Library General Public License
 
21
# along with this program; if not, write to the Free Software
 
22
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 
23
 
 
24
package Time::NTP;
 
25
 
 
26
sub get_config_file ()
 
27
{
 
28
  my %dist_map =
 
29
  (
 
30
    "redhat-6.2"      => "redhat-6.2",
 
31
    "redhat-7.0"      => "redhat-6.2",
 
32
    "redhat-7.1"      => "redhat-6.2",
 
33
    "redhat-7.2"      => "redhat-6.2",
 
34
    "redhat-7.3"      => "redhat-6.2",
 
35
    "redhat-8.0"      => "redhat-6.2",
 
36
    "mandrake-9.0"    => "redhat-6.2",
 
37
    "debian"          => "redhat-6.2",
 
38
    "suse-9.0"        => "redhat-6.2",
 
39
    "slackware-9.1.0" => "redhat-6.2",
 
40
    "gentoo"          => "redhat-6.2",
 
41
    "pld-1.0"         => "pld-1.0",
 
42
    "vine-3.0"        => "redhat-6.2",
 
43
    "freebsd-5"       => "redhat-6.2",
 
44
    "archlinux"       => "redhat-6.2",
 
45
    "solaris-2.11"    => "solaris-2.11",
 
46
  );
 
47
 
 
48
  my %dist_table =
 
49
  (
 
50
    "redhat-6.2"   => "/etc/ntp.conf",
 
51
    "pld-1.0"      => "/etc/ntp/ntp.conf",
 
52
    "solaris-2.11" => "/etc/inet/ntp.conf",
 
53
  );
 
54
 
 
55
  my $dist = $dist_map{$Utils::Backend::tool{"platform"}};
 
56
  return $dist_table{$dist} if $dist;
 
57
 
 
58
  &Utils::Report::do_report ("platform_no_table", $$tool{"platform"});
 
59
  return undef;
 
60
}
 
61
 
 
62
sub get_ntp_servers
 
63
{
 
64
  $ntp_conf = &get_config_file ();
 
65
 
 
66
  return &Utils::Parse::split_all_array_with_pos ($ntp_conf, "server", 0, "[ \t]+", "[ \t]+");
 
67
}
 
68
 
 
69
sub ntp_conf_replace
 
70
{
 
71
  my ($file, $key, $re, $value) = @_;
 
72
  my ($fd, @line, @res);
 
73
  my ($buff, $i);
 
74
  my ($pre_space, $post_comment);
 
75
  my ($line_key, $val, $rest);
 
76
  my ($n, $ret);
 
77
 
 
78
  &Utils::Report::enter ();
 
79
  &Utils::Report::do_report ("replace_split", $key, $file);
 
80
 
 
81
  $buff = &Utils::File::load_buffer ($file);
 
82
  
 
83
  foreach $i (@$buff)
 
84
  {
 
85
    $pre_space = $post_comment = "";
 
86
 
 
87
    chomp $i;
 
88
 
 
89
    $pre_space    = $1 if $i =~ s/^([ \t]+)//;
 
90
    $post_comment = $1 if $i =~ s/([ \t]*\#.*)//;
 
91
    
 
92
    if ($i ne "")
 
93
    {
 
94
      @line = split ($re, $i, 3);
 
95
      $line_key = shift (@line);
 
96
      $val      = shift (@line);
 
97
      $rest     = shift (@line);
 
98
 
 
99
      # found the key?
 
100
      if ($line_key eq $key)
 
101
      {
 
102
        $n = 0;
 
103
 
 
104
        while (@$value[$n] && (@$value[$n] ne $val))
 
105
        {
 
106
          $n++;
 
107
        }
 
108
 
 
109
        if (@$value[$n] ne $val)
 
110
        {
 
111
          $i = "";
 
112
          next;
 
113
        }
 
114
 
 
115
        delete @$value[$n];
 
116
        chomp $val;
 
117
        $i  = &Utils::Replace::set_value ($key, $val, $re) . " " . $rest;
 
118
      }
 
119
    }
 
120
 
 
121
    $i = $pre_space . $i . $post_comment . "\n";
 
122
  }
 
123
 
 
124
  foreach $i (@$value)
 
125
  {
 
126
    push (@$buff, &Utils::Replace::set_value ($key, $i, $re) . "\n") if ($i ne "");
 
127
  }
 
128
 
 
129
  &Utils::File::clean_buffer ($buff);
 
130
  $ret = &Utils::File::save_buffer ($buff, $file);
 
131
  &Utils::Report::leave ();
 
132
  return $ret;
 
133
}
 
134
 
 
135
sub set_ntp_servers
 
136
{
 
137
  my (@config) = @_;
 
138
  my ($ntp_conf);
 
139
 
 
140
  $ntp_conf = &get_config_file ();
 
141
  return &ntp_conf_replace ($ntp_conf, "server", "[ \t]+", @config);
 
142
}
 
143
 
 
144
sub apply_ntp_date
 
145
{
 
146
  my ($config) = @_;
 
147
  my ($servers, $server);
 
148
 
 
149
  foreach $server (@$config) {
 
150
      $servers .= " $server";
 
151
  }
 
152
 
 
153
  if ($server eq "") {
 
154
      # There are no servers, pick them from the ntp.org pool
 
155
      $server = "0.pool.ntp.org 1.pool.ntp.org 2.pool.ntp.org";
 
156
  }
 
157
 
 
158
  # run ntpdate, this will only be effective
 
159
  # when there isn't any NTP server running
 
160
  &Utils::File::run ("ntpdate", "-b", $servers) if ($servers);
 
161
}
 
162
 
 
163
sub get
 
164
{
 
165
  return &get_ntp_servers ();
 
166
}
 
167
 
 
168
sub set
 
169
{
 
170
  &apply_ntp_date (@_);
 
171
  return &set_ntp_servers (@_);
 
172
}
 
173
 
 
174
sub get_files
 
175
{
 
176
  my ($files);
 
177
 
 
178
  push @$files, &get_config_file ();
 
179
  return $files;
 
180
}
 
181
 
 
182
1;