diff --git a/doc/chronyd.adoc b/doc/chronyd.adoc index 94301b9..9cb8eff 100644 --- a/doc/chronyd.adoc +++ b/doc/chronyd.adoc @@ -110,6 +110,13 @@ time and the RTC time, the system time will be set to it to restore the time when *chronyd* was previously stopped. This is useful on computers that have no RTC or the RTC is broken (e.g. it has no battery). +*-t* _timeout_:: +This option sets a timeout (in seconds) after which *chronyd* will exit. If the +clock is not synchronised, it will exit with a non-zero status. This is useful +with the *-q* or *-Q* option to shorten the maximum time waiting for +measurements, or with the *-r* option to limit the time when *chronyd* is +running, but still allow it to adjust the frequency of the system clock. + *-u* _user_:: This option sets the name of the system user to which *chronyd* will switch after start in order to drop root privileges. It overrides the diff --git a/main.c b/main.c index 71916fa..63d9cdc 100644 --- a/main.c +++ b/main.c @@ -145,6 +145,16 @@ signal_cleanup(int x) /* ================================================== */ +static void +quit_timeout(void *arg) +{ + /* Return with non-zero status if the clock is not synchronised */ + exit_status = REF_GetOurStratum() >= NTP_MAX_STRATUM; + SCH_QuitProgram(); +} + +/* ================================================== */ + static void ntp_source_resolving_end(void) { @@ -362,7 +372,7 @@ int main char *user = NULL; struct passwd *pw; int debug = 0, nofork = 0, address_family = IPADDR_UNSPEC; - int do_init_rtc = 0, restarted = 0; + int do_init_rtc = 0, restarted = 0, timeout = 0; int other_pid; int scfilter_level = 0, lock_memory = 0, sched_priority = 0; int system_log = 1; @@ -420,6 +430,10 @@ int main ref_mode = REF_ModePrintOnce; nofork = 1; system_log = 0; + } else if (!strcmp("-t", *argv)) { + ++argv, --argc; + if (argc == 0 || sscanf(*argv, "%d", &timeout) != 1 || timeout <= 0) + LOG_FATAL(LOGF_Main, "Bad timeout"); } else if (!strcmp("-4", *argv)) { address_family = IPADDR_INET4; } else if (!strcmp("-6", *argv)) { @@ -549,6 +563,9 @@ int main REF_SetModeEndHandler(reference_mode_end); REF_SetMode(ref_mode); + if (timeout) + SCH_AddTimeoutByDelay(timeout, quit_timeout, NULL); + if (do_init_rtc) { RTC_TimeInit(post_init_rtc_hook, NULL); } else {