[systemd-devel] [PATCH 1/2] connection: add function kdbus_conn_find_peer()

Djalal Harouni tixxdz at opendz.org
Sun Mar 30 17:43:03 PDT 2014


Use this function to find a connection peer with a given id.

The returned connection is on the same bus of the source connection
and it is ref'ed.

This allows to reduce code duplication and will make it easy to
read code and differentiate between messages that have a source
connection and other kernel generated messages.

Signed-off-by: Djalal Harouni <tixxdz at opendz.org>
---
 connection.c | 24 ++++++++++++++++++++++++
 connection.h |  2 ++
 2 files changed, 26 insertions(+)

diff --git a/connection.c b/connection.c
index e01e0ab..97fde97 100644
--- a/connection.c
+++ b/connection.c
@@ -1568,6 +1568,30 @@ bool kdbus_conn_active(const struct kdbus_conn *conn)
 	return !conn->disconnected;
 }
 
+/**
+ * kdbus_conn_find_peer() - find a connection peer with a given id
+ * @conn_src:		The source connection
+ * @id:                 The 64-bit connection id of the destination
+ *
+ * Finds a connection peer with a given id. The returned connection
+ * is on the same bus of the source connection and it is ref'ed.
+ * Returns NULL if the peer can't be found.
+ *
+ * On success caller must ensure to unref the returned connection.
+ */
+struct kdbus_conn *kdbus_conn_find_peer(struct kdbus_conn *conn_src,
+					u64 id)
+{
+	struct kdbus_conn *conn_dst;
+	struct kdbus_bus *bus = conn_src->bus;
+
+	mutex_lock(&bus->lock);
+	conn_dst = kdbus_bus_find_conn_by_id(bus, id);
+	mutex_unlock(&bus->lock);
+
+	return conn_dst;
+}
+
 static void __kdbus_conn_free(struct kref *kref)
 {
 	struct kdbus_conn *conn = container_of(kref, struct kdbus_conn, kref);
diff --git a/connection.h b/connection.h
index 43a9b61..a91ea4c 100644
--- a/connection.h
+++ b/connection.h
@@ -106,6 +106,8 @@ struct kdbus_conn *kdbus_conn_ref(struct kdbus_conn *conn);
 struct kdbus_conn *kdbus_conn_unref(struct kdbus_conn *conn);
 int kdbus_conn_disconnect(struct kdbus_conn *conn, bool ensure_queue_empty);
 bool kdbus_conn_active(const struct kdbus_conn *conn);
+struct kdbus_conn *kdbus_conn_find_peer(struct kdbus_conn *conn_src,
+					u64 id);
 
 int kdbus_cmd_msg_recv(struct kdbus_conn *conn,
 		       struct kdbus_cmd_recv *recv);
-- 
1.8.5.3



More information about the systemd-devel mailing list