summaryrefslogtreecommitdiffstats
path: root/leapdb.c
blob: e4b2f9f1fe4840378e006532ccdf6f183857c02e (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
  chronyd/chronyc - Programs for keeping computer clocks accurate.

 **********************************************************************
 * Copyright (C) Miroslav Lichvar  2009-2018, 2020, 2022
 * Copyright (C) Patrick Oppenlander 2023, 2024
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 **********************************************************************

  =======================================================================

  This module provides leap second information. */

#include "config.h"

#include "sysincl.h"

#include "conf.h"
#include "leapdb.h"
#include "logging.h"
#include "util.h"

/* ================================================== */

/* Source of leap second data */
enum {
  SRC_NONE,
  SRC_TIMEZONE,
  SRC_LIST,
} leap_src;

/* Offset between leap-seconds.list timestamp epoch and Unix epoch.
   leap-seconds.list epoch is 1 Jan 1900, 00:00:00 */
#define LEAP_SEC_LIST_OFFSET 2208988800

/* ================================================== */

static NTP_Leap
get_tz_leap(time_t when, int *tai_offset)
{
  struct tm stm, *tm;
  time_t t;
  char *tz_env, tz_orig[128];
  NTP_Leap tz_leap = LEAP_Normal;

  tm = gmtime(&when);
  if (!tm)
    return tz_leap;

  stm = *tm;

  /* Temporarily switch to the timezone containing leap seconds */
  tz_env = getenv("TZ");
  if (tz_env) {
    if (strlen(tz_env) >= sizeof (tz_orig))
      return tz_leap;
    strcpy(tz_orig, tz_env);
  }
  setenv("TZ", CNF_GetLeapSecTimezone(), 1);
  tzset();

  /* Get the TAI-UTC offset, which started at the epoch at 10 seconds */
  t = mktime(&stm);
  if (t != -1)
    *tai_offset = t - when + 10;

  /* Set the time to 23:59:60 and see how it overflows in mktime() */
  stm.tm_sec = 60;
  stm.tm_min = 59;
  stm.tm_hour = 23;

  t = mktime(&stm);

  if (tz_env)
    setenv("TZ", tz_orig, 1);
  else
    unsetenv("TZ");
  tzset();

  if (t == -1)
    return tz_leap;

  if (stm.tm_sec == 60)
    tz_leap = LEAP_InsertSecond;
  else if (stm.tm_sec == 1)
    tz_leap = LEAP_DeleteSecond;

  return tz_leap;
}

/* ================================================== */

static NTP_Leap
get_list_leap(time_t when, int *tai_offset)
{
  FILE *f;
  char line[1024];
  NTP_Leap ret_leap = LEAP_Normal;
  int ret_tai_offset = 0, prev_lsl_tai_offset = 10;
  int64_t when1900, lsl_updated = 0, lsl_expiry = 0;
  const char *leap_sec_list = CNF_GetLeapSecList();

  if (!(f = UTI_OpenFile(NULL, leap_sec_list, NULL, 'r', 0))) {
    LOG(LOGS_ERR, "Failed to open leap seconds list %s", leap_sec_list);
    goto out;
  }

  /* Leap second happens at midnight */
  when = (when / (24 * 3600) + 1) * (24 * 3600);

  /* leap-seconds.list timestamps are relative to 1 Jan 1900, 00:00:00 */
  when1900 = (int64_t)when + LEAP_SEC_LIST_OFFSET;

  while (fgets(line, sizeof line, f) > 0) {
    int64_t lsl_when;
    int lsl_tai_offset;
    char *p;

    /* Ignore blank lines */
    for (p = line; *p && isspace(*p); ++p)
      ;
    if (!*p)
      continue;

    if (*line == '#') {
      /* Update time line starts with #$ */
      if (line[1] == '$' && sscanf(line + 2, "%"SCNd64, &lsl_updated) != 1)
        goto error;
      /* Expiration time line starts with #@ */
      if (line[1] == '@' && sscanf(line + 2, "%"SCNd64, &lsl_expiry) != 1)
        goto error;
      /* Comment or a special comment we don't care about */
      continue;
    }

    /* Leap entry */
    if (sscanf(line, "%"SCNd64" %d", &lsl_when, &lsl_tai_offset) != 2)
      goto error;

    if (when1900 == lsl_when) {
      if (lsl_tai_offset > prev_lsl_tai_offset)
        ret_leap = LEAP_InsertSecond;
      else if (lsl_tai_offset < prev_lsl_tai_offset)
        ret_leap = LEAP_DeleteSecond;
      /* When is rounded to the end of the day, so offset hasn't changed yet! */
      ret_tai_offset = prev_lsl_tai_offset;
    } else if (when1900 > lsl_when) {
      ret_tai_offset = lsl_tai_offset;
    }

    prev_lsl_tai_offset = lsl_tai_offset;
  }

  /* Make sure the file looks sensible */
  if (!feof(f) || !lsl_updated || !lsl_expiry)
    goto error;

  if (when1900 >= lsl_expiry)
    LOG(LOGS_WARN, "Leap second list %s needs update", leap_sec_list);

  goto out;

error:
  if (f)
    fclose(f);
  LOG(LOGS_ERR, "Failed to parse leap seconds list %s", leap_sec_list);
  return LEAP_Normal;

out:
  if (f)
    fclose(f);
  *tai_offset = ret_tai_offset;
  return ret_leap;
}

/* ================================================== */

static int
check_leap_source(NTP_Leap (*src)(time_t when, int *tai_offset))
{
  int tai_offset = 0;

  /* Check that the leap second source has good data for Jun 30 2012 and Dec 31 2012 */
  if (src(1341014400, &tai_offset) == LEAP_InsertSecond && tai_offset == 34 &&
      src(1356912000, &tai_offset) == LEAP_Normal && tai_offset == 35)
    return 1;

  return 0;
}

/* ================================================== */

void
LDB_Initialise(void)
{
  const char *leap_tzname, *leap_sec_list;

  leap_tzname = CNF_GetLeapSecTimezone();
  if (leap_tzname && !check_leap_source(get_tz_leap)) {
    LOG(LOGS_WARN, "Timezone %s failed leap second check, ignoring", leap_tzname);
    leap_tzname = NULL;
  }

  leap_sec_list = CNF_GetLeapSecList();
  if (leap_sec_list && !check_leap_source(get_list_leap)) {
    LOG(LOGS_WARN, "Leap second list %s failed check, ignoring", leap_sec_list);
    leap_sec_list = NULL;
  }

  if (leap_sec_list) {
    LOG(LOGS_INFO, "Using leap second list %s", leap_sec_list);
    leap_src = SRC_LIST;
  } else if (leap_tzname) {
    LOG(LOGS_INFO, "Using %s timezone to obtain leap second data", leap_tzname);
    leap_src = SRC_TIMEZONE;
  }
}

/* ================================================== */

NTP_Leap
LDB_GetLeap(time_t when, int *tai_offset)
{
  static time_t last_ldb_leap_check;
  static NTP_Leap ldb_leap;
  static int ldb_tai_offset;

  /* Do this check at most twice a day */
  when = when / (12 * 3600) * (12 * 3600);
  if (last_ldb_leap_check == when)
    goto out;

  last_ldb_leap_check = when;
  ldb_leap = LEAP_Normal;
  ldb_tai_offset = 0;

  switch (leap_src) {
  case SRC_NONE:
    break;
  case SRC_TIMEZONE:
    ldb_leap = get_tz_leap(when, &ldb_tai_offset);
    break;
  case SRC_LIST:
    ldb_leap = get_list_leap(when, &ldb_tai_offset);
    break;
  }

out:
  *tai_offset = ldb_tai_offset;
  return ldb_leap;
}

/* ================================================== */

void
LDB_Finalise(void)
{
  /* Nothing to do */
}