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