osaf/services/saf/logsv/lgs/lgs_file.c    |  103 ++++++++++++++++++++++-
 osaf/services/saf/logsv/lgs/lgs_file.h    |    2 +
 osaf/services/saf/logsv/lgs/lgs_filehdl.c |  132 ++++++++++++++++++++++++++---
 osaf/services/saf/logsv/lgs/lgs_filehdl.h |    2 +-
 osaf/services/saf/logsv/lgs/lgs_stream.c  |   69 +++++++-------
 tests/logsv/tet_LogOiOps.c                |   21 ++++-
 6 files changed, 271 insertions(+), 58 deletions(-)


Fix log service fault creation of .nfs file in the log directories.
* Always do fdatasync() before closing a file.
* Always invalidate the stream file descriptor on close also if failed
* Save file descriptors if file thread is busy on close. Close files
  when available again.
* If file open time out but file is opened the fd is not used and must be
  invalidated (closed)
* Correct mutex unlock/lock around file I/O
* Create a new filename for current log file even if removal of oldest
  file fail

diff --git a/osaf/services/saf/logsv/lgs/lgs_file.c 
b/osaf/services/saf/logsv/lgs/lgs_file.c
--- a/osaf/services/saf/logsv/lgs/lgs_file.c
+++ b/osaf/services/saf/logsv/lgs/lgs_file.c
@@ -130,14 +130,17 @@ static void *file_hndl_thread(void *nopa
                         * The handler is supposed to know the format of it's 
own in and
                         * out data. The data is given to the corresponding API 
that also is
                         * assumed to know the data format.
+                        * Note:
+                        * Mutex is unlocked inside the _hdl functions while 
calling
+                        * file I/O functions. Mutex is locked when _hdl 
function returns.
                         */
-                       osaf_mutex_unlock_ordie(&lgs_ftcom_mutex); /* UNLOCK */
-               
+
                        /* Invoke requested handler function */
                        switch (lgs_com_data.request_code)      {
                        case LGSF_FILEOPEN:
                                hndl_rc = fileopen_hdl(lgs_com_data.indata_ptr, 
-                                               lgs_com_data.outdata_ptr, 
lgs_com_data.outdata_size);
+                                               lgs_com_data.outdata_ptr, 
lgs_com_data.outdata_size,
+                                               &lgs_com_data.timeout_f);
                                break;
                        case LGSF_FILECLOSE:
                                hndl_rc = fileclose_hdl(lgs_com_data.indata_ptr,
@@ -179,8 +182,6 @@ static void *file_hndl_thread(void *nopa
                                break;
                        }
                        
-                       osaf_mutex_lock_ordie(&lgs_ftcom_mutex); /* LOCK */
-
                        /* Handle answer flag and return data
                         * Note: This must be done after handler is done 
(handler may hang)
                         */
@@ -264,6 +265,76 @@ uint32_t lgs_file_init(void)
        return rc;
 }
 
+/* 
+ * List for saving file descriptors.
+ * A file descriptor is added to the list if a close request fails because
+ * the file thread is busy. If the thread is busy the close function is never
+ * called. A new attempt to close is made the next time the file API is used.
+ */
+
+typedef struct fd_list {
+       struct fd_list *fd_next_p;
+       int32_t fd;
+} fd_list_t;
+
+static fd_list_t *fd_first_p = NULL;
+static fd_list_t *fd_last_p = NULL;
+
+/**
+ * Add stream file descriptor to list
+ * @param fd
+ */
+void lgs_fd_list_add(int32_t fd)
+{
+       fd_list_t *fd_new_p;
+       
+       TRACE_ENTER2("fd = %d", fd);
+       
+       fd_new_p = (fd_list_t *) malloc(sizeof(fd_list_t));
+       osafassert(fd_new_p);
+       
+       if (fd_first_p == NULL) {
+               /* First in list */
+               fd_first_p = fd_new_p;
+               fd_last_p = fd_new_p;
+       } else {
+               fd_last_p->fd_next_p = fd_new_p;
+               fd_last_p = fd_new_p;
+       }
+       
+       fd_new_p->fd = fd;
+       fd_new_p->fd_next_p = NULL;
+       
+       TRACE_LEAVE();
+}
+
+/**
+ * Get and remove file descriptor from list 
+ * @return fd If list empty return -1
+ */
+int32_t lgs_fd_list_get(void)
+{
+       int32_t r_fp;
+       fd_list_t *fd_rem_p;
+       
+       if (fd_first_p == NULL) {
+               /* List empty */
+               return -1;
+       }
+       
+       r_fp = fd_first_p->fd; /* fd to return */
+       fd_rem_p = fd_first_p;
+       fd_first_p = fd_rem_p->fd_next_p;
+       if (fd_first_p == NULL) {
+               /* List is empty */
+               fd_last_p = NULL;
+       }
+
+       free(fd_rem_p);
+
+       return r_fp;
+}
+
 /**
  * Generic file API handler
  * Handles everything that is generic thread handling for the APIs
@@ -276,6 +347,10 @@ lgsf_retcode_t log_file_api(lgsf_apipar_
        lgsf_retcode_t api_rc = LGSF_SUCESS;
        int rc = 0;
        struct timespec timeout_time;
+       int fd = 0;
+       
+       /* If this flag is true always return LGSF_BUSY */
+       bool busy_close_flag = false;
        
        osaf_mutex_lock_ordie(&lgs_ftcom_mutex); /* LOCK */
        
@@ -288,6 +363,20 @@ lgsf_retcode_t log_file_api(lgsf_apipar_
                goto api_exit;
        }
        
+       /* Close files in fd list if any
+        * The original request is overridden. Always return busy
+        */
+       if ((fd = lgs_fd_list_get()) != -1) {
+               TRACE("Closing files in fd list. fd = %d, replaced req code = 
%d", 
+                               fd, apipar_in->req_code_in);
+               apipar_in->req_code_in = LGSF_FILECLOSE;
+               apipar_in->data_in_size = sizeof(int);
+               apipar_in->data_in = (void*) &fd;
+               apipar_in->data_out_size = 0;
+               apipar_in->data_out = NULL;
+               busy_close_flag = true;
+       }
+       
        /* Free request data before allocating new memeory */
        if (lgs_com_data.indata_ptr != NULL) {
                free(lgs_com_data.indata_ptr);
@@ -366,6 +455,10 @@ api_timeout:
 api_exit:
        osaf_mutex_unlock_ordie(&lgs_ftcom_mutex); /* UNLOCK */
 
+       if (busy_close_flag == true) {
+               api_rc = LGSF_BUSY;
+       }
+
        return api_rc;  
 }
 
diff --git a/osaf/services/saf/logsv/lgs/lgs_file.h 
b/osaf/services/saf/logsv/lgs/lgs_file.h
--- a/osaf/services/saf/logsv/lgs/lgs_file.h
+++ b/osaf/services/saf/logsv/lgs/lgs_file.h
@@ -62,6 +62,8 @@ typedef struct {
 char *lgsf_retcode_str(lgsf_retcode_t rc);
 uint32_t lgs_file_init(void);
 lgsf_retcode_t log_file_api(lgsf_apipar_t *param_in);
+void lgs_fd_list_add(int32_t fd);
+int32_t lgs_fd_list_get(void);
 
 #ifdef __cplusplus
 }
diff --git a/osaf/services/saf/logsv/lgs/lgs_filehdl.c 
b/osaf/services/saf/logsv/lgs/lgs_filehdl.c
--- a/osaf/services/saf/logsv/lgs/lgs_filehdl.c
+++ b/osaf/services/saf/logsv/lgs/lgs_filehdl.c
@@ -34,11 +34,51 @@
 #include <logtrace.h>
 #include <ncsgl_defs.h>
 #include <osaf_utility.h>
+#include <osaf_time.h>
 
 #include "lgs.h"
 #include "lgs_util.h"
 #include "lgs_file.h"
 
+/* If the following define is used a delay is randomly inserted in the file I/O
+ * handlers below. The delay will cause a timeout error.
+ * ~10% of the file requests will fail.
+ */
+//#define RAND_TIMEOUT_TEST
+
+/* If the following define is used a seed based on current time is used else
+ * the default value of 1 is used as seed every time (same random series at
+ * every run)
+ */
+//#define RAND_TIMEOUT_TEST_SEEDCHANGE
+
+#ifdef  RAND_TIMEOUT_TEST
+static inline void random_sleep_tst(void)
+{
+#ifdef RAND_TIMEOUT_TEST_SEEDCHANGE
+       struct timespec tmp_ts;
+       static bool set_seed_flag = true;
+       
+       LOG_IN("OsafLOG %s RAND_TIMEOUT_TEST is defined!",__FUNCTION__);
+       
+       if (set_seed_flag) {
+               osaf_clock_gettime(CLOCK_MONOTONIC, &tmp_ts);
+               srand((unsigned int) tmp_ts.tv_nsec);
+               set_seed_flag = false;
+               TRACE("Seeding srand(%d)", (unsigned int) tmp_ts.tv_nsec);
+       }
+#endif
+       int r;
+       r = rand()%100 + 1;
+       if ((r > 10) && (r < 20)) {
+               LOG_NO("%s - sleep!",__FUNCTION__);
+               sleep(2);
+       }
+}
+#else
+#define random_sleep_tst()
+#endif
+
 
 extern pthread_mutex_t lgs_ftcom_mutex;        /* For locking communication */
 
@@ -67,6 +107,8 @@ int path_is_writeable_dir_hdl(void *inda
        
        TRACE("%s - pathname \"%s\"",__FUNCTION__,pathname);
 
+       osaf_mutex_unlock_ordie(&lgs_ftcom_mutex); /* UNLOCK  Critical section 
*/
+
        /* Check if the pathname violates security rules e.g. contains ../ */
        if (lgs_relative_path_check_ts(pathname) || stat(pathname, &pathstat) 
!= 0) {
                LOG_NO("Path %s not allowed", pathname);
@@ -87,6 +129,10 @@ int path_is_writeable_dir_hdl(void *inda
                goto done;
        }
 
+       random_sleep_tst();
+       
+       osaf_mutex_lock_ordie(&lgs_ftcom_mutex); /* LOCK after critical section 
*/
+       
        is_writeable_dir = 1;
 done:
        TRACE_LEAVE2("is_writeable_dir = %d",is_writeable_dir);
@@ -107,9 +153,16 @@ int check_path_exists_hdl(void *indata, 
        char *path_str = (char *) indata;
        int rc = 0;
        
+       osaf_mutex_unlock_ordie(&lgs_ftcom_mutex); /* UNLOCK  Critical section 
*/
+
        rc = stat(path_str, &pathstat);
        TRACE("%s - path_str \"%s\", rc=%d",__FUNCTION__,path_str,rc);
        TRACE("%s - errno \"%s\"",__FUNCTION__,strerror(errno));
+       
+       random_sleep_tst();
+
+       osaf_mutex_lock_ordie(&lgs_ftcom_mutex); /* LOCK after critical section 
*/
+
        return rc;
 }
 
@@ -130,8 +183,13 @@ int rename_file_hdl(void *indata, void *
        char *old_path = (char *) indata + sizeof(size_t);
        char *new_path = old_path + old_path_size;
        
+       osaf_mutex_unlock_ordie(&lgs_ftcom_mutex); /* UNLOCK  Critical section 
*/
+       
        if ((rc = rename(old_path, new_path)) == -1)
                LOG_NO("rename: FAILED - %s", strerror(errno));
+       
+       random_sleep_tst();
+       osaf_mutex_lock_ordie(&lgs_ftcom_mutex); /* LOCK after critical section 
*/
 
        TRACE_LEAVE();
        return rc;
@@ -157,16 +215,21 @@ int create_config_file_hdl(void *indata,
        TRACE_ENTER();
        
        TRACE("%s - file_path \"%s\"",__FUNCTION__,file_path);
-fopen_retry:
-       if ((filp = fopen(file_path, "w")) == NULL) {
-               if (errno == EINTR)
-                       goto fopen_retry;
 
+       osaf_mutex_unlock_ordie(&lgs_ftcom_mutex); /* UNLOCK  Critical section 
*/
+       
+       /* Create the config file */
+       do {
+               if ((filp = fopen(file_path, "w")) != NULL)
+                       break;
+       } while (errno == EINTR);
+       if (filp == NULL) {
                LOG_NO("Could not open '%s' - %s", file_path, strerror(errno));
                rc = -1;
                goto done;
        }
 
+       random_sleep_tst();
        /* version */
        if ((rc = fprintf(filp, "%s %c.%d.%d\n", LOG_VER_EXP,
                        params_in->version.releaseCode,
@@ -204,15 +267,14 @@ fopen_retry:
        if (rc == -1)
                LOG_NO("Could not write to \"%s\"", file_path);
 
-fclose_retry:
-       if ((rc = fclose(filp)) == -1) {
-               if (errno == EINTR)
-                       goto fclose_retry;
-
+       /* Close the file */
+       rc = fclose(filp);
+       if (rc == -1) {
                LOG_NO("Could not close \"%s\" - \"%s\"", file_path, 
strerror(errno));
        }
-
+       
 done:
+       osaf_mutex_lock_ordie(&lgs_ftcom_mutex); /* LOCK after critical section 
*/
        TRACE_LEAVE2("rc = %d", rc);
        return rc;      
 }
@@ -246,6 +308,8 @@ int write_log_record_hdl(void *indata, v
        
        TRACE_ENTER();
        
+       osaf_mutex_unlock_ordie(&lgs_ftcom_mutex); /* UNLOCK  Critical section 
*/
+       
  retry:
        rc = write(params_in->fd, &logrecord[bytes_written],
                 params_in->record_size - bytes_written);
@@ -262,6 +326,7 @@ int write_log_record_hdl(void *indata, v
                if (bytes_written < params_in->record_size)
                        goto retry;
        }
+       random_sleep_tst();
 #ifdef LLD_DELAY_WRTST /* LLDTEST Wait first time thread is used */
        if (strstr(logrecord, "xxx")) {
                if (lld_once_f == true) {
@@ -277,12 +342,12 @@ int write_log_record_hdl(void *indata, v
                TRACE("LLDTEST yyy Rearmed Hang write");
        }
 #endif
- 
+       osaf_mutex_lock_ordie(&lgs_ftcom_mutex); /* LOCK after critical section 
*/
+
        /* If the thread was hanging and has timed out and the log record was
         * written it is invalid and shall be removed from file (log service has
         * returned SA_AIS_TRY_AGAIN). 
         */
-       osaf_mutex_lock_ordie(&lgs_ftcom_mutex); /* LOCK */
        if (*timeout_f == true) {
                TRACE("Timeout, removing last log record");
                file_length = lseek(params_in->fd, -bytes_written, SEEK_END);
@@ -300,7 +365,6 @@ int write_log_record_hdl(void *indata, v
                                        __FUNCTION__,strerror(errno));          
        
                }
        }
-       osaf_mutex_unlock_ordie(&lgs_ftcom_mutex); /* UNLOCK */
  
 done:
        TRACE_LEAVE2("rc = %d",rc);
@@ -343,6 +407,7 @@ int make_log_dir_hdl(void *indata, void 
        TRACE("rootpath \"%s\"",rootpath);
        TRACE("relpath \"%s\"",relpath);
        
+       osaf_mutex_unlock_ordie(&lgs_ftcom_mutex); /* UNLOCK  Critical section 
*/
        /* 
         * Create root directory if it does not exists.
         * TBD. Handle via separate ticket
@@ -364,6 +429,7 @@ int make_log_dir_hdl(void *indata, void 
                TRACE("%s - create rootpath \"%s\"",__FUNCTION__,rootpath);
        }
        
+       random_sleep_tst();
        /*
         * Create root path without preceding '/' and ending '/'
         * Example: ro1/ro2/ro3
@@ -420,6 +486,7 @@ int make_log_dir_hdl(void *indata, void 
        TRACE("%s - Dir \"%s\" created",__FUNCTION__, mpath);
        
 done:
+       osaf_mutex_lock_ordie(&lgs_ftcom_mutex); /* LOCK after critical section 
*/
        TRACE_LEAVE2("mldh_rc = %u", mldh_rc);
        return mldh_rc;
 }
@@ -437,7 +504,7 @@ done:
  * @param max_outsize[in], always sizeof(int)
  * @return file descriptor or -1 if error
  */
-int fileopen_hdl(void *indata, void *outdata, size_t max_outsize)
+int fileopen_hdl(void *indata, void *outdata, size_t max_outsize, bool 
*timeout_f)
 {
        int errno_save = 0;
        char *filepath = (char *) indata;
@@ -447,10 +514,12 @@ int fileopen_hdl(void *indata, void *out
        TRACE_ENTER();
        
        TRACE("%s - filepath \"%s\"",__FUNCTION__,filepath);
+       osaf_mutex_unlock_ordie(&lgs_ftcom_mutex); /* UNLOCK  Critical section  
*/
 open_retry:
        fd = open(filepath, O_CREAT | O_RDWR | O_APPEND | O_NONBLOCK,
                                                        S_IRUSR | S_IWUSR | 
S_IRGRP);
 
+       random_sleep_tst();
        if (fd == -1) {
                if (errno == EINTR)
                        goto open_retry;
@@ -460,8 +529,20 @@ open_retry:
                 * Can be called in context of log_stream_write */
                LOG_IN("Could not open: %s - %s", filepath, strerror(errno));
        }
+       osaf_mutex_lock_ordie(&lgs_ftcom_mutex); /* LOCK after critical section 
*/
 
        *errno_out_p = errno_save;
+       
+       /* If the file was opened but thread was hanging and has timed out than
+        * opening the file is reported as failed. This means that the file has 
to
+        * be closed again in order to not get a stray fd.
+        */
+       if ((*timeout_f == true) && (fd != -1)) {
+               (void) close(fd);
+               fd = -1;
+               errno_save = 0;
+       }
+       
        TRACE_LEAVE();
        return fd;
 }
@@ -481,11 +562,26 @@ int fileclose_hdl(void *indata, void *ou
        fd = *(int *) indata;
        TRACE_ENTER2("fd=%d", fd);
 
+       osaf_mutex_unlock_ordie(&lgs_ftcom_mutex); /* UNLOCK critical section */
+       /* Flush and synchronize the file before closing to guaranty that the 
file
+        * is not written to after it's closed
+        */
+       if ((rc = fdatasync(fd)) == -1) {
+               if ((errno == EROFS) || (errno == EINVAL)) {
+                       TRACE("Synchronization is not supported for this file");
+               } else {
+                       LOG_NO("%s: fdatasync() error \"%s\"",__FUNCTION__, 
strerror(errno));
+               }
+       }
+ 
+       random_sleep_tst();
+       /* Close the file */
        rc = close(fd);
-
        if (rc == -1) {
                LOG_ER("fileclose() %s",strerror(errno));
        }
+       
+       osaf_mutex_lock_ordie(&lgs_ftcom_mutex); /* LOCK after critical section 
*/
        TRACE_LEAVE2("rc=%d", rc);
        return rc;
 }
@@ -504,6 +600,7 @@ int delete_file_hdl(void *indata, void *
        
        TRACE_ENTER();
        
+       osaf_mutex_unlock_ordie(&lgs_ftcom_mutex); /* UNLOCK critical section */
        if ((rc = unlink(pathname)) == -1) {
                if (errno == ENOENT)
                        rc = 0;
@@ -511,6 +608,8 @@ int delete_file_hdl(void *indata, void *
                        LOG_NO("could not unlink: %s - %s", pathname, 
strerror(errno));
        }
 
+       random_sleep_tst();
+       osaf_mutex_lock_ordie(&lgs_ftcom_mutex); /* LOCK after critical section 
*/
        TRACE_LEAVE();
        return rc;
 }
@@ -608,7 +707,10 @@ int get_number_of_log_files_hdl(void *in
                goto done_exit;
        }
 
+       osaf_mutex_unlock_ordie(&lgs_ftcom_mutex); /* UNLOCK critical section */
        files = n = scandir(path, &namelist, filter_func, alphasort);
+       random_sleep_tst();
+       osaf_mutex_lock_ordie(&lgs_ftcom_mutex); /* LOCK after critical section 
*/
        if (n == -1 && errno == ENOENT) {
                rc = 0;
                goto done_exit;
diff --git a/osaf/services/saf/logsv/lgs/lgs_filehdl.h 
b/osaf/services/saf/logsv/lgs/lgs_filehdl.h
--- a/osaf/services/saf/logsv/lgs/lgs_filehdl.h
+++ b/osaf/services/saf/logsv/lgs/lgs_filehdl.h
@@ -159,7 +159,7 @@ int rename_file_hdl(void *indata, void *
 int create_config_file_hdl(void *indata, void *outdata, size_t max_outsize);
 int write_log_record_hdl(void *indata, void *outdata, size_t max_outsize, bool 
*timeout_f);
 int make_log_dir_hdl(void *indata, void *outdata, size_t max_outsize);
-int fileopen_hdl(void *indata, void *outdata, size_t max_outsize);
+int fileopen_hdl(void *indata, void *outdata, size_t max_outsize, bool 
*timeout_f);
 int fileclose_hdl(void *indata, void *outdata, size_t max_outsize);
 int delete_file_hdl(void *indata, void *outdata, size_t max_outsize);
 int get_number_of_log_files_hdl(void *indata, void *outdata, size_t 
max_outsize);
diff --git a/osaf/services/saf/logsv/lgs/lgs_stream.c 
b/osaf/services/saf/logsv/lgs/lgs_stream.c
--- a/osaf/services/saf/logsv/lgs/lgs_stream.c
+++ b/osaf/services/saf/logsv/lgs/lgs_stream.c
@@ -95,9 +95,9 @@ done:
 
 /**
  * Close with retry at EINTR
- * @param fd
- *
- * @return int
+ * 
+ * @param fd [in]
+ * @return -1 on error
  */
 static int fileclose_h(int fd)
 {
@@ -115,7 +115,12 @@ static int fileclose_h(int fd)
        apipar.data_out = NULL;
        
        api_rc = log_file_api(&apipar);
-       if (api_rc != LGSF_SUCESS) {
+       if (api_rc == LGSF_BUSY) {
+               /* The fd is not invalidated. Save to close later */
+               TRACE("%s - API error 
%s",__FUNCTION__,lgsf_retcode_str(api_rc));
+               lgs_fd_list_add(fd);
+               rc = -1;
+       } else if (api_rc != LGSF_SUCESS) {
                TRACE("%s - API error 
%s",__FUNCTION__,lgsf_retcode_str(api_rc));
                rc = -1;
        } else {
@@ -224,7 +229,7 @@ static int rotate_if_needed(log_stream_t
         */
        while (file_cnt >= stream->maxFilesRotated) {
                if ((rc = file_unlink_h(oldest_file)) == -1) {
-                       LOG_NO("could not delete: %s - %s", oldest_file, 
strerror(errno));
+                       LOG_NO("Could not delete: %s - %s", oldest_file, 
strerror(errno));
                        goto done;
                }
 
@@ -838,11 +843,7 @@ void log_stream_close(log_stream_t **s, 
                        
                        /* Close the log file */
                        rc = fileclose_h(*stream->p_fd);
-                       while ((rc == -1) && (msecs_waited < max_waiting_time)) 
{
-                               usleep(sleep_delay_ms * 1000);
-                               msecs_waited += sleep_delay_ms;
-                               rc = fileclose_h(*stream->p_fd);
-                       }
+                       *stream->p_fd = -1;
                        if (rc == -1) {
                                LOG_ER("Could not close log files: %s", 
strerror(errno));
                                goto done_files;
@@ -918,9 +919,8 @@ int log_stream_file_close(log_stream_t *
        if (*stream->p_fd != -1) {
                if ((rc = fileclose_h(*stream->p_fd)) == -1) {
                        LOG_ER("log_stream_file_close FAILED: %s", 
strerror(errno));
-                       *stream->p_fd = -1; /* Force reset the fd, otherwise fd 
will be stale. */
-               } else
-                       *stream->p_fd = -1;
+               }
+               *stream->p_fd = -1;
        }
 
        TRACE_LEAVE2("%d", rc);
@@ -1054,11 +1054,12 @@ static int log_rotation_stb(log_stream_t
                 */
                
                /* Close current log file */
-               if ((rc = fileclose_h(*stream->p_fd)) == -1) {
+               rc = fileclose_h(*stream->p_fd);
+               *stream->p_fd = -1;
+               if (rc == -1) {
                        LOG_IN("close FAILED: %s", strerror(errno));
                        goto done;
                }
-               *stream->p_fd = -1;
                
                /* Rename file to give it the "close timestamp" */
                rc = lgs_file_rename_h(stream->pathName, 
stream->stb_logFileCurrent,
@@ -1067,9 +1068,8 @@ static int log_rotation_stb(log_stream_t
                        goto done;
 
                /* Remove oldest file if needed */
-               if ((rc = rotate_if_needed(stream)) == -1) {
-                       TRACE("rotate_if_needed failed");
-                       goto done;
+               if (rotate_if_needed(stream) == -1) {
+                       TRACE("Old file removal failed");
                }
                
                /* Save new name for current log file and open it */
@@ -1114,11 +1114,12 @@ static int log_rotation_act(log_stream_t
                char *current_time = lgs_get_time(&closetime);
 
                /* Close current log file */
-               if ((rc = fileclose_h(*stream->p_fd)) == -1) {
+               rc = fileclose_h(*stream->p_fd);
+               *stream->p_fd = -1;
+               if (rc == -1) {
                        LOG_IN("close FAILED: %s", strerror(errno));
                        goto done;
                }
-               *stream->p_fd = -1;
 
                /* Rename file to give it the "close timestamp" */
                rc = lgs_file_rename_h(stream->pathName, stream->logFileCurrent,
@@ -1132,15 +1133,14 @@ static int log_rotation_act(log_stream_t
                /* Invalidate logFileCurrent for this stream */
                stream->logFileCurrent[0] = 0;
                
+               /* Reset file size for current log file */
+               stream->curFileSize = 0;
+               
                /* Remove oldest file if needed */
-               if ((rc = rotate_if_needed(stream)) == -1)
-                       goto done;
-#if 0
-               /* LLDTEST XXX Must be wrong. Should be time when stream was 
created.
-                * Why change this time here???
-                */
-               stream->creationTimeStamp = lgs_get_SaTime();
-#endif         
+               if (rotate_if_needed(stream) == -1) {
+                       TRACE("Old file removal failed");
+               }
+
                /* Create a new file name that includes "open time stamp" and 
open the file */
                snprintf(stream->logFileCurrent, NAME_MAX, "%s_%s", 
stream->fileName,
                                current_time);
@@ -1151,8 +1151,6 @@ static int log_rotation_act(log_stream_t
                        rc = -1;
                        goto done;
                }
-               
-               stream->curFileSize = 0;
        }
        
 done:  
@@ -1185,7 +1183,7 @@ int log_stream_write_h(log_stream_t *str
 
        osafassert(stream != NULL && buf != NULL);
        TRACE_ENTER2("%s", stream->name);
-
+       
        /* Open files on demand e.g. on new active after fail/switch-over. This
         * enables LOG to cope with temporary file system problems. */
        if (*stream->p_fd == -1) {
@@ -1250,8 +1248,10 @@ int log_stream_write_h(log_stream_t *str
                }
        
                if (*stream->p_fd != -1) {
-                       /* Try to close the file and invalidate the stream fd */
-                       fileclose_h(*stream->p_fd);
+                       /* Close the file and invalidate the stream fd */
+                       if (fileclose_h(*stream->p_fd) == -1) {
+                               TRACE("fileclose failed");
+                       }
                        *stream->p_fd = -1;
                }
                
@@ -1460,9 +1460,10 @@ int log_stream_config_change(bool create
                /* close the existing log file, and only when there is a valid 
fd */
 
                if ((rc = fileclose_h(*stream->p_fd)) == -1) {
-                       LOG_ER("log_stream_config_change file close  FAILED: 
%s", strerror(errno));
+                       LOG_ER("log_stream log file close  FAILED: %s", 
strerror(errno));
                        goto done;
                }
+               *stream->p_fd = -1;
 
                rc = lgs_file_rename_h(stream->pathName, current_logfile_name,
                                current_time, LGS_LOG_FILE_EXT, NULL);
diff --git a/tests/logsv/tet_LogOiOps.c b/tests/logsv/tet_LogOiOps.c
--- a/tests/logsv/tet_LogOiOps.c
+++ b/tests/logsv/tet_LogOiOps.c
@@ -382,11 +382,16 @@ void saLogOi_22(void)
 void saLogOi_23()
 {
     int rc;
+       int i;
     char command[256];
 
     sprintf(command, "immcfg -c SaLogStreamConfig 
safLgStrCfg=strA,safApp=safLogService "
                        "-a saLogStreamFileName=strA -a 
saLogStreamPathName=strAdir");
-    rc = system(command);
+       /* Make more than one attempt */
+       for (i=0; i<3; i++) {
+               if ((rc = system(command)) == 0)
+                       break;
+       }
     rc_validate(WEXITSTATUS(rc), 0);
 }
 
@@ -396,11 +401,16 @@ void saLogOi_23()
 void saLogOi_24()
 {
     int rc;
+       int i;
     char command[256];
 
     sprintf(command, "immcfg -c SaLogStreamConfig 
safLgStrCfg=strB,safApp=safLogService "
                        "-a saLogStreamFileName=strB -a 
saLogStreamPathName=strBdir");
-    rc = system(command);
+       /* Make more than one attempt */
+       for (i=0; i<3; i++) {
+               if ((rc = system(command)) == 0)
+                       break;
+       }
     rc_validate(WEXITSTATUS(rc), 0);
 }
 
@@ -410,11 +420,16 @@ void saLogOi_24()
 void saLogOi_25()
 {
     int rc;
+       int i;
     char command[256];
 
     sprintf(command, "immcfg -c SaLogStreamConfig 
safLgStrCfg=strC,safApp=safLogService "
                        "-a saLogStreamFileName=strC -a 
saLogStreamPathName=strCdir");
-    rc = system(command);
+       /* Make more than one attempt */
+       for (i=0; i<3; i++) {
+               if ((rc = system(command)) == 0)
+                       break;
+       }
     rc_validate(WEXITSTATUS(rc), 0);
 }
 

------------------------------------------------------------------------------
Want excitement?
Manually upgrade your production database.
When you want reliability, choose Perforce
Perforce version control. Predictably reliable.
http://pubads.g.doubleclick.net/gampad/clk?id=157508191&iu=/4140/ostg.clktrk
_______________________________________________
Opensaf-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opensaf-devel

Reply via email to