Coverage Report

Created: 2022-03-10 07:56

/src/bind9/lib/isc/stdtime.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4
 * SPDX-License-Identifier: MPL-2.0
5
 *
6
 * This Source Code Form is subject to the terms of the Mozilla Public
7
 * License, v. 2.0. If a copy of the MPL was not distributed with this
8
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9
 *
10
 * See the COPYRIGHT file distributed with this work for additional
11
 * information regarding copyright ownership.
12
 */
13
14
/*! \file */
15
16
#include <errno.h>
17
#include <stdbool.h>
18
#include <stddef.h> /* NULL */
19
#include <stdlib.h> /* NULL */
20
#include <syslog.h>
21
#include <time.h>
22
23
#include <isc/stdtime.h>
24
#include <isc/strerr.h>
25
#include <isc/util.h>
26
27
#define NS_PER_S 1000000000 /*%< Nanoseconds per second. */
28
29
#if defined(CLOCK_REALTIME_COARSE)
30
31.8k
#define CLOCKSOURCE CLOCK_REALTIME_COARSE
31
#elif defined(CLOCK_REALTIME_FAST)
32
#define CLOCKSOURCE CLOCK_REALTIME_FAST
33
#else /* if defined(CLOCK_REALTIME_COARSE) */
34
#define CLOCKSOURCE CLOCK_REALTIME
35
#endif /* if defined(CLOCK_REALTIME_COARSE) */
36
37
void
38
31.8k
isc_stdtime_get(isc_stdtime_t *t) {
39
31.8k
  REQUIRE(t != NULL);
40
41
0
  struct timespec ts;
42
43
31.8k
  if (clock_gettime(CLOCKSOURCE, &ts) == -1) {
44
0
    char strbuf[ISC_STRERRORSIZE];
45
0
    strerror_r(errno, strbuf, sizeof(strbuf));
46
0
    isc_error_fatal(__FILE__, __LINE__, "clock_gettime failed: %s",
47
0
        strbuf);
48
0
  }
49
50
31.8k
  REQUIRE(ts.tv_sec > 0 && ts.tv_nsec >= 0 && ts.tv_nsec < NS_PER_S);
51
52
0
  *t = (isc_stdtime_t)ts.tv_sec;
53
31.8k
}
54
55
void
56
0
isc_stdtime_tostring(isc_stdtime_t t, char *out, size_t outlen) {
57
0
  time_t when;
58
59
0
  REQUIRE(out != NULL);
60
0
  REQUIRE(outlen >= 26);
61
62
0
  UNUSED(outlen);
63
64
  /* time_t and isc_stdtime_t might be different sizes */
65
0
  when = t;
66
0
  INSIST((ctime_r(&when, out) != NULL));
67
0
  *(out + strlen(out) - 1) = '\0';
68
0
}