From 3c7f8409f03e9448b5e56a04f0f3b23788a8ecfc Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Mon, 18 Dec 2017 00:41:24 +0100 Subject: [PATCH] Output the PID of the current FDN daemon When the DBus name lost function is called, either the whole DBus connection is lost, or another rivaling FDN daemon acquired the name. Without enough experience, this is impossible to trace back and examine further. To make it easier for new users, dunst prints out the PID of the process, which currently acquired the FDN DBus name. --- src/dbus.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 88 insertions(+), 4 deletions(-) diff --git a/src/dbus.c b/src/dbus.c index 9ec39d8..0438033 100644 --- a/src/dbus.c +++ b/src/dbus.c @@ -422,15 +422,99 @@ static void on_name_acquired(GDBusConnection *connection, dbus_conn = connection; } +/* + * Get the PID of the current process, which acquired FDN DBus Name. + * + * Returns: valid PID, else -1 + */ +static int dbus_get_fdn_pid(GDBusConnection *connection) +{ + char *owner = NULL; + GError *error = NULL; + int pid = -1; + + GDBusProxy *proxy_fdn; + GDBusProxy *proxy_dbus; + + if (!connection) + return pid; + + proxy_fdn = g_dbus_proxy_new_sync( + connection, + /* do not trigger a start of the notification daemon */ + G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, + NULL, /* info */ + FDN_NAME, + FDN_PATH, + FDN_IFAC, + NULL, /* cancelable */ + &error); + + if (error) { + g_error_free(error); + return pid; + } + + owner = g_dbus_proxy_get_name_owner(proxy_fdn); + + proxy_dbus = g_dbus_proxy_new_sync( + connection, + G_DBUS_PROXY_FLAGS_NONE, + NULL, /* info */ + "org.freedesktop.DBus", + "/org/freedesktop/DBus", + "org.freedesktop.DBus", + NULL, /* cancelable */ + &error); + + if (error) { + g_error_free(error); + return pid; + } + + GVariant *pidinfo = g_dbus_proxy_call_sync( + proxy_dbus, + "org.freedesktop.DBus.GetConnectionUnixProcessID", + g_variant_new("(s)", owner), + G_DBUS_CALL_FLAGS_NONE, + /* It's not worth to wait for the PID + * longer than half a second when dying */ + 500, + NULL, + &error); + + if (error) { + g_error_free(error); + return pid; + } + + g_variant_get(pidinfo, "(u)", &pid); + + g_object_unref(proxy_fdn); + g_object_unref(proxy_dbus); + g_free(owner); + if (pidinfo) + g_variant_unref(pidinfo); + + return pid; +} + + static void on_name_lost(GDBusConnection *connection, const gchar *name, gpointer user_data) { - if (connection) - fprintf(stderr, "Cannot acquire '"FDN_NAME"'." - "Is Another notification daemon running?\n"); - else + if (connection) { + int pid = dbus_get_fdn_pid(connection); + if (pid > 0) + fprintf(stderr, "Cannot acquire '"FDN_NAME"': " + "Name is acquired by PID '%d'.\n", pid); + else + fprintf(stderr, "Cannot acquire '"FDN_NAME"'.\n"); + + } else { fprintf(stderr, "Cannot connect to DBus.\n"); + } exit(1); }