From 675d4b3b3df1cb16ffe2ed94b1600262b6f76f63 Mon Sep 17 00:00:00 2001 From: Suvendhu Hansa Date: Wed, 4 Feb 2026 19:48:16 +0530 Subject: [PATCH] Collect cpe port from netstat on failure --- bbf_plugin/datamodel.c | 44 ++++++++++++++++++++++++++++++++++++++++-- src/cwmp.c | 13 +++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/bbf_plugin/datamodel.c b/bbf_plugin/datamodel.c index fb0d5e6..bcbe5cb 100644 --- a/bbf_plugin/datamodel.c +++ b/bbf_plugin/datamodel.c @@ -515,6 +515,25 @@ static int set_management_server_periodic_inform_time(char *refparam, struct dmc return 0; } +static int read_daemon_pid(void) +{ + // cppcheck-suppress cert-MSC24-C + FILE *fp = fopen("/var/run/icwmpd.pid", "r"); + if (fp == NULL) { + // If file doesn't exist, daemon isn't running or didn't create it yet + return -1; + } + + int pid = 0; + // Read the integer from the file + if (fscanf(fp, "%d", &pid) != 1) { + pid = 0; // Could not parse an integer + } + + fclose(fp); + return pid; +} + static void get_management_ip_port(char **listen_addr) { char *ip = NULL, *port = NULL, *interface = NULL, *ip_version = NULL; @@ -525,8 +544,29 @@ static void get_management_ip_port(char **listen_addr) dmuci_get_option_value_string_varstate("icwmp", "cpe", "port", &port); dmuci_get_option_value_string_varstate("icwmp", "acs", "ip_version", &ip_version); - if (DM_STRLEN(port) == 0) - port = dmstrdup("7547"); + if (DM_STRLEN(port) == 0) { + int pid = read_daemon_pid(); + if (pid > 0) { + BBFDM_ERR("CPE port not found in /var/state, fallback to netstat for port information"); + char cmd[128] = {0}, line[16] = {0}; + + snprintf(cmd, sizeof(cmd), "netstat -nltp 2>/dev/null | grep \'%d/icwmpd\' | awk \'{print $4}\' | sed \'s/.*://\'", pid); + // Flawfinder: ignore + FILE *pp = popen(cmd, "r"); + if (pp) { + fgets(line, sizeof(line), pp); + pclose(pp); + } + + int len = DM_STRLEN(line); + if (len == 0) + BBFDM_INFO("CWMP port not found in netstat, icwmpd may be stopped"); + else if (line[len - 1] == '\n') + line[len - 1] = '\0'; // omit \n from end + + port = dmstrdup(line); + } + } if (DM_STRLEN(l3_device) == 0 && DM_STRLEN(interface) != 0) l3_device = get_l3_device(interface); diff --git a/src/cwmp.c b/src/cwmp.c index 0404c54..d9915de 100644 --- a/src/cwmp.c +++ b/src/cwmp.c @@ -182,6 +182,11 @@ static int cwmp_init(void) /* Only One instance should run*/ // cppcheck-suppress cert-MSC24-C cwmp_ctx.pid_file = fopen("/var/run/icwmpd.pid", "w+"); + if (!cwmp_ctx.pid_file) { + CWMP_LOG(ERROR, "%s", "Could not open pid file"); + exit(EXIT_FAILURE); + } + fcntl(fileno(cwmp_ctx.pid_file), F_SETFD, fcntl(fileno(cwmp_ctx.pid_file), F_GETFD) | FD_CLOEXEC); int rc = flock(fileno(cwmp_ctx.pid_file), LOCK_EX | LOCK_NB); if (rc) { @@ -194,6 +199,14 @@ static int cwmp_init(void) exit(EXIT_SUCCESS); } + /* Write the pid */ + // cppcheck-suppress cert-MSC24-C + rewind(cwmp_ctx.pid_file); + ftruncate(fileno(cwmp_ctx.pid_file), 0); + fprintf(cwmp_ctx.pid_file, "%d\n", getpid()); + fflush(cwmp_ctx.pid_file); + fsync(fileno(cwmp_ctx.pid_file)); + if (CWMP_OK != create_cwmp_temporary_files()) { return CWMP_GEN_ERR; }