]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lmtp: lmtp-client - Add reference counting
authorStephan Bosch <stephan.bosch@open-xchange.com>
Sun, 21 Jan 2024 16:46:07 +0000 (17:46 +0100)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Mon, 22 Jan 2024 19:02:32 +0000 (19:02 +0000)
src/lmtp/lmtp-client.c
src/lmtp/lmtp-client.h

index 60fec8dbb45a562cbb9fed62111e4963dd1bd5b4..3ac84737b371db55ff2ecfa7682f42d2a191d20b 100644 (file)
@@ -147,6 +147,7 @@ struct client *client_create(int fd_in, int fd_out,
 
        pool = pool_alloconly_create("lmtp client", 2048);
        client = p_new(pool, struct client, 1);
+       client->refcount = 1;
        client->pool = pool;
        client->v = lmtp_client_vfuncs;
        client->remote_ip = conn->remote_ip;
@@ -240,6 +241,27 @@ void client_state_reset(struct client *client)
        p_clear(client->state_pool);
 }
 
+void client_ref(struct client *client)
+{
+       i_assert(client->refcount > 0);
+       client->refcount++;
+}
+
+void client_unref(struct client **_client)
+{
+       struct client *client = *_client;
+
+       if (client == NULL)
+               return;
+       *_client = NULL;
+
+       i_assert(client->refcount > 0);
+       if (--client->refcount > 0)
+               return;
+
+       client->v.destroy(client);
+}
+
 void client_destroy(struct client **_client, const char *enh_code,
                    const char *reason)
 {
@@ -383,7 +405,7 @@ static void client_connection_free(void *context)
 {
        struct client *client = context;
 
-       client->v.destroy(client);
+       client_unref(&client);
 }
 
 static bool client_connection_is_trusted(void *context)
index 00ed96dda62f405d3499b9dfee238539501ea4ff..3bec871abd7faf02c1900db605fca8a58396b05f 100644 (file)
@@ -67,6 +67,7 @@ struct lmtp_client_vfuncs {
 
 struct client {
        struct client *prev, *next;
+       int refcount;
        pool_t pool;
 
        struct lmtp_client_vfuncs v;
@@ -112,6 +113,8 @@ extern struct lmtp_module_register lmtp_module_register;
 
 struct client *client_create(int fd_in, int fd_out,
                             const struct master_service_connection *conn);
+void client_ref(struct client *client);
+void client_unref(struct client **_client);
 void client_destroy(struct client **client, const char *enh_code,
                    const char *reason) ATTR_NULL(2, 3);