commit 4d656c8: [Fix] Preserve fd when mapping file to scan

Vsevolod Stakhov vsevolod at highsecure.ru
Thu May 9 09:49:03 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-05-09 10:43:12 +0100
URL: https://github.com/rspamd/rspamd/commit/4d656c8d0af7e793dbcf29fb2990af09ebdb74f9 (HEAD -> master)

[Fix] Preserve fd when mapping file to scan

---
 src/libserver/task.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/src/libserver/task.c b/src/libserver/task.c
index d0ef40621..b69b96341 100644
--- a/src/libserver/task.c
+++ b/src/libserver/task.c
@@ -355,6 +355,7 @@ rspamd_task_free (struct rspamd_task *task)
 struct rspamd_task_map {
 	gpointer begin;
 	gulong len;
+	gint fd;
 };
 
 static void
@@ -363,6 +364,7 @@ rspamd_task_unmapper (gpointer ud)
 	struct rspamd_task_map *m = ud;
 
 	munmap (m->begin, m->len);
+	close (m->fd);
 }
 
 gboolean
@@ -437,8 +439,6 @@ rspamd_task_load_message (struct rspamd_task *task,
 			return FALSE;
 		}
 
-		close (fd);
-
 		tok = rspamd_task_get_request_header (task, "shm-offset");
 
 		if (tok) {
@@ -448,6 +448,7 @@ rspamd_task_load_message (struct rspamd_task *task,
 				msg_err_task ("invalid offset %ul (%ul available) for shm "
 						"segment %s", offset, st.st_size, fp);
 				munmap (map, st.st_size);
+				close (fd);
 
 				return FALSE;
 			}
@@ -464,6 +465,7 @@ rspamd_task_load_message (struct rspamd_task *task,
 				msg_err_task ("invalid length %ul (%ul available) for %s "
 						"segment %s", shmem_size, st.st_size, ft, fp);
 				munmap (map, st.st_size);
+				close (fd);
 
 				return FALSE;
 			}
@@ -474,9 +476,10 @@ rspamd_task_load_message (struct rspamd_task *task,
 		m = rspamd_mempool_alloc (task->task_pool, sizeof (*m));
 		m->begin = map;
 		m->len = st.st_size;
+		m->fd = fd;
 
-		msg_info_task ("loaded message from shared memory %s (%ul size, %ul offset)",
-				fp, shmem_size, offset);
+		msg_info_task ("loaded message from shared memory %s (%ul size, %ul offset), fd=%d",
+				fp, shmem_size, offset, fd);
 
 		rspamd_mempool_add_destructor (task->task_pool, rspamd_task_unmapper, m);
 
@@ -540,12 +543,12 @@ rspamd_task_load_message (struct rspamd_task *task,
 				return FALSE;
 			}
 
-			close (fd);
 			task->msg.begin = map;
 			task->msg.len = st.st_size;
 			m = rspamd_mempool_alloc (task->task_pool, sizeof (*m));
 			m->begin = map;
 			m->len = st.st_size;
+			m->fd = fd;
 
 			rspamd_mempool_add_destructor (task->task_pool, rspamd_task_unmapper, m);
 		}


More information about the Commits mailing list