diff --git a/src/subs.c b/src/subs.c index c860f3af..e723587d 100644 --- a/src/subs.c +++ b/src/subs.c @@ -322,13 +322,15 @@ static int sub__add_context(struct mosquitto_db *db, struct mosquitto *context, { struct mosquitto__subhier *branch; int topic_index = 0; + size_t topiclen; /* Find leaf node */ while(topics && topics[topic_index] != NULL){ - HASH_FIND(hh, subhier->children, topics[topic_index], strlen(topics[topic_index]), branch); + topiclen = strlen(topics[topic_index]); + HASH_FIND(hh, subhier->children, topics[topic_index], topiclen, branch); if(!branch){ /* Not found */ - branch = sub__add_hier_entry(subhier, &subhier->children, topics[topic_index], strlen(topics[topic_index])); + branch = sub__add_hier_entry(subhier, &subhier->children, topics[topic_index], topiclen); if(!branch) return MOSQ_ERR_NOMEM; } subhier = branch; @@ -562,6 +564,7 @@ int sub__add(struct mosquitto_db *db, struct mosquitto *context, const char *sub const char *sharename = NULL; char *local_sub; char **topics; + size_t topiclen; assert(root); assert(*root); @@ -570,9 +573,10 @@ int sub__add(struct mosquitto_db *db, struct mosquitto *context, const char *sub rc = sub__topic_tokenise(sub, &local_sub, &topics, &sharename); if(rc) return rc; - HASH_FIND(hh, *root, topics[0], strlen(topics[0]), subhier); + topiclen = strlen(topics[0]); + HASH_FIND(hh, *root, topics[0], topiclen, subhier); if(!subhier){ - subhier = sub__add_hier_entry(NULL, root, topics[0], strlen(topics[0])); + subhier = sub__add_hier_entry(NULL, root, topics[0], topiclen); if(!subhier){ mosquitto__free(local_sub); mosquitto__free(topics); diff --git a/src/sys_tree.c b/src/sys_tree.c index c7d746f5..8e8cf487 100644 --- a/src/sys_tree.c +++ b/src/sys_tree.c @@ -45,14 +45,15 @@ unsigned int g_connection_count = 0; void sys_tree__init(struct mosquitto_db *db) { char buf[64]; + int len; if(db->config->sys_interval == 0){ return; } /* Set static $SYS messages */ - snprintf(buf, 64, "mosquitto version %s", VERSION); - db__messages_easy_queue(db, NULL, "$SYS/broker/version", SYS_TREE_QOS, strlen(buf), buf, 1, 0, NULL); + len = snprintf(buf, 64, "mosquitto version %s", VERSION); + db__messages_easy_queue(db, NULL, "$SYS/broker/version", SYS_TREE_QOS, len, buf, 1, 0, NULL); } static void sys_tree__update_clients(struct mosquitto_db *db, char *buf) @@ -62,6 +63,7 @@ static void sys_tree__update_clients(struct mosquitto_db *db, char *buf) static int client_max = 0; static int disconnected_count = -1; static int connected_count = -1; + int len; int count_total, count_by_sock; @@ -70,13 +72,13 @@ static void sys_tree__update_clients(struct mosquitto_db *db, char *buf) if(client_count != count_total){ client_count = count_total; - snprintf(buf, BUFLEN, "%d", client_count); - db__messages_easy_queue(db, NULL, "$SYS/broker/clients/total", SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); + len = snprintf(buf, BUFLEN, "%d", client_count); + db__messages_easy_queue(db, NULL, "$SYS/broker/clients/total", SYS_TREE_QOS, len, buf, 1, 60, NULL); if(client_count > client_max){ client_max = client_count; - snprintf(buf, BUFLEN, "%d", client_max); - db__messages_easy_queue(db, NULL, "$SYS/broker/clients/maximum", SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); + len = snprintf(buf, BUFLEN, "%d", client_max); + db__messages_easy_queue(db, NULL, "$SYS/broker/clients/maximum", SYS_TREE_QOS, len, buf, 1, 60, NULL); } } @@ -89,20 +91,20 @@ static void sys_tree__update_clients(struct mosquitto_db *db, char *buf) * not last for long, so just cap at zero and ignore. */ disconnected_count = 0; } - snprintf(buf, BUFLEN, "%d", disconnected_count); - db__messages_easy_queue(db, NULL, "$SYS/broker/clients/inactive", SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); - db__messages_easy_queue(db, NULL, "$SYS/broker/clients/disconnected", SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); + len = snprintf(buf, BUFLEN, "%d", disconnected_count); + db__messages_easy_queue(db, NULL, "$SYS/broker/clients/inactive", SYS_TREE_QOS, len, buf, 1, 60, NULL); + db__messages_easy_queue(db, NULL, "$SYS/broker/clients/disconnected", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if(connected_count != count_by_sock){ connected_count = count_by_sock; - snprintf(buf, BUFLEN, "%d", connected_count); - db__messages_easy_queue(db, NULL, "$SYS/broker/clients/active", SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); - db__messages_easy_queue(db, NULL, "$SYS/broker/clients/connected", SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); + len = snprintf(buf, BUFLEN, "%d", connected_count); + db__messages_easy_queue(db, NULL, "$SYS/broker/clients/active", SYS_TREE_QOS, len, buf, 1, 60, NULL); + db__messages_easy_queue(db, NULL, "$SYS/broker/clients/connected", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if(g_clients_expired != clients_expired){ clients_expired = g_clients_expired; - snprintf(buf, BUFLEN, "%d", clients_expired); - db__messages_easy_queue(db, NULL, "$SYS/broker/clients/expired", SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); + len = snprintf(buf, BUFLEN, "%d", clients_expired); + db__messages_easy_queue(db, NULL, "$SYS/broker/clients/expired", SYS_TREE_QOS, len, buf, 1, 60, NULL); } } @@ -112,18 +114,19 @@ static void sys_tree__update_memory(struct mosquitto_db *db, char *buf) static unsigned long current_heap = -1; static unsigned long max_heap = -1; unsigned long value_ul; + int len; value_ul = mosquitto__memory_used(); if(current_heap != value_ul){ current_heap = value_ul; - snprintf(buf, BUFLEN, "%lu", current_heap); - db__messages_easy_queue(db, NULL, "$SYS/broker/heap/current", SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); + len = snprintf(buf, BUFLEN, "%lu", current_heap); + db__messages_easy_queue(db, NULL, "$SYS/broker/heap/current", SYS_TREE_QOS, len, buf, 1, 60, NULL); } value_ul =mosquitto__max_memory_used(); if(max_heap != value_ul){ max_heap = value_ul; - snprintf(buf, BUFLEN, "%lu", max_heap); - db__messages_easy_queue(db, NULL, "$SYS/broker/heap/maximum", SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); + len = snprintf(buf, BUFLEN, "%lu", max_heap); + db__messages_easy_queue(db, NULL, "$SYS/broker/heap/maximum", SYS_TREE_QOS, len, buf, 1, 60, NULL); } } #endif @@ -131,16 +134,17 @@ static void sys_tree__update_memory(struct mosquitto_db *db, char *buf) static void calc_load(struct mosquitto_db *db, char *buf, const char *topic, bool initial, double exponent, double interval, double *current) { double new_value; + int len; if (initial) { new_value = *current; - snprintf(buf, BUFLEN, "%.2f", new_value); - db__messages_easy_queue(db, NULL, topic, SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); + len = snprintf(buf, BUFLEN, "%.2f", new_value); + db__messages_easy_queue(db, NULL, topic, SYS_TREE_QOS, len, buf, 1, 60, NULL); } else { new_value = interval + exponent*((*current) - interval); if(fabs(new_value - (*current)) >= 0.01){ - snprintf(buf, BUFLEN, "%.2f", new_value); - db__messages_easy_queue(db, NULL, topic, SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); + len = snprintf(buf, BUFLEN, "%.2f", new_value); + db__messages_easy_queue(db, NULL, topic, SYS_TREE_QOS, len, buf, 1, 60, NULL); } } (*current) = new_value; @@ -213,13 +217,14 @@ void sys_tree__update(struct mosquitto_db *db, int interval, time_t start_time) double exponent; double i_mult; + int len; now = mosquitto_time(); if(interval && now - interval > last_update){ uptime = now - start_time; - snprintf(buf, BUFLEN, "%d seconds", (int)uptime); - db__messages_easy_queue(db, NULL, "$SYS/broker/uptime", SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); + len = snprintf(buf, BUFLEN, "%d seconds", (int)uptime); + db__messages_easy_queue(db, NULL, "$SYS/broker/uptime", SYS_TREE_QOS, len, buf, 1, 60, NULL); sys_tree__update_clients(db, buf); bool initial_publish = false; @@ -287,33 +292,33 @@ void sys_tree__update(struct mosquitto_db *db, int interval, time_t start_time) if(db->msg_store_count != msg_store_count){ msg_store_count = db->msg_store_count; - snprintf(buf, BUFLEN, "%d", msg_store_count); - db__messages_easy_queue(db, NULL, "$SYS/broker/messages/stored", SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); - db__messages_easy_queue(db, NULL, "$SYS/broker/store/messages/count", SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); + len = snprintf(buf, BUFLEN, "%d", msg_store_count); + db__messages_easy_queue(db, NULL, "$SYS/broker/messages/stored", SYS_TREE_QOS, len, buf, 1, 60, NULL); + db__messages_easy_queue(db, NULL, "$SYS/broker/store/messages/count", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if (db->msg_store_bytes != msg_store_bytes){ msg_store_bytes = db->msg_store_bytes; - snprintf(buf, BUFLEN, "%lu", msg_store_bytes); - db__messages_easy_queue(db, NULL, "$SYS/broker/store/messages/bytes", SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); + len = snprintf(buf, BUFLEN, "%lu", msg_store_bytes); + db__messages_easy_queue(db, NULL, "$SYS/broker/store/messages/bytes", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if(db->subscription_count != subscription_count){ subscription_count = db->subscription_count; - snprintf(buf, BUFLEN, "%d", subscription_count); - db__messages_easy_queue(db, NULL, "$SYS/broker/subscriptions/count", SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); + len = snprintf(buf, BUFLEN, "%d", subscription_count); + db__messages_easy_queue(db, NULL, "$SYS/broker/subscriptions/count", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if(db->shared_subscription_count != shared_subscription_count){ shared_subscription_count = db->shared_subscription_count; - snprintf(buf, BUFLEN, "%d", shared_subscription_count); - db__messages_easy_queue(db, NULL, "$SYS/broker/shared_subscriptions/count", SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); + len = snprintf(buf, BUFLEN, "%d", shared_subscription_count); + db__messages_easy_queue(db, NULL, "$SYS/broker/shared_subscriptions/count", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if(db->retained_count != retained_count){ retained_count = db->retained_count; - snprintf(buf, BUFLEN, "%d", retained_count); - db__messages_easy_queue(db, NULL, "$SYS/broker/retained messages/count", SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); + len = snprintf(buf, BUFLEN, "%d", retained_count); + db__messages_easy_queue(db, NULL, "$SYS/broker/retained messages/count", SYS_TREE_QOS, len, buf, 1, 60, NULL); } #ifdef REAL_WITH_MEMORY_TRACKING @@ -322,56 +327,56 @@ void sys_tree__update(struct mosquitto_db *db, int interval, time_t start_time) if(msgs_received != g_msgs_received){ msgs_received = g_msgs_received; - snprintf(buf, BUFLEN, "%lu", msgs_received); - db__messages_easy_queue(db, NULL, "$SYS/broker/messages/received", SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); + len = snprintf(buf, BUFLEN, "%lu", msgs_received); + db__messages_easy_queue(db, NULL, "$SYS/broker/messages/received", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if(msgs_sent != g_msgs_sent){ msgs_sent = g_msgs_sent; - snprintf(buf, BUFLEN, "%lu", msgs_sent); - db__messages_easy_queue(db, NULL, "$SYS/broker/messages/sent", SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); + len = snprintf(buf, BUFLEN, "%lu", msgs_sent); + db__messages_easy_queue(db, NULL, "$SYS/broker/messages/sent", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if(publish_dropped != g_msgs_dropped){ publish_dropped = g_msgs_dropped; - snprintf(buf, BUFLEN, "%lu", publish_dropped); - db__messages_easy_queue(db, NULL, "$SYS/broker/publish/messages/dropped", SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); + len = snprintf(buf, BUFLEN, "%lu", publish_dropped); + db__messages_easy_queue(db, NULL, "$SYS/broker/publish/messages/dropped", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if(pub_msgs_received != g_pub_msgs_received){ pub_msgs_received = g_pub_msgs_received; - snprintf(buf, BUFLEN, "%lu", pub_msgs_received); - db__messages_easy_queue(db, NULL, "$SYS/broker/publish/messages/received", SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); + len = snprintf(buf, BUFLEN, "%lu", pub_msgs_received); + db__messages_easy_queue(db, NULL, "$SYS/broker/publish/messages/received", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if(pub_msgs_sent != g_pub_msgs_sent){ pub_msgs_sent = g_pub_msgs_sent; - snprintf(buf, BUFLEN, "%lu", pub_msgs_sent); - db__messages_easy_queue(db, NULL, "$SYS/broker/publish/messages/sent", SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); + len = snprintf(buf, BUFLEN, "%lu", pub_msgs_sent); + db__messages_easy_queue(db, NULL, "$SYS/broker/publish/messages/sent", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if(bytes_received != g_bytes_received){ bytes_received = g_bytes_received; - snprintf(buf, BUFLEN, "%llu", bytes_received); - db__messages_easy_queue(db, NULL, "$SYS/broker/bytes/received", SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); + len = snprintf(buf, BUFLEN, "%llu", bytes_received); + db__messages_easy_queue(db, NULL, "$SYS/broker/bytes/received", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if(bytes_sent != g_bytes_sent){ bytes_sent = g_bytes_sent; - snprintf(buf, BUFLEN, "%llu", bytes_sent); - db__messages_easy_queue(db, NULL, "$SYS/broker/bytes/sent", SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); + len = snprintf(buf, BUFLEN, "%llu", bytes_sent); + db__messages_easy_queue(db, NULL, "$SYS/broker/bytes/sent", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if(pub_bytes_received != g_pub_bytes_received){ pub_bytes_received = g_pub_bytes_received; - snprintf(buf, BUFLEN, "%llu", pub_bytes_received); - db__messages_easy_queue(db, NULL, "$SYS/broker/publish/bytes/received", SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); + len = snprintf(buf, BUFLEN, "%llu", pub_bytes_received); + db__messages_easy_queue(db, NULL, "$SYS/broker/publish/bytes/received", SYS_TREE_QOS, len, buf, 1, 60, NULL); } if(pub_bytes_sent != g_pub_bytes_sent){ pub_bytes_sent = g_pub_bytes_sent; - snprintf(buf, BUFLEN, "%llu", pub_bytes_sent); - db__messages_easy_queue(db, NULL, "$SYS/broker/publish/bytes/sent", SYS_TREE_QOS, strlen(buf), buf, 1, 60, NULL); + len = snprintf(buf, BUFLEN, "%llu", pub_bytes_sent); + db__messages_easy_queue(db, NULL, "$SYS/broker/publish/bytes/sent", SYS_TREE_QOS, len, buf, 1, 60, NULL); } last_update = mosquitto_time();