2 * Copyright (c) 2013 Frank Lahm <franklahm@gmail.com>
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.
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.
17 #endif /* HAVE_CONFIG_H */
26 #include <dbus/dbus-glib.h>
28 #include <atalk/logger.h>
29 #include <atalk/compat.h>
30 #include <atalk/errchk.h>
31 #include <atalk/server_child.h>
33 #include "afpstats_obj.h"
34 #include "afpstats_service_glue.h"
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.
40 static server_child_t *childs;
42 static gpointer afpstats_thread(gpointer _data)
45 DBusGProxy *bus_proxy;
48 GMainLoop *thread_loop;
49 guint request_name_result;
52 /* Block all signals in this thread */
54 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
56 ctxt = g_main_context_new();
57 thread_loop = g_main_loop_new(ctxt, FALSE);
59 dbus_g_object_type_install_info(AFPSTATS_TYPE_OBJECT, &dbus_glib_afpstats_obj_object_info);
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);
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");
73 if (!dbus_g_proxy_call(bus_proxy, "RequestName", &error,
74 G_TYPE_STRING, "org.netatalk.AFPStats",
77 G_TYPE_UINT, &request_name_result,
79 LOG(log_error, logtype_afpd, "Failed to acquire DBUS name: %s", error->message);
83 AFPStatsObj *obj = g_object_new(AFPSTATS_TYPE_OBJECT, NULL);
84 dbus_g_connection_register_g_object(bus, "/org/netatalk/AFPStats", G_OBJECT(obj));
86 g_main_loop_run(thread_loop);
90 static void my_glib_log(const gchar *log_domain,
91 GLogLevelFlags log_level,
95 LOG(log_error, logtype_afpd, "%s: %s", log_domain, message);
98 server_child_t *afpstats_get_and_lock_childs(void)
100 pthread_mutex_lock(&childs->servch_lock);
104 void afpstats_unlock_childs(void)
106 pthread_mutex_unlock(&childs->servch_lock);
109 int afpstats_init(server_child_t *childs_in)
116 dbus_g_thread_init();
117 (void)g_log_set_default_handler(my_glib_log, NULL);
119 thread = g_thread_create(afpstats_thread, NULL, TRUE, NULL);