X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fstats.c;h=828c1ef8ac2dcef4535b428e049be80ffabd004e;hb=89077bfd2abf7984027552c8cee223a0c6dab57c;hp=929e09bf51c8764fe7610da837443ceeb07059b1;hpb=268f4a8f7e81d8b32591e01b6dbcdca0bee1322f;p=framework%2Fconnectivity%2Fconnman.git diff --git a/src/stats.c b/src/stats.c index 929e09b..828c1ef 100644 --- a/src/stats.c +++ b/src/stats.c @@ -24,6 +24,7 @@ #endif #define _GNU_SOURCE +#include #include #include #include @@ -32,9 +33,13 @@ #include #include #include +#include #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 @@ -214,6 +219,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 +240,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 +282,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); @@ -286,6 +296,13 @@ 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 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 { @@ -295,6 +312,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; } @@ -309,13 +331,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; } @@ -324,13 +349,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; } @@ -344,6 +370,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", @@ -351,6 +379,7 @@ static int stats_file_setup(struct stats_file *file) TFR(close(file->fd)); g_free(file->name); + file->name = NULL; return -errno; } @@ -365,6 +394,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; } @@ -580,6 +610,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, @@ -640,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); @@ -656,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. */