Blame SOURCES/0015-vdagentd-Limit-number-of-agents-per-session-to-1.patch

39f53b
From 570b15e0ea71950ff14ddc2bf667e9e361720939 Mon Sep 17 00:00:00 2001
39f53b
From: Frediano Ziglio <freddy77@gmail.com>
39f53b
Date: Thu, 24 Sep 2020 12:13:44 +0100
39f53b
Subject: [PATCH vd_agent_linux 15/17] vdagentd: Limit number of agents per
39f53b
 session to 1
39f53b
39f53b
Signed-off-by: Frediano Ziglio <freddy77@gmail.com>
39f53b
Acked-by: Uri Lublin <uril@redhat.com>
39f53b
---
39f53b
 src/vdagentd/vdagentd.c | 24 ++++++++++++++++++++++++
39f53b
 1 file changed, 24 insertions(+)
39f53b
39f53b
diff --git a/src/vdagentd/vdagentd.c b/src/vdagentd/vdagentd.c
39f53b
index bb39340..5ef547e 100644
39f53b
--- a/src/vdagentd/vdagentd.c
39f53b
+++ b/src/vdagentd/vdagentd.c
39f53b
@@ -955,6 +955,20 @@ static gboolean remove_active_xfers(gpointer key, gpointer value, gpointer conn)
39f53b
         return 0;
39f53b
 }
39f53b
 
39f53b
+/* Check if this connection matches the passed session */
39f53b
+static int connection_matches_session(UdscsConnection *conn, void *priv)
39f53b
+{
39f53b
+    const char *session = priv;
39f53b
+    const struct agent_data *agent_data = g_object_get_data(G_OBJECT(conn), "agent_data");
39f53b
+
39f53b
+    if (!agent_data || !agent_data->session ||
39f53b
+        strcmp(agent_data->session, session) != 0) {
39f53b
+        return 0;
39f53b
+    }
39f53b
+
39f53b
+    return 1;
39f53b
+}
39f53b
+
39f53b
 /* Check a given process has a given UID */
39f53b
 static bool check_uid_of_pid(pid_t pid, uid_t uid)
39f53b
 {
39f53b
@@ -1007,6 +1021,16 @@ static void agent_connect(UdscsConnection *conn)
39f53b
             udscs_server_destroy_connection(server, conn);
39f53b
             return;
39f53b
         }
39f53b
+
39f53b
+        // Check there are no other connection for this session
39f53b
+        // Note that "conn" is not counted as "agent_data" is still not attached to it
39f53b
+        if (udscs_server_for_all_clients(server, connection_matches_session,
39f53b
+                                         agent_data->session) > 0) {
39f53b
+            syslog(LOG_ERR, "An agent is already connected for this session");
39f53b
+            agent_data_destroy(agent_data);
39f53b
+            udscs_server_destroy_connection(server, conn);
39f53b
+            return;
39f53b
+        }
39f53b
     }
39f53b
 
39f53b
     g_object_set_data_full(G_OBJECT(conn), "agent_data", agent_data,
39f53b
-- 
39f53b
2.26.2
39f53b