technology: return already enabled when tethering is enabled
[framework/connectivity/connman.git] / src / stats.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2010  BMW Car IT GmbH. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #define _GNU_SOURCE
27 #include <errno.h>
28 #include <sys/mman.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/time.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <limits.h>
36 #include <sys/stat.h>
37
38 #include "connman.h"
39
40 #define MODE            (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | \
41                         S_IXGRP | S_IROTH | S_IXOTH)
42
43 #ifdef TEMP_FAILURE_RETRY
44 #define TFR TEMP_FAILURE_RETRY
45 #else
46 #define TFR
47 #endif
48
49 #define MAGIC 0xFA00B916
50
51 /*
52  * Statistics counters are stored into a ring buffer which is stored
53  * into a file
54  *
55  * File properties:
56  *   The ring buffer is mmap to a file
57  *   Initialy only the smallest possible amount of disk space is allocated
58  *   The files grow to the configured maximal size
59  *   The grows by _SC_PAGESIZE step size
60  *   For each service a file is created
61  *   Each file has a header where the indexes are stored
62  *
63  * Entries properties:
64  *   Each entry has a timestamp
65  *   A flag to mark if the entry is either home (0) or roaming (1) entry
66  *   The entries are fixed sized (stats_record)
67  *
68  * Ring buffer properties:
69  *   There are to indexes 'begin', 'end', 'home' and 'roaming'
70  *   'begin' points to the oldest entry
71  *   'end' points to the newest/current entry
72  *   'home' points to the current home entry
73  *   'roaming' points to the current roaming entry
74  *   If 'begin' == 'end' then the buffer is empty
75  *   If 'end' + 1 == 'begin then it's full
76  *   The ring buffer is valid in the range (begin, end]
77  *   If 'home' has the value UINT_MAX', 'home' is invalid
78  *   if 'roaming' has the value UINT_MAX', 'roaming' is invalid
79  *   'first' points to the first entry in the ring buffer
80  *   'last' points to the last entry in the ring buffer
81  *
82  * History file:
83  *   Same format as the ring buffer file
84  *   For a period of at least 2 months dayly records are keept
85  *   If older, then only a monthly record is keept
86  */
87
88
89 struct stats_file_header {
90         unsigned int magic;
91         unsigned int begin;
92         unsigned int end;
93         unsigned int home;
94         unsigned int roaming;
95 };
96
97 struct stats_record {
98         time_t ts;
99         unsigned int roaming;
100         struct connman_stats_data data;
101 };
102
103 struct stats_file {
104         int fd;
105         char *name;
106         char *addr;
107         size_t len;
108         size_t max_len;
109
110         /* cached values */
111         struct stats_record *first;
112         struct stats_record *last;
113         struct stats_record *home;
114         struct stats_record *roaming;
115
116         /* history */
117         char *history_name;
118         int account_period_offset;
119 };
120
121 struct stats_iter {
122         struct stats_file *file;
123         struct stats_record *begin;
124         struct stats_record *end;
125         struct stats_record *it;
126 };
127
128 GHashTable *stats_hash = NULL;
129
130 static struct stats_file_header *get_hdr(struct stats_file *file)
131 {
132         return (struct stats_file_header *)file->addr;
133 }
134
135 static struct stats_record *get_begin(struct stats_file *file)
136 {
137         unsigned int off = get_hdr(file)->begin;
138
139         return (struct stats_record *)(file->addr + off);
140 }
141
142 static struct stats_record *get_end(struct stats_file *file)
143 {
144         unsigned int off = get_hdr(file)->end;
145
146         return (struct stats_record *)(file->addr + off);
147 }
148
149 static struct stats_record *get_home(struct stats_file *file)
150 {
151         struct stats_file_header *hdr;
152
153         hdr = get_hdr(file);
154
155         if (hdr->home == UINT_MAX)
156                 return NULL;
157
158         return (struct stats_record *)(file->addr + hdr->home);
159 }
160
161 static struct stats_record *get_roaming(struct stats_file *file)
162 {
163         struct stats_file_header *hdr;
164
165         hdr = get_hdr(file);
166
167         if (hdr->roaming == UINT_MAX)
168                 return NULL;
169
170         return (struct stats_record *)(file->addr + hdr->roaming);
171 }
172
173 static void set_end(struct stats_file *file, struct stats_record *end)
174 {
175         struct stats_file_header *hdr;
176
177         hdr = get_hdr(file);
178         hdr->end = (char *)end - file->addr;
179 }
180
181 static void set_home(struct stats_file *file, struct stats_record *home)
182 {
183         struct stats_file_header *hdr;
184
185         hdr = get_hdr(file);
186         hdr->home = (char *)home - file->addr;
187 }
188
189 static void set_roaming(struct stats_file *file, struct stats_record *roaming)
190 {
191         struct stats_file_header *hdr;
192
193         hdr = get_hdr(file);
194         hdr->roaming = (char *)roaming - file->addr;
195 }
196
197 static struct stats_record *get_next(struct stats_file *file,
198                                         struct stats_record *cur)
199 {
200         cur++;
201
202         if (cur > file->last)
203                 cur = file->first;
204
205         return cur;
206 }
207
208 static struct stats_record *get_iterator_begin(struct stats_file *file)
209 {
210         return get_next(file, get_begin(file));
211 }
212
213 static struct stats_record *get_iterator_end(struct stats_file *file)
214 {
215         return get_next(file, get_end(file));
216 }
217
218 static void stats_free(gpointer user_data)
219 {
220         struct stats_file *file = user_data;
221
222         if (file == NULL)
223                 return;
224
225         msync(file->addr, file->len, MS_SYNC);
226
227         munmap(file->addr, file->len);
228         file->addr = NULL;
229
230         TFR(close(file->fd));
231         file->fd = -1;
232
233         if (file->history_name != NULL) {
234                 g_free(file->history_name);
235                 file->history_name = NULL;
236         }
237
238         if (file->name != NULL) {
239                 g_free(file->name);
240                 file->name = NULL;
241         }
242
243         g_free(file);
244 }
245
246 static void update_first(struct stats_file *file)
247 {
248         file->first = (struct stats_record *)
249                         (file->addr + sizeof(struct stats_file_header));
250 }
251
252 static void update_last(struct stats_file *file)
253 {
254         unsigned int max_entries;
255
256         max_entries = (file->len - sizeof(struct stats_file_header)) /
257                         sizeof(struct stats_record);
258         file->last = file->first + max_entries - 1;
259 }
260
261 static void update_home(struct stats_file *file)
262 {
263         file->home = get_home(file);
264 }
265
266 static void update_roaming(struct stats_file *file)
267 {
268         file->roaming = get_roaming(file);
269 }
270
271 static void stats_file_update_cache(struct stats_file *file)
272 {
273         update_first(file);
274         update_last(file);
275         update_home(file);
276         update_roaming(file);
277 }
278
279 static int stats_file_remap(struct stats_file *file, size_t size)
280 {
281         size_t page_size, new_size;
282         void *addr;
283         int err;
284
285         DBG("file %p size %zu addr %p len %zu", file, size, file->addr,
286                 file->len);
287
288         page_size = sysconf(_SC_PAGESIZE);
289         new_size = (size + page_size - 1) & ~(page_size - 1);
290
291         err = ftruncate(file->fd, new_size);
292         if (err < 0) {
293                 connman_error("ftrunctate error %s for %s",
294                                 strerror(errno), file->name);
295                 return -errno;
296         }
297
298         if (file->addr == NULL) {
299                 /*
300                  * Though the buffer is not shared between processes, we still
301                  * have to take MAP_SHARED because MAP_PRIVATE does not
302                  * guarantee that writes will hit the file without an explicit
303                  * call to munmap or msync. For more details please read the
304                  * mmap man pages.
305                  */
306                 addr = mmap(NULL, new_size, PROT_READ | PROT_WRITE,
307                                 MAP_SHARED, file->fd, 0);
308         } else {
309                 addr = mremap(file->addr, file->len, new_size, MREMAP_MAYMOVE);
310         }
311
312         if (addr == MAP_FAILED) {
313                 connman_error("mmap error %s for %s",
314                                 strerror(errno), file->name);
315                 if (errno == EINVAL) {
316                         connman_error("%s might be on a file system, such as "
317                                         "JFFS2, that does not allow shared "
318                                         "writable mappings.", file->name);
319                 }
320                 return -errno;
321         }
322
323         file->addr = addr;
324         file->len = new_size;
325
326         stats_file_update_cache(file);
327
328         return 0;
329 }
330
331 static int stats_open(struct stats_file *file,
332                         const char *name)
333 {
334         DBG("file %p name %s", file, name);
335
336         file->name = g_strdup(name);
337
338         file->fd = TFR(open(file->name, O_RDWR | O_CREAT | O_CLOEXEC, 0644));
339         if (file->fd < 0) {
340                 connman_error("open error %s for %s",
341                                 strerror(errno), file->name);
342                 g_free(file->name);
343                 file->name = NULL;
344                 return -errno;
345         }
346
347         return 0;
348 }
349
350 static int stats_open_temp(struct stats_file *file)
351 {
352         file->name = g_strdup_printf("%s/stats.XXXXXX.tmp",
353                                         STORAGEDIR);
354         file->fd = g_mkstemp_full(file->name, O_RDWR | O_CREAT, 0644);
355         if (file->fd < 0) {
356                 connman_error("create tempory file error %s for %s",
357                                 strerror(errno), file->name);
358                 g_free(file->name);
359                 file->name = NULL;
360                 return -errno;
361         }
362
363         return 0;
364 }
365
366 static int stats_file_setup(struct stats_file *file)
367 {
368         struct stats_file_header *hdr;
369         struct stat st;
370         size_t size = 0;
371         int err;
372
373         DBG("file %p fd %d name %s", file, file->fd, file->name);
374
375         err = fstat(file->fd, &st);
376         if (err < 0) {
377                 connman_error("fstat error %s for %s\n",
378                         strerror(errno), file->name);
379
380                 TFR(close(file->fd));
381                 g_free(file->name);
382                 file->name = NULL;
383
384                 return -errno;
385         }
386
387         size = (size_t)st.st_size;
388         file->max_len = STATS_MAX_FILE_SIZE;
389
390         if (size < (size_t)sysconf(_SC_PAGESIZE))
391                 size = sysconf(_SC_PAGESIZE);
392
393         err = stats_file_remap(file, size);
394         if (err < 0) {
395                 TFR(close(file->fd));
396                 g_free(file->name);
397                 file->name = NULL;
398
399                 return err;
400         }
401
402         hdr = get_hdr(file);
403
404         if (hdr->magic != MAGIC ||
405                         hdr->begin < sizeof(struct stats_file_header) ||
406                         hdr->end < sizeof(struct stats_file_header) ||
407                         hdr->home < sizeof(struct stats_file_header) ||
408                         hdr->roaming < sizeof(struct stats_file_header) ||
409                         hdr->begin > file->len ||
410                         hdr->end > file->len) {
411                 hdr->magic = MAGIC;
412                 hdr->begin = sizeof(struct stats_file_header);
413                 hdr->end = sizeof(struct stats_file_header);
414                 hdr->home = UINT_MAX;
415                 hdr->roaming = UINT_MAX;
416
417                 stats_file_update_cache(file);
418         }
419
420         return 0;
421 }
422
423
424 static struct stats_record *get_next_record(struct stats_iter *iter)
425 {
426         if (iter->it != iter->end) {
427                 struct stats_record *tmp;
428
429                 tmp = iter->it;
430                 iter->it = get_next(iter->file, iter->it);
431
432                 return tmp;
433         }
434
435         return NULL;
436 }
437
438 static int append_record(struct stats_file *file,
439                                 struct stats_record *rec)
440 {
441         struct stats_record *cur, *next;
442         int err;
443
444         if (file->last == get_end(file)) {
445                 err = stats_file_remap(file, file->len +
446                                         sysconf(_SC_PAGESIZE));
447                 if (err < 0)
448                         return err;
449
450                 stats_file_update_cache(file);
451         }
452
453         cur = get_end(file);
454         next = get_next(file, cur);
455
456         memcpy(next, rec, sizeof(struct stats_record));
457
458         set_end(file, next);
459
460         return 0;
461 }
462
463 static struct stats_record *process_file(struct stats_iter *iter,
464                                         struct stats_file *temp_file,
465                                         struct stats_record *cur,
466                                         GDate *date_change_step_size,
467                                         int account_period_offset)
468 {
469         struct stats_record *home, *roaming;
470         struct stats_record *next;
471
472         home = NULL;
473         roaming = NULL;
474
475         if (cur == NULL)
476                 cur = get_next_record(iter);
477         next = get_next_record(iter);
478
479         while (next != NULL) {
480                 GDate date_cur;
481                 GDate date_next;
482                 int append;
483
484                 append = FALSE;
485
486                 if (cur->roaming == TRUE)
487                         roaming = cur;
488                 else
489                         home = cur;
490
491                 g_date_set_time_t(&date_cur, cur->ts);
492                 g_date_set_time_t(&date_next, next->ts);
493
494                 if (g_date_compare(&date_cur, date_change_step_size) < 0) {
495                         /* month period size */
496                         GDateDay day_cur, day_next;
497                         GDateMonth month_cur, month_next;
498
499                         month_cur = g_date_get_month(&date_cur);
500                         month_next = g_date_get_month(&date_next);
501
502                         day_cur = g_date_get_day(&date_cur);
503                         day_next = g_date_get_day(&date_next);
504
505                         if (day_cur == day_next && month_cur != month_next) {
506                                 append = TRUE;
507                         } else if (day_cur < account_period_offset &&
508                                         day_next >= account_period_offset) {
509                                 append = TRUE;
510                         }
511                 } else {
512                         /* day period size */
513                         if (g_date_days_between(&date_cur, &date_next) > 0)
514                                 append = TRUE;
515                 }
516
517                 if (append == TRUE) {
518                         if (home != NULL) {
519                                 append_record(temp_file, home);
520                                 home = NULL;
521                         }
522
523                         if (roaming != NULL) {
524                                 append_record(temp_file, roaming);
525                                 roaming = NULL;
526                         }
527                 }
528
529                 cur = next;
530                 next = get_next_record(iter);
531         }
532
533         return cur;
534 }
535
536 static int summarize(struct stats_file *data_file,
537                         struct stats_file *history_file,
538                         struct stats_file *temp_file)
539 {
540         struct stats_iter data_iter;
541         struct stats_iter history_iter;
542         struct stats_record *cur, *next;
543
544         GDate today, date_change_step_size;
545
546         /*
547          * First calculate the date when switch from monthly
548          * accounting period size to daily size
549          */
550         g_date_set_time_t(&today, time(NULL));
551
552         date_change_step_size = today;
553         if (g_date_get_day(&today) - data_file->account_period_offset >= 0)
554                 g_date_subtract_months(&date_change_step_size, 2);
555         else
556                 g_date_subtract_months(&date_change_step_size, 3);
557
558         g_date_set_day(&date_change_step_size,
559                         data_file->account_period_offset);
560
561
562         /* Now process history file */
563         cur = NULL;
564
565         if (history_file != NULL) {
566                 history_iter.file = history_file;
567                 history_iter.begin = get_iterator_begin(history_iter.file);
568                 history_iter.end = get_iterator_end(history_iter.file);
569                 history_iter.it = history_iter.begin;
570
571                 cur = process_file(&history_iter, temp_file, NULL,
572                                         &date_change_step_size,
573                                         data_file->account_period_offset);
574         }
575
576         data_iter.file = data_file;
577         data_iter.begin = get_iterator_begin(data_iter.file);
578         data_iter.end = get_iterator_end(data_iter.file);
579         data_iter.it = data_iter.begin;
580
581         /*
582          * Ensure date_file records are newer than the history_file
583          * record
584          */
585         if (cur != NULL) {
586                 next = get_next_record(&data_iter);
587                 while (next != NULL && cur->ts > next->ts)
588                         next = get_next_record(&data_iter);
589         }
590
591         /* And finally process the new data records */
592         cur = process_file(&data_iter, temp_file, cur,
593                                 &date_change_step_size,
594                                 data_file->account_period_offset);
595
596         if (cur != NULL)
597                 append_record(temp_file, cur);
598
599         return 0;
600 }
601
602 static void stats_file_unmap(struct stats_file *file)
603 {
604         msync(file->addr, file->len, MS_SYNC);
605         munmap(file->addr, file->len);
606         file->addr = NULL;
607 }
608
609 static void stats_file_cleanup(struct stats_file *file)
610 {
611         file->fd = -1;
612         g_free(file->name);
613         file->name = NULL;
614 }
615
616 static int stats_file_close_swap(struct stats_file *history_file,
617                                         struct stats_file *temp_file)
618 {
619         int err;
620
621         stats_file_unmap(history_file);
622         stats_file_unmap(temp_file);
623
624         TFR(close(temp_file->fd));
625
626         unlink(history_file->name);
627
628         err = link(temp_file->name, history_file->name);
629
630         unlink(temp_file->name);
631
632         TFR(close(history_file->fd));
633
634         stats_file_cleanup(history_file);
635         stats_file_cleanup(temp_file);
636
637         return err;
638 }
639
640 static int stats_file_history_update(struct stats_file *data_file)
641 {
642         struct stats_file _history_file, *history_file;
643         struct stats_file _temp_file, *temp_file;
644         int err;
645
646         history_file = &_history_file;
647         temp_file = &_temp_file;
648
649         bzero(history_file, sizeof(struct stats_file));
650         bzero(temp_file, sizeof(struct stats_file));
651
652         err = stats_open(history_file, data_file->history_name);
653         if (err < 0)
654                 return err;
655         stats_file_setup(history_file);
656
657         err = stats_open_temp(temp_file);
658         if (err < 0) {
659                 stats_free(history_file);
660                 return err;
661         }
662         stats_file_setup(temp_file);
663
664         summarize(data_file, history_file, temp_file);
665
666         err = stats_file_close_swap(history_file, temp_file);
667
668         return err;
669 }
670
671 int __connman_stats_service_register(struct connman_service *service)
672 {
673         struct stats_file *file;
674         char *name, *dir;
675         int err;
676
677         DBG("service %p", service);
678
679         file = g_hash_table_lookup(stats_hash, service);
680         if (file == NULL) {
681                 file = g_try_new0(struct stats_file, 1);
682                 if (file == NULL)
683                         return -ENOMEM;
684
685                 g_hash_table_insert(stats_hash, service, file);
686         } else {
687                 return -EALREADY;
688         }
689
690         dir = g_strdup_printf("%s/%s", STORAGEDIR,
691                                 __connman_service_get_ident(service));
692
693         /* If the dir doesn't exist, create it */
694         if (!g_file_test(dir, G_FILE_TEST_IS_DIR)) {
695                 if(mkdir(dir, MODE) < 0) {
696                         if (errno != EEXIST) {
697                                 g_free(dir);
698
699                                 err = -errno;
700                                 goto err;
701                         }
702                 }
703         }
704
705         g_free(dir);
706
707         name = g_strdup_printf("%s/%s/data", STORAGEDIR,
708                                 __connman_service_get_ident(service));
709         file->history_name = g_strdup_printf("%s/%s/history", STORAGEDIR,
710                                 __connman_service_get_ident(service));
711
712         /* TODO: Use a global config file instead of hard coded value. */
713         file->account_period_offset = 1;
714
715         err = stats_open(file, name);
716         g_free(name);
717         if (err < 0)
718                 goto err;
719
720         err = stats_file_setup(file);
721         if (err < 0)
722                 goto err;
723
724         return 0;
725
726 err:
727         g_hash_table_remove(stats_hash, service);
728
729         return err;
730 }
731
732 void __connman_stats_service_unregister(struct connman_service *service)
733 {
734         DBG("service %p", service);
735
736         g_hash_table_remove(stats_hash, service);
737 }
738
739 int  __connman_stats_update(struct connman_service *service,
740                                 connman_bool_t roaming,
741                                 struct connman_stats_data *data)
742 {
743         struct stats_file *file;
744         struct stats_record *next;
745         int err;
746
747         file = g_hash_table_lookup(stats_hash, service);
748         if (file == NULL)
749                 return -EEXIST;
750
751         if (file->len < file->max_len &&
752                         file->last == get_end(file)) {
753                 DBG("grow file %s", file->name);
754
755                 err = stats_file_remap(file, file->len + sysconf(_SC_PAGESIZE));
756                 if (err < 0)
757                         return err;
758         }
759
760         next = get_next(file, get_end(file));
761
762         if (next == get_begin(file)) {
763                 DBG("ring buffer is full, update history file");
764
765                 if (stats_file_history_update(file) < 0) {
766                         connman_warn("history file update failed %s",
767                                         file->history_name);
768                 }
769         }
770
771         next->ts = time(NULL);
772         next->roaming = roaming;
773         memcpy(&next->data, data, sizeof(struct connman_stats_data));
774
775         if (roaming != TRUE)
776                 set_home(file, next);
777         else
778                 set_roaming(file, next);
779
780         set_end(file, next);
781
782         return 0;
783 }
784
785 int __connman_stats_get(struct connman_service *service,
786                                 connman_bool_t roaming,
787                                 struct connman_stats_data *data)
788 {
789         struct stats_file *file;
790         struct stats_record *rec;
791
792         file = g_hash_table_lookup(stats_hash, service);
793         if (file == NULL)
794                 return -EEXIST;
795
796         if (roaming != TRUE)
797                 rec = file->home;
798         else
799                 rec = file->roaming;
800
801         if (rec != NULL) {
802                 memcpy(data, &rec->data,
803                         sizeof(struct connman_stats_data));
804         }
805
806         return 0;
807 }
808
809 int __connman_stats_init(void)
810 {
811         DBG("");
812
813         stats_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
814                                                         NULL, stats_free);
815
816         return 0;
817 }
818
819 void __connman_stats_cleanup(void)
820 {
821         DBG("");
822
823         g_hash_table_destroy(stats_hash);
824         stats_hash = NULL;
825 }