]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afpstats_obj.c
AFP statistics via dbus IPC
[netatalk.git] / etc / afpd / afpstats_obj.c
1 /*
2  * Copyright (c) 2013 Frank Lahm <franklahm@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif /* HAVE_CONFIG_H */
18
19 #include <strings.h>
20 #include <pwd.h>
21
22 #include <glib-object.h>
23 #include <glib.h>
24 #include <glib/gprintf.h>
25
26 #include <atalk/logger.h>
27 #include <atalk/dsi.h>
28
29 #include "afpstats.h"
30 #include "afpstats_obj.h"
31
32 struct AFPStatsObj
33 {
34   GObject parent;
35 };
36
37 struct AFPStatsObjClass
38 {
39   GObjectClass parent;
40 };
41
42 static void afpstats_obj_init(AFPStatsObj *obj)
43 {
44 }
45
46 static void afpstats_obj_class_init(AFPStatsObjClass *klass)
47 {
48 }
49
50 static gpointer afpstats_obj_parent_class = NULL;
51
52 static void afpstats_obj_class_intern_init(gpointer klass)
53 {
54         afpstats_obj_parent_class = g_type_class_peek_parent(klass);
55         afpstats_obj_class_init((AFPStatsObjClass *)klass);
56 }
57
58 GType afpstats_obj_get_type(void)
59 {
60         static volatile gsize g_define_type_id__volatile = 0;
61         if (g_once_init_enter(&g_define_type_id__volatile)) {
62                 GType g_define_type_id = g_type_register_static_simple(
63             G_TYPE_OBJECT,
64             g_intern_static_string("AFPStatsObj"),
65             sizeof(AFPStatsObjClass),
66             (GClassInitFunc)afpstats_obj_class_intern_init,
67                         sizeof(AFPStatsObj),
68                         (GInstanceInitFunc)afpstats_obj_init,
69                         (GTypeFlags)0);
70                 g_once_init_leave(&g_define_type_id__volatile, g_define_type_id);
71         }
72         return g_define_type_id__volatile;
73 }
74
75 gboolean afpstats_obj_get_users(AFPStatsObj *obj, gchar ***ret, GError **error)
76 {
77     gchar **names;
78     server_child_t *childs = afpstats_get_and_lock_childs();
79     afp_child_t *child;
80     struct passwd *pw;
81     int i = 0, j;
82     char buf[256];
83
84     names = g_new(char *, childs->servch_count + 1);
85
86     for (j = 0; j < CHILD_HASHSIZE && i < childs->servch_count; j++) {
87         child = childs->servch_table[j];
88         while (child) {
89             if (child->afpch_valid && (pw = getpwuid(child->afpch_uid))) {
90                 time_t time = child->afpch_logintime;
91                 strftime(buf, sizeof(buf), "%b %d %H:%M:%S", localtime(&time));
92                 names[i++] = g_strdup_printf("name: %s, pid: %d, logintime: %s, state: %s, volumes: %s",
93                                              pw->pw_name, child->afpch_pid, buf,
94                                              child->afpch_state == DSI_RUNNING ? "active" :
95                                              child->afpch_state == DSI_SLEEPING ? "sleeping" :
96                                              child->afpch_state == DSI_EXTSLEEP ? "sleeping" :
97                                              child->afpch_state == DSI_DISCONNECTED ? "disconnected" :
98                                              "unknown",
99                                              child->afpch_volumes ? child->afpch_volumes : "-"); 
100             }
101             child = child->afpch_next;
102         }
103     }
104     names[i] = NULL;
105     *ret = names;
106
107     afpstats_unlock_childs();
108
109     return TRUE;
110 }