Added debugging statements and ensure, following calls to g_free, that
the statistics file name field is set to NULL to ensure that a failed
file mapping and a subsequent call to stats_free doesn't fault when
trying to double-free it.

Added an informational message when mmap fails with EINVAL in
stats_file_remap.

diff --git a/src/stats.c b/src/stats.c
index 679a372..b99c2ae 100644
--- a/src/stats.c
+++ b/src/stats.c
@@ -214,6 +214,9 @@ static void stats_free(gpointer user_data)
 {
        struct stats_file *file = user_data;
 
+       if (file == NULL)
+               return;
+
        msync(file->addr, file->len, MS_SYNC);
 
        munmap(file->addr, file->len);
@@ -232,8 +235,7 @@ static void stats_free(gpointer user_data)
                file->name = NULL;
        }
 
-       if (file != NULL)
-               g_free(file);
+       g_free(file);
 }
 
 static void update_first(struct stats_file *file)
@@ -275,6 +277,8 @@ static int stats_file_remap(struct stats_file *file, size_t 
size)
        void *addr;
        int err;
 
+       DBG("file %p size %u addr %p len %u", file, size, file->addr, 
file->len);
+
        page_size = sysconf(_SC_PAGESIZE);
        new_size = (size + page_size - 1) & ~(page_size - 1);
 
@@ -288,9 +292,10 @@ static int stats_file_remap(struct stats_file *file, 
size_t size)
        if (file->addr == NULL) {
                /*
                 * Though the buffer is not shared between processes, we still
-                * have to take MAP_SHARED because MAP_PRIVATE does not 
guarantee
-                * that writes will hit the file eventually. For more details
-                * please read the mmap man pages.
+                * have to take MAP_SHARED because MAP_PRIVATE does not
+                * guarantee that writes will hit the file without an explicit
+                * call to munmap or msync. For more details please read the
+                * mmap man pages.
                 */
                addr = mmap(NULL, new_size, PROT_READ | PROT_WRITE,
                                MAP_SHARED, file->fd, 0);
@@ -301,6 +306,10 @@ static int stats_file_remap(struct stats_file *file, 
size_t size)
        if (addr == MAP_FAILED) {
                connman_error("mmap error %s for %s",
                                strerror(errno), file->name);
+               if (errno == EINVAL) {
+                       connman_error("%s might be on a file system, such as 
JFFS2, that "
+                                                 "does not allow shared 
writable mappings.", file->name);
+               }
                return -errno;
        }
 
@@ -315,6 +324,8 @@ static int stats_file_remap(struct stats_file *file, size_t 
size)
 static int stats_open(struct stats_file *file,
                        const char *name)
 {
+       DBG("file %p name %s", file, name);
+
        file->name = g_strdup(name);
 
        file->fd = TFR(open(file->name, O_RDWR | O_CREAT, 0644));
@@ -322,6 +333,7 @@ static int stats_open(struct stats_file *file,
                connman_error("open error %s for %s",
                                strerror(errno), file->name);
                g_free(file->name);
+               file->name = NULL;
                return -errno;
        }
 
@@ -337,6 +349,7 @@ static int stats_open_temp(struct stats_file *file)
                connman_error("create tempory file error %s for %s",
                                strerror(errno), file->name);
                g_free(file->name);
+               file->name = NULL;
                return -errno;
        }
 
@@ -350,6 +363,8 @@ static int stats_file_setup(struct stats_file *file)
        size_t size = 0;
        int err;
 
+       DBG("file %p fd %d name %s", file, file->fd, file->name);
+
        err = fstat(file->fd, &st);
        if (err < 0) {
                connman_error("fstat error %s for %s\n",
@@ -357,6 +372,7 @@ static int stats_file_setup(struct stats_file *file)
 
                TFR(close(file->fd));
                g_free(file->name);
+               file->name = NULL;
 
                return -errno;
        }
@@ -371,6 +387,7 @@ static int stats_file_setup(struct stats_file *file)
        if (err < 0) {
                TFR(close(file->fd));
                g_free(file->name);
+               file->name = NULL;
 
                return err;
        }
@@ -586,7 +603,7 @@ static void stats_file_cleanup(struct stats_file *file)
 {
        file->fd = -1;
        g_free(file->name);
+       file->name = NULL;
 }
 
 static int stats_file_close_swap(struct stats_file *history_file,
-- 
1.7.3.4
_______________________________________________
connman mailing list
[email protected]
http://lists.connman.net/listinfo/connman

Reply via email to