summaryrefslogtreecommitdiff
path: root/xfce4-session
diff options
context:
space:
mode:
authorEric Koegel <eric.koegel@gmail.com>2016-06-07 12:18:37 +0300
committerEric Koegel <eric.koegel@gmail.com>2016-06-28 10:25:28 +0300
commit854f47b9ee2cc77c5a63b2d14afe0013bfc707bc (patch)
tree76426dff2a0bf420b126f776f48f20f11ecaccd0 /xfce4-session
parent2ba26c273b296ccfd82a868283a1a565e480ccf1 (diff)
downloadxfce4-session-854f47b9ee2cc77c5a63b2d14afe0013bfc707bc.tar.gz
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.
Diffstat (limited to 'xfce4-session')
-rw-r--r--xfce4-session/xfsm-client.c26
-rw-r--r--xfce4-session/xfsm-client.h3
-rw-r--r--xfce4-session/xfsm-manager.c60
3 files changed, 87 insertions, 2 deletions
diff --git a/xfce4-session/xfsm-client.c b/xfce4-session/xfsm-client.c
index c7c8cd5e..aad20d15 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 61923e4d..7685300e 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 054ee7c4..864d7202 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;