@cryptotaxi247 / netdata-1 / commits / 7b30c8440

added cleanup on exit

Costa Tsaousis (ktsaou) committed Dec 1, 2015 at 13:19 UTC 7b30c8440759e30e4ef67f138fc40951575ae946
1 file changed +40 -3
src/plugin_tc.c
+40 -3
@@ -14,6 +14,7 @@
14 #include "rrd.h"
15 #include "popen.h"
16 #include "plugin_tc.h"
17 +#include "main.h"
18
19 #define RRD_TYPE_TC "tc"
20 #define RRD_TYPE_TC_LEN strlen(RRD_TYPE_TC)
@@ -61,9 +62,14 @@ struct tc_device {
62 avl_tree classes_index;
63
64 struct tc_class *classes;
65 +
66 + struct tc_device *next;
67 + struct tc_device *prev;
68 };
69
70
71 +struct tc_device *tc_device_root = NULL;
72 +
73 // ----------------------------------------------------------------------------
74 // tc_device index
75
@@ -124,7 +130,10 @@ static void tc_class_free(struct tc_device *n, struct tc_class *c) {
130
131 if(c->next) c->next->prev = c->prev;
132 if(c->prev) c->prev->next = c->next;
127 - if(n->classes == c) n->classes = c->next;
133 + if(n->classes == c) {
134 + if(c->next) n->classes = c->next;
135 + else n->classes = c->prev;
136 + }
137
138 tc_class_index_del(n, c);
139
@@ -327,6 +336,15 @@ static struct tc_device *tc_device_create(char *id)
336 pthread_rwlock_init(&d->classes_index.rwlock, NULL);
337
338 tc_device_index_add(d);
339 +
340 + if(!tc_device_root) {
341 + tc_device_root = d;
342 + }
343 + else {
344 + d->next = tc_device_root;
345 + tc_device_root->prev = d;
346 + tc_device_root = d;
347 + }
348 }
349
350 return(d);
@@ -373,9 +391,16 @@ static struct tc_class *tc_class_add(struct tc_device *n, char *id, char *parent
391
392 return(c);
393 }
376 -/*
394 +
395 static void tc_device_free(struct tc_device *n)
396 {
397 + if(n->next) n->next->prev = n->prev;
398 + if(n->prev) n->prev->next = n->next;
399 + if(tc_device_root == n) {
400 + if(n->next) tc_device_root = n->next;
401 + else tc_device_root = n->prev;
402 + }
403 +
404 tc_device_index_del(n);
405
406 while(n->classes) tc_class_free(n, n->classes);
@@ -386,7 +411,12 @@ static void tc_device_free(struct tc_device *n)
411
412 free(n);
413 }
389 -*/
414 +
415 +static void tc_device_free_all()
416 +{
417 + while(tc_device_root)
418 + tc_device_free(tc_device_root);
419 +}
420
421 #define MAX_WORDS 20
422
@@ -485,6 +515,8 @@ void *tc_main(void *ptr)
515 }
516
517 while(fgets(buffer, TC_LINE_MAX, fp) != NULL) {
518 + if(netdata_exit) break;
519 +
520 buffer[TC_LINE_MAX] = '\0';
521 // debug(D_TC_LOOP, "TC: read '%s'", buffer);
522
@@ -628,6 +660,11 @@ void *tc_main(void *ptr)
660 class = NULL;
661 }
662
663 + if(netdata_exit) {
664 + tc_device_free_all();
665 + return NULL;
666 + }
667 +
668 sleep(rrd_update_every);
669 }
670