]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afpstats.c
quotas: improve Linux quota behaviour
[netatalk.git] / etc / afpd / afpstats.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 <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <pthread.h>
24
25 #include <glib.h>
26 #include <dbus/dbus-glib.h>
27
28 #include <atalk/logger.h>
29 #include <atalk/compat.h>
30 #include <atalk/errchk.h>
31 #include <atalk/server_child.h>
32
33 #include "afpstats_obj.h"
34 #include "afpstats_service_glue.h"
35
36 /*
37  * Beware: this struct is accessed and modified from the main thread
38  * and from this thread, thus be careful to lock and unlock the mutex.
39  */
40 static server_child_t *childs;
41
42 static gpointer afpstats_thread(gpointer _data)
43 {
44     DBusGConnection *bus;
45     DBusGProxy *bus_proxy;
46     GError *error = NULL;
47     GMainContext *ctxt;
48     GMainLoop *thread_loop;
49     guint request_name_result;
50     sigset_t sigs;
51
52     /* Block all signals in this thread */
53     sigfillset(&sigs);
54     pthread_sigmask(SIG_BLOCK, &sigs, NULL);
55
56     ctxt = g_main_context_new();
57     thread_loop = g_main_loop_new(ctxt, FALSE);
58
59     dbus_g_object_type_install_info(AFPSTATS_TYPE_OBJECT, &dbus_glib_afpstats_obj_object_info);
60    
61     if (!(bus = dbus_g_bus_get_private(DBUS_BUS_SYSTEM, ctxt, &error))) {
62         LOG(log_error, logtype_afpd,"Couldn't connect to system bus: %s", error->message);
63         return NULL;
64     }
65
66     if (!(bus_proxy = dbus_g_proxy_new_for_name(bus, "org.freedesktop.DBus",
67                                                 "/org/freedesktop/DBus",
68                                                 "org.freedesktop.DBus"))) {
69         LOG(log_error, logtype_afpd,"Couldn't create bus proxy");
70         return NULL;
71     }
72
73     if (!dbus_g_proxy_call(bus_proxy, "RequestName", &error,
74                            G_TYPE_STRING, "org.netatalk.AFPStats",
75                            G_TYPE_UINT, 0,
76                            G_TYPE_INVALID,
77                            G_TYPE_UINT, &request_name_result,
78                            G_TYPE_INVALID)) {
79         LOG(log_error, logtype_afpd, "Failed to acquire DBUS name: %s", error->message);
80         return NULL;
81     }
82
83     AFPStatsObj *obj = g_object_new(AFPSTATS_TYPE_OBJECT, NULL);
84     dbus_g_connection_register_g_object(bus, "/org/netatalk/AFPStats", G_OBJECT(obj));
85
86     g_main_loop_run(thread_loop);
87     return thread_loop;
88 }
89
90 static void my_glib_log(const gchar *log_domain,
91                         GLogLevelFlags log_level,
92                         const gchar *message,
93                         gpointer user_data)
94 {
95     LOG(log_error, logtype_afpd, "%s: %s", log_domain, message);
96 }
97
98 server_child_t *afpstats_get_and_lock_childs(void)
99 {
100     pthread_mutex_lock(&childs->servch_lock);
101     return childs;
102 }
103
104 void afpstats_unlock_childs(void)
105 {
106     pthread_mutex_unlock(&childs->servch_lock);
107 }
108
109 int afpstats_init(server_child_t *childs_in)
110 {
111     GThread *thread;
112
113     childs = childs_in;
114     g_type_init();
115     g_thread_init(NULL);
116     dbus_g_thread_init();
117     (void)g_log_set_default_handler(my_glib_log, NULL);
118
119     thread = g_thread_create(afpstats_thread, NULL, TRUE, NULL);
120
121     return 0;
122 }