#include "main.h"
+int netdata_exit = 0;
+
+void netdata_cleanup_and_exit(int ret)
+{
+ kill_childs();
+ rrdset_free_all();
+ //unlink("/var/run/netdata.pid");
+ info("NetData exiting. Bye bye...");
+ exit(ret);
+}
+
struct netdata_static_thread {
char *name;
errno = 0;
if(kill(pid, 0) == -1) {
switch(errno) {
- case ESRCH:
+ case ESRCH:
error("Request to kill pid %d, but it is not running.", pid);
break;
}
else {
errno = 0;
-
- void (*old)(int);
+
+ void (*old)(int);
old = signal(sig, SIG_IGN);
if(old == SIG_ERR) {
error("Cannot overwrite signal handler for signal %d", sig);
if(ret == -1) {
switch(errno) {
- case ESRCH:
+ case ESRCH:
error("Cannot kill pid %d, but it is not running.", pid);
break;
pthread_join(w->thread, NULL);
}
- int i;
+ int i;
for (i = 0; static_threads[i].name != NULL ; i++) {
if(static_threads[i].thread) {
debug(D_EXIT, "Stopping %s thread", static_threads[i].name);
if(tc_child_pid) {
info("Killing tc-qos-helper procees");
if(killpid(tc_child_pid, SIGTERM) != -1)
- waitid(tc_child_pid, 0, &info, WEXITED);
+ waitid(P_PID, tc_child_pid, &info, WEXITED);
}
tc_child_pid = 0;
if(cd->pid && !cd->obsolete) {
debug(D_EXIT, "killing %s plugin process", cd->id);
if(killpid(cd->pid, SIGTERM) != -1)
- waitid(cd->pid, 0, &info, WEXITED);
+ waitid(P_PID, cd->pid, &info, WEXITED);
}
}
+ // if, for any reason there is any child exited
+ // catch it here
+ waitid(P_PID, 0, &info, WEXITED|WNOHANG);
+
debug(D_EXIT, "All threads/childs stopped.");
}
}
// --------------------------------------------------------------------
-
+
for (i = 0; static_threads[i].name != NULL ; i++) {
struct netdata_static_thread *st = &static_threads[i];
}
// for future use - the main thread
- while(1) sleep(60);
+ while(1) {
+ if(netdata_exit != 0) {
+ netdata_exit++;
+
+ if(netdata_exit > 5) {
+ netdata_cleanup_and_exit(0);
+ exit(0);
+ }
+ }
+ sleep(2);
+ }
exit(0);
}