diff --git a/source/AdvSecurityDml/Makefile.am b/source/AdvSecurityDml/Makefile.am index 3fc1176..d2d8456 100644 --- a/source/AdvSecurityDml/Makefile.am +++ b/source/AdvSecurityDml/Makefile.am @@ -26,7 +26,7 @@ hardware_platform = i686-linux-gnu lib_LTLIBRARIES = libdmlasecurity.la libdmlasecurity_la_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/source/AdvSecurityDml -I$(top_srcdir)/source/AdvSecuritySsp $(CPPFLAGS) -I$(top_srcdir)/../Utopia/source/include/sysevent -I${PKG_CONFIG_SYSROOT_DIR}$(includedir)/rbus libdmlasecurity_la_SOURCES = plugin_main.c cosa_adv_security_internal.c cosa_adv_security_dml.c cosa_adv_security_webconfig.c advsecurity_helpers.c advsecurity_param.c -libdmlasecurity_la_LDFLAGS = -lccsp_common -lsyscfg -lsysevent -lwebconfig_framework -lmsgpackc -ltrower-base64 -lsecure_wrapper -lrbus $(SSP_LDFLAGS) +libdmlasecurity_la_LDFLAGS = -lccsp_common -lsyscfg -lsysevent -lwebconfig_framework -lmsgpackc -ltrower-base64 -lsecure_wrapper -lrbus -lev -lpthread $(SSP_LDFLAGS) if WIFI_DATA_COLLECTION libdmlasecurity_la_CPPFLAGS += -DDML_SUPPORT -DNON_PRIVILEGED -DWIFI_DATA_COLLECTION diff --git a/source/AdvSecurityDml/cosa_adv_security_internal.c b/source/AdvSecurityDml/cosa_adv_security_internal.c index f722552..6dfb319 100644 --- a/source/AdvSecurityDml/cosa_adv_security_internal.c +++ b/source/AdvSecurityDml/cosa_adv_security_internal.c @@ -43,6 +43,9 @@ #include "safec_lib_common.h" #include "secure_wrapper.h" #include +#include +#include +#include #if defined(_COSA_BCM_MIPS_) #include #else @@ -91,6 +94,13 @@ #define SAFEBRO_CONFIG_FILE_PATH "/tmp/safebro.json" #define ADVSEC_PRIMARY_WAN_IF_NAME "erouter0" +/* Logrotate configuration for agent.txt */ +#define ADVSEC_AGENT_LOG_FILE "/rdklogs/logs/agent.txt" +#define ADVSEC_AGENT_LOG_MAX_SIZE (2 * 1024 * 1024) /* 2MB */ +#define ADVSEC_AGENT_LOG_INTERVAL 5.0 +#define LOGROTATE_BINARY "/usr/sbin/logrotate" +#define ADVSEC_AGENT_LOGROTATE_CONF "/etc/logrotate.d/advsec-agent" + #ifdef CONFIG_CISCO #define CONFIG_VENDOR_NAME "Cisco" #endif @@ -153,6 +163,8 @@ static char prevWanIfname[MAX_INTERFACE_SIZE] = {0}; void advsec_handle_sysevent_async(void); static void advsec_start_logger_thread(void); +static void* agent_log_monitor_thread(void* arg); +static void advsec_start_log_monitor_thread(void); static BOOL WaitForLoggerTimeout(ULONG period); enum advSysEvent_e{ SYSEVENT_BRIDGE_MODE_EVENT, @@ -1398,6 +1410,7 @@ CosaSecurityInitialize rc = strcpy_s(prevWanIfname, sizeof(prevWanIfname), ADVSEC_PRIMARY_WAN_IF_NAME); ERR_CHK(rc); advsec_start_logger_thread(); + advsec_start_log_monitor_thread(); advsec_handle_sysevent_async(); #ifdef WAN_FAILOVER_SUPPORTED @@ -1747,6 +1760,93 @@ static void advsec_start_logger_thread(void) } } +/* Log rotation function for agent.txt using logrotate binary */ +void rotate_agent_log(void) +{ + struct stat st; + int result; + + if (stat(ADVSEC_AGENT_LOG_FILE, &st) != 0) + { + return; + } + + if (st.st_size < ADVSEC_AGENT_LOG_MAX_SIZE) + { + return; + } + + CcspTraceInfo(("Agent log reached %ld bytes, calling logrotate...\n", st.st_size)); + + result = v_secure_system("%s %s", + LOGROTATE_BINARY, ADVSEC_AGENT_LOGROTATE_CONF); + if (result != 0) + { + CcspTraceError(("Logrotate failed with return code: %d\n", result)); + } + else + { + CcspTraceInfo(("Logrotate completed successfully\n")); + } +} + +/* Callback function for libev stat watcher */ +void agent_log_stat_cb(EV_P_ ev_stat *w, int revents) +{ + (void)loop; + (void)revents; + + if (w->attr.st_nlink) + { + rotate_agent_log(); + } +} + +/* Thread function to run libev event loop for log monitoring */ +void* agent_log_monitor_thread(void* arg) +{ + (void)arg; + + struct ev_loop *loop = NULL; + static ev_stat stat_watcher; + + CcspTraceDebug(("Starting agent log monitor thread\n")); + + loop = ev_loop_new(0); + if (!loop) + { + CcspTraceError(("Failed to create libev event loop\n")); + return NULL; + } + + ev_stat_init(&stat_watcher, agent_log_stat_cb, ADVSEC_AGENT_LOG_FILE, ADVSEC_AGENT_LOG_INTERVAL); + + ev_stat_start(loop, &stat_watcher); + + CcspTraceDebug(("Agent log monitoring started on %s\n", ADVSEC_AGENT_LOG_FILE)); + + ev_run(loop, 0); + ev_loop_destroy(loop); + return NULL; +} + +static void advsec_start_log_monitor_thread(void) +{ + int err; + pthread_t log_monitor_tid; + + err = pthread_create(&log_monitor_tid, NULL, agent_log_monitor_thread, NULL); + if (err != 0) + { + CcspTraceError(("%s: Failed to create agent log monitor thread\n", __FUNCTION__)); + } + else + { + pthread_detach(log_monitor_tid); + CcspTraceDebug(("%s: Agent log monitor thread created successfully\n", __FUNCTION__)); + } +} + ANSC_STATUS CosaAdvSecStartFeatures(advsec_feature_type type) { ANSC_STATUS returnStatus = ANSC_STATUS_SUCCESS; diff --git a/source/test/CcspAdvSecurityDmlTest/Makefile.am b/source/test/CcspAdvSecurityDmlTest/Makefile.am index 2d61c7b..111e72d 100644 --- a/source/test/CcspAdvSecurityDmlTest/Makefile.am +++ b/source/test/CcspAdvSecurityDmlTest/Makefile.am @@ -46,7 +46,7 @@ CcspAdvSecurityDmlTest_gtest_bin_SOURCES = CcspAdvSecurityMock.cpp \ ${top_builddir}/source/AdvSecurityDml/plugin_main.c \ gtest_main.cpp -CcspAdvSecurityDmlTest_gtest_bin_LDFLAGS = -lgtest -lgmock -lgcov -pthread +CcspAdvSecurityDmlTest_gtest_bin_LDFLAGS = -lgtest -lgmock -lgcov -pthread -lev CcspAdvSecurityDmlTest_gtest_bin_LDADD = \ $(HOME)/usr/local/lib/libmock_syscfg.la \