From 2eac512589868eb68e4671f5e914a3718854c63c Mon Sep 17 00:00:00 2001 From: Vivek Kumar Dutta Date: Mon, 18 Sep 2023 10:00:53 +0530 Subject: [PATCH] Added number of expected micro-services --- bbfdmd/ubus/bbfdmd.c | 4 ++++ bbfdmd/ubus/common.c | 41 +++++++++++++++++++++++++++++++++++++++++ bbfdmd/ubus/common.h | 15 ++------------- 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/bbfdmd/ubus/bbfdmd.c b/bbfdmd/ubus/bbfdmd.c index 4dbf13af..df329166 100644 --- a/bbfdmd/ubus/bbfdmd.c +++ b/bbfdmd/ubus/bbfdmd.c @@ -941,12 +941,16 @@ static const struct blobmsg_policy service_policy[] = { static void service_list(struct blob_buf *bb) { void *array; + char val[32] = {0}; + char cmd[] = "grep -hsw bbfdm_add_service /etc/init.d/*|wc -l"; array = blobmsg_open_array(bb, "supported_cmd"); blobmsg_add_string(bb, NULL, "register"); blobmsg_add_string(bb, NULL, "list"); blobmsg_close_array(bb, array); + run_cmd(cmd, val, 32); + blobmsg_add_u32(bb, "expected_services_num", atoi(val)); array = blobmsg_open_array(bb, "registered_service"); get_list_of_registered_service(&head_registered_service, bb); blobmsg_close_array(bb, array); diff --git a/bbfdmd/ubus/common.c b/bbfdmd/ubus/common.c index 531c9ed7..c0562cea 100644 --- a/bbfdmd/ubus/common.c +++ b/bbfdmd/ubus/common.c @@ -208,3 +208,44 @@ int get_instance_mode(int instance_mode) return instance_mode; } + + +int run_cmd(const char *cmd, char *output, size_t out_len) +{ + int ret = -1; + FILE *pp; + + if (cmd == NULL) + return 0; + + if (output == NULL || out_len == 0) { + return ret; + } + + memset(output, 0, out_len); + + pp = popen(cmd, "r"); + if (pp != NULL) { + char line[512] = {0}; + + fgets(line, sizeof(line), pp); + strncpyt(output, line, out_len); + pclose(pp); + ret = 0; + } + + return ret; +} + +// glibc doesn't guarantee a 0 termianted string on strncpy +// strncpy with always 0 terminated string +void strncpyt(char *dst, const char *src, size_t n) +{ + if (dst == NULL || src == NULL) + return; + + if (n > 1) { + strncpy(dst, src, n - 1); + dst[n - 1] = 0; + } +} diff --git a/bbfdmd/ubus/common.h b/bbfdmd/ubus/common.h index e2f10d3c..8177ecc6 100644 --- a/bbfdmd/ubus/common.h +++ b/bbfdmd/ubus/common.h @@ -74,18 +74,7 @@ int get_instance_mode(int instance_mode); print_warning("[%s:%d] " fmt, __func__, __LINE__, ##args) int get_resolved_paths(struct dmctx *bbf_ctx, char *qpath, struct list_head *resolved_paths); - -// glibc doesn't guarantee a 0 termianted string on strncpy -// strncpy with always 0 terminated string -static inline void strncpyt(char *dst, const char *src, size_t n) -{ - if (dst == NULL || src == NULL) - return; - - if (n > 1) { - strncpy(dst, src, n - 1); - dst[n - 1] = 0; - } -} +int run_cmd(const char *cmd, char *output, size_t out_len); +void strncpyt(char *dst, const char *src, size_t n); #endif /* COMMON_H */