From 0a40358f9e1fb1145d0e46c59ab8bfe2eccc072d Mon Sep 17 00:00:00 2001 From: Steve Schnepp Date: Mon, 16 Apr 2018 19:48:06 +0200 Subject: [PATCH] p/*1sec-c: improve error handling --- plugins/cpu/multicpu1sec-c.c | 10 +++++++--- plugins/network/if1sec-c.c | 11 ++++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/plugins/cpu/multicpu1sec-c.c b/plugins/cpu/multicpu1sec-c.c index 42aaffd4..c1f72574 100644 --- a/plugins/cpu/multicpu1sec-c.c +++ b/plugins/cpu/multicpu1sec-c.c @@ -95,7 +95,11 @@ int acquire() { /* fork ourselves if not asked otherwise */ char* no_fork = getenv("no_fork"); if (! no_fork || strcmp("1", no_fork)) { - if (fork()) return 0; + pid_t child_pid = fork(); + if (child_pid) { + printf("# acquire() launched as PID %d\n", child_pid); + return 0; + } // we are the child, complete the daemonization /* Close standard IO */ @@ -118,7 +122,7 @@ int acquire() { /* open the spoolfile */ FILE* cache_file = fopen(cache_filename, "a"); if (!cache_file) { - return fail("cannot create cache_file"); + return acquire(); } int cache_file_fd = fileno(cache_file); @@ -176,7 +180,7 @@ int acquire() { int fetch() { FILE* cache_file = fopen(cache_filename, "r+"); if (!cache_file) { - return fail("cannot read cache_file"); + return acquire(); } /* lock */ diff --git a/plugins/network/if1sec-c.c b/plugins/network/if1sec-c.c index b7ec5bfc..4786d956 100644 --- a/plugins/network/if1sec-c.c +++ b/plugins/network/if1sec-c.c @@ -120,7 +120,12 @@ int acquire() { /* fork ourselves if not asked otherwise */ char* no_fork = getenv("no_fork"); if (! no_fork || strcmp("1", no_fork)) { - if (fork()) return 0; + pid_t child_pid = fork(); + if (child_pid) { + printf("# acquire() launched as PID %d\n", child_pid); + return 0; + } + // we are the child, complete the daemonization /* Close standard IO */ @@ -146,7 +151,7 @@ int acquire() { /* open the spoolfile */ int cache_file = open(cache_filename, O_CREAT | O_APPEND | O_WRONLY, S_IRUSR | S_IWUSR); if ( cache_file == -1 ) { - return fail("cannot open the spoolfile"); + return fail("# cannot open cache_file"); } /* loop each second */ @@ -220,7 +225,7 @@ int acquire() { int fetch() { FILE* cache_file = fopen(cache_filename, "r+"); if ( !cache_file ) { - return fail("cannot open cache file"); + return acquire(); } /* lock */