summaryrefslogtreecommitdiff
path: root/monotonic.c
blob: de12fa5cdb7fb3d57ac781113e0a5ac8ad9cf4c2 (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
/*
Copyright (c) 2008, 2009 by Juliusz Chroboczek

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#define _GNU_SOURCE 1
#define _POSIX_C_SOURCE 200112L

#if defined(__linux__)
#define HAVE_ADJTIMEX
#endif

#if defined(__NetBSD__) || defined(__FreeBSD__)
#define HAVE_NTP_GETTIME
#endif

#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <stdlib.h>

#include "monotonic.h"

#if defined(__GNUC__) && (__GNUC__ >= 3)
#define UNLIKELY(_x) __builtin_expect(!!(_x), 0)
#else
#define UNLIKELY(_x) !!(_x)
#endif

/* Whether we use CLOCK_MONOTONIC */
static int have_posix_clocks = -1;

/* The clock status, broken, untrusted, or trusted. */
static int clock_status = CLOCK_BROKEN;

/* The (monotonic) time since when monotonic time has been stable.  If we
   have TIME_MONOTONIC, this is the time at which we inited. */
time_t clock_stable_time;

/* The last (monotonic) time we checked for time sync. */
time_t ntp_check_time = 0;

/* These variables are used for simulating monotonic time when we don't
   have CLOCK_MONOTONIC.  Offset is the offset to add to real time to get
   monotonic time; previous is the previous real time. */
static time_t offset, previous;

#if defined(HAVE_ADJTIMEX)

#include <sys/timex.h>

static int
ntp_sync(void)
{
    int rc;
    struct timex timex;

    timex.modes = 0;

    rc = adjtimex(&timex);
    return (rc >= 0 && rc != TIME_ERROR);
}

#elif defined(HAVE_NTP_GETTIME)

#include <sys/timex.h>

static int
ntp_sync(void)
{
    int rc;
    struct ntptimeval ntptv;

    rc = ntp_gettime(&ntptv);
    return (rc >= 0 && ntptv.time_state != TIME_ERROR);
}

#else

static int
ntp_sync(void)
{
    return 0;
}

#endif

static void
fix_clock(struct timeval *real)
{
    if(!have_posix_clocks) {
        if(previous > real->tv_sec) {
            offset += previous - real->tv_sec;
        } else if(previous + MAX_SLEEP < real->tv_sec) {
            offset += previous + MAX_SLEEP - real->tv_sec;
        }
    }

    if(real->tv_sec < 1200000000)
        clock_status = CLOCK_BROKEN;
    else if(ntp_sync())
        clock_status = CLOCK_TRUSTED;
    else
        clock_status = CLOCK_UNTRUSTED;
}

void
time_init()
{
    struct timeval now, real;
#if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 && defined(CLOCK_MONOTONIC)
    int rc;
    struct timespec ts;
    rc = clock_gettime(CLOCK_MONOTONIC, &ts);
    have_posix_clocks = (rc >= 0);
#else
    have_posix_clocks = 0;
#endif

    gettimeofday(&real, NULL);
    offset = 0;
    previous = real.tv_sec;
    fix_clock(&real);
    
    gettime(&now, NULL);
    clock_stable_time = now.tv_sec;
}

/* -1: no confirmation, just check NTP status.
    0: confirm that our time is broken.
    1: confirm that our time seems reasonable. */

void
time_confirm(int confirm)
{
    struct timeval tv;

    gettimeofday(&tv, NULL);

    if(tv.tv_sec < 1200000000 || confirm == 0)
        clock_status = CLOCK_BROKEN;
    else if(ntp_sync())
        clock_status = CLOCK_TRUSTED;
    else if(confirm > 0)
        clock_status = CLOCK_UNTRUSTED;
}

int
get_real_time(struct timeval *tv, int *status_return)
{
    int rc;
    rc = gettimeofday(tv, NULL);
    if(rc < 0)
        return rc;

    if(UNLIKELY(tv->tv_sec < previous || tv->tv_sec > previous + MAX_SLEEP)) {
        fix_clock(tv);
    }
    previous = tv->tv_sec;

    if(status_return)
        *status_return = clock_status;
    return rc;
}

int
gettime(struct timeval *tv, time_t *stable)
{
    int rc;

#if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 && defined(CLOCK_MONOTONIC)
    if(have_posix_clocks) {
        struct timespec ts;

        rc = clock_gettime(CLOCK_MONOTONIC, &ts);
        if(rc < 0)
            return rc;
        tv->tv_sec = ts.tv_sec;
        tv->tv_usec = ts.tv_nsec / 1000;

        if(stable)
            *stable = tv->tv_sec - clock_stable_time;
    } else
#else
#warning No CLOCK_MONOTONIC on this platform
#endif
    {
        rc = get_real_time(tv, NULL);
        if(rc < 0)
            return rc;
        tv->tv_sec += offset;
    }

    if(stable)
        *stable = tv->tv_sec - clock_stable_time;

    if(UNLIKELY(tv->tv_sec - ntp_check_time > 3600)) {
        time_confirm(-1);
        ntp_check_time = tv->tv_sec;
    }

    return rc;
}