Skip to content

Commit

Permalink
remove comment_list
Browse files Browse the repository at this point in the history
it is not required anymore since we store comments in a hash table now.

Signed-off-by: Sven Nierlein <[email protected]>
  • Loading branch information
sni committed Jun 22, 2023
1 parent a2bed87 commit 12de0b0
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 144 deletions.
114 changes: 15 additions & 99 deletions src/naemon/comments.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
#include "nm_alloc.h"
#include <glib.h>

comment *comment_list = NULL;
int defer_comment_sorting = 0;
static GHashTable *comment_hashtable;
GHashTable *comment_hashtable = NULL;


/******************************************************************/
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -388,76 +348,32 @@ 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 **************************/
/******************************************************************/

/* 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;
}
Expand Down
3 changes: 1 addition & 2 deletions src/naemon/comments.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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 */

Expand Down
2 changes: 0 additions & 2 deletions src/naemon/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
46 changes: 24 additions & 22 deletions src/naemon/xrddefault.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -1685,8 +1689,6 @@ int xrddefault_read_state_information(void)

if (sort_downtime() != OK)
return ERROR;
if (sort_comments() != OK)
return ERROR;

return OK;
}
43 changes: 24 additions & 19 deletions src/naemon/xsddefault.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit 12de0b0

Please sign in to comment.