]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
log.c:
authorDamir Tomic <viperus@ntp.org>
Thu, 6 Aug 2015 18:24:05 +0000 (20:24 +0200)
committerDamir Tomic <viperus@ntp.org>
Thu, 6 Aug 2015 18:24:05 +0000 (20:24 +0200)
  modified to allow calling of multiple atexit(cleanup_log) without causing a segfault.
t-log.c:
  minor cleanup, fixes, stuff works (in combination with updated log.c)

bk: 55c3a645HQAwncPigo2_2eZGpg5N7g

sntp/log.c
sntp/tests/t-log.c

index d27e885e25863dfd3c084db236efcfab88cc6d99..db6614d027109fcf1e7cf0833f896633985dc253 100644 (file)
@@ -2,7 +2,9 @@
 
 #include "log.h"
 
-char const *progname;          /* for msyslog use too */
+const char *progname;          /* for msyslog use too */
+
+static int counter = 0;
 
 static void cleanup_log(void);
 
@@ -11,6 +13,7 @@ sntp_init_logging(
        const char *prog
        )
 {
+       
        msyslog_term = TRUE;
        init_logging(prog, 0, FALSE);
        msyslog_term_pid = FALSE;
@@ -24,15 +27,21 @@ open_logfile(
        )
 {
        change_logfile(logfile, FALSE);
+       counter = 1; //counter++;
        atexit(cleanup_log);
 }
 
-
+//not sure about this. Are the atexit() functions called by FIFO or LIFO order? The end result is PROBABLY the same
 static void
 cleanup_log(void)
 {
-       syslogit = TRUE;
-       fflush(syslog_file);
-       fclose(syslog_file);
-       syslog_file = NULL;
+       //counter--;
+       //if(counter <= 0){
+       if(counter == 1){
+               syslogit = TRUE;
+               fflush(syslog_file);
+               fclose(syslog_file);
+               syslog_file = NULL;
+               counter = 0;
+       }
 }
index cc43735341304f6b032b9d02e5d9a1f476686124..154658474e56e8e68250f632f8c447c6e745db22 100644 (file)
@@ -2,12 +2,12 @@
 #include "unity.h"
 #include "ntp_types.h"
 
-//#include "sntptest.h"
-//#include "crypto.h"
-#include "log.h"
+
+//#include "log.h"
+#include "log.c"
 
 void testChangePrognameInMysyslog(void);
-//void testOpenLogfileTest(void);
+void testOpenLogfileTest(void);
 
 
 //in var/log/syslog (may differ depending on your OS), logged name of the program will be "TEST_PROGNAME".
@@ -18,18 +18,19 @@ void testChangePrognameInMysyslog(void){
 }
 
 //writes log files in your own file instead of syslog! (MAY BE USEFUL TO SUPPRESS ERROR MESSAGES!)
-/*
+
 void testOpenLogfileTest(void){
        sntp_init_logging("TEST_PROGNAME2"); //this name is consistent through the entire program unless changed
        open_logfile("testLogfile.log"); 
        //open_logfile("/var/log/syslog"); //this gives me "Permission Denied" when i do %m
        
        msyslog(LOG_ERR, "Cannot open log file %s","abcXX");
-       //msyslog(LOG_INFO, "%s", "eee");
+       //cleanup_log(); //unnecessary  after log.c fix!
+       
 }
-*/
 
-//multiple cleanup_log() causes segfault. Probably the reason it's static. Opening multiple open_logfile(name) will cause segfault x.x I'm guessing it's not intended to be changed. Cleanup after unity test doesn't fix it, looks like. Tried using counter's to only call atexit once, didn't help???
+
+//multiple cleanup_log() causes segfault. Probably the reason it's static. Opening multiple open_logfile(name) will cause segfault x.x I'm guessing it's not intended to be changed. Cleanup after unity test doesn't fix it, looks like. Calling in tearDown() also causes issues.
 
 void testWriteInCustomLogfile(void){
        char testString[256] = "12345 ABC";
@@ -58,8 +59,9 @@ void testWriteInCustomLogfile(void){
 
        x = strstr(line,testString);
        TEST_ASSERT_TRUE( x != NULL);
-
-       //fclose(f); //using this will also cause segfault, because at the end, log.c will  call (using atexit(func) function) cleanup_log(void)-> fclose(syslog_file); After the 1st fclose, syslog_file = NULL, and is never reset
+       //cleanup_log();
+       fclose(f); //using this will also cause segfault, because at the end, log.c will  call (using atexit(func) function) cleanup_log(void)-> fclose(syslog_file); 
+       //After the 1st fclose, syslog_file = NULL, and is never reset -> hopefully fixed by editing log.c
        //TEST_ASSERT_EQUAL_STRING(testString,line); //doesn't work, line is dynamic because the process name is random.
 }