dnsproxy: Only one copy of the relevant buffers will be made to a TCP request
[framework/connectivity/connman.git] / src / stats.c
index c4aa24e..828c1ef 100644 (file)
@@ -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
@@ -277,7 +282,7 @@ 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,
+       DBG("file %p size %zu addr %p len %zu", file, size, file->addr,
                file->len);
 
        page_size = sysconf(_SC_PAGESIZE);
@@ -330,7 +335,7 @@ static int stats_open(struct stats_file *file,
 
        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);
@@ -344,7 +349,7 @@ 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) {
@@ -666,7 +671,7 @@ 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);
@@ -682,9 +687,26 @@ int __connman_stats_service_register(struct connman_service *service)
                return -EALREADY;
        }
 
-       name = g_strdup_printf("%s/stats/%s.data", STORAGEDIR,
+       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);
+
+       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. */