0xStubs

Computer science, IT, Photography

How systemd-timesyncd handles leap seconds

This night we get another leap second, meaning that the last second of today is not 23:59:59 but 23:59:60 instead. The last time this happened was on June 30, 2012 and lead to issues on several servers due to bugs in the Linux kernel. Since then these bugs were fixed and also methods of hiding the leap second altogether were implemented. Here you can find a good overview over different configurations of kernel, ntpd and tzdata and how a leap second will be handled using these configurations. What is missing here is how a setup using systemd-timesyncd instead of ntpd will handle this situation.

Looking at the code of systemd-timesync (src/timesync/timesyncd-manager.c) we can see the following*:

switch (leap_sec) {
case 1:
    tmx.status |= STA_INS;
    break;
case -1:
    tmx.status |= STA_DEL;
    break;
}

r = clock_adjtime(CLOCK_REALTIME, &tmx);

leap_sec is set depending on the corresponding field in the NTP response. So systemd-timesyncd does not try to hide the leap second by slowing down the clock but passes the information about it to the kernel using the STA_INS flag.

Now, a few hours before a leap second, we can easily verify that this actually happens using a little C code (taken from richvdh, stackoverflow, slightly modified):

#include <sys/timex.h>
#include <stdio.h>

int main(int argc, char **argv) {
    struct timex buf;
    int res;

    buf.modes = 0;
    res = adjtimex(&buf);

    printf("clock status: %i\n", res);
    return 0;
}

Currently this outputs status 1 (TIME_INS), meaning that a leap second will be inserted by the kernel.

How this is handled by the kernel can be seen in kernel/time/ntp.c and kernel/time/timekeeping.c. To make it short: At the end of the day the time will just be set back by one second, so we will see 23:59:59 for two seconds.


Code from systemd is licensed under GNU Lesser General Public License Version 2.1 (LGPLv2.1).

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.