better handling of startup / cleanup
Costa Tsaousis (ktsaou) committed
May 20, 2014 at 03:31 UTC
44da997ac356e7224cc46dc36dc6b778b6aa8c86
3 files changed
+98
-46
Makefile
+16
-12
@@ -11,28 +11,32 @@ else
11
CFLAGS = $(COMMON_FLAGS) -Wall -Wextra -O3
12
endif
13
14
-all: setuid
15
-
16
-bin: netdata plugins
17
-
18
-setuid: bin
14
+all: netdata plugins
15
@echo
20
- @echo " >>> apps.plugin requires root access to access files in /proc"
21
- @echo " >>> Please authorize it!"
16
+ @echo "Compilation Done!"
17
+ @echo "You can 'make getconf' to get the config file conf.d/netdata.conf from the netdata server."
18
@echo
23
- sudo chown root plugins.d/apps.plugin
24
- sudo chmod 4775 plugins.d/apps.plugin
19
+
20
+plugins: plugins.d/apps.plugin
21
22
netdata: netdata.c
23
+ @echo
24
+ @echo "Compiling netdata server..."
25
$(CC) $(CFLAGS) -o netdata netdata.c -lpthread -lz
26
29
-plugins: plugins.d/apps.plugin
30
-
27
plugins.d/apps.plugin: apps_plugin.c
28
+ @echo
29
+ @echo "Compiling apps.plugin..."
30
$(CC) $(CFLAGS) -o plugins.d/apps.plugin apps_plugin.c
31
+ @echo
32
+ @echo " >>> apps.plugin requires root access to access files in /proc"
33
+ @echo " >>> Please authorize it!"
34
+ @echo
35
+ -sudo chown root plugins.d/apps.plugin
36
+ -sudo chmod 4775 plugins.d/apps.plugin
37
38
clean:
35
- rm -f *.o netdata plugins.d/apps.plugin core
39
+ -rm -f *.o netdata plugins.d/apps.plugin core
40
41
getconf:
42
wget -O conf.d/netdata.conf.new "http://localhost:19999/netdata.conf"
netdata.c
+68
-32
@@ -93,6 +93,7 @@
93
#define D_CONFIG 0x00000400
94
#define D_PLUGINSD 0x00000800
95
#define D_CHILDS 0x00001000
96
+#define D_EXIT 0x00002000
97
98
#define CT_APPLICATION_JSON 1
99
#define CT_TEXT_PLAIN 2
@@ -1464,19 +1465,15 @@ void rrd_stats_dimension_free(RRD_DIMENSION *rd)
1465
1466
void rrd_stats_free_all(void)
1467
{
1467
- pthread_rwlock_wrlock(&root_rwlock);
1468
+ info("Freeing all memory...");
1469
+
1470
RRD_STATS *st;
1471
for(st = root; st ;) {
1472
RRD_STATS *next = st->next;
1473
1472
- pthread_rwlock_wrlock(&st->rwlock);
1473
-
1474
if(st->dimensions) rrd_stats_dimension_free(st->dimensions);
1475
st->dimensions = NULL;
1476
1477
- // we leave it locked, or other threads may crash...
1478
- // pthread_rwlock_unlock(&st->rwlock);
1479
-
1477
if(st->mapped == NETDATA_MEMORY_MODE_SAVE) {
1478
debug(D_RRD_STATS, "Saving stats '%s' to '%s'.", st->name, st->cache_file);
1479
savememory(st->cache_file, st, st->memsize);
@@ -1494,7 +1491,8 @@ void rrd_stats_free_all(void)
1491
st = next;
1492
}
1493
root = NULL;
1497
- pthread_rwlock_unlock(&root_rwlock);
1494
+
1495
+ info("Memory cleanup completed...");
1496
}
1497
1498
void rrd_stats_save_all(void)
@@ -6881,59 +6879,77 @@ pthread_t *p_proc = NULL, *p_tc = NULL, *p_jitter = NULL, *p_pluginsd = NULL, *p
6879
6880
void kill_childs()
6881
{
6882
+ siginfo_t info;
6883
+
6884
+ struct web_client *w;
6885
+ for(w = web_clients; w ; w = w->next) {
6886
+ debug(D_EXIT, "Stopping web client %s", w->client_ip);
6887
+ pthread_cancel(w->thread);
6888
+ pthread_join(w->thread, NULL);
6889
+ }
6890
+
6891
if(p_proc) {
6892
+ debug(D_EXIT, "Stopping proc thread");
6893
pthread_cancel(*p_proc);
6894
pthread_join(*p_proc, NULL);
6895
p_proc = NULL;
6896
}
6897
6898
if(p_jitter) {
6899
+ debug(D_EXIT, "Stopping idlejitter thread");
6900
pthread_cancel(*p_jitter);
6901
pthread_join(*p_jitter, NULL);
6902
p_jitter = NULL;
6903
}
6904
6905
if(p_checks) {
6906
+ debug(D_EXIT, "Stopping self-checks thread");
6907
pthread_cancel(*p_checks);
6908
pthread_join(*p_checks, NULL);
6909
p_checks = NULL;
6910
}
6911
6902
- if(tc_child_pid) kill(tc_child_pid, SIGTERM);
6903
- tc_child_pid = 0;
6904
-
6912
if(p_tc) {
6913
+ if(tc_child_pid) {
6914
+ debug(D_EXIT, "Killing tc-qos-helper procees");
6915
+ kill(tc_child_pid, SIGTERM);
6916
+ waitid(tc_child_pid, 0, &info, WEXITED);
6917
+ }
6918
+ tc_child_pid = 0;
6919
+
6920
+ debug(D_EXIT, "Stopping tc plugin thread");
6921
pthread_cancel(*p_tc);
6922
pthread_join(*p_tc, NULL);
6923
p_tc = NULL;
6924
}
6925
6926
if(p_pluginsd) {
6927
+ struct plugind *cd;
6928
+ for(cd = pluginsd_root ; cd ; cd = cd->next) {
6929
+ debug(D_EXIT, "Stopping %s plugin thread", cd->id);
6930
+ pthread_cancel(cd->thread);
6931
+ pthread_join(cd->thread, NULL);
6932
+
6933
+ if(cd->pid && !cd->obsolete) {
6934
+ debug(D_EXIT, "killing %s plugin process", cd->id);
6935
+ kill(cd->pid, SIGTERM);
6936
+ waitid(cd->pid, 0, &info, WEXITED);
6937
+ }
6938
+ }
6939
+
6940
+ debug(D_EXIT, "Stopping plugin manager thread");
6941
pthread_cancel(*p_pluginsd);
6942
pthread_join(*p_pluginsd, NULL);
6943
p_pluginsd = NULL;
6944
}
6945
6917
- struct plugind *cd;
6918
- for(cd = pluginsd_root ; cd ; cd = cd->next) {
6919
- if(cd->pid && !cd->obsolete) {
6920
- kill(cd->pid, SIGTERM);
6921
- cd->pid = 0;
6922
- }
6923
-
6924
- pthread_cancel(cd->thread);
6925
- pthread_join(cd->thread, NULL);
6926
- }
6927
-
6928
- struct web_client *w;
6929
- for(w = web_clients; w ; w = w->next) {
6930
- pthread_cancel(w->thread);
6931
- pthread_join(w->thread, NULL);
6932
- }
6946
+ debug(D_EXIT, "All threads/childs stopped.");
6947
}
6948
6949
void sig_handler(int signo)
6950
{
6951
+ siginfo_t info;
6952
+
6953
switch(signo) {
6954
case SIGTERM:
6955
case SIGQUIT:
@@ -6941,21 +6957,29 @@ void sig_handler(int signo)
6957
case SIGHUP:
6958
case SIGFPE:
6959
case SIGSEGV:
6944
- error("Signaled exit (signal %d). Errno: %d (%s)", signo, errno, strerror(errno));
6945
- signal(signo, SIG_IGN);
6960
+ debug(D_EXIT, "Signaled exit (signal %d). Errno: %d (%s)", signo, errno, strerror(errno));
6961
+ signal(SIGCHLD, SIG_IGN);
6962
+ signal(SIGPIPE, SIG_IGN);
6963
+ signal(SIGTERM, SIG_IGN);
6964
+ signal(SIGQUIT, SIG_IGN);
6965
+ signal(SIGHUP, SIG_IGN);
6966
+ signal(SIGINT, SIG_IGN);
6967
kill_childs();
6968
rrd_stats_free_all();
6969
+ debug(D_EXIT, "Waiting for any child exits...");
6970
+ while(waitid(P_ALL, 0, &info, WEXITED|WNOHANG) == 0) if(!info.si_pid) break;
6971
+ //unlink("/var/run/netdata.pid");
6972
+ info("NetData exiting. Bye bye...");
6973
exit(1);
6974
break;
6975
6976
case SIGPIPE:
6952
- error("Ignoring signal %d. Errno: %d (%s)", signo, errno, strerror(errno));
6977
+ info("Ignoring signal %d. Errno: %d (%s)", signo, errno, strerror(errno));
6978
break;
6979
6980
6981
case SIGCHLD:
6957
- error("Received SIGCHLD (signal %d).", signo);
6958
- siginfo_t info;
6982
+ info("Received SIGCHLD (signal %d).", signo);
6983
while(waitid(P_ALL, 0, &info, WEXITED|WNOHANG) == 0) {
6984
if(!info.si_pid) break;
6985
switch(info.si_code) {
@@ -6991,7 +7015,7 @@ void sig_handler(int signo)
7015
break;
7016
7017
default:
6994
- error("Signal %d received. Falling back to default action for it.", signo);
7018
+ info("Signal %d received. Falling back to default action for it.", signo);
7019
signal(signo, SIG_DFL);
7020
break;
7021
}
@@ -7183,6 +7207,18 @@ int become_daemon(int close_all_files, const char *input, const char *output, co
7207
if(dev_null != STDIN_FILENO && dev_null != STDOUT_FILENO && dev_null != STDERR_FILENO)
7208
close(dev_null);
7209
7210
+/* // generate our pid file
7211
+ {
7212
+ unlink("/var/run/netdata.pid");
7213
+ int fd = open("/var/run/netdata.pid", O_RDWR | O_CREAT, 0666);
7214
+ if(fd >= 0) {
7215
+ char b[100];
7216
+ sprintf(b, "%d\n", getpid());
7217
+ write(fd, b, strlen(b));
7218
+ close(fd);
7219
+ }
7220
+ }
7221
+*/
7222
return(0);
7223
}
7224
netdata.start
+14
-2
@@ -70,8 +70,20 @@ else
70
fi
71
72
echo "Stopping a (possibly) running netdata..."
73
-killall netdata 2>/dev/null
74
-sleep 2
73
+ret=0
74
+count=0
75
+while [ $ret -eq 0 ]
76
+do
77
+ if [ $count -gt 10 ]
78
+ then
79
+ echo >&2 "Cannot stop the running netdata."
80
+ exit 1
81
+ fi
82
+ count=$((count + 1))
83
+ killall netdata 2>/dev/null
84
+ ret=$?
85
+ test $ret -eq 0 && sleep 1
86
+done
87
88
if [ "$USER" = "root" -a ! -z "$NETDATA_CONFIG_USER" -a -d "$NETDATA_CONFIG_WEB_DIR" ]
89
then