@cryptotaxi247 / netdata-1 / commits / d4097f04d

added command line option -nd or -nodaemon to disable forking, #10

Costa Tsaousis (ktsaou) committed Oct 13, 2015 at 20:59 UTC d4097f04d612405884e292e627932b1733498d64
4 files changed +47 -24
src/daemon.c
+28 -20
@@ -109,7 +109,7 @@ int become_user(const char *username)
109 return(0);
110 }
111
112 -int become_daemon(int close_all_files, const char *input, const char *output, const char *error, const char *access, int *access_fd, FILE **access_fp)
112 +int become_daemon(int dont_fork, int close_all_files, const char *input, const char *output, const char *error, const char *access, int *access_fd, FILE **access_fp)
113 {
114 fflush(NULL);
115
@@ -182,31 +182,37 @@ int become_daemon(int close_all_files, const char *input, const char *output, co
182 // all files opened
183 // lets do it
184
185 - int i = fork();
186 - if(i == -1) {
187 - perror("cannot fork");
188 - exit(1);
189 - }
190 - if(i != 0) {
191 - exit(0); // the parent
192 - }
185 + if(!dont_fork) {
186 + int i = fork();
187 + if(i == -1) {
188 + perror("cannot fork");
189 + exit(1);
190 + }
191 + if(i != 0) {
192 + exit(0); // the parent
193 + }
194
194 - // become session leader
195 - if (setsid() < 0)
196 - exit(2);
195 + // become session leader
196 + if (setsid() < 0) {
197 + perror("Cannot become session leader.");
198 + exit(2);
199 + }
200 + }
201
202 signal(SIGCHLD, SIG_IGN);
203 signal(SIGHUP, SIG_IGN);
204 signal(SIGWINCH, SIG_IGN);
205
206 // fork() again
203 - i = fork();
204 - if(i == -1) {
205 - perror("cannot fork");
206 - exit(1);
207 - }
208 - if(i != 0) {
209 - exit(0); // the parent
207 + if(!dont_fork) {
208 + int i = fork();
209 + if(i == -1) {
210 + perror("cannot fork");
211 + exit(1);
212 + }
213 + if(i != 0) {
214 + exit(0); // the parent
215 + }
216 }
217
218 // Set new file permissions
@@ -214,6 +220,7 @@ int become_daemon(int close_all_files, const char *input, const char *output, co
220
221 // close all files
222 if(close_all_files) {
223 + int i;
224 for(i = sysconf(_SC_OPEN_MAX); i > 0; i--)
225 if(
226 ((access_fd && i != *access_fd) || !access_fd)
@@ -270,7 +277,8 @@ int become_daemon(int close_all_files, const char *input, const char *output, co
277 if(fd >= 0) {
278 char b[100];
279 sprintf(b, "%d\n", getpid());
273 - i = write(fd, b, strlen(b));
280 + int i = write(fd, b, strlen(b));
281 + if(i <= 0) perror("Cannot write pid to file.");
282 close(fd);
283 }
284 }
src/daemon.h
+1 -1
@@ -7,6 +7,6 @@ extern void prepare_rundir();
7
8 extern int become_user(const char *username);
9
10 -extern int become_daemon(int close_all_files, const char *input, const char *output, const char *error, const char *access, int *access_fd, FILE **access_fp);
10 +extern int become_daemon(int dont_fork, int close_all_files, const char *input, const char *output, const char *error, const char *access, int *access_fd, FILE **access_fp);
11
12 #endif /* NETDATA_DAEMON_H */
src/main.c
+4 -1
@@ -166,6 +166,7 @@ int main(int argc, char **argv)
166 {
167 int i;
168 int config_loaded = 0;
169 + int dont_fork = 0;
170
171 // set the name for logging
172 program_name = "netdata";
@@ -189,6 +190,7 @@ int main(int argc, char **argv)
190 else if(strcmp(argv[i], "-l") == 0 && (i+1) < argc) { config_set("global", "history", argv[i+1]); i++; }
191 else if(strcmp(argv[i], "-t") == 0 && (i+1) < argc) { config_set("global", "update every", argv[i+1]); i++; }
192 else if(strcmp(argv[i], "-ch") == 0 && (i+1) < argc) { config_set("global", "host access prefix", argv[i+1]); i++; }
193 + else if(strcmp(argv[i], "-nodeamon") == 0 || strcmp(argv[i], "-nd") == 0) dont_fork = 1;
194 else if(strcmp(argv[i], "--unittest") == 0) {
195 if(unit_test_storage()) exit(1);
196 exit(0);
@@ -217,6 +219,7 @@ int main(int argc, char **argv)
219 fprintf(stderr, " -p LISTEN_PORT can be from 1 to %d. Default: %d.\n", 65535, LISTEN_PORT);
220 fprintf(stderr, " -u USERNAME can be any system username to run as. Default: none.\n");
221 fprintf(stderr, " -ch path to access host /proc and /sys when running in a container. Default: empty.\n");
222 + fprintf(stderr, " -nd or -nodeamon to disable forking in the background. Default: unset.\n");
223 fprintf(stderr, " -df FLAGS debug options. Default: 0x%8llx.\n", debug_flags);
224 exit(1);
225 }
@@ -366,7 +369,7 @@ int main(int argc, char **argv)
369 if(nice(20) == -1) fprintf(stderr, "Cannot lower my CPU priority. Error: %s.\n", strerror(errno));
370
371 #ifndef NETDATA_NO_DAEMON
369 - if(become_daemon(0, input_log_file, output_log_file, error_log_file, access_log_file, &access_fd, &stdaccess) == -1) {
372 + if(become_daemon(dont_fork, 0, input_log_file, output_log_file, error_log_file, access_log_file, &access_fd, &stdaccess) == -1) {
373 fprintf(stderr, "Cannot demonize myself (%s).", strerror(errno));
374 exit(1);
375 }
web/index.js
+14 -2
@@ -20,10 +20,22 @@ var MAINCHART_CONTROL_HEIGHT = 75; // how tall the control chart will be
20 var MAINCHART_CONTROL_DIVISOR = 2; // how much detailed will the control chart be? 1 = finest, higher is faster
21 var MAINCHART_INITIAL_SELECTOR= 20; // 1/20th of the width, this overrides MAINCHART_MIN_TIME_TO_SHOW
22
23 -var CHARTS_REFRESH_LOOP = 100; // delay between chart refreshes
23 +var CHARTS_REFRESH_LOOP = 50; // delay between chart refreshes
24 var CHARTS_REFRESH_IDLE = 500; // delay between chart refreshes when no chart was ready for refresh the last time
25 var CHARTS_CHECK_NO_FOCUS = 500; // delay to check for visibility when the page has no focus
26 -var CHARTS_SCROLL_IDLE = 300; // delay to wait after a page scroll
26 +var CHARTS_SCROLL_IDLE = 100; // delay to wait after a page scroll
27 +
28 +
29 +function isIE() {
30 + userAgent = navigator.userAgent;
31 + return userAgent.indexOf("MSIE ") > -1 || userAgent.indexOf("Trident/") > -1;
32 +}
33 +
34 +if(isIE()){
35 + // do stuff with ie-users
36 + CHARTS_REFRESH_LOOP=250;
37 + CHARTS_SCROLL_IDLE=500;
38 +}
39
40 var MODE_THUMBS = 1;
41 var MODE_MAIN = 2;