From e335456ff4f4588fa85cddbbdba675d448344dbb Mon Sep 17 00:00:00 2001 From: smuthu545 Date: Thu, 21 May 2026 04:12:11 +0000 Subject: [PATCH 01/11] Unified NTP sync from systimemgr --- systimemgr.cpp | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ systimemgr.h | 2 ++ 2 files changed, 78 insertions(+) diff --git a/systimemgr.cpp b/systimemgr.cpp index 71f3e0e4..9608fe32 100644 --- a/systimemgr.cpp +++ b/systimemgr.cpp @@ -38,6 +38,8 @@ #endif #include "systimerfactory/networkstatussrc.h" +#include +#include #ifdef T2_EVENT_ENABLED #include @@ -190,6 +192,7 @@ void SysTimeMgr::run(bool forever) std::thread processThrd(SysTimeMgr::processThr,this); std::thread timerThrd(SysTimeMgr::timerThr,this); std::thread pathMonitorThrd(SysTimeMgr::pathThr,this); + std::thread ntpSyncMonitorThrd(SysTimeMgr::ntpSyncMonitorThr,this); bool chronyRfcEnabled = (access("/opt/secure/RFC/chrony/chronyd_enabled", F_OK) == 0); RDK_LOG(RDK_LOG_INFO,LOG_SYSTIME,"[%s:%d]:RFC chronyd_enabled file %s\n", __FUNCTION__,__LINE__, @@ -215,6 +218,7 @@ void SysTimeMgr::run(bool forever) processThrd.join(); timerThrd.join(); pathMonitorThrd.join(); + ntpSyncMonitorThrd.join(); if (nwEventProcessThrd.joinable()) nwEventProcessThrd.join(); if (nwEventSubscribeThrd.joinable()) @@ -225,6 +229,7 @@ void SysTimeMgr::run(bool forever) processThrd.detach(); timerThrd.detach(); pathMonitorThrd.detach(); + ntpSyncMonitorThrd.detach(); if (nwEventProcessThrd.joinable()) nwEventProcessThrd.detach(); if (nwEventSubscribeThrd.joinable()) @@ -269,6 +274,77 @@ void SysTimeMgr::nwEventProcessThr(SysTimeMgr* instance) instance->runNWEventProcessing(); } +void SysTimeMgr::ntpSyncMonitorThr(SysTimeMgr* instance) +{ + if (instance) + instance->runNTPSyncMonitor(); +} + +/* + * runNTPSyncMonitor: polls adjtimex() once per second until the kernel clock is + * synchronised with NTP (STA_UNSYNC flag cleared). On success it logs the + * event, creates /tmp/clock-event and /tmp/systimemgr/ntp, then returns. + * The primary purpose is to capture the NTP convergence time. + */ +void SysTimeMgr::runNTPSyncMonitor() +{ + RDK_LOG(RDK_LOG_INFO, LOG_SYSTIME, + "[%s:%d]: NTP sync monitor thread started\n", __FUNCTION__, __LINE__); + + while (1) + { + struct timex tx; + memset(&tx, 0, sizeof(tx)); + + if (adjtimex(&tx) < 0) + { + RDK_LOG(RDK_LOG_ERROR, LOG_SYSTIME, + "[%s:%d]: adjtimex() failed, retrying\n", __FUNCTION__, __LINE__); + std::this_thread::sleep_for(std::chrono::seconds(1)); + continue; + } + + if (tx.status & STA_UNSYNC) + { + /* Clock not yet synchronised — keep polling. */ + std::this_thread::sleep_for(std::chrono::seconds(1)); + continue; + } + + /* NTP synchronisation achieved. */ + RDK_LOG(RDK_LOG_INFO, LOG_SYSTIME, + "[%s:%d]: NTP synchronised\n", __FUNCTION__, __LINE__); + + /* Create /tmp/systimemgr/ntp */ + { + int fd = open((m_directory + "/ntp").c_str(), O_CREAT | O_WRONLY, 0644); + if (fd >= 0) + close(fd); + else + RDK_LOG(RDK_LOG_ERROR, LOG_SYSTIME, + "[%s:%d]: Failed to create %s/ntp\n", + __FUNCTION__, __LINE__, m_directory.c_str()); + } + + /* Create /tmp/clock-event */ + { + int fd = open("/tmp/clock-event", O_CREAT | O_WRONLY, 0644); + if (fd >= 0) + close(fd); + else + RDK_LOG(RDK_LOG_ERROR, LOG_SYSTIME, + "[%s:%d]: Failed to create /tmp/clock-event\n", + __FUNCTION__, __LINE__); + } + + /* Synchronisation captured — stop polling. */ + break; + } + + RDK_LOG(RDK_LOG_INFO, LOG_SYSTIME, + "[%s:%d]: NTP sync monitor thread exiting\n", __FUNCTION__, __LINE__); +} + void SysTimeMgr::processMsg() { diff --git a/systimemgr.h b/systimemgr.h index 742ab4c0..6ae10481 100644 --- a/systimemgr.h +++ b/systimemgr.h @@ -118,10 +118,12 @@ class SysTimeMgr static void pathThr(SysTimeMgr* instance); static void nwEventSubscribeThr(SysTimeMgr* instance); static void nwEventProcessThr(SysTimeMgr* instance); + static void ntpSyncMonitorThr(SysTimeMgr* instance); void runNetworkStatusMonitor(); void runNWEventProcessing(); + void runNTPSyncMonitor(); void updateTimeSync(long long updateTime); void publishStatus(publishEvent event,string message); From d04cd366a4dc2b4fd92806070cab25a2eaa59be3 Mon Sep 17 00:00:00 2001 From: sindhu-krishnan <102755514+sindhu-krishnan@users.noreply.github.com> Date: Thu, 21 May 2026 12:00:22 +0530 Subject: [PATCH 02/11] Update systimemgr.cpp --- systimemgr.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/systimemgr.cpp b/systimemgr.cpp index 9608fe32..5bde86f7 100644 --- a/systimemgr.cpp +++ b/systimemgr.cpp @@ -337,6 +337,17 @@ void SysTimeMgr::runNTPSyncMonitor() __FUNCTION__, __LINE__); } + int fd = open("/tmp/ntp_status", O_CREAT | O_WRONLY | O_TRUNC, 0644); + if (fd >= 0) + { + const char* status = "Synchronized\n"; + write(fd, status, strlen(status)); + close(fd); + } else { + RDK_LOG(RDK_LOG_ERROR, LOG_SYSTIME, + "[%s:%d]: Failed to create /tmp/ntp_status\n", + __FUNCTION__, __LINE__); + } /* Synchronisation captured — stop polling. */ break; } From e32265557d9e081080a7da39a9b959293a671a43 Mon Sep 17 00:00:00 2001 From: sindhu-krishnan <102755514+sindhu-krishnan@users.noreply.github.com> Date: Thu, 21 May 2026 12:15:52 +0530 Subject: [PATCH 03/11] Update systimemgr.cpp --- systimemgr.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/systimemgr.cpp b/systimemgr.cpp index 5bde86f7..f0f37f74 100644 --- a/systimemgr.cpp +++ b/systimemgr.cpp @@ -31,7 +31,7 @@ #include #include "irdklog.h" #include "itimermsg.h" -#include +#include Date: Thu, 21 May 2026 12:59:04 +0530 Subject: [PATCH 04/11] Update systimemgr.cpp --- systimemgr.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/systimemgr.cpp b/systimemgr.cpp index f0f37f74..f3b82c2e 100644 --- a/systimemgr.cpp +++ b/systimemgr.cpp @@ -31,7 +31,7 @@ #include #include "irdklog.h" #include "itimermsg.h" -#include #include "secure_wrapper.h" #if !defined(MILESTONE_SUPPORT_DISABLED) #include "rdk_logger_milestone.h" From b2534699656026f50bd2df2a9198d9e954ff35a3 Mon Sep 17 00:00:00 2001 From: sindhu-krishnan <102755514+sindhu-krishnan@users.noreply.github.com> Date: Thu, 21 May 2026 13:16:22 +0530 Subject: [PATCH 05/11] Update systimemgr.cpp --- systimemgr.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/systimemgr.cpp b/systimemgr.cpp index f3b82c2e..426b34c6 100644 --- a/systimemgr.cpp +++ b/systimemgr.cpp @@ -219,7 +219,6 @@ void SysTimeMgr::run(bool forever) processThrd.join(); timerThrd.join(); pathMonitorThrd.join(); - ntpSyncMonitorThrd.join(); if (nwEventProcessThrd.joinable()) nwEventProcessThrd.join(); if (nwEventSubscribeThrd.joinable()) @@ -233,12 +232,11 @@ void SysTimeMgr::run(bool forever) processThrd.detach(); timerThrd.detach(); pathMonitorThrd.detach(); - ntpSyncMonitorThrd.detach(); if (nwEventProcessThrd.joinable()) nwEventProcessThrd.detach(); if (nwEventSubscribeThrd.joinable()) nwEventSubscribeThrd.detach(); - if (ntpSyncMonitorThrd.joinable()) + if (ntpSyncMonitorThrd.joinable()) ntpSyncMonitorThrd.detach(); } } From 23aa89fe004468674856640ce084ac0c8409e84d Mon Sep 17 00:00:00 2001 From: sindhu-krishnan <102755514+sindhu-krishnan@users.noreply.github.com> Date: Thu, 21 May 2026 13:20:14 +0530 Subject: [PATCH 06/11] Update systimemgr.cpp --- systimemgr.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/systimemgr.cpp b/systimemgr.cpp index 426b34c6..ba6fb9fb 100644 --- a/systimemgr.cpp +++ b/systimemgr.cpp @@ -219,12 +219,15 @@ void SysTimeMgr::run(bool forever) processThrd.join(); timerThrd.join(); pathMonitorThrd.join(); - if (nwEventProcessThrd.joinable()) + if (nwEventProcessThrd.joinable()) { nwEventProcessThrd.join(); - if (nwEventSubscribeThrd.joinable()) + } + if (nwEventSubscribeThrd.joinable()) { nwEventSubscribeThrd.join(); - if (ntpSyncMonitorThrd.joinable()) + } + if (ntpSyncMonitorThrd.joinable()) { ntpSyncMonitorThrd.join(); + } } else @@ -232,12 +235,15 @@ void SysTimeMgr::run(bool forever) processThrd.detach(); timerThrd.detach(); pathMonitorThrd.detach(); - if (nwEventProcessThrd.joinable()) + if (nwEventProcessThrd.joinable()) { nwEventProcessThrd.detach(); - if (nwEventSubscribeThrd.joinable()) + } + if (nwEventSubscribeThrd.joinable()) { nwEventSubscribeThrd.detach(); - if (ntpSyncMonitorThrd.joinable()) + } + if (ntpSyncMonitorThrd.joinable()) { ntpSyncMonitorThrd.detach(); + } } } From 47feeda7b70f4ffee25bbb52001b1c9dc7a257ae Mon Sep 17 00:00:00 2001 From: sindhu-krishnan <102755514+sindhu-krishnan@users.noreply.github.com> Date: Thu, 21 May 2026 16:41:30 +0530 Subject: [PATCH 07/11] Update systimemgr.cpp --- systimemgr.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/systimemgr.cpp b/systimemgr.cpp index ba6fb9fb..9bd72765 100644 --- a/systimemgr.cpp +++ b/systimemgr.cpp @@ -299,7 +299,7 @@ void SysTimeMgr::ntpSyncMonitorThr(SysTimeMgr* instance) void SysTimeMgr::runNTPSyncMonitor() { RDK_LOG(RDK_LOG_INFO, LOG_SYSTIME, - "[%s:%d]: NTP sync monitor thread started\n", __FUNCTION__, __LINE__); + "[%s:%d]: CHRONY: NTP sync monitor thread started\n", __FUNCTION__, __LINE__); while (1) { @@ -309,7 +309,7 @@ void SysTimeMgr::runNTPSyncMonitor() if (adjtimex(&tx) < 0) { RDK_LOG(RDK_LOG_ERROR, LOG_SYSTIME, - "[%s:%d]: adjtimex() failed, retrying\n", __FUNCTION__, __LINE__); + "[%s:%d]: CHRONY: adjtimex() failed, retrying\n", __FUNCTION__, __LINE__); std::this_thread::sleep_for(std::chrono::seconds(1)); continue; } @@ -323,7 +323,7 @@ void SysTimeMgr::runNTPSyncMonitor() /* NTP synchronisation achieved. */ RDK_LOG(RDK_LOG_INFO, LOG_SYSTIME, - "[%s:%d]: NTP synchronised\n", __FUNCTION__, __LINE__); + "[%s:%d]: CHRONY: NTP synchronised\n", __FUNCTION__, __LINE__); /* Create /tmp/systimemgr/ntp */ { @@ -363,7 +363,7 @@ void SysTimeMgr::runNTPSyncMonitor() } RDK_LOG(RDK_LOG_INFO, LOG_SYSTIME, - "[%s:%d]: NTP sync monitor thread exiting\n", __FUNCTION__, __LINE__); + "[%s:%d]: CHRONY: NTP sync monitor thread exiting\n", __FUNCTION__, __LINE__); } From 8fe3935b10b89afc7019bdb80d9b38e581a00d14 Mon Sep 17 00:00:00 2001 From: sindhu-krishnan <102755514+sindhu-krishnan@users.noreply.github.com> Date: Thu, 21 May 2026 16:41:30 +0530 Subject: [PATCH 08/11] Update systimemgr.cpp --- systimemgr.cpp | 64 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 17 deletions(-) diff --git a/systimemgr.cpp b/systimemgr.cpp index 9bd72765..33af8da1 100644 --- a/systimemgr.cpp +++ b/systimemgr.cpp @@ -306,7 +306,10 @@ void SysTimeMgr::runNTPSyncMonitor() struct timex tx; memset(&tx, 0, sizeof(tx)); - if (adjtimex(&tx) < 0) + /* Capture adjtimex() return value: TIME_ERROR means the kernel clock + * is not disciplined, which is treated the same as STA_UNSYNC. */ + int adjtimex_state = adjtimex(&tx); + if (adjtimex_state < 0) { RDK_LOG(RDK_LOG_ERROR, LOG_SYSTIME, "[%s:%d]: CHRONY: adjtimex() failed, retrying\n", __FUNCTION__, __LINE__); @@ -314,7 +317,7 @@ void SysTimeMgr::runNTPSyncMonitor() continue; } - if (tx.status & STA_UNSYNC) + if (adjtimex_state == TIME_ERROR || (tx.status & STA_UNSYNC)) { /* Clock not yet synchronised — keep polling. */ std::this_thread::sleep_for(std::chrono::seconds(1)); @@ -325,20 +328,35 @@ void SysTimeMgr::runNTPSyncMonitor() RDK_LOG(RDK_LOG_INFO, LOG_SYSTIME, "[%s:%d]: CHRONY: NTP synchronised\n", __FUNCTION__, __LINE__); - /* Create /tmp/systimemgr/ntp */ + /* Create /tmp/systimemgr/ntp and update its timestamps via futimens() + * so that the inotify IN_ATTRIB event fires and the path monitor thread + * reliably dispatches eSYSMGR_EVENT_NTP_AVAILABLE into the state machine. + * O_NOFOLLOW|O_CLOEXEC guard against symlink attacks. */ { - int fd = open((m_directory + "/ntp").c_str(), O_CREAT | O_WRONLY, 0644); + int fd = open((m_directory + "/ntp").c_str(), + O_CREAT | O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_NOCTTY, 0644); if (fd >= 0) + { + struct timespec ts[2]; + timespec_get(&ts[0], TIME_UTC); + ts[1] = ts[0]; + if (futimens(fd, ts) != 0) + RDK_LOG(RDK_LOG_ERROR, LOG_SYSTIME, + "[%s:%d]: futimens() failed for %s/ntp\n", + __FUNCTION__, __LINE__, m_directory.c_str()); close(fd); + } else RDK_LOG(RDK_LOG_ERROR, LOG_SYSTIME, "[%s:%d]: Failed to create %s/ntp\n", __FUNCTION__, __LINE__, m_directory.c_str()); } - /* Create /tmp/clock-event */ + /* Create /tmp/clock-event — O_NOFOLLOW|O_CLOEXEC guard against + * symlink/hardlink attacks when running with elevated privileges. */ { - int fd = open("/tmp/clock-event", O_CREAT | O_WRONLY, 0644); + int fd = open("/tmp/clock-event", + O_CREAT | O_WRONLY | O_NOFOLLOW | O_CLOEXEC, 0644); if (fd >= 0) close(fd); else @@ -347,17 +365,29 @@ void SysTimeMgr::runNTPSyncMonitor() __FUNCTION__, __LINE__); } - int fd = open("/tmp/ntp_status", O_CREAT | O_WRONLY | O_TRUNC, 0644); - if (fd >= 0) - { - const char* status = "Synchronized\n"; - write(fd, status, strlen(status)); - close(fd); - } else { - RDK_LOG(RDK_LOG_ERROR, LOG_SYSTIME, - "[%s:%d]: Failed to create /tmp/ntp_status\n", - __FUNCTION__, __LINE__); - } + /* Write NTP sync status to /tmp/ntp_status — O_NOFOLLOW|O_CLOEXEC + * guards against symlink attacks; write() return value is checked. */ + { + int fd = open("/tmp/ntp_status", + O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC, 0644); + if (fd >= 0) + { + const char* status = "Synchronized\n"; + ssize_t written = write(fd, status, strlen(status)); + if (written < 0) + RDK_LOG(RDK_LOG_ERROR, LOG_SYSTIME, + "[%s:%d]: Failed to write to /tmp/ntp_status\n", + __FUNCTION__, __LINE__); + close(fd); + } + else + { + RDK_LOG(RDK_LOG_ERROR, LOG_SYSTIME, + "[%s:%d]: Failed to create /tmp/ntp_status\n", + __FUNCTION__, __LINE__); + } + } + /* Synchronisation captured — stop polling. */ break; } From 3b59ff04adcc136c50d35b9743a1b7710e27ad7e Mon Sep 17 00:00:00 2001 From: sindhu-krishnan <102755514+sindhu-krishnan@users.noreply.github.com> Date: Thu, 21 May 2026 23:51:56 +0530 Subject: [PATCH 09/11] Update systimemgr.cpp --- systimemgr.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/systimemgr.cpp b/systimemgr.cpp index 33af8da1..1051719b 100644 --- a/systimemgr.cpp +++ b/systimemgr.cpp @@ -392,6 +392,19 @@ void SysTimeMgr::runNTPSyncMonitor() break; } + double offset = 0.0; + int ret = chronyctl_get_offset(&offset); + if (ret == CHRONYCTL_SUCCESS) { + char offset_str[16]; + RDK_LOG(RDK_LOG_INFO, LOG_SYSTIME, "CHRONY: Offset: %f seconds\n", offset); + snprintf(offset_str, sizeof(offset_str), "%.3f", offset); + #ifdef T2_EVENT_ENABLED + t2ValNotify((char *) "SYST_INFO_NTP_DELTA_split",offset_str); + #endif + } else { + RDK_LOG(RDK_LOG_ERROR, LOG_SYSTIME, "CHRONY: Error fetching offset: %s\n", chronyctl_strerror(ret)); + } + RDK_LOG(RDK_LOG_INFO, LOG_SYSTIME, "[%s:%d]: CHRONY: NTP sync monitor thread exiting\n", __FUNCTION__, __LINE__); } From 2c8abd4203746a87117188c10b20653e69e47a35 Mon Sep 17 00:00:00 2001 From: sindhu-krishnan <102755514+sindhu-krishnan@users.noreply.github.com> Date: Fri, 22 May 2026 00:54:41 +0530 Subject: [PATCH 10/11] Update systimemgr.cpp --- systimemgr.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/systimemgr.cpp b/systimemgr.cpp index 1051719b..8fa8750f 100644 --- a/systimemgr.cpp +++ b/systimemgr.cpp @@ -41,6 +41,8 @@ #include #include +#include "libchronyctl.h" + #ifdef T2_EVENT_ENABLED #include #endif From 7a71107bbdf6237e2f88b5499e016fbe03e22bb6 Mon Sep 17 00:00:00 2001 From: sindhu-krishnan <102755514+sindhu-krishnan@users.noreply.github.com> Date: Fri, 22 May 2026 10:24:33 +0530 Subject: [PATCH 11/11] Update systimemgr.cpp --- systimemgr.cpp | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/systimemgr.cpp b/systimemgr.cpp index 8fa8750f..5302be6a 100644 --- a/systimemgr.cpp +++ b/systimemgr.cpp @@ -41,7 +41,6 @@ #include #include -#include "libchronyctl.h" #ifdef T2_EVENT_ENABLED #include @@ -394,18 +393,6 @@ void SysTimeMgr::runNTPSyncMonitor() break; } - double offset = 0.0; - int ret = chronyctl_get_offset(&offset); - if (ret == CHRONYCTL_SUCCESS) { - char offset_str[16]; - RDK_LOG(RDK_LOG_INFO, LOG_SYSTIME, "CHRONY: Offset: %f seconds\n", offset); - snprintf(offset_str, sizeof(offset_str), "%.3f", offset); - #ifdef T2_EVENT_ENABLED - t2ValNotify((char *) "SYST_INFO_NTP_DELTA_split",offset_str); - #endif - } else { - RDK_LOG(RDK_LOG_ERROR, LOG_SYSTIME, "CHRONY: Error fetching offset: %s\n", chronyctl_strerror(ret)); - } RDK_LOG(RDK_LOG_INFO, LOG_SYSTIME, "[%s:%d]: CHRONY: NTP sync monitor thread exiting\n", __FUNCTION__, __LINE__);