iptables-test: Fix builtin chain rule addition
[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 #define MAGIC 0xFA00B916
39
40 /*
41  * Statistics counters are stored into a ring buffer which is stored
42  * into a file
43  *
44  * File properties:
45  *   The ring buffer is mmap to a file
46  *   Initialy only the smallest possible amount of disk space is allocated
47  *   The files grow to the configured maximal size
48  *   The grows by _SC_PAGESIZE step size
49  *   For each service a file is created
50  *   Each file has a header where the indexes are stored
51  *
52  * Entries properties:
53  *   Each entry has a timestamp
54  *   A flag to mark if the entry is either home (0) or roaming (1) entry
55  *   The entries are fixed sized (stats_record)
56  *
57  * Ring buffer properties:
58  *   There are to indexes 'begin', 'end', 'home' and 'roaming'
59  *   'begin' points to the oldest entry
60  *   'end' points to the newest/current entry
61  *   'home' points to the current home entry
62  *   'roaming' points to the current roaming entry
63  *   If 'begin' == 'end' then the buffer is empty
64  *   If 'end' + 1 == 'begin then it's full
65  *   The ring buffer is valid in the range (begin, end]
66  *   If 'home' has the value UINT_MAX', 'home' is invalid
67  *   if 'roaming' has the value UINT_MAX', 'roaming' is invalid
68  *   'first' points to the first entry in the ring buffer
69  *   'last' points to the last entry in the ring buffer
70  */
71
72 struct stats_file_header {
73         unsigned int magic;
74         unsigned int begin;
75         unsigned int end;
76         unsigned int home;
77         unsigned int roaming;
78 };
79
80 struct stats_record {
81         time_t ts;
82         unsigned int roaming;
83         struct connman_stats_data data;
84 };
85
86 struct stats_file {
87         int fd;
88         char *name;
89         char *addr;
90         size_t len;
91         size_t max_len;
92
93         /* cached values */
94         struct stats_record *first;
95         struct stats_record *last;
96         struct stats_record *home;
97         struct stats_record *roaming;
98 };
99
100 GHashTable *stats_hash = NULL;
101
102 static struct stats_file_header *get_hdr(struct stats_file *file)
103 {
104         return (struct stats_file_header *)file->addr;
105 }
106
107 static struct stats_record *get_begin(struct stats_file *file)
108 {
109         unsigned int off = get_hdr(file)->begin;
110
111         return (struct stats_record *)(file->addr + off);
112 }
113
114 static struct stats_record *get_end(struct stats_file *file)
115 {
116         unsigned int off = get_hdr(file)->end;
117
118         return (struct stats_record *)(file->addr + off);
119 }
120
121 static struct stats_record *get_home(struct stats_file *file)
122 {
123         struct stats_file_header *hdr;
124
125         hdr = get_hdr(file);
126
127         if (hdr->home == UINT_MAX)
128                 return NULL;
129
130         return (struct stats_record *)(file->addr + hdr->home);
131 }
132
133 static struct stats_record *get_roaming(struct stats_file *file)
134 {
135         struct stats_file_header *hdr;
136
137         hdr = get_hdr(file);
138
139         if (hdr->roaming == UINT_MAX)
140                 return NULL;
141
142         return (struct stats_record *)(file->addr + hdr->roaming);
143 }
144
145 static void set_begin(struct stats_file *file, struct stats_record *begin)
146 {
147         struct stats_file_header *hdr;
148
149         hdr = get_hdr(file);
150         hdr->begin = (char *)begin - file->addr;
151 }
152
153 static void set_end(struct stats_file *file, struct stats_record *end)
154 {
155         struct stats_file_header *hdr;
156
157         hdr = get_hdr(file);
158         hdr->end = (char *)end - file->addr;
159 }
160
161 static void set_home(struct stats_file *file, struct stats_record *home)
162 {
163         struct stats_file_header *hdr;
164
165         hdr = get_hdr(file);
166         hdr->home = (char *)home - file->addr;
167 }
168
169 static void set_roaming(struct stats_file *file, struct stats_record *roaming)
170 {
171         struct stats_file_header *hdr;
172
173         hdr = get_hdr(file);
174         hdr->roaming = (char *)roaming - file->addr;
175 }
176
177 static struct stats_record *get_next(struct stats_file *file,
178                                         struct stats_record *cur)
179 {
180         cur++;
181
182         if (cur > file->last)
183                 cur = file->first;
184
185         return cur;
186 }
187
188 static void stats_free(gpointer user_data)
189 {
190         struct stats_file *file = user_data;
191
192         msync(file->addr, file->len, MS_SYNC);
193
194         munmap(file->addr, file->len);
195         file->addr = NULL;
196
197         close(file->fd);
198         file->fd = -1;
199
200         g_free(file->name);
201         g_free(file);
202 }
203
204 static void update_first(struct stats_file *file)
205 {
206         file->first = (struct stats_record *)
207                         (file->addr + sizeof(struct stats_file_header));
208 }
209
210 static void update_last(struct stats_file *file)
211 {
212         unsigned int max_entries;
213
214         max_entries = (file->len - sizeof(struct stats_file_header)) /
215                         sizeof(struct stats_record);
216         file->last = file->first + max_entries - 1;
217 }
218
219 static void update_home(struct stats_file *file)
220 {
221         file->home = get_home(file);
222 }
223
224 static void update_roaming(struct stats_file *file)
225 {
226         file->roaming = get_roaming(file);
227 }
228
229 static void stats_file_update_cache(struct stats_file *file)
230 {
231         update_first(file);
232         update_last(file);
233         update_home(file);
234         update_roaming(file);
235 }
236
237 static int stats_file_remap(struct stats_file *file, size_t size)
238 {
239         size_t page_size, new_size;
240         void *addr;
241         int err;
242
243         page_size = sysconf(_SC_PAGESIZE);
244         new_size = (size + page_size - 1) & ~(page_size - 1);
245
246         err = ftruncate(file->fd, new_size);
247         if (err < 0) {
248                 connman_error("ftrunctate error %s for %s",
249                                 strerror(errno), file->name);
250                 return -errno;
251         }
252
253         if (file->addr == NULL) {
254                 addr = mmap(NULL, new_size, PROT_READ | PROT_WRITE,
255                                 MAP_SHARED, file->fd, 0);
256         } else {
257                 addr = mremap(file->addr, file->len, new_size, MREMAP_MAYMOVE);
258         }
259
260         if (addr == MAP_FAILED) {
261                 connman_error("mmap error %s for %s",
262                                 strerror(errno), file->name);
263                 return -errno;
264         }
265
266         file->addr = addr;
267         file->len = new_size;
268
269         stats_file_update_cache(file);
270
271         return 0;
272 }
273
274 static int stats_open(struct connman_service *service,
275                         struct stats_file *file)
276 {
277         struct stat st;
278         int err;
279         size_t size;
280         struct stats_file_header *hdr;
281         connman_bool_t new_file = FALSE;
282
283         file->name = g_strdup_printf("%s/stats/%s.data", STORAGEDIR,
284                         __connman_service_get_ident(service));
285
286         err = stat(file->name, &st);
287         if (err < 0) {
288                 /* according documentation the only possible error is ENOENT */
289                 st.st_size = 0;
290                 new_file = TRUE;
291         }
292
293         file->fd = open(file->name, O_RDWR | O_CREAT, 0644);
294
295         if (file->fd < 0) {
296                 connman_error("open error %s for %s",
297                                 strerror(errno), file->name);
298                 return -errno;
299         }
300
301         file->max_len = STATS_MAX_FILE_SIZE;
302
303         if (st.st_size < sysconf(_SC_PAGESIZE))
304                 size = sysconf(_SC_PAGESIZE);
305         else
306                 size = st.st_size;
307
308         err = stats_file_remap(file, size);
309         if (err < 0)
310                 return err;
311
312         hdr = get_hdr(file);
313
314         if (hdr->magic != MAGIC ||
315                         hdr->begin < sizeof(struct stats_file_header) ||
316                         hdr->end < sizeof(struct stats_file_header) ||
317                         hdr->home < sizeof(struct stats_file_header) ||
318                         hdr->roaming < sizeof(struct stats_file_header) ||
319                         hdr->begin > file->len ||
320                         hdr->end > file->len) {
321                 if (new_file == FALSE) {
322                         /*
323                          * A newly created file can't have a correct
324                          * header so we only warn if the file already
325                          * existed and doesn't have a proper
326                          * header.
327                          */
328                         connman_warn("invalid file header for %s", file->name);
329                 }
330
331                 hdr->magic = MAGIC;
332                 hdr->begin = sizeof(struct stats_file_header);
333                 hdr->end = sizeof(struct stats_file_header);
334                 hdr->home = UINT_MAX;
335                 hdr->roaming = UINT_MAX;
336
337                 stats_file_update_cache(file);
338         }
339
340         return 0;
341 }
342
343 int __connman_stats_service_register(struct connman_service *service)
344 {
345         struct stats_file *file;
346         int err;
347
348         DBG("service %p", service);
349
350         file = g_hash_table_lookup(stats_hash, service);
351         if (file == NULL) {
352                 file = g_try_new0(struct stats_file, 1);
353                 if (file == NULL)
354                         return -ENOMEM;
355
356                 g_hash_table_insert(stats_hash, service, file);
357         } else {
358                 return -EALREADY;
359         }
360
361         err = stats_open(service, file);
362         if (err < 0)
363                 g_hash_table_remove(stats_hash, service);
364
365         return err;
366 }
367
368 void __connman_stats_service_unregister(struct connman_service *service)
369 {
370         DBG("service %p", service);
371
372         g_hash_table_remove(stats_hash, service);
373 }
374
375 int  __connman_stats_update(struct connman_service *service,
376                                 connman_bool_t roaming,
377                                 struct connman_stats_data *data)
378 {
379         struct stats_file *file;
380         struct stats_record *next;
381         int err;
382
383         file = g_hash_table_lookup(stats_hash, service);
384         if (file == NULL)
385                 return -EEXIST;
386
387         if (file->len < file->max_len &&
388                         file->last == get_end(file)) {
389                 DBG("grow file %s", file->name);
390
391                 err = stats_file_remap(file, file->len + sysconf(_SC_PAGESIZE));
392                 if (err < 0)
393                         return err;
394         }
395
396         next = get_next(file, get_end(file));
397
398         if (next == get_begin(file))
399                 set_begin(file, get_next(file, next));
400
401         next->ts = time(NULL);
402         next->roaming = roaming;
403         memcpy(&next->data, data, sizeof(struct connman_stats_data));
404
405         if (roaming != TRUE)
406                 set_home(file, next);
407         else
408                 set_roaming(file, next);
409
410         set_end(file, next);
411
412         return 0;
413 }
414
415 int __connman_stats_get(struct connman_service *service,
416                                 connman_bool_t roaming,
417                                 struct connman_stats_data *data)
418 {
419         struct stats_file *file;
420         struct stats_record *rec;
421
422         file = g_hash_table_lookup(stats_hash, service);
423         if (file == NULL)
424                 return -EEXIST;
425
426         if (roaming != TRUE)
427                 rec = file->home;
428         else
429                 rec = file->roaming;
430
431         if (rec != NULL) {
432                 memcpy(data, &rec->data,
433                         sizeof(struct connman_stats_data));
434         }
435
436         return 0;
437 }
438
439 int __connman_stats_init(void)
440 {
441         DBG("");
442
443         stats_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
444                                                         NULL, stats_free);
445
446         return 0;
447 }
448
449 void __connman_stats_cleanup(void)
450 {
451         DBG("");
452
453         g_hash_table_destroy(stats_hash);
454         stats_hash = NULL;
455 }