Skip to content

Commit

Permalink
Migrate afpstats from dbus-glib to GDBus, GitHub #666
Browse files Browse the repository at this point in the history
Co-authored-by: Simon McVittie <smcv@collabora.com>
  • Loading branch information
2 people authored and rdmark committed Sep 21, 2024
1 parent 04da114 commit fad8a57
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 177 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ env:
libcups2-dev \
libdb-dev \
libdbus-1-dev \
libdbus-glib-1-dev \
libevent-dev \
libgcrypt-dev \
libglib2.0-dev \
Expand Down Expand Up @@ -91,7 +90,6 @@ jobs:
curl \
db-dev \
dbus-dev \
dbus-glib-dev \
docbook-xsl \
flex \
gcc \
Expand Down Expand Up @@ -163,6 +161,7 @@ jobs:
- name: Configure
run: |
meson setup build \
-Dwith-afpstats=false \
-Dwith-appletalk=true \
-Dwith-docbook-path=/usr/share/xml/docbook/xsl-stylesheets-1.79.2 \
-Dwith-init-hooks=false \
Expand Down Expand Up @@ -228,7 +227,6 @@ jobs:
cracklib-devel \
cups-devel \
dbus-devel \
dbus-glib-devel \
docbook-style-xsl \
flex \
glib2-devel \
Expand All @@ -246,6 +244,7 @@ jobs:
openssl-devel \
pam-devel \
perl \
perl-Net-DBus \
quota-devel \
systemd \
systemtap-sdt-devel \
Expand Down Expand Up @@ -474,6 +473,7 @@ jobs:
mysql84-client \
nettle \
openldap26-client-2.6.8 \
p5-Net-DBus \
perl5 \
pkgconf \
talloc \
Expand Down Expand Up @@ -515,7 +515,6 @@ jobs:
avahi \
bison \
db5 \
dbus-glib \
docbook-xsl \
flex \
gcc13 \
Expand Down Expand Up @@ -567,8 +566,8 @@ jobs:
pkg_add -I \
avahi \
bison \
dbus-glib \
db-4.6.21p7v0 \
dbus \
docbook-xsl \
gcc-11.2.0p11 \
libevent \
Expand All @@ -580,6 +579,7 @@ jobs:
meson \
openldap-client-2.6.7v0 \
openpam \
p5-Net-DBus \
pkgconf \
tracker3 \
wget
Expand Down Expand Up @@ -621,7 +621,6 @@ jobs:
export PATH=/opt/local/sbin:/opt/local/bin:/usr/gnu/bin:/usr/bin:/usr/sbin:/sbin:$PATH
pkgin -y install \
avahi \
dbus-glib \
docbook-xsl \
gnome-tracker \
libevent \
Expand Down Expand Up @@ -680,6 +679,7 @@ jobs:
wget https://www.unicode.org/Public/UNIDATA/UnicodeData.txt
meson setup build \
-Dpkg_config_path=/usr/lib/amd64/pkgconfig \
-Dwith-afpstats=false \
-Dwith-appletalk=true \
-Dwith-docbook-path=/usr/share/sgml/docbook/xsl-stylesheets \
-Dwith-tests=true
Expand Down
2 changes: 0 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ ENV LIB_DEPS \
cups \
db \
dbus \
dbus-glib \
krb5 \
libevent \
libgcrypt \
Expand All @@ -28,7 +27,6 @@ ENV BUILD_DEPS \
curl \
db-dev \
dbus-dev \
dbus-glib-dev \
build-base \
flex \
gcc \
Expand Down
2 changes: 1 addition & 1 deletion INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Optional:
- avahi or mDNSresponder (for Zeroconf support)
- cracklib and cracklib dictionary (for password strength check in afppasswd)
- Docbook XSL and xsltproc (for manpages & manual documentation)
- GLib 2 and dbus-glib (for afpstats support)
- GLib 2 and D-Bus (for afpstats support)
- Kerberos V (for krbV UAM support)
- libacl (for ACL support)
- libldap (for LDAP support)
Expand Down
2 changes: 1 addition & 1 deletion doc/ja/manual/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ Resolving deltas: 100% (32227/32227), done.
</listitem>

<listitem>
<para>D-Bus および GLib バインディング</para>
<para>D-Bus および GLib</para>

<para>afpd にファイル サーバーの使用統計を問い合わせるために
<userinput>afpstats</userinput> ツールによって使用されます。</para>
Expand Down
2 changes: 1 addition & 1 deletion doc/manual/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ Resolving deltas: 100% (32227/32227), done.
</listitem>

<listitem>
<para>D-Bus and GLib bindings</para>
<para>D-Bus and GLib</para>

<para>Used by the <userinput>afpstats</userinput> tool to inquire
afpd about file server usage statistics.</para>
Expand Down
128 changes: 102 additions & 26 deletions etc/afpd/afpstats.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,68 +22,145 @@
#include <stdlib.h>
#include <string.h>

#include <dbus/dbus-glib.h>
#include <glib.h>
#include <gio/gio.h>

#include <atalk/compat.h>
#include <atalk/errchk.h>
#include <atalk/logger.h>
#include <atalk/server_child.h>

#include "afpstats_obj.h"
#include "afpstats_service_glue.h"

/*
* Beware: this struct is accessed and modified from the main thread
* and from this thread, thus be careful to lock and unlock the mutex.
*/
static server_child_t *childs;
static GDBusNodeInfo *introspection_data = NULL;

static void handle_method_call (GDBusConnection *connection _U_,
const gchar *sender _U_,
const gchar *object_path _U_,
const gchar *interface_name _U_,
const gchar *method_name,
GVariant *parameters _U_,
GDBusMethodInvocation *invocation,
gpointer user_data)
{
AFPStatsObj *obj = AFPSTATS_OBJECT (user_data);

if (g_strcmp0 (method_name, "GetUsers") == 0)
{
GError *local_error = NULL;
gchar **users = NULL;

if (afpstats_obj_get_users (obj, &users, &local_error))
g_dbus_method_invocation_return_value (invocation, g_variant_new ("(^as)", users));
else
g_dbus_method_invocation_return_gerror (invocation, local_error);

g_strfreev (users);
g_clear_error (&local_error);
}
}

static const GDBusInterfaceVTable interface_vtable =
{
&handle_method_call,
NULL,
NULL,
{ 0 }
};

static void on_bus_acquired (GDBusConnection *connection _U_,
const gchar *name,
gpointer user_data _U_)
{
LOG(log_debug, logtype_afpd, "[afpstats] on_bus_acquired(): %s", name);
}

static void on_name_acquired (GDBusConnection *connection _U_,
const gchar *name,
gpointer user_data _U_)
{
LOG(log_debug, logtype_afpd, "[afpstats] on_name_acquired(): %s", name);
}

static void on_name_lost (GDBusConnection *connection _U_,
const gchar *name,
gpointer user_data _U_)
{
LOG(log_debug, logtype_afpd, "[afpstats] on_name_lost(): %s", name);
}

static gpointer afpstats_thread(gpointer _data _U_)
{
DBusGConnection *bus;
DBusGProxy *bus_proxy;
GDBusConnection *bus;
GError *error = NULL;
GMainContext *ctxt;
GMainLoop *thread_loop;
guint request_name_result;
GBusNameOwnerFlags request_name_result;
guint registration_id;
sigset_t sigs;

/* Block all signals in this thread */
sigfillset(&sigs);
pthread_sigmask(SIG_BLOCK, &sigs, NULL);

ctxt = g_main_context_new();
g_main_context_push_thread_default(ctxt);
thread_loop = g_main_loop_new(ctxt, FALSE);

dbus_g_object_type_install_info(AFPSTATS_TYPE_OBJECT, &dbus_glib_afpstats_obj_object_info);

if (!(bus = dbus_g_bus_get_private(DBUS_BUS_SYSTEM, ctxt, &error))) {
if (!(bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM,
NULL,
&error))) {
LOG(log_error, logtype_afpd,"Couldn't connect to system bus: %s", error->message);
return NULL;
}

if (!(bus_proxy = dbus_g_proxy_new_for_name(bus, "org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus"))) {
LOG(log_error, logtype_afpd,"Couldn't create bus proxy");
return NULL;
}
static const gchar introspection_xml[] =
"<node>"
" <interface name='org.netatalk.AFPStats'>"
" <method name='GetUsers'>"
" <arg name='ret' type='as' direction='out'/>"
" </method>"
" </interface>"
"</node>";

if (!dbus_g_proxy_call(bus_proxy, "RequestName", &error,
G_TYPE_STRING, "org.netatalk.AFPStats",
G_TYPE_UINT, 0,
G_TYPE_INVALID,
G_TYPE_UINT, &request_name_result,
G_TYPE_INVALID)) {
LOG(log_error, logtype_afpd, "Failed to acquire DBUS name: %s", error->message);
return NULL;
}
introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
g_assert (introspection_data != NULL);

AFPStatsObj *obj = g_object_new(AFPSTATS_TYPE_OBJECT, NULL);
dbus_g_connection_register_g_object(bus, "/org/netatalk/AFPStats", G_OBJECT(obj));

registration_id = g_dbus_connection_register_object(bus,
"/org/netatalk/AFPStats",
introspection_data->interfaces[0],
&interface_vtable,
g_object_ref (obj),
g_object_unref,
&error);
g_assert (registration_id > 0);

request_name_result = g_bus_own_name(G_BUS_TYPE_SYSTEM,
"org.netatalk.AFPStats",
G_BUS_NAME_OWNER_FLAGS_NONE,
on_bus_acquired,
on_name_acquired,
on_name_lost,
NULL,
NULL);

g_main_loop_run(thread_loop);

g_bus_unown_name(request_name_result);
g_dbus_connection_unregister_object (bus, registration_id);
g_dbus_node_info_unref(introspection_data);

g_object_unref(obj);
g_main_context_pop_thread_default(ctxt);
g_main_context_unref(ctxt);

return thread_loop;
}

Expand All @@ -92,7 +169,7 @@ static void my_glib_log(const gchar *log_domain,
const gchar *message,
gpointer user_data _U_)
{
LOG(log_error, logtype_afpd, "%s: %s", log_domain, message);
LOG(log_debug, logtype_afpd, "[afpstats] %s: %s", log_domain, message);
}

server_child_t *afpstats_get_and_lock_childs(void)
Expand All @@ -111,7 +188,6 @@ int afpstats_init(server_child_t *childs_in)
GThread *thread;

childs = childs_in;
dbus_g_thread_init();
(void)g_log_set_default_handler(my_glib_log, NULL);

thread = g_thread_new("afpstats", afpstats_thread, NULL);
Expand Down
Loading

0 comments on commit fad8a57

Please sign in to comment.