Hi Developers,
I'm currently working on an NMS agent that integrates with Ubus to fetch data from an OpenWrt router. The architecture of my program involves a scheduler thread that spawns worker threads to collect the necessary data. However, I've encountered a problem where the worker thread responsible for executing collect_interfacedata
, which initiates a Ubus socket connection and invokes Ubus calls, does not terminate as expected. Additionally, sending SIGINT
to the process seems to have no effect.
Below is the code snippet for the module in question:
#include <libubus.h>
#include <libubox/blobmsg_json.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <cjson/cJSON.h>
static struct ubus_context *ctx;
static char *json_string = NULL; // Global variable to store JSON string
static void receive_data(struct ubus_request *req, int type, struct blob_attr *msg) {
if (msg) {
free(json_string); // Free previous string if any
json_string = blobmsg_format_json_indent(msg, true, 0);
if (!json_string) {
fprintf(stderr, "Failed to format JSON\n");
}
} else {
fprintf(stderr, "No message received in callback\n");
}
}
cJSON* collect_interfacedata() {
const char *ubus_socket = NULL;
uint32_t id;
int ret;
ctx = ubus_connect(ubus_socket);
if (!ctx) {
fprintf(stderr, "Failed to connect to ubus\n");
return NULL;
}
ret = ubus_lookup_id(ctx, "network.device", &id);
if (ret) {
fprintf(stderr, "Failed to lookup network device object: %s\n", ubus_strerror(ret));
ubus_free(ctx);
return NULL;
}
struct blob_buf b;
memset(&b, 0, sizeof(b));
printf("Connecting to ubus...4\n");
blob_buf_init(&b, 0);
struct ubus_request req;
memset(&req, 0, sizeof(req));
ret = ubus_invoke(ctx, id, "status", b.head, receive_data, NULL, 3000);
if (ret) {
fprintf(stderr, "Failed to invoke network device method: %s\n", ubus_strerror(ret));
blob_buf_free(&b);
ubus_free(ctx);
return NULL;
}
// Parse the response
cJSON *json_data = NULL;
if (json_string) {
json_data = cJSON_Parse(json_string);
if (!json_data) {
fprintf(stderr, "Failed to parse JSON\n");
free(json_string);
json_string = NULL;
ubus_free(ctx);
blob_buf_free(&b);
return NULL;
}
free(json_string);
json_string = NULL;
} else {
fprintf(stderr, "No JSON data received\n");
ubus_free(ctx);
blob_buf_free(&b);
return NULL;
}
// Create new JSON object with "interfaces" key
cJSON *root = cJSON_CreateObject();
if (!root) {
fprintf(stderr, "Failed to create JSON object\n");
cJSON_Delete(json_data);
ubus_free(ctx);
blob_buf_free(&b);
return NULL;
}
cJSON_AddItemToObject(root, "interfaces", json_data);
ubus_free(ctx);
blob_buf_free(&b);
return root; // Caller is responsible for deleting this object
}
I'm seeking advice on how to troubleshoot and resolve this issue where the worker thread does not terminate after performing its task. Also, why might the SIGINT
signal not be effectively stopping the process as expected?
Any insights or suggestions would be greatly appreciated.