Updated connman to version 1.35
[platform/upstream/connman.git] / src / stats.c
old mode 100644 (file)
new mode 100755 (executable)
index 0dc45f0..663bc38
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2010  BMW Car IT GmbH. All rights reserved.
+ *  Copyright (C) 2010-2014  BMW Car IT GmbH.
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License version 2 as
@@ -24,6 +24,7 @@
 #endif
 
 #define _GNU_SOURCE
+#include <errno.h>
 #include <sys/mman.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <string.h>
 #include <limits.h>
+#include <sys/stat.h>
 
 #include "connman.h"
 
+#define MODE           (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | \
+                       S_IXGRP | S_IROTH | S_IXOTH)
+
 #ifdef TEMP_FAILURE_RETRY
 #define TFR TEMP_FAILURE_RETRY
 #else
@@ -120,7 +125,7 @@ struct stats_iter {
        struct stats_record *it;
 };
 
-GHashTable *stats_hash = NULL;
+static GHashTable *stats_hash = NULL;
 
 static struct stats_file_header *get_hdr(struct stats_file *file)
 {
@@ -214,17 +219,23 @@ static void stats_free(gpointer user_data)
 {
        struct stats_file *file = user_data;
 
+       if (!file)
+               return;
+
        msync(file->addr, file->len, MS_SYNC);
 
        munmap(file->addr, file->len);
        file->addr = NULL;
 
-       TFR(close(file->fd));
+       close(file->fd);
        file->fd = -1;
 
-       if (file->history_name != NULL)
-               g_free(file->history_name);
+       g_free(file->history_name);
+       file->history_name = NULL;
+
        g_free(file->name);
+       file->name = NULL;
+
        g_free(file);
 }
 
@@ -267,6 +278,9 @@ static int stats_file_remap(struct stats_file *file, size_t size)
        void *addr;
        int err;
 
+       DBG("file %p size %zu addr %p len %zu", file, size, file->addr,
+               file->len);
+
        page_size = sysconf(_SC_PAGESIZE);
        new_size = (size + page_size - 1) & ~(page_size - 1);
 
@@ -277,7 +291,14 @@ static int stats_file_remap(struct stats_file *file, size_t size)
                return -errno;
        }
 
-       if (file->addr == NULL) {
+       if (!file->addr) {
+               /*
+                * 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 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);
        } else {
@@ -287,6 +308,11 @@ 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;
        }
 
@@ -301,13 +327,16 @@ 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));
+       file->fd = TFR(open(file->name, O_RDWR | O_CREAT | O_CLOEXEC, 0644));
        if (file->fd < 0) {
                connman_error("open error %s for %s",
                                strerror(errno), file->name);
                g_free(file->name);
+               file->name = NULL;
                return -errno;
        }
 
@@ -316,13 +345,14 @@ static int stats_open(struct stats_file *file,
 
 static int stats_open_temp(struct stats_file *file)
 {
-       file->name = g_strdup_printf("%s/stats/stats.XXXXXX.tmp",
+       file->name = g_strdup_printf("%s/stats.XXXXXX.tmp",
                                        STORAGEDIR);
        file->fd = g_mkstemp_full(file->name, O_RDWR | O_CREAT, 0644);
        if (file->fd < 0) {
                connman_error("create tempory file error %s for %s",
                                strerror(errno), file->name);
                g_free(file->name);
+               file->name = NULL;
                return -errno;
        }
 
@@ -336,13 +366,17 @@ 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",
                        strerror(errno), file->name);
 
-               TFR(close(file->fd));
+               close(file->fd);
+               file->fd = -1;
                g_free(file->name);
+               file->name = NULL;
 
                return -errno;
        }
@@ -355,8 +389,10 @@ static int stats_file_setup(struct stats_file *file)
 
        err = stats_file_remap(file, size);
        if (err < 0) {
-               TFR(close(file->fd));
+               close(file->fd);
+               file->fd = -1;
                g_free(file->name);
+               file->name = NULL;
 
                return err;
        }
@@ -434,18 +470,18 @@ static struct stats_record *process_file(struct stats_iter *iter,
        home = NULL;
        roaming = NULL;
 
-       if (cur == NULL)
+       if (!cur)
                cur = get_next_record(iter);
        next = get_next_record(iter);
 
-       while (next != NULL) {
+       while (next) {
                GDate date_cur;
                GDate date_next;
-               int append;
+               bool append;
 
-               append = FALSE;
+               append = false;
 
-               if (cur->roaming == TRUE)
+               if (cur->roaming)
                        roaming = cur;
                else
                        home = cur;
@@ -465,24 +501,24 @@ static struct stats_record *process_file(struct stats_iter *iter,
                        day_next = g_date_get_day(&date_next);
 
                        if (day_cur == day_next && month_cur != month_next) {
-                               append = TRUE;
+                               append = true;
                        } else if (day_cur < account_period_offset &&
                                        day_next >= account_period_offset) {
-                               append = TRUE;
+                               append = true;
                        }
                } else {
                        /* day period size */
                        if (g_date_days_between(&date_cur, &date_next) > 0)
-                               append = TRUE;
+                               append = true;
                }
 
-               if (append == TRUE) {
-                       if (home != NULL) {
+               if (append) {
+                       if (home) {
                                append_record(temp_file, home);
                                home = NULL;
                        }
 
-                       if (roaming != NULL) {
+                       if (roaming) {
                                append_record(temp_file, roaming);
                                roaming = NULL;
                        }
@@ -524,7 +560,7 @@ static int summarize(struct stats_file *data_file,
        /* Now process history file */
        cur = NULL;
 
-       if (history_file != NULL) {
+       if (history_file) {
                history_iter.file = history_file;
                history_iter.begin = get_iterator_begin(history_iter.file);
                history_iter.end = get_iterator_end(history_iter.file);
@@ -544,9 +580,9 @@ static int summarize(struct stats_file *data_file,
         * Ensure date_file records are newer than the history_file
         * record
         */
-       if (cur != NULL) {
+       if (cur) {
                next = get_next_record(&data_iter);
-               while (next != NULL && cur->ts > next->ts)
+               while (next && cur->ts > next->ts)
                        next = get_next_record(&data_iter);
        }
 
@@ -555,7 +591,7 @@ static int summarize(struct stats_file *data_file,
                                &date_change_step_size,
                                data_file->account_period_offset);
 
-       if (cur != NULL)
+       if (cur)
                append_record(temp_file, cur);
 
        return 0;
@@ -572,6 +608,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,
@@ -582,7 +619,7 @@ static int stats_file_close_swap(struct stats_file *history_file,
        stats_file_unmap(history_file);
        stats_file_unmap(temp_file);
 
-       TFR(close(temp_file->fd));
+       close(temp_file->fd);
 
        unlink(history_file->name);
 
@@ -590,7 +627,7 @@ static int stats_file_close_swap(struct stats_file *history_file,
 
        unlink(temp_file->name);
 
-       TFR(close(history_file->fd));
+       close(history_file->fd);
 
        stats_file_cleanup(history_file);
        stats_file_cleanup(temp_file);
@@ -610,6 +647,9 @@ static int stats_file_history_update(struct stats_file *data_file)
        bzero(history_file, sizeof(struct stats_file));
        bzero(temp_file, sizeof(struct stats_file));
 
+       history_file->fd = -1;
+       temp_file->fd = -1;
+
        err = stats_open(history_file, data_file->history_name);
        if (err < 0)
                return err;
@@ -632,25 +672,43 @@ static int stats_file_history_update(struct stats_file *data_file)
 int __connman_stats_service_register(struct connman_service *service)
 {
        struct stats_file *file;
-       char *name;
+       char *name, *dir;
        int err;
 
        DBG("service %p", service);
 
+       dir = g_strdup_printf("%s/%s", STORAGEDIR,
+                               __connman_service_get_ident(service));
+
+       /* If the dir doesn't exist, create it */
+       if (!g_file_test(dir, G_FILE_TEST_IS_DIR)) {
+               if (mkdir(dir, MODE) < 0) {
+                       if (errno != EEXIST) {
+                               g_free(dir);
+
+                               err = -errno;
+                               goto err;
+                       }
+               }
+       }
+
+       g_free(dir);
        file = g_hash_table_lookup(stats_hash, service);
-       if (file == NULL) {
+       if (!file) {
                file = g_try_new0(struct stats_file, 1);
-               if (file == NULL)
+               if (!file)
                        return -ENOMEM;
 
+               file->fd = -1;
+
                g_hash_table_insert(stats_hash, service, file);
        } else {
                return -EALREADY;
        }
 
-       name = g_strdup_printf("%s/stats/%s.data", STORAGEDIR,
+       name = g_strdup_printf("%s/%s/data", STORAGEDIR,
                                __connman_service_get_ident(service));
-       file->history_name = g_strdup_printf("%s/stats/%s.history", STORAGEDIR,
+       file->history_name = g_strdup_printf("%s/%s/history", STORAGEDIR,
                                __connman_service_get_ident(service));
 
        /* TODO: Use a global config file instead of hard coded value. */
@@ -681,7 +739,7 @@ void __connman_stats_service_unregister(struct connman_service *service)
 }
 
 int  __connman_stats_update(struct connman_service *service,
-                               connman_bool_t roaming,
+                               bool roaming,
                                struct connman_stats_data *data)
 {
        struct stats_file *file;
@@ -689,7 +747,7 @@ int  __connman_stats_update(struct connman_service *service,
        int err;
 
        file = g_hash_table_lookup(stats_hash, service);
-       if (file == NULL)
+       if (!file)
                return -EEXIST;
 
        if (file->len < file->max_len &&
@@ -716,7 +774,7 @@ int  __connman_stats_update(struct connman_service *service,
        next->roaming = roaming;
        memcpy(&next->data, data, sizeof(struct connman_stats_data));
 
-       if (roaming != TRUE)
+       if (!roaming)
                set_home(file, next);
        else
                set_roaming(file, next);
@@ -727,22 +785,22 @@ int  __connman_stats_update(struct connman_service *service,
 }
 
 int __connman_stats_get(struct connman_service *service,
-                               connman_bool_t roaming,
+                               bool roaming,
                                struct connman_stats_data *data)
 {
        struct stats_file *file;
        struct stats_record *rec;
 
        file = g_hash_table_lookup(stats_hash, service);
-       if (file == NULL)
+       if (!file)
                return -EEXIST;
 
-       if (roaming != TRUE)
+       if (!roaming)
                rec = file->home;
        else
                rec = file->roaming;
 
-       if (rec != NULL) {
+       if (rec) {
                memcpy(data, &rec->data,
                        sizeof(struct connman_stats_data));
        }