summaryrefslogtreecommitdiffstats
path: root/net/linuxptp/files/sysclock-discipline-check.c
blob: 0ac9f50437f1bc4d381f44019e6d5b0076003001 (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
/*
 * Helper utility to query the kernel for the synchronization status of the
 * system clock (CLOCK_REALTIME) via adjtimex(). Used by the ptp4l init script
 * to delay startup in server mode until the clock is known to be accurate.
 *
 * Exit codes: 0 = synchronized, 1 = unsynchronized, 2 = error
 */

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

int main(void) {
    struct timex txc;

    memset(&txc, 0, sizeof(txc));

    if (adjtimex(&txc) < 0) {
        perror("adjtimex failed");
        return 2;
    }

    if (txc.status & STA_UNSYNC) {
        fprintf(stdout, "Clock status: 0x%04X (STA_UNSYNC is SET - Unsynchronized)\n", (unsigned int)txc.status);
        return 1;
    } else {
        fprintf(stdout, "Clock status: 0x%04X (STA_UNSYNC is CLEAR - Synchronized)\n", (unsigned int)txc.status);
        return 0;
    }
}