From 12de0b011f2c1bb48306a2e9518b44b80c0ba2e4 Mon Sep 17 00:00:00 2001 From: Sven Nierlein Date: Mon, 20 Mar 2023 10:26:15 +0100 Subject: [PATCH] remove comment_list it is not required anymore since we store comments in a hash table now. Signed-off-by: Sven Nierlein --- src/naemon/comments.c | 114 ++++++---------------------------------- src/naemon/comments.h | 3 +- src/naemon/common.h | 2 - src/naemon/xrddefault.c | 46 ++++++++-------- src/naemon/xsddefault.c | 43 ++++++++------- 5 files changed, 64 insertions(+), 144 deletions(-) diff --git a/src/naemon/comments.c b/src/naemon/comments.c index 1231d3b6..47be837a 100644 --- a/src/naemon/comments.c +++ b/src/naemon/comments.c @@ -11,9 +11,7 @@ #include "nm_alloc.h" #include -comment *comment_list = NULL; -int defer_comment_sorting = 0; -static GHashTable *comment_hashtable; +GHashTable *comment_hashtable = NULL; /******************************************************************/ @@ -149,16 +147,6 @@ int delete_comment(int type, unsigned long comment_id) /* remove the comment from the list in memory */ g_hash_table_remove(comment_hashtable, GINT_TO_POINTER(this_comment->comment_id)); - if (comment_list == this_comment) { - comment_list = this_comment->next; - if (comment_list) - comment_list->prev = NULL; - } else { - this_comment->prev->next = this_comment->next; - if (this_comment->next) - this_comment->next->prev = this_comment->prev; - } - // remove from svc or host if (type == HOST_COMMENT) { host *temp_host = find_host(this_comment->host_name); @@ -348,34 +336,6 @@ int add_comment(int comment_type, int entry_type, char *host_name, char *svc_des g_hash_table_insert(comment_hashtable, GINT_TO_POINTER(new_comment->comment_id), new_comment); - if (defer_comment_sorting || !comment_list) { - if (comment_list) { - comment_list->prev = new_comment; - } - new_comment->next = comment_list; - new_comment->prev = NULL; - comment_list = new_comment; - } else { - /* add new comment to comment list, sorted by comment id */ - comment *cur; - for (cur = comment_list; cur; cur = cur->next) { - if (new_comment->comment_id < cur->comment_id) { - new_comment->prev = cur->prev; - if (cur->prev) - cur->prev->next = new_comment; - new_comment->next = cur; - cur->prev = new_comment; - break; - } - if (!cur->next) { - new_comment->next = NULL; - cur->next = new_comment; - new_comment->prev = cur; - break; - } - } - } - if (comment_type == HOST_COMMENT) prepend_object_to_objectlist(&temp_host->comments_list, (void *)new_comment); @@ -388,51 +348,6 @@ int add_comment(int comment_type, int entry_type, char *host_name, char *svc_des } -static int comment_compar(const void *p1, const void *p2) -{ - comment *c1 = *(comment **)p1; - comment *c2 = *(comment **)p2; - return c1->comment_id - c2->comment_id; -} - - -int sort_comments(void) -{ - comment **array, *temp_comment; - unsigned long i = 0, unsorted_comments = 0; - - if (!defer_comment_sorting) - return OK; - defer_comment_sorting = 0; - - temp_comment = comment_list; - while (temp_comment != NULL) { - temp_comment = temp_comment->next; - unsorted_comments++; - } - - if (!unsorted_comments) - return OK; - - array = nm_malloc(sizeof(*array) * unsorted_comments); - while (comment_list) { - array[i++] = comment_list; - comment_list = comment_list->next; - } - - qsort((void *)array, i, sizeof(*array), comment_compar); - comment_list = temp_comment = array[0]; - for (i = 1; i < unsorted_comments; i++) { - temp_comment->next = array[i]; - temp_comment = temp_comment->next; - temp_comment->prev = array[i - 1]; - } - temp_comment->next = NULL; - nm_free(array); - return OK; -} - - /******************************************************************/ /********************* CLEANUP FUNCTIONS **************************/ /******************************************************************/ @@ -440,24 +355,25 @@ int sort_comments(void) /* frees memory allocated for the comment data */ void free_comment_data(void) { - comment *this_comment = NULL; - comment *next_comment = NULL; + GHashTableIter iter; + gpointer comment_; - if(comment_hashtable != NULL) - g_hash_table_destroy(comment_hashtable); - comment_hashtable = NULL; + if(comment_hashtable == NULL) + return; /* free memory for the comment list */ - for (this_comment = comment_list; this_comment != NULL; this_comment = next_comment) { - next_comment = this_comment->next; - nm_free(this_comment->host_name); - nm_free(this_comment->service_description); - nm_free(this_comment->author); - nm_free(this_comment->comment_data); - nm_free(this_comment); + g_hash_table_iter_init(&iter, comment_hashtable); + while (g_hash_table_iter_next(&iter, NULL, &comment_)) { + comment *temp_comment = comment_; + nm_free(temp_comment->host_name); + nm_free(temp_comment->service_description); + nm_free(temp_comment->author); + nm_free(temp_comment->comment_data); + nm_free(temp_comment); } - comment_list = NULL; + g_hash_table_destroy(comment_hashtable); + comment_hashtable = NULL; return; } diff --git a/src/naemon/comments.h b/src/naemon/comments.h index 7850c079..0e3bffb3 100644 --- a/src/naemon/comments.h +++ b/src/naemon/comments.h @@ -48,7 +48,7 @@ typedef struct comment { struct comment *next; } comment; -extern struct comment *comment_list; +extern GHashTable *comment_hashtable; int initialize_comment_data(void); /* initializes comment data */ int add_new_comment(int, int, char *, char *, time_t, char *, char *, int, int, int, time_t, unsigned long *); /* adds a new host or service comment */ @@ -71,7 +71,6 @@ int number_of_service_comments(char *, char *); /* int number_of_comments(void); int add_comment(int, int, char *, char *, time_t, char *, char *, unsigned long, int, int, time_t, int); /* adds a comment (host or service) */ -int sort_comments(void); int add_host_comment(int, char *, time_t, char *, char *, unsigned long, int, int, time_t, int); /* adds a host comment */ int add_service_comment(int, char *, char *, time_t, char *, char *, unsigned long, int, int, time_t, int); /* adds a service comment */ diff --git a/src/naemon/common.h b/src/naemon/common.h index 21e5bdad..26e8ed07 100644 --- a/src/naemon/common.h +++ b/src/naemon/common.h @@ -19,8 +19,6 @@ extern char illegal_output_char_map[256]; extern int log_rotation_method; extern int check_external_commands; -/* set this if you're going to add a ton of comments at once */ -extern int defer_comment_sorting; extern unsigned long next_downtime_id; extern char *object_cache_file; diff --git a/src/naemon/xrddefault.c b/src/naemon/xrddefault.c index 4f80980a..b2893d6f 100644 --- a/src/naemon/xrddefault.c +++ b/src/naemon/xrddefault.c @@ -67,6 +67,8 @@ int xrddefault_save_state_information(void) service *temp_service = NULL; contact *temp_contact = NULL; comment *temp_comment = NULL; + GHashTableIter iter; + gpointer comment_; scheduled_downtime *temp_downtime = NULL; int x = 0; int fd = 0; @@ -378,25 +380,28 @@ int xrddefault_save_state_information(void) } /* save all comments */ - for (temp_comment = comment_list; temp_comment != NULL; temp_comment = temp_comment->next) { - - if (temp_comment->comment_type == HOST_COMMENT) - fprintf(fp, "hostcomment {\n"); - else - fprintf(fp, "servicecomment {\n"); - fprintf(fp, "host_name=%s\n", temp_comment->host_name); - if (temp_comment->comment_type == SERVICE_COMMENT) - fprintf(fp, "service_description=%s\n", temp_comment->service_description); - fprintf(fp, "entry_type=%d\n", temp_comment->entry_type); - fprintf(fp, "comment_id=%lu\n", temp_comment->comment_id); - fprintf(fp, "source=%d\n", temp_comment->source); - fprintf(fp, "persistent=%d\n", temp_comment->persistent); - fprintf(fp, "entry_time=%lu\n", temp_comment->entry_time); - fprintf(fp, "expires=%d\n", temp_comment->expires); - fprintf(fp, "expire_time=%lu\n", temp_comment->expire_time); - fprintf(fp, "author=%s\n", temp_comment->author); - fprintf(fp, "comment_data=%s\n", temp_comment->comment_data); - fprintf(fp, "}\n"); + if(comment_hashtable != NULL) { + g_hash_table_iter_init(&iter, comment_hashtable); + while (g_hash_table_iter_next(&iter, NULL, &comment_)) { + temp_comment = comment_; + if (temp_comment->comment_type == HOST_COMMENT) + fprintf(fp, "hostcomment {\n"); + else + fprintf(fp, "servicecomment {\n"); + fprintf(fp, "host_name=%s\n", temp_comment->host_name); + if (temp_comment->comment_type == SERVICE_COMMENT) + fprintf(fp, "service_description=%s\n", temp_comment->service_description); + fprintf(fp, "entry_type=%d\n", temp_comment->entry_type); + fprintf(fp, "comment_id=%lu\n", temp_comment->comment_id); + fprintf(fp, "source=%d\n", temp_comment->source); + fprintf(fp, "persistent=%d\n", temp_comment->persistent); + fprintf(fp, "entry_time=%lu\n", temp_comment->entry_time); + fprintf(fp, "expires=%d\n", temp_comment->expires); + fprintf(fp, "expire_time=%lu\n", temp_comment->expire_time); + fprintf(fp, "author=%s\n", temp_comment->author); + fprintf(fp, "comment_data=%s\n", temp_comment->comment_data); + fprintf(fp, "}\n"); + } } /* save all downtime */ @@ -549,7 +554,6 @@ int xrddefault_read_state_information(void) /* Big speedup when reading retention.dat in bulk */ defer_downtime_sorting = 1; - defer_comment_sorting = 1; /* read all lines in the retention file */ while (1) { @@ -1685,8 +1689,6 @@ int xrddefault_read_state_information(void) if (sort_downtime() != OK) return ERROR; - if (sort_comments() != OK) - return ERROR; return OK; } diff --git a/src/naemon/xsddefault.c b/src/naemon/xsddefault.c index 35e8b997..a0c26e7e 100644 --- a/src/naemon/xsddefault.c +++ b/src/naemon/xsddefault.c @@ -76,6 +76,8 @@ int xsddefault_save_status_data(void) service *temp_service = NULL; contact *temp_contact = NULL; comment *temp_comment = NULL; + GHashTableIter iter; + gpointer comment_; scheduled_downtime *temp_downtime = NULL; time_t current_time; int fd = 0; @@ -335,25 +337,28 @@ int xsddefault_save_status_data(void) } /* save all comments */ - for (temp_comment = comment_list; temp_comment != NULL; temp_comment = temp_comment->next) { - - if (temp_comment->comment_type == HOST_COMMENT) - fprintf(fp, "hostcomment {\n"); - else - fprintf(fp, "servicecomment {\n"); - fprintf(fp, "\thost_name=%s\n", temp_comment->host_name); - if (temp_comment->comment_type == SERVICE_COMMENT) - fprintf(fp, "\tservice_description=%s\n", temp_comment->service_description); - fprintf(fp, "\tentry_type=%d\n", temp_comment->entry_type); - fprintf(fp, "\tcomment_id=%lu\n", temp_comment->comment_id); - fprintf(fp, "\tsource=%d\n", temp_comment->source); - fprintf(fp, "\tpersistent=%d\n", temp_comment->persistent); - fprintf(fp, "\tentry_time=%lu\n", temp_comment->entry_time); - fprintf(fp, "\texpires=%d\n", temp_comment->expires); - fprintf(fp, "\texpire_time=%lu\n", temp_comment->expire_time); - fprintf(fp, "\tauthor=%s\n", temp_comment->author); - fprintf(fp, "\tcomment_data=%s\n", temp_comment->comment_data); - fprintf(fp, "\t}\n\n"); + if(comment_hashtable != NULL) { + g_hash_table_iter_init(&iter, comment_hashtable); + while (g_hash_table_iter_next(&iter, NULL, &comment_)) { + temp_comment = comment_; + if (temp_comment->comment_type == HOST_COMMENT) + fprintf(fp, "hostcomment {\n"); + else + fprintf(fp, "servicecomment {\n"); + fprintf(fp, "\thost_name=%s\n", temp_comment->host_name); + if (temp_comment->comment_type == SERVICE_COMMENT) + fprintf(fp, "\tservice_description=%s\n", temp_comment->service_description); + fprintf(fp, "\tentry_type=%d\n", temp_comment->entry_type); + fprintf(fp, "\tcomment_id=%lu\n", temp_comment->comment_id); + fprintf(fp, "\tsource=%d\n", temp_comment->source); + fprintf(fp, "\tpersistent=%d\n", temp_comment->persistent); + fprintf(fp, "\tentry_time=%lu\n", temp_comment->entry_time); + fprintf(fp, "\texpires=%d\n", temp_comment->expires); + fprintf(fp, "\texpire_time=%lu\n", temp_comment->expire_time); + fprintf(fp, "\tauthor=%s\n", temp_comment->author); + fprintf(fp, "\tcomment_data=%s\n", temp_comment->comment_data); + fprintf(fp, "\t}\n\n"); + } } /* save all downtime */