wifi: Add wifi pointer NULL checks
[framework/connectivity/connman.git] / tools / stats-tool.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 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #define _GNU_SOURCE
26 #include <sys/mman.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/time.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <string.h>
33
34 #include <sys/time.h>
35 #include <time.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <errno.h>
39
40 #include <glib.h>
41 #include <glib/gstdio.h>
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 struct connman_stats_data {
52         unsigned int rx_packets;
53         unsigned int tx_packets;
54         unsigned int rx_bytes;
55         unsigned int tx_bytes;
56         unsigned int rx_errors;
57         unsigned int tx_errors;
58         unsigned int rx_dropped;
59         unsigned int tx_dropped;
60         unsigned int time;
61 };
62
63 struct stats_file_header {
64         unsigned int magic;
65         unsigned int begin;
66         unsigned int end;
67         unsigned int home;
68         unsigned int roaming;
69 };
70
71 struct stats_record {
72         time_t ts;
73         unsigned int roaming;
74         struct connman_stats_data data;
75 };
76
77 struct stats_file {
78         int fd;
79         char *name;
80         char *addr;
81         size_t len;
82         size_t max_len;
83
84         /* cached values */
85         int max_nr;
86         int nr;
87         struct stats_record *first;
88         struct stats_record *last;
89         struct stats_record *home_first;
90         struct stats_record *roaming_first;
91 };
92
93 struct stats_iter {
94         struct stats_file *file;
95         struct stats_record *begin;
96         struct stats_record *end;
97         struct stats_record *it;
98 };
99
100 static gint option_create = 0;
101 static gint option_interval = 3;
102 static gboolean option_dump = FALSE;
103 static gboolean option_summary = FALSE;
104 static char *option_info_file_name = NULL;
105 static time_t option_start_ts = -1;
106 static char *option_last_file_name = NULL;
107
108 static gboolean parse_start_ts(const char *key, const char *value,
109                                         gpointer user_data, GError **error)
110 {
111         GTimeVal time_val;
112
113         if (g_time_val_from_iso8601(value, &time_val) == FALSE)
114                 return FALSE;
115
116         option_start_ts = time_val.tv_sec;
117
118         return TRUE;
119 }
120
121 static GOptionEntry options[] = {
122         { "create", 'c', 0, G_OPTION_ARG_INT, &option_create,
123                         "Create a .data file with NR faked entries", "NR" },
124         { "interval", 'i', 0, G_OPTION_ARG_INT, &option_interval,
125                         "Interval in seconds (used with create)", "INTERVAL" },
126         { "dump", 'd', 0, G_OPTION_ARG_NONE, &option_dump,
127                         "Dump contents of .data file" },
128         { "summary", 's', 0, G_OPTION_ARG_NONE, &option_summary,
129                         "Summary of .data file" },
130         { "info", 'f', 0, G_OPTION_ARG_FILENAME, &option_info_file_name,
131                         ".info file name" },
132         { "startts", 't', 0, G_OPTION_ARG_CALLBACK, parse_start_ts,
133                         "Set start time for creating .data file "
134                         "(example 2010-11-05T23:00:12Z)", "TS"},
135         { "last", 'l', 0, G_OPTION_ARG_FILENAME, &option_last_file_name,
136                           "Start values from last .data file" },
137         { NULL },
138 };
139
140 static struct stats_file_header *get_hdr(struct stats_file *file)
141 {
142         return (struct stats_file_header *)file->addr;
143 }
144
145 static struct stats_record *get_begin(struct stats_file *file)
146 {
147         unsigned int off = get_hdr(file)->begin;
148
149         return (struct stats_record *)(file->addr + off);
150 }
151
152 static struct stats_record *get_end(struct stats_file *file)
153 {
154         unsigned int off = get_hdr(file)->end;
155
156         return (struct stats_record *)(file->addr + off);
157 }
158
159 static struct stats_record *get_home(struct stats_file *file)
160 {
161         struct stats_file_header *hdr;
162
163         hdr = get_hdr(file);
164
165         if (hdr->home == UINT_MAX)
166                 return NULL;
167
168         return (struct stats_record *)(file->addr + hdr->home);
169 }
170
171 static struct stats_record *get_roaming(struct stats_file *file)
172 {
173         struct stats_file_header *hdr;
174
175         hdr = get_hdr(file);
176
177         if (hdr->roaming == UINT_MAX)
178                 return NULL;
179
180         return (struct stats_record *)(file->addr + hdr->roaming);
181 }
182
183 static void set_end(struct stats_file *file, struct stats_record *end)
184 {
185         struct stats_file_header *hdr;
186
187         hdr = get_hdr(file);
188         hdr->end = (char *)end - file->addr;
189 }
190
191 static int get_index(struct stats_file *file, struct stats_record *rec)
192 {
193         return rec - file->first;
194 }
195
196 static struct stats_record *get_next(struct stats_file *file,
197                                         struct stats_record *cur)
198 {
199         cur++;
200
201         if (cur > file->last)
202                 cur = file->first;
203
204         return cur;
205 }
206
207 static struct stats_record *get_iterator_begin(struct stats_file *file)
208 {
209         return get_next(file, get_begin(file));
210 }
211
212 static struct stats_record *get_iterator_end(struct stats_file *file)
213 {
214         return get_next(file, get_end(file));
215 }
216
217 static void stats_print_record(struct stats_record *rec)
218 {
219         char buffer[30];
220
221         strftime(buffer, 30, "%d-%m-%Y %T", localtime(&rec->ts));
222         printf("%p %lld %s %01d %d %d %d %d %d %d %d %d %d\n",
223                 rec, (long long int)rec->ts, buffer,
224                 rec->roaming,
225                 rec->data.rx_packets,
226                 rec->data.tx_packets,
227                 rec->data.rx_bytes,
228                 rec->data.tx_bytes,
229                 rec->data.rx_errors,
230                 rec->data.tx_errors,
231                 rec->data.rx_dropped,
232                 rec->data.tx_dropped,
233                 rec->data.time);
234 }
235
236 static void stats_hdr_info(struct stats_file *file)
237 {
238         struct stats_file_header *hdr;
239         struct stats_record *begin, *end, *home, *roaming;
240         unsigned int home_idx, roaming_idx;
241
242         hdr = get_hdr(file);
243         begin = get_begin(file);
244         end = get_end(file);
245
246         home = get_home(file);
247         if (home == NULL)
248                 home_idx = UINT_MAX;
249         else
250                 home_idx = get_index(file, home);
251
252         roaming = get_roaming(file);
253         if (roaming == NULL)
254                 roaming_idx = UINT_MAX;
255         else
256                 roaming_idx = get_index(file, roaming);
257
258         printf("Data Structure Sizes\n");
259         printf("  sizeof header   %zd/0x%02zx\n",
260                 sizeof(struct stats_file_header),
261                 sizeof(struct stats_file_header));
262         printf("  sizeof entry    %zd/0%02zx\n\n",
263                 sizeof(struct stats_record),
264                 sizeof(struct stats_record));
265
266         printf("File\n");
267         printf("  addr            %p\n",  file->addr);
268         printf("  len             %zd\n", file->len);
269
270         printf("  max nr entries  %d\n", file->max_nr);
271         printf("  nr entries      %d\n\n", file->nr);
272
273         printf("Header\n");
274         printf("  magic           0x%08x\n", hdr->magic);
275         printf("  begin           [%d] 0x%08x\n",
276                 get_index(file, begin), hdr->begin);
277         printf("  end             [%d] 0x%08x\n",
278                 get_index(file, end), hdr->end);
279         printf("  home            [%d] 0x%08x\n",
280                 home_idx, hdr->home);
281         printf("  roaming         [%d] 0x%08x\n\n",
282                 roaming_idx, hdr->roaming);
283
284
285         printf("Pointers\n");
286         printf("  hdr             %p\n", hdr);
287         printf("  begin           %p\n", begin);
288         printf("  end             %p\n", end);
289         printf("  home            %p\n", home);
290         printf("  romaing         %p\n", roaming);
291         printf("  first           %p\n", file->first);
292         printf("  last            %p\n\n", file->last);
293 }
294
295 static void stats_print_entries(struct stats_file *file)
296 {
297         struct stats_record *it;
298         int i;
299
300         printf("[ idx] ptr ts ts rx_packets tx_packets rx_bytes "
301                 "tx_bytes rx_errors tx_errors rx_dropped tx_dropped time\n\n");
302
303         for (i = 0, it = file->first; it <= file->last; it++, i++) {
304                 printf("[%04d] ", i);
305                 stats_print_record(it);
306         }
307 }
308
309 static void stats_print_rec_diff(struct stats_record *begin,
310                                         struct stats_record *end)
311 {
312         printf("\trx_packets: %d\n",
313                 end->data.rx_packets - begin->data.rx_packets);
314         printf("\ttx_packets: %d\n",
315                 end->data.tx_packets - begin->data.tx_packets);
316         printf("\trx_bytes:   %d\n",
317                 end->data.rx_bytes - begin->data.rx_bytes);
318         printf("\ttx_bytes:   %d\n",
319                 end->data.tx_bytes - begin->data.tx_bytes);
320         printf("\trx_errors:  %d\n",
321                 end->data.rx_errors - begin->data.rx_errors);
322         printf("\ttx_errors:  %d\n",
323                 end->data.tx_errors - begin->data.tx_errors);
324         printf("\trx_dropped: %d\n",
325                 end->data.rx_dropped - begin->data.rx_dropped);
326         printf("\ttx_dropped: %d\n",
327                 end->data.tx_dropped - begin->data.tx_dropped);
328         printf("\ttime:       %d\n",
329                 end->data.time - begin->data.time);
330 }
331
332 static void stats_print_diff(struct stats_file *file)
333 {
334         struct stats_record *begin, *end;
335
336         begin = get_begin(file);
337         begin = get_next(file, begin);
338         end = get_end(file);
339
340         printf("\n(begin + 1)\n");
341         printf("\t[%04d] ", get_index(file, begin));
342         stats_print_record(begin);
343         printf("end\n");
344         printf("\t[%04d] ", get_index(file, end));
345         stats_print_record(end);
346
347         if (file->home_first != NULL && get_home(file) != NULL) {
348                 printf("\nhome\n");
349                 stats_print_rec_diff(file->home_first, get_home(file));
350         }
351
352         if (file->roaming_first != NULL && get_roaming(file) != NULL) {
353                 printf("\roaming\n");
354                 stats_print_rec_diff(file->roaming_first, get_roaming(file));
355         }
356 }
357
358 static void update_max_nr_entries(struct stats_file *file)
359 {
360         file->max_nr = (file->len - sizeof(struct stats_file_header)) /
361                 sizeof(struct stats_record);
362 }
363
364 static void update_nr_entries(struct stats_file *file)
365 {
366         struct stats_record *begin, *end;
367         int nr;
368
369         begin = get_begin(file);
370         end = get_end(file);
371
372         nr = get_index(file, end) - get_index(file, begin);
373
374         if (nr < 0)
375                 nr += file->max_nr;
376
377         file->nr = nr;
378 }
379
380 static void update_first(struct stats_file *file)
381 {
382         file->first = (struct stats_record *)(file->addr +
383                                         sizeof(struct stats_file_header));
384 }
385
386 static void update_last(struct stats_file *file)
387 {
388         struct stats_record *last;
389
390         last = file->first;
391         last += file->max_nr - 1;
392
393         file->last = last;
394 }
395
396 static int stats_file_update_cache(struct stats_file *file)
397 {
398         struct stats_record *it, *end;
399
400         update_max_nr_entries(file);
401         update_nr_entries(file);
402         update_first(file);
403         update_last(file);
404         file->home_first = NULL;
405         file->roaming_first = NULL;
406
407         end = get_iterator_end(file);
408         for (it = get_iterator_begin(file);
409                         it != end;
410                         it = get_next(file, it)) {
411
412                 if (file->home_first == NULL && it->roaming == 0)
413                         file->home_first = it;
414
415                 if (file->roaming_first == NULL && it->roaming == 1)
416                         file->roaming_first = it;
417
418                 if (file->home_first != NULL && file->roaming_first != NULL)
419                         break;
420         }
421
422         return 0;
423 }
424
425 static int stats_file_remap(struct stats_file *file, size_t size)
426 {
427         size_t page_size, new_size;
428         void *addr;
429         int err;
430
431         page_size = sysconf(_SC_PAGESIZE);
432         new_size = (size + page_size - 1) & ~(page_size - 1);
433
434         err = ftruncate(file->fd, new_size);
435         if (err < 0) {
436                 fprintf(stderr, "ftrunctate error %s for %s",
437                                 strerror(errno), file->name);
438                 return -errno;
439         }
440
441         if (file->addr == NULL) {
442                 addr = mmap(NULL, new_size, PROT_READ | PROT_WRITE,
443                                 MAP_SHARED, file->fd, 0);
444         } else {
445                 addr = mremap(file->addr, file->len, new_size, MREMAP_MAYMOVE);
446         }
447
448         if (addr == MAP_FAILED) {
449                 fprintf(stderr, "mmap error %s for %s\n",
450                         strerror(errno), file->name);
451                 return -errno;
452         }
453
454         file->addr = addr;
455         file->len = new_size;
456         file->max_len = new_size;
457
458         return 0;
459 }
460
461 static int stats_open(struct stats_file *file, const char *name)
462 {
463         struct stats_file_header *hdr;
464         struct stat tm;
465         int err;
466         size_t size = 0;
467
468         bzero(file, sizeof(struct stats_file));
469
470         if (name != NULL) {
471                 file->name = g_strdup(name);
472
473                 file->fd = TFR(open(file->name,
474                                         O_RDWR | O_CREAT | O_CLOEXEC, 0644));
475                 if (file->fd == -1) {
476                         fprintf(stderr, "open error %s for %s\n",
477                                 strerror(errno), file->name);
478                         return -errno;
479                 }
480
481                 err = fstat(file->fd, &tm);
482                 if (err < 0) {
483                         fprintf(stderr, "fstat error %s for %s\n",
484                                 strerror(errno), file->name);
485                         return err;
486                 }
487
488                 size = (size_t)tm.st_size;
489         } else {
490                 file->name = g_strdup("stats.XXXXXX.tmp");
491                 file->fd = g_mkstemp_full(file->name, O_RDWR | O_CREAT, 0644);
492                 if (file->fd == -1) {
493                         fprintf(stderr, "creating tmp failed\n");
494                         return -1;
495                 }
496         }
497
498         if (size == 0)
499                 size = sysconf(_SC_PAGESIZE);
500
501         err = stats_file_remap(file, size);
502         if (err < 0) {
503                 fprintf(stderr, "remap failed\n");
504                 return err;
505         }
506
507         /* Initialize new file */
508         hdr = get_hdr(file);
509         if (hdr->magic != MAGIC ||
510                         hdr->begin < sizeof(struct stats_file_header) ||
511                         hdr->end < sizeof(struct stats_file_header) ||
512                         hdr->home < sizeof(struct stats_file_header) ||
513                         hdr->roaming < sizeof(struct stats_file_header) ||
514                         hdr->begin > file->len ||
515                         hdr->end > file->len) {
516                 hdr->magic = MAGIC;
517                 hdr->begin = sizeof(struct stats_file_header);
518                 hdr->end = sizeof(struct stats_file_header);
519                 hdr->home = UINT_MAX;
520                 hdr->roaming = UINT_MAX;
521
522         }
523         stats_file_update_cache(file);
524
525         return 0;
526 }
527
528 static void stats_close(struct stats_file *file)
529 {
530         munmap(file->addr, file->len);
531         close(file->fd);
532         g_free(file->name);
533 }
534
535 static int stats_create(struct stats_file *file, unsigned int nr,
536                         unsigned int interval, time_t start_ts,
537                         struct stats_record *start)
538 {
539         unsigned int i;
540         int err;
541         struct stats_record *cur, *next;
542         struct stats_file_header *hdr;
543         unsigned int pkt;
544         unsigned int step_ts;
545         unsigned int roaming = FALSE;
546
547         hdr = get_hdr(file);
548
549         hdr->magic = MAGIC;
550         hdr->begin = sizeof(struct stats_file_header);
551         hdr->end = sizeof(struct stats_file_header);
552         hdr->home = UINT_MAX;
553         hdr->roaming = UINT_MAX;
554
555         stats_file_update_cache(file);
556
557         if (start != NULL) {
558                 struct stats_record *rec;
559
560                 rec = get_end(file);
561                 memcpy(rec, start, sizeof(struct stats_record));
562         } else {
563                 get_end(file)->ts = start_ts;
564         }
565
566         for (i = 0; i < nr; i++) {
567                 if (file->last == get_end(file)) {
568                         err = stats_file_remap(file, file->len +
569                                                 sysconf(_SC_PAGESIZE));
570                         if (err < 0)
571                                 return err;
572
573                         stats_file_update_cache(file);
574                 }
575                 cur = get_end(file);
576                 next = get_next(file, cur);
577
578                 step_ts = (rand() % interval);
579                 if (step_ts == 0)
580                         step_ts = 1;
581
582                 next->ts = cur->ts + step_ts;
583                 next->roaming = roaming;
584                 next->data.time = cur->data.time + step_ts;
585
586                 next->data.rx_packets = cur->data.rx_packets;
587                 next->data.rx_bytes = cur->data.rx_bytes;
588
589                 if (rand() % 3 == 0) {
590                         pkt = rand() % 5;
591                         next->data.rx_packets += pkt;
592                         next->data.rx_bytes += pkt * (rand() % 1500);
593                 }
594
595                 next->data.tx_packets = cur->data.tx_packets;
596                 next->data.tx_bytes = cur->data.tx_bytes;
597
598                 if (rand() % 3 == 0) {
599                         pkt = rand() % 5;
600                         next->data.tx_packets += pkt;
601                         next->data.tx_bytes += pkt * (rand() % 1500);
602                 }
603
604                 set_end(file, next);
605
606                 if ((rand() % 50) == 0)
607                         roaming = roaming == TRUE? FALSE : TRUE;
608
609         }
610
611         return 0;
612 }
613
614 static struct stats_record *get_next_record(struct stats_iter *iter)
615 {
616         if (iter->it != iter->end) {
617                 struct stats_record *tmp;
618
619                 tmp = iter->it;
620                 iter->it = get_next(iter->file, iter->it);
621
622                 return tmp;
623         }
624
625         return NULL;
626 }
627
628 static int append_record(struct stats_file *file,
629                                 struct stats_record *rec)
630 {
631         struct stats_record *cur, *next;
632         int err;
633
634         if (file->last == get_end(file)) {
635                 err = stats_file_remap(file, file->len +
636                                         sysconf(_SC_PAGESIZE));
637                 if (err < 0)
638                         return err;
639
640                 stats_file_update_cache(file);
641         }
642
643         cur = get_end(file);
644         next = get_next(file, cur);
645
646         memcpy(next, rec, sizeof(struct stats_record));
647
648         set_end(file, next);
649
650         return 0;
651 }
652
653 static struct stats_record *process_file(struct stats_iter *iter,
654                                         struct stats_file *temp_file,
655                                         struct stats_record *cur,
656                                         GDate *date_change_step_size,
657                                         int account_period_offset)
658 {
659         struct stats_record *home, *roaming;
660         struct stats_record *next;
661
662         home = NULL;
663         roaming = NULL;
664
665         if (cur == NULL)
666                 cur = get_next_record(iter);
667         next = get_next_record(iter);
668
669         while (next != NULL) {
670                 GDate date_cur;
671                 GDate date_next;
672                 int append;
673
674                 append = FALSE;
675
676                 if (cur->roaming == TRUE)
677                         roaming = cur;
678                 else
679                         home = cur;
680
681                 g_date_set_time_t(&date_cur, cur->ts);
682                 g_date_set_time_t(&date_next, next->ts);
683
684                 if (g_date_compare(&date_cur, date_change_step_size) < 0) {
685                         /* month period size */
686                         GDateDay day_cur, day_next;
687                         GDateMonth month_cur, month_next;
688
689                         month_cur = g_date_get_month(&date_cur);
690                         month_next = g_date_get_month(&date_next);
691
692                         day_cur = g_date_get_day(&date_cur);
693                         day_next = g_date_get_day(&date_next);
694
695                         if (day_cur == day_next && month_cur != month_next)
696                                 append = TRUE;
697                         else if (day_cur < account_period_offset && day_next >= account_period_offset)
698                                 append = TRUE;
699                 } else {
700                         /* day period size */
701                         if (g_date_days_between(&date_cur, &date_next) > 0)
702                                 append = TRUE;
703                 }
704
705                 if (append == TRUE) {
706                         if (home != NULL) {
707                                 append_record(temp_file, home);
708                                 home = NULL;
709                         }
710
711                         if (roaming != NULL) {
712                                 append_record(temp_file, roaming);
713                                 roaming = NULL;
714                         }
715                 }
716
717                 cur = next;
718                 next = get_next_record(iter);
719         }
720
721         return cur;
722 }
723
724 static int summarize(struct stats_file *data_file,
725                         struct stats_file *history_file,
726                         struct stats_file *temp_file,
727                         int account_period_offset)
728 {
729         struct stats_iter data_iter;
730         struct stats_iter history_iter;
731         struct stats_record *cur, *next;
732
733         GDate today, date_change_step_size;
734
735         /*
736          * First calculate the date when switch from monthly
737          * accounting period size to daily size
738          */
739         g_date_set_time_t(&today, time(NULL));
740
741         date_change_step_size = today;
742         if (g_date_get_day(&today) - account_period_offset >= 0)
743                 g_date_subtract_months(&date_change_step_size, 2);
744         else
745                 g_date_subtract_months(&date_change_step_size, 3);
746
747         g_date_set_day(&date_change_step_size, account_period_offset);
748
749
750         /* Now process history file */
751         cur = NULL;
752
753         if (history_file != NULL) {
754                 history_iter.file = history_file;
755                 history_iter.begin = get_iterator_begin(history_iter.file);
756                 history_iter.end = get_iterator_end(history_iter.file);
757                 history_iter.it = history_iter.begin;
758
759                 cur = process_file(&history_iter, temp_file, NULL,
760                                         &date_change_step_size, account_period_offset);
761         }
762
763         data_iter.file = data_file;
764         data_iter.begin = get_iterator_begin(data_iter.file);
765         data_iter.end = get_iterator_end(data_iter.file);
766         data_iter.it = data_iter.begin;
767
768         /*
769          * Ensure date_file records are newer than the history_file
770          * record
771          */
772         if (cur != NULL) {
773                 next = get_next_record(&data_iter);
774                 while(next != NULL && cur->ts > next->ts)
775                         next = get_next_record(&data_iter);
776         }
777
778         /* And finally process the new data records */
779         cur = process_file(&data_iter, temp_file, cur,
780                                 &date_change_step_size, account_period_offset);
781
782         if (cur != NULL)
783                 append_record(temp_file, cur);
784
785         return 0;
786 }
787
788 static void swap_and_close_files(struct stats_file *history_file,
789                         struct stats_file *temp_file)
790 {
791         munmap(history_file->addr, history_file->len);
792         munmap(temp_file->addr, temp_file->len);
793
794         TFR(close(temp_file->fd));
795
796         unlink(history_file->name);
797
798         if (link(temp_file->name, history_file->name) < 0)
799                 return;
800
801         unlink(temp_file->name);
802         TFR(close(history_file->fd));
803 }
804
805 static void history_file_update(struct stats_file *data_file,
806                                 const char *history_file_name)
807 {
808         struct stats_file _history_file;
809         struct stats_file tempory_file;
810
811         struct stats_file *history_file = NULL;
812
813         if (stats_open(&_history_file, history_file_name) == 0)
814                 history_file = &_history_file;
815
816         if (stats_open(&tempory_file, NULL) < 0) {
817                 if (history_file != NULL)
818                         stats_close(history_file);
819                 return;
820         }
821
822         summarize(data_file, history_file, &tempory_file, 13);
823
824         swap_and_close_files(history_file, &tempory_file);
825 }
826
827 int main(int argc, char *argv[])
828 {
829         GOptionContext *context;
830         GError *error = NULL;
831
832         struct stats_file_header *hdr;
833         struct stats_file data, *data_file;
834         struct stats_record *rec;
835         time_t start_ts;
836         int err;
837
838         rec = NULL;
839
840         data_file = &data;
841
842         putenv("TZ=GMT0");
843
844         context = g_option_context_new(NULL);
845         g_option_context_add_main_entries(context, options, NULL);
846
847         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
848                 if (error != NULL) {
849                         g_printerr("%s\n", error->message);
850                         g_error_free(error);
851                 } else
852                         g_printerr("An unknown error occurred\n");
853                 exit(1);
854         }
855
856         g_option_context_free(context);
857
858         if (argc < 2) {
859                 printf("Usage: %s [FILENAME]\n", argv[0]);
860                 exit(0);
861         }
862
863         err = stats_open(data_file, argv[1]);
864         if (err < 0) {
865                 fprintf(stderr, "failed open file %s\n", argv[1]);
866                 exit(1);
867         }
868
869         if (option_last_file_name != NULL) {
870                 struct stats_file last;
871                 if (stats_open(&last, option_last_file_name) < 0) {
872                         fprintf(stderr, "failed open file %s\n",
873                                 option_last_file_name);
874                         exit(1);
875                 }
876
877                 rec = get_end(&last);
878         }
879
880         if (option_start_ts == -1)
881                 start_ts = time(NULL);
882         else
883                 start_ts = option_start_ts;
884
885         if (option_create > 0)
886                 stats_create(data_file, option_create, option_interval, start_ts, rec);
887
888         hdr = get_hdr(data_file);
889         if (hdr->magic != MAGIC) {
890                 fprintf(stderr, "header file magic test failed\n");
891                 goto err;
892         }
893
894         stats_file_update_cache(data_file);
895
896         stats_hdr_info(data_file);
897
898         if (option_dump == TRUE)
899                 stats_print_entries(data_file);
900
901         if (option_summary == TRUE)
902                 stats_print_diff(data_file);
903
904         if (option_info_file_name != NULL)
905                 history_file_update(data_file, option_info_file_name);
906
907 err:
908         stats_close(data_file);
909
910         return 0;
911 }