summaryrefslogtreecommitdiffstats
path: root/tools/ck-history.c
blob: 716c3ef8107ea08e86c3f8907663c8e0ccfb6463 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <string.h>
#include <errno.h>

#include <locale.h>
#include <zlib.h>

#include <glib.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>

#include "ck-log-event.h"

typedef enum {
        REPORT_TYPE_SUMMARY = 0,
        REPORT_TYPE_LAST,
        REPORT_TYPE_LAST_COMPAT,
        REPORT_TYPE_FREQUENT,
        REPORT_TYPE_LOG,
} ReportType;

/* same record types as sysvinit last */
typedef enum {
        RECORD_STATUS_CRASH = 1,  /* No logout record, system boot in between */
        RECORD_STATUS_DOWN,       /* System brought down in decent way */
        RECORD_STATUS_NORMAL,     /* Normal */
        RECORD_STATUS_NOW,        /* Still logged in */
        RECORD_STATUS_REBOOT,     /* Reboot record. */
        RECORD_STATUS_PHANTOM,    /* No logout record but session is stale. */
        RECORD_STATUS_TIMECHANGE, /* NEW_TIME or OLD_TIME */
} RecordStatus;

#define DEFAULT_LOG_FILENAME LOCALSTATEDIR "/log/ConsoleKit/history"
#define MAX_LINE_LEN 2048

static GList *all_events = NULL;

static gboolean
process_event_line (const char *line)
{
        GString    *str;
        CkLogEvent *event;

        str = g_string_new (line);
        event = ck_log_event_new_from_string (str);
        g_string_free (str, TRUE);

        if (event != NULL) {
                all_events = g_list_prepend (all_events, event);
        }

        return TRUE;
}

static gboolean
process_log_gzstream (gzFile *fstream)
{
        char line[MAX_LINE_LEN];

        while (gzgets (fstream, line, sizeof (line)) != Z_NULL) {
                if (strlen (line) == sizeof (line) - 1) {
                        g_warning ("Log line truncated");
                }

                process_event_line (line);
        }

        return TRUE;
}

static gboolean
process_log_stream (FILE *fstream)
{
        char line[MAX_LINE_LEN];

        while (fgets (line, sizeof (line), fstream) != NULL) {
                if (strlen (line) == sizeof (line) - 1) {
                        g_warning ("Log line truncated");
                }

                process_event_line (line);
        }

        return TRUE;
}

static gboolean
process_log_file (const char *filename)
{
        gboolean ret;

        if (g_str_has_suffix (filename, ".gz")) {
                gzFile *f;
                f = gzopen (filename, "r");
                if (f == NULL) {
                        int         errnum;
                        const char *errmsg;
                        errmsg = gzerror (f, &errnum);
                        if (errnum == Z_ERRNO) {
                                errmsg = g_strerror (errno);
                        }
                        g_warning ("Error opening %s (%s)\n",
                                   filename,
                                   errmsg);
                        return FALSE;
                }
                ret = process_log_gzstream (f);
                gzclose (f);
        } else {
                FILE    *f;

                f = g_fopen (filename, "r");
                if (f == NULL) {
                        g_warning ("Error opening %s (%s)\n",
                                   filename,
                                   g_strerror (errno));
                        return FALSE;
                }
                ret = process_log_stream (f);
                fclose (f);
        }

        return ret;
}

static GList *
get_log_file_list (void)
{
        int      num;
        GList   *files;

        /* always try the primary file */
        files = NULL;
        files = g_list_prepend (files, g_strdup (DEFAULT_LOG_FILENAME));
        num = 1;
        while (1) {
                char *filename;
                filename = g_strdup_printf ("%s.%d", DEFAULT_LOG_FILENAME, num);
                if (g_access (filename, R_OK) != 0) {
                        char *filename_gz;

                        /* check for .gz */
                        filename_gz = g_strdup_printf ("%s.gz", filename);
                        g_free (filename);

                        if (g_access (filename_gz, R_OK) != 0) {
                                g_free (filename_gz);
                                break;
                        }
                        filename = filename_gz;
                }
                num++;
                files = g_list_prepend (files, filename);
        };

        return files;
}

static gboolean
process_logs (void)
{
        gboolean ret;
        GList   *files;
        GList   *l;

        ret = FALSE;

        files = get_log_file_list ();

        for (l = files; l != NULL; l = l->next) {
                gboolean res;
                char    *filename;

                filename = l->data;

                res = process_log_file (filename);
                if (! res) {
                        goto out;
                }
        }

        ret = TRUE;

 out:
        g_list_foreach (files, (GFunc)g_free, NULL);
        g_list_free (files);

        return ret;
}

static void
generate_report_summary (int         uid,
                         const char *seat,
                         const char *session_type)
{
}

static CkLogEvent *
find_first_matching_remove_event (GList                      *events,
                                  CkLogSeatSessionAddedEvent *event)
{
        CkLogEvent *revent;
        GList      *l;

        revent = NULL;

        for (l = events; l != NULL; l = l->next) {
                CkLogEventType etype;

                etype = ((CkLogEvent *)l->data)->type;
                /* skip all non removal events */
                if (etype != CK_LOG_EVENT_SEAT_SESSION_REMOVED
                    || etype == CK_LOG_EVENT_SYSTEM_START
                    || etype == CK_LOG_EVENT_SYSTEM_STOP
                    || etype == CK_LOG_EVENT_SYSTEM_RESTART) {
                        continue;
                }

                if (etype == CK_LOG_EVENT_SEAT_SESSION_REMOVED) {
                        CkLogSeatSessionRemovedEvent *e;
                        e = l->data;

                        if (e->session_id != NULL
                            && event->session_id != NULL
                            && strcmp (e->session_id, event->session_id) == 0) {
                                revent = (CkLogEvent *)l->data;
                                break;
                        }
                } else {
                        revent = (CkLogEvent *)l->data;
                        break;
                }
        }

        return revent;
}

static char *
get_user_name_for_uid (int  uid)
{
        struct passwd *pwent;
        char          *name;

        name = NULL;

        pwent = getpwuid (uid);

        if (pwent != NULL) {
                name = g_strdup (pwent->pw_name);
        }

        return name;
}

static int
get_uid_for_username (const char *username)
{
        struct passwd *pwent;
        int            uid;

        g_assert (username != NULL);

        uid = -1;

        pwent = getpwnam (username);
        if (pwent != NULL) {
                uid = pwent->pw_uid;
        }

        return uid;
}

static char *
get_utline_for_event (CkLogEvent *event)
{
        char *utline;

        utline = NULL;

        switch (event->type) {
        case CK_LOG_EVENT_SEAT_SESSION_ADDED:
                {
                        CkLogSeatSessionAddedEvent *e;
                        e = (CkLogSeatSessionAddedEvent *)event;
                        if (e->session_x11_display != NULL && e->session_x11_display[0] != '\0') {
                                utline = g_strdup (e->session_x11_display);
                        } else {
                                if (g_str_has_prefix (e->session_display_device, "/dev/")) {
                                        utline = g_strdup (e->session_display_device + 5);
                                } else {
                                        utline = g_strdup (e->session_display_device);
                                }
                        }
                }
                break;
        case CK_LOG_EVENT_SYSTEM_START:
                utline = g_strdup ("system boot");
                break;
        default:
                g_assert_not_reached ();
        }

        return utline;
}

static char *
get_user_name_for_event (CkLogEvent *event)
{
        char *username;

        username = NULL;

        switch (event->type) {
        case CK_LOG_EVENT_SEAT_SESSION_ADDED:
                username = get_user_name_for_uid (((CkLogSeatSessionAddedEvent *)event)->session_unix_user);
                break;
        case CK_LOG_EVENT_SYSTEM_START:
                username = g_strdup ("reboot");
                break;
        default:
                g_assert_not_reached ();
        }

        return username;
}

static char *
get_host_for_event (CkLogEvent *event)
{
        char *username;

        username = NULL;

        switch (event->type) {
        case CK_LOG_EVENT_SEAT_SESSION_ADDED:
                username = g_strdup (((CkLogSeatSessionAddedEvent *)event)->session_remote_host_name);
                break;
        case CK_LOG_EVENT_SYSTEM_START:
                /* FIXME: get kernel version */
                username = g_strdup ("");
                break;
        default:
                g_assert_not_reached ();
        }

        return username;
}

static RecordStatus
get_event_record_status (CkLogEvent *remove_event)
{
        RecordStatus status;

        status = RECORD_STATUS_NOW;

        if (remove_event == NULL) {
                goto out;
        }

        if (remove_event->type == CK_LOG_EVENT_SEAT_SESSION_REMOVED) {
                status = RECORD_STATUS_NORMAL;
        } else if (remove_event->type == CK_LOG_EVENT_SYSTEM_START) {
                status = RECORD_STATUS_CRASH;
        } else if (remove_event->type == CK_LOG_EVENT_SYSTEM_STOP) {
                status = RECORD_STATUS_DOWN;
        } else if (remove_event->type == CK_LOG_EVENT_SYSTEM_RESTART) {
                status = RECORD_STATUS_DOWN;
        }

 out:
        return status;
}

static char *
get_duration (CkLogEvent *event,
              CkLogEvent *remove_event)
{
        time_t secs;
        int    mins;
        int    hours;
        int    days;
        char  *duration;

        secs = remove_event->timestamp.tv_sec - event->timestamp.tv_sec;
        mins  = (secs / 60) % 60;
        hours = (secs / 3600) % 24;
        days  = secs / 86400;
        if (days > 0) {
                duration = g_strdup_printf ("(%d+%02d:%02d)", days, hours, mins);
        } else {
                duration = g_strdup_printf (" (%02d:%02d)", hours, mins);
        }
        return duration;
}

static void
print_last_report_record (GList      *list,
                          CkLogEvent *event,
                          gboolean    legacy_compat)
{
        GString                    *str;
        char                       *username;
        char                       *utline;
        char                       *host;
        char                       *addedtime;
        char                       *removedtime;
        char                       *duration;
        char                       *session_type;
        char                       *session_id;
        char                       *seat_id;
        CkLogSeatSessionAddedEvent *e;
        CkLogEvent                 *remove_event;
        RecordStatus                status;

        if (event->type != CK_LOG_EVENT_SEAT_SESSION_ADDED
            && event->type != CK_LOG_EVENT_SYSTEM_START) {
                return;
        }

        remove_event = NULL;

        if (event->type == CK_LOG_EVENT_SEAT_SESSION_ADDED) {
                e = (CkLogSeatSessionAddedEvent *)event;

                remove_event = find_first_matching_remove_event (list, e);
                status = get_event_record_status (remove_event);

                session_type = e->session_type;
                session_id = e->session_id;
                seat_id = e->seat_id;
        } else {
                status = RECORD_STATUS_REBOOT;

                session_type = "";
                session_id = "";
                seat_id = "";
        }

        str = g_string_new (NULL);

        username = get_user_name_for_event (event);
        utline = get_utline_for_event (event);
        host = get_host_for_event (event);

        addedtime = g_strndup (ctime (&event->timestamp.tv_sec), 16);

        if (legacy_compat) {
                g_string_printf (str,
                                 "%-8.8s %-12.12s %-16.16s %-16.16s",
                                 username,
                                 utline != NULL ? utline : "",
                                 host != NULL ? host : "",
                                 addedtime);
        } else {
                g_string_printf (str,
                                 "%-8.8s %12s %-10.10s %-7.7s %-12.12s %-16.16s %-16.16s",
                                 username,
                                 session_type,
                                 session_id,
                                 seat_id,
                                 utline,
                                 host != NULL ? host : "",
                                 addedtime);
        }

        g_free (username);
        g_free (addedtime);
        g_free (utline);
        g_free (host);

        removedtime = NULL;
        duration = NULL;

        switch (status) {
        case RECORD_STATUS_CRASH:
                duration = get_duration (event, remove_event);
                removedtime = g_strdup ("- crash");
                break;
        case RECORD_STATUS_DOWN:
                duration = get_duration (event, remove_event);
                removedtime = g_strdup ("- down ");
                break;
        case RECORD_STATUS_NOW:
                duration = g_strdup ("logged in");
                removedtime = g_strdup ("  still");
                break;
        case RECORD_STATUS_PHANTOM:
                duration = g_strdup ("- no logout");
                removedtime = g_strdup ("   gone");
                break;
        case RECORD_STATUS_REBOOT:
                removedtime = g_strdup ("");
                break;
        case RECORD_STATUS_TIMECHANGE:
                removedtime = g_strdup ("");
                break;
        case RECORD_STATUS_NORMAL:
                duration = get_duration (event, remove_event);
                removedtime = g_strdup_printf ("- %s", ctime (&remove_event->timestamp.tv_sec) + 11);
                removedtime[7] = 0;
                break;
        default:
                g_assert_not_reached ();
                break;
        }

        g_string_append_printf (str,
                                " %-7.7s %-12.12s",
                                removedtime,
                                duration);

        g_print ("%s\n", str->str);
        g_string_free (str, TRUE);
        g_free (removedtime);
        g_free (duration);
}

static void
generate_report_last (int         uid,
                      const char *seat,
                      const char *session_type)
{
        GList      *oldest;
        CkLogEvent *oldest_event;
        GList      *l;

        /* print events in reverse time order */

        for (l = g_list_last (all_events); l != NULL; l = l->prev) {
                CkLogEvent *event;

                event = l->data;

                if (event->type == CK_LOG_EVENT_SEAT_SESSION_ADDED) {
                        CkLogSeatSessionAddedEvent *e;
                        e = (CkLogSeatSessionAddedEvent *)event;

                        if (uid >= 0 && e->session_unix_user != uid) {
                                continue;
                        }

                        if (seat != NULL && e->seat_id != NULL && strcmp (e->seat_id, seat) != 0) {
                                continue;
                        }

                        if (session_type != NULL && e->session_type != NULL && strcmp (e->session_type, session_type) != 0) {
                                continue;
                        }
                }

                print_last_report_record (l, event, FALSE);
        }

        oldest = g_list_first (all_events);
        if (oldest != NULL) {
                oldest_event = oldest->data;
                g_print ("\nLog begins %s", ctime (&oldest_event->timestamp.tv_sec));
        }
}

static void
generate_report_last_compat (int         uid,
                             const char *seat,
                             const char *session_type)
{
        GList      *oldest;
        CkLogEvent *oldest_event;
        GList      *l;

        /* print events in reverse time order */

        for (l = g_list_last (all_events); l != NULL; l = l->prev) {
                CkLogEvent *event;

                event = l->data;

                if (event->type == CK_LOG_EVENT_SEAT_SESSION_ADDED) {
                        CkLogSeatSessionAddedEvent *e;
                        e = (CkLogSeatSessionAddedEvent *)event;

                        if (uid >= 0 && e->session_unix_user != uid) {
                                continue;
                        }

                        if (seat != NULL && e->seat_id != NULL && strcmp (e->seat_id, seat) != 0) {
                                continue;
                        }

                        if (session_type != NULL && e->session_type != NULL && strcmp (e->session_type, session_type) != 0) {
                                continue;
                        }
                }

                print_last_report_record (l, event, TRUE);
        }

        oldest = g_list_first (all_events);
        if (oldest != NULL) {
                oldest_event = oldest->data;
                g_print ("\nLog begins %s", ctime (&oldest_event->timestamp.tv_sec));
        }
}

typedef struct {
        int   uid;
        guint count;
} CountData;

static void
listify_counts (gpointer key,
                gpointer val,
                GList **list)
{
        CountData *count_data;
        count_data = g_new0 (CountData, 1);
        count_data->uid = GPOINTER_TO_INT (key);
        count_data->count = GPOINTER_TO_UINT (val);
        *list = g_list_prepend (*list, count_data);
}

static int
counts_compare (CountData *a,
                CountData *b)
{
        if (a->count < b->count) {
                return 1;
        } else if (a->count > b->count) {
                return -1;
        } else {
                return 0;
        }
}

static void
generate_report_frequent (int         uid,
                          const char *seat,
                          const char *session_type)
{
        GHashTable *counts;
        GList      *l;
        GList      *user_counts;

        /* FIXME: we can probably do this more efficiently */

        counts = g_hash_table_new (NULL, NULL);

        for (l = g_list_first (all_events); l != NULL; l = l->next) {
                CkLogEvent                 *event;
                CkLogSeatSessionAddedEvent *e;
                guint                       count;
                gpointer                    val;

                event = l->data;

                if (event->type != CK_LOG_EVENT_SEAT_SESSION_ADDED) {
                        continue;
                }

                e = (CkLogSeatSessionAddedEvent *)event;

                if (uid >= 0 && e->session_unix_user != uid) {
                        continue;
                }

                if (seat != NULL && e->seat_id != NULL && strcmp (e->seat_id, seat) != 0) {
                        continue;
                }

                if (session_type != NULL && e->session_type != NULL && strcmp (e->session_type, session_type) != 0) {
                        continue;
                }

                val = g_hash_table_lookup (counts, GINT_TO_POINTER (e->session_unix_user));
                if (val != NULL) {
                        count = GPOINTER_TO_INT (val);
                } else {
                        count = 0;
                }

                g_hash_table_insert (counts,
                                     GINT_TO_POINTER (e->session_unix_user),
                                     GUINT_TO_POINTER (count + 1));
        }

        user_counts = NULL;
        g_hash_table_foreach (counts, (GHFunc)listify_counts, &user_counts);
        g_hash_table_destroy (counts);

        if (user_counts == NULL) {
                return;
        }

        user_counts = g_list_sort (user_counts, (GCompareFunc)counts_compare);
        while (user_counts != NULL) {
                CountData *data;
                char      *username;

                data = user_counts->data;

                username = get_user_name_for_uid (data->uid);
                g_print ("%-8.8s %u\n", username, data->count);
                g_free (data);
                user_counts = g_list_delete_link (user_counts, user_counts);
                g_free (username);
        }

        g_list_free (user_counts);
}

static void
generate_report_log (int         uid,
                     const char *seat,
                     const char *session_type)
{
        GList *l;

        for (l = all_events; l != NULL; l = l->next) {
                CkLogEvent *event;
                GString    *str;

                event = l->data;
                str = g_string_new (NULL);
                ck_log_event_to_string (event, str);
                g_print ("%s\n", str->str);
                g_string_free (str, TRUE);
        }
}

static void
generate_report (int         report_type,
                 int         uid,
                 const char *seat,
                 const char *session_type)
{

        all_events = g_list_reverse (all_events);

        switch (report_type) {
        case REPORT_TYPE_SUMMARY:
                generate_report_summary (uid, seat, session_type);
                break;
        case REPORT_TYPE_LAST:
                generate_report_last (uid, seat, session_type);
                break;
        case REPORT_TYPE_LAST_COMPAT:
                generate_report_last_compat (uid, seat, session_type);
                break;
        case REPORT_TYPE_FREQUENT:
                generate_report_frequent (uid, seat, session_type);
                break;
        case REPORT_TYPE_LOG:
                generate_report_log (uid, seat, session_type);
                break;
        default:
                g_assert_not_reached ();
                break;
        }
}

static void
free_events (void)
{
        /* FIXME: */
}

int
main (int    argc,
      char **argv)
{
        GOptionContext     *context;
        gboolean            retval;
        GError             *error = NULL;
        int                 report_type;
        int                 uid;
        static gboolean     do_version = FALSE;
        static gboolean     report_last_compat = FALSE;
        static gboolean     report_last = FALSE;
        static gboolean     report_frequent = FALSE;
        static gboolean     report_log = FALSE;
        static char        *username = NULL;
        static char        *seat = NULL;
        static char        *session_type = NULL;
        static GOptionEntry entries [] = {
                { "version", 'V', 0, G_OPTION_ARG_NONE, &do_version, N_("Version of this application"), NULL },
                { "frequent", 0, 0, G_OPTION_ARG_NONE, &report_frequent, N_("Show listing of frequent users"), NULL },
                { "last", 0, 0, G_OPTION_ARG_NONE, &report_last, N_("Show listing of last logged in users"), NULL },
                { "last-compat", 0, 0, G_OPTION_ARG_NONE, &report_last_compat, N_("Show 'last' compatible listing of last logged in users"), NULL },
                { "log", 0, 0, G_OPTION_ARG_NONE, &report_log, N_("Show full event log"), NULL },
                { "seat", 's', 0, G_OPTION_ARG_STRING, &seat, N_("Show entries for the specified seat"), N_("SEAT") },
                { "session-type", 't', 0, G_OPTION_ARG_STRING, &session_type, N_("Show entries for the specified session type"), N_("TYPE") },
                { "user", 'u', 0, G_OPTION_ARG_STRING, &username, N_("Show entries for the specified user"), N_("NAME") },
                { NULL }
        };

        context = g_option_context_new (NULL);
        g_option_context_add_main_entries (context, entries, NULL);
        retval = g_option_context_parse (context, &argc, &argv, &error);

        g_option_context_free (context);

        if (! retval) {
                g_warning ("%s", error->message);
                g_error_free (error);
                exit (1);
        }

        if (do_version) {
                g_print ("%s %s\n", argv [0], VERSION);
                exit (1);
        }

        if (report_last_compat) {
                report_type = REPORT_TYPE_LAST_COMPAT;
        } else if (report_last) {
                report_type = REPORT_TYPE_LAST;
        } else if (report_frequent) {
                report_type = REPORT_TYPE_FREQUENT;
        } else if (report_log) {
                report_type = REPORT_TYPE_LOG;
        } else {
                report_type = REPORT_TYPE_SUMMARY;
        }

        if (username != NULL) {
                uid = get_uid_for_username (username);
                if (uid == -1) {
                        g_warning ("Unknown username: %s", username);
                        exit (1);
                }
        } else {
                uid = -1;
        }

        process_logs ();
        generate_report (report_type, uid, seat, session_type);
        free_events ();

        return 0;
}