main: add -t option to chronyd

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.
This commit is contained in:
Miroslav Lichvar 2016-12-01 15:57:39 +01:00
parent 2d67871bbf
commit 3513484852
2 changed files with 25 additions and 1 deletions

View file

@ -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 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). 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_:: *-u* _user_::
This option sets the name of the system user to which *chronyd* will switch 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 after start in order to drop root privileges. It overrides the

19
main.c
View file

@ -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 static void
ntp_source_resolving_end(void) ntp_source_resolving_end(void)
{ {
@ -362,7 +372,7 @@ int main
char *user = NULL; char *user = NULL;
struct passwd *pw; struct passwd *pw;
int debug = 0, nofork = 0, address_family = IPADDR_UNSPEC; 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 other_pid;
int scfilter_level = 0, lock_memory = 0, sched_priority = 0; int scfilter_level = 0, lock_memory = 0, sched_priority = 0;
int system_log = 1; int system_log = 1;
@ -420,6 +430,10 @@ int main
ref_mode = REF_ModePrintOnce; ref_mode = REF_ModePrintOnce;
nofork = 1; nofork = 1;
system_log = 0; 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)) { } else if (!strcmp("-4", *argv)) {
address_family = IPADDR_INET4; address_family = IPADDR_INET4;
} else if (!strcmp("-6", *argv)) { } else if (!strcmp("-6", *argv)) {
@ -549,6 +563,9 @@ int main
REF_SetModeEndHandler(reference_mode_end); REF_SetModeEndHandler(reference_mode_end);
REF_SetMode(ref_mode); REF_SetMode(ref_mode);
if (timeout)
SCH_AddTimeoutByDelay(timeout, quit_timeout, NULL);
if (do_init_rtc) { if (do_init_rtc) {
RTC_TimeInit(post_init_rtc_hook, NULL); RTC_TimeInit(post_init_rtc_hook, NULL);
} else { } else {