[Xfce4-commits] [xfce/xfce4-session] 03/11: get pid for dbus client

noreply at xfce.org noreply at xfce.org
Tue Jun 28 09:26:06 CEST 2016


This is an automated email from the git hooks/post-receive script.

eric pushed a commit to branch master
in repository xfce/xfce4-session.

commit 854f47b9ee2cc77c5a63b2d14afe0013bfc707bc
Author: Eric Koegel <eric.koegel at gmail.com>
Date:   Tue Jun 7 12:18:37 2016 +0300

    get pid for dbus client
    
    When the client registers over dbus, use the dbus message to get
    the pid of the caller so that we can track it.
---
 xfce4-session/xfsm-client.c  | 26 +++++++++++++++++++
 xfce4-session/xfsm-client.h  |  3 +++
 xfce4-session/xfsm-manager.c | 60 ++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 87 insertions(+), 2 deletions(-)

diff --git a/xfce4-session/xfsm-client.c b/xfce4-session/xfsm-client.c
index c7c8cd5..aad20d1 100644
--- a/xfce4-session/xfsm-client.c
+++ b/xfce4-session/xfsm-client.c
@@ -358,6 +358,32 @@ xfsm_client_get_object_path (XfsmClient *client)
 
 
 
+void
+xfsm_client_set_pid (XfsmClient *client,
+                     pid_t       pid)
+{
+  XfsmProperties *properties;
+  gchar          *pid_str;
+
+  g_return_if_fail (XFSM_IS_CLIENT (client));
+  g_return_if_fail (client->properties != NULL);
+
+  properties = client->properties;
+
+  /* save the pid */
+  properties->pid = pid;
+
+  /* convert it to a string */
+  pid_str = g_strdup_printf ("%d", pid);
+
+  /* store the string as well (so we can export it over dbus */
+  xfsm_properties_set_string (properties, "ProcessID", pid_str);
+
+  g_free (pid_str);
+}
+
+
+
 /*
  * dbus server impl
  */
diff --git a/xfce4-session/xfsm-client.h b/xfce4-session/xfsm-client.h
index 61923e4..7685300 100644
--- a/xfce4-session/xfsm-client.h
+++ b/xfce4-session/xfsm-client.h
@@ -79,6 +79,9 @@ void xfsm_client_delete_properties (XfsmClient *client,
 
 const gchar *xfsm_client_get_object_path (XfsmClient *client);
 
+void xfsm_client_set_pid (XfsmClient *client,
+                          pid_t       pid);
+
 G_END_DECLS
 
 #endif /* !__XFSM_CLIENT_H__ */
diff --git a/xfce4-session/xfsm-manager.c b/xfce4-session/xfsm-manager.c
index 054ee7c..864d720 100644
--- a/xfce4-session/xfsm-manager.c
+++ b/xfce4-session/xfsm-manager.c
@@ -2302,6 +2302,52 @@ xfsm_manager_dbus_can_hibernate (XfsmDbusManager *object,
   return TRUE;
 }
 
+/* adapted from ConsoleKit2 whch was adapted from PolicyKit */
+static gboolean
+get_caller_info (XfsmManager *manager,
+                 const char  *sender,
+                 pid_t       *calling_pid)
+{
+        gboolean  res   = FALSE;
+        GVariant *value = NULL;
+        GError   *error = NULL;
+
+        if (sender == NULL) {
+                xfsm_verbose ("sender == NULL");
+                goto out;
+        }
+
+        if (manager->connection == NULL) {
+                xfsm_verbose ("manager->connection == NULL");
+                goto out;
+        }
+
+        value = g_dbus_connection_call_sync (manager->connection,
+                                             "org.freedesktop.DBus",
+                                             "/org/freedesktop/DBus",
+                                             "org.freedesktop.DBus",
+                                             "GetConnectionUnixProcessID",
+                                             g_variant_new ("(s)", sender),
+                                             G_VARIANT_TYPE ("(u)"),
+                                             G_DBUS_CALL_FLAGS_NONE,
+                                             -1,
+                                             NULL,
+                                             &error);
+
+        if (value == NULL) {
+                xfsm_verbose ("GetConnectionUnixProcessID() failed: %s", error->message);
+                g_error_free (error);
+                goto out;
+        }
+        g_variant_get (value, "(u)", calling_pid);
+        g_variant_unref (value);
+
+        res = TRUE;
+
+out:
+        return res;
+}
+
 static gboolean
 xfsm_manager_dbus_register_client (XfsmDbusManager *object,
                                    GDBusMethodInvocation *invocation,
@@ -2309,8 +2355,9 @@ xfsm_manager_dbus_register_client (XfsmDbusManager *object,
                                    const gchar *arg_client_startup_id)
 {
   XfsmManager *manager;
-  XfsmClient *client;
-  gchar *client_id;
+  XfsmClient  *client;
+  gchar       *client_id;
+  pid_t        pid = 0;
 
   manager = XFSM_MANAGER (object);
 
@@ -2326,8 +2373,17 @@ xfsm_manager_dbus_register_client (XfsmDbusManager *object,
   /* create a new dbus-based client */
   client = xfsm_client_new (manager, NULL, manager->connection);
 
+  /* register it so that it exports the dbus name */
   xfsm_manager_register_client (manager, client, client_id, NULL);
 
+  /* attempt to get the caller'd pid so we can monitor it */
+  if (!get_caller_info (manager, g_dbus_method_invocation_get_sender (invocation), &pid))
+    {
+      pid = 0;
+    }
+
+  xfsm_client_set_pid (client, pid);
+
   xfsm_dbus_manager_complete_register_client (object, invocation, xfsm_client_get_object_path (client));
   g_free (client_id);
   return TRUE;

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list