[Swan-dev] now() and other topics

D. Hugh Redelmeier hugh at mimosa.com
Thu May 1 10:08:33 EEST 2014

| From: Paul Wouters <paul at nohats.ca>

| The whole point of that change was to fix a dead lock:
| https://github.com/xelerance/Openswan/issues/74

What you want is a "monotonic clock".  That's not what time(2) does.

A long time ago, I wrote now() to try to implement a monotonic clock
by conditioning the output of time(2).

These days, the best way to do that is to use clock_gettime(3),
specifying CLOCK_MONOTONIC (not guaranteed to be present, but I would
be surpirsed if it was missing).

What's wrong with now() as a montonic clock?  It is monotonic in the
mathematical sense (never going backwards), but CLOCK_MONOTONIC avoids
big jumps forward in time and now() does not and cannot.  Furthermore,
if time flutters (as in the bug report), the offset could grow until
there is time overflow (unlikely but possible, especially if that is
the goal of an attacker).

Don't mix up monotonic time with wall clock time (AKA CLOCK_REALTIME).
Some uses need one and some need another.

1) now() should be rewritten to use CLOCK_MONOTONIC

2) all uses of time should be examined to see if they need REALTIME
   (eg. logging), or MONOTONIC (eg. timers).

Note that time could be more precise than just a time_t.  I don't know
if we need that.

I don't think that everything you converted to using now() should be
using MONTONIC time (or now()).

I guess now should be forked: now_monotonic() and now_real().

More information about the Swan-dev mailing list