3

I'm writing a software in C and it is running as a systemd service. I need the service to run after time has synced or alternatively, I need a way for my C program to identify if time has synced.

I'm not using GPS or RTC. Just the plain Raspberry (both Zero W and 3B+).

I'm using Raspbian Strecth. I was trying to use time-sync.target but it still call my service before date/time is synced.

How can I detect if system has updated its time?

Ingo
  • 42,961
  • 20
  • 87
  • 207
Diogo Melo
  • 133
  • 1
  • 4

4 Answers4

2

timedatectl will show if synced

               Local time: Tue 2019-11-19 09:59:09 AEDT
           Universal time: Mon 2019-11-18 22:59:09 UTC
                 RTC time: n/a
                Time zone: Australia/Sydney (AEDT, +1100)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

/var/lib/systemd/timesync/clock shows last synch time (NOTE this is a zero length file - look at the timestamp)

May be /var/lib/systemd/clock on older Debian OS

NOTE Raspbian Stretch DOES NOT use ntpd

Milliways
  • 62,573
  • 32
  • 113
  • 225
2

If you install ntpd instead of timesyncd, you can check out the first line of the output of ntpstat:

$ ntpstat | head -n1
unsynchronised

... later on ..

$ ntpstat | head -n1
synchronised to NTP server (10.5.26.10) at stratum 2

Inside a script or a C program, use the return code:

ntpstat returns 0 if clock is synchronised. ntpstat returns 1 if clock is not synchronised. ntpstat returns 2 if clock state is indeterminant, for example if ntpd is not contactable.

Dmitry Grigoryev
  • 28,277
  • 6
  • 54
  • 147
2

Since systemd 239 there is a systemd-time-wait-sync.service available that you can just use in your systemd service to start After=systemd-time-wait-sync.service. For further information look at How can I delay the startup of systemd services until the datetime is set (no RTC on the Raspberry Pi). Raspbian Buster comes with systemd 241.

The problem is that you use old-stable Raspbian Stretch which comes with systemd 232 so the nice time wait service isn't available in that version. For this situation you can look at the journal for a message about time syncing. I have made a service for this so you can also use it to start After= it. You will find it also at How can I delay the startup of systemd services until the datetime is set (no RTC on the Raspberry Pi).

Ingo
  • 42,961
  • 20
  • 87
  • 207
0

you can see in the systemd -> datetimed source code about how the datetimecli is fetching that details and then you can easily replicate that in your C code.

here is the link: https://github.com/systemd/systemd/blob/fce07bcb7aa647200638b3f502fb988ffdddec82/src/timedate/timedated.c#L566

the code:

static bool ntp_synced(void) {
        struct timex txc = {};
    if (adjtimex(&txc) < 0)
            return false;

    /* Consider the system clock synchronized if the reported maximum error is smaller than the maximum
     * value (16 seconds). Ignore the STA_UNSYNC flag as it may have been set to prevent the kernel from
     * touching the RTC. */
    return txc.maxerror < 16000000;

}

you just need to include the <sys/timex.h> in you C file

Anshu Meena
  • 101
  • 1