From: Daniel Wagner <[email protected]>

In order to get the PID of the remote client one needs to ask
the D-Bus server who is the owner of a message. This can
be done using the Freedesktop API GetConnectoniUnixProcessID.
---
 gdbus/gdbus.h  |  6 ++++
 gdbus/object.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 95 insertions(+)

diff --git a/gdbus/gdbus.h b/gdbus/gdbus.h
index 0a8a27c..e00442f 100644
--- a/gdbus/gdbus.h
+++ b/gdbus/gdbus.h
@@ -62,6 +62,9 @@ typedef void (* GDBusSecurityFunction) (DBusConnection 
*connection,
                                                gboolean interaction,
                                                GDBusPendingReply pending);
 
+typedef void (* GDBusGetPIDFunction) (const char *owner, pid_t pid,
+                                       void *user_data);
+
 typedef enum {
        G_DBUS_METHOD_FLAG_DEPRECATED = (1 << 0),
        G_DBUS_METHOD_FLAG_NOREPLY    = (1 << 1),
@@ -220,6 +223,9 @@ guint g_dbus_add_signal_watch(DBusConnection *connection,
 gboolean g_dbus_remove_watch(DBusConnection *connection, guint tag);
 void g_dbus_remove_all_watches(DBusConnection *connection);
 
+gboolean g_dbus_get_pid(DBusConnection *connection, const char *owner,
+                       void *user_data, GDBusGetPIDFunction function);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/gdbus/object.c b/gdbus/object.c
index 900e7ab..694ad08 100644
--- a/gdbus/object.c
+++ b/gdbus/object.c
@@ -856,3 +856,92 @@ gboolean g_dbus_emit_signal_valist(DBusConnection 
*connection,
        return emit_signal_valist(connection, path, interface,
                                                        name, type, args);
 }
+
+struct get_pid_data {
+       GDBusGetPIDFunction function;
+       char *owner;
+       void *user_data;
+       DBusPendingCall *call;
+};
+
+static void get_pid_reply(DBusPendingCall *call, void *user_data)
+{
+       struct get_pid_data *data = user_data;
+       DBusMessage *reply;
+       DBusError err;
+       guint32 pid;
+
+       reply = dbus_pending_call_steal_reply(call);
+       if (reply == NULL)
+               return;
+
+       dbus_error_init(&err);
+
+       if (dbus_set_error_from_message(&err, reply))
+               goto fail;
+
+       if (dbus_message_get_args(reply, &err,
+                                       DBUS_TYPE_UINT32, &pid,
+                                       DBUS_TYPE_INVALID) == FALSE)
+               goto fail;
+
+       (*data->function)(data->owner, pid, data->user_data);
+
+       goto done;
+
+fail:
+       error("%s", err.message);
+       dbus_error_free(&err);
+
+done:
+       dbus_pending_call_unref(data->call);
+       g_free(data->owner);
+       g_free(data);
+
+       dbus_message_unref(reply);
+}
+
+gboolean g_dbus_get_pid(DBusConnection *connection, const char *owner,
+                       void *user_data, GDBusGetPIDFunction function)
+{
+       struct get_pid_data *data;
+       DBusMessage *call;
+
+       data = g_try_new0(struct get_pid_data, 1);
+       if (data == NULL)
+               return FALSE;
+
+       data->owner = g_strdup(owner);
+       data->user_data = user_data;
+       data->function = function;
+
+       call = dbus_message_new_method_call(DBUS_SERVICE_DBUS,
+                                               DBUS_PATH_DBUS,
+                                               DBUS_INTERFACE_DBUS,
+                                               "GetConnectionUnixProcessID");
+       if (call == NULL) {
+               g_free(data);
+               return FALSE;
+       }
+
+       dbus_message_append_args(call, DBUS_TYPE_STRING, &owner,
+                                                       DBUS_TYPE_INVALID);
+
+       if (dbus_connection_send_with_reply(connection, call,
+                                               &data->call, -1) == FALSE) {
+               g_free(data);
+               goto done;
+       }
+
+       if (data->call == NULL) {
+               g_free(data);
+               goto done;
+       }
+
+       dbus_pending_call_set_notify(data->call, get_pid_reply, data, NULL);
+
+done:
+       dbus_message_unref(call);
+
+       return TRUE;
+}
-- 
1.7.12.rc1.16.g05a20c8

_______________________________________________
connman mailing list
[email protected]
http://lists.connman.net/listinfo/connman

Reply via email to