From a57e1eb542b2a79245d3bf242bb531b186c7c0b6 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Tue, 10 Dec 2019 17:30:42 +0100 Subject: [PATCH] rtc: don't finalize driver if initialization failed --- rtc.c | 2 +- rtc_linux.c | 23 +++++++++++------------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/rtc.c b/rtc.c index 33d04bf..fa83173 100644 --- a/rtc.c +++ b/rtc.c @@ -160,7 +160,7 @@ RTC_Initialise(int initial_set) void RTC_Finalise(void) { - if (driver.fini) { + if (driver_initialised) { (driver.fini)(); } } diff --git a/rtc_linux.c b/rtc_linux.c index a88368b..f5bc9bd 100644 --- a/rtc_linux.c +++ b/rtc_linux.c @@ -505,6 +505,17 @@ switch_interrupts(int on_off) int RTC_Linux_Initialise(void) { + /* Try to open the device */ + fd = open(CNF_GetRtcDevice(), O_RDWR); + if (fd < 0) { + LOG(LOGS_ERR, "Could not open RTC device %s : %s", + CNF_GetRtcDevice(), strerror(errno)); + return 0; + } + + /* Close on exec */ + UTI_FdSetCloexec(fd); + rtc_sec = MallocArray(time_t, MAX_SAMPLES); rtc_trim = MallocArray(double, MAX_SAMPLES); system_times = MallocArray(struct timespec, MAX_SAMPLES); @@ -515,18 +526,6 @@ RTC_Linux_Initialise(void) /* In case it didn't get done by pre-init */ coefs_file_name = CNF_GetRtcFile(); - /* Try to open device */ - - fd = open (CNF_GetRtcDevice(), O_RDWR); - if (fd < 0) { - LOG(LOGS_ERR, "Could not open RTC device %s : %s", - CNF_GetRtcDevice(), strerror(errno)); - return 0; - } - - /* Close on exec */ - UTI_FdSetCloexec(fd); - n_samples = 0; n_samples_since_regression = 0; n_runs = 0;