Browse code

Added thread safety

git-svn-id: file:///var/lib/svn/clamav-devel/trunk/clamav-devel@614 77e5149b-7576-45b1-b177-96237e5ba77b

Nigel Horne authored on 2004/06/16 17:07:39
Showing 3 changed files
... ...
@@ -16,6 +16,9 @@
16 16
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 17
  *
18 18
  * $Log: blob.c,v $
19
+ * Revision 1.13  2004/06/16 08:07:39  nigelhorne
20
+ * Added thread safety
21
+ *
19 22
  * Revision 1.12  2004/05/21 11:31:48  nigelhorne
20 23
  * Fix logic error in blobClose
21 24
  *
... ...
@@ -38,7 +41,7 @@
38 38
  * Change LOG to Log
39 39
  *
40 40
  */
41
-static	char	const	rcsid[] = "$Id: blob.c,v 1.12 2004/05/21 11:31:48 nigelhorne Exp $";
41
+static	char	const	rcsid[] = "$Id: blob.c,v 1.13 2004/06/16 08:07:39 nigelhorne Exp $";
42 42
 
43 43
 #if HAVE_CONFIG_H
44 44
 #include "clamav-config.h"
... ...
@@ -201,11 +204,18 @@ blobGetDataSize(const blob *b)
201 201
 void
202 202
 blobClose(blob *b)
203 203
 {
204
-	if(b->size > b->len) {
204
+	/*
205
+	 * Nothing more is going to be added to this blob. If it'll save more
206
+	 * than a trivial amount (say 64 bytes) of memory, shrink the allocation
207
+	 */
208
+	if((b->size - b->len) >= 64) {
205 209
 		unsigned char *ptr = cli_realloc(b->data, b->len);
206 210
 
207 211
 		if(ptr == NULL)
208 212
 			return;
213
+
214
+		cli_dbgmsg("blobClose: recovered %u bytes from %u\n",
215
+			b->size - b->len, b->size);
209 216
 		b->size = b->len;
210 217
 		b->data = ptr;
211 218
 	}
... ...
@@ -17,6 +17,9 @@
17 17
  *
18 18
  * Change History:
19 19
  * $Log: mbox.c,v $
20
+ * Revision 1.76  2004/06/16 08:07:39  nigelhorne
21
+ * Added thread safety
22
+ *
20 23
  * Revision 1.75  2004/06/14 09:07:10  nigelhorne
21 24
  * Handle spam using broken e-mail generators for multipart/alternative
22 25
  *
... ...
@@ -213,7 +216,7 @@
213 213
  * Compilable under SCO; removed duplicate code with message.c
214 214
  *
215 215
  */
216
-static	char	const	rcsid[] = "$Id: mbox.c,v 1.75 2004/06/14 09:07:10 nigelhorne Exp $";
216
+static	char	const	rcsid[] = "$Id: mbox.c,v 1.76 2004/06/16 08:07:39 nigelhorne Exp $";
217 217
 
218 218
 #if HAVE_CONFIG_H
219 219
 #include "clamav-config.h"
... ...
@@ -244,6 +247,10 @@ static	char	const	rcsid[] = "$Id: mbox.c,v 1.75 2004/06/14 09:07:10 nigelhorne E
244 244
 #include <sys/param.h>
245 245
 #include <clamav.h>
246 246
 
247
+#ifdef	CL_THREAD_SAFE
248
+#include <pthread.h>
249
+#endif
250
+
247 251
 #include "table.h"
248 252
 #include "mbox.h"
249 253
 #include "blob.h"
... ...
@@ -333,6 +340,10 @@ static	const	struct tableinit {
333 333
 	{	"appledouble",	APPLEDOUBLE	},
334 334
 	{	NULL,		0		}
335 335
 };
336
+
337
+#ifdef	CL_THREAD_SAFE
338
+static	pthread_mutex_t	tables_mutex = PTHREAD_MUTEX_INITIALIZER;
339
+#endif
336 340
 static	table_t	*rfc821Table, *subtypeTable;
337 341
 
338 342
 /* Maximum filenames under various systems */
... ...
@@ -398,15 +409,26 @@ cl_mbox(const char *dir, int desc)
398 398
 		return 0;
399 399
 	}
400 400
 
401
+#ifdef	CL_THREAD_SAFE
402
+	pthread_mutex_lock(&tables_mutex);
403
+#endif
401 404
 	if(rfc821Table == NULL) {
402 405
 		assert(subtypeTable == NULL);
403 406
 
404 407
 		if(initialiseTables(&rfc821Table, &subtypeTable) < 0) {
408
+			rfc821Table = NULL;
409
+			subtypeTable = NULL;
410
+#ifdef	CL_THREAD_SAFE
411
+			pthread_mutex_unlock(&tables_mutex);
412
+#endif
405 413
 			messageDestroy(m);
406 414
 			fclose(fd);
407 415
 			return -1;
408 416
 		}
409 417
 	}
418
+#ifdef	CL_THREAD_SAFE
419
+	pthread_mutex_unlock(&tables_mutex);
420
+#endif
410 421
 
411 422
 	/*
412 423
 	 * is it a UNIX style mbox with more than one
... ...
@@ -1369,7 +1391,7 @@ parseEmailBody(message *messageIn, blob **blobsIn, int nBlobs, text *textIn, con
1369 1369
 				break;
1370 1370
 			} else if(strcasecmp(mimeSubtype, "partial") == 0)
1371 1371
 				/* TODO */
1372
-				cli_warnmsg("Content-type message/partial not yet supported");
1372
+				cli_warnmsg("Content-type message/partial not yet supported\n");
1373 1373
 			else if(strcasecmp(mimeSubtype, "external-body") == 0)
1374 1374
 				/*
1375 1375
 				 * I don't believe that we should be going
... ...
@@ -17,6 +17,9 @@
17 17
  *
18 18
  * Change History:
19 19
  * $Log: message.c,v $
20
+ * Revision 1.60  2004/06/16 08:07:39  nigelhorne
21
+ * Added thread safety
22
+ *
20 23
  * Revision 1.59  2004/06/02 10:11:09  nigelhorne
21 24
  * Corrupted binHex could crash on non Linux systems
22 25
  *
... ...
@@ -174,7 +177,7 @@
174 174
  * uuencodebegin() no longer static
175 175
  *
176 176
  */
177
-static	char	const	rcsid[] = "$Id: message.c,v 1.59 2004/06/02 10:11:09 nigelhorne Exp $";
177
+static	char	const	rcsid[] = "$Id: message.c,v 1.60 2004/06/16 08:07:39 nigelhorne Exp $";
178 178
 
179 179
 #if HAVE_CONFIG_H
180 180
 #include "clamav-config.h"
... ...
@@ -200,6 +203,10 @@ static	char	const	rcsid[] = "$Id: message.c,v 1.59 2004/06/02 10:11:09 nigelhorn
200 200
 #include <ctype.h>
201 201
 #include <stdio.h>
202 202
 
203
+#ifdef	CL_THREAD_SAFE
204
+#include <pthread.h>
205
+#endif
206
+
203 207
 #include "mbox.h"
204 208
 #include "table.h"
205 209
 #include "blob.h"
... ...
@@ -310,6 +317,9 @@ messageReset(message *m)
310 310
 void
311 311
 messageSetMimeType(message *mess, const char *type)
312 312
 {
313
+#ifdef	CL_THREAD_SAFE
314
+	static pthread_mutex_t mime_mutex = PTHREAD_MUTEX_INITIALIZER;
315
+#endif
313 316
 	static table_t *mime_table;
314 317
 	int typeval;
315 318
 
... ...
@@ -325,19 +335,33 @@ messageSetMimeType(message *mess, const char *type)
325 325
 		if(*type++ == '\0')
326 326
 			return;
327 327
 
328
+#ifdef	CL_THREAD_SAFE
329
+	pthread_mutex_lock(&mime_mutex);
330
+#endif
328 331
 	if(mime_table == NULL) {
329 332
 		const struct mime_map *m;
330 333
 
331 334
 		mime_table = tableCreate();
332
-		if(mime_table == NULL)
335
+		if(mime_table == NULL) {
336
+#ifdef	CL_THREAD_SAFE
337
+			pthread_mutex_unlock(&mime_mutex);
338
+#endif
333 339
 			return;
340
+		}
334 341
 
335 342
 		for(m = mime_map; m->string; m++)
336 343
 			if(!tableInsert(mime_table, m->string, m->type)) {
337 344
 				tableDestroy(mime_table);
345
+				mime_table = NULL;
346
+#ifdef	CL_THREAD_SAFE
347
+				pthread_mutex_unlock(&mime_mutex);
348
+#endif
338 349
 				return;
339 350
 			}
340 351
 	}
352
+#ifdef	CL_THREAD_SAFE
353
+	pthread_mutex_unlock(&mime_mutex);
354
+#endif
341 355
 
342 356
 	typeval = tableFind(mime_table, type);
343 357
 
... ...
@@ -783,23 +807,13 @@ messageAddLine(message *m, const char *line, int takeCopy)
783 783
 	/*
784 784
 	 * See if this line marks the start of a non MIME inclusion that
785 785
 	 * will need to be scanned
786
-	 *
787
-	 * Notes that X- lines are not taken as start of mails because
788
-	 * cli_filetype() is too keen: any line it finds that starts X-
789
-	 * is seen to be the start of a new message, which results in too
790
-	 * many false positives for locating the possible start of a bounce,
791
-	 * and allows some instances of Worm.SomeFool.Gen-1 to get through in
792
-	 * bounce messages which end up not being correctly handled because
793
-	 * the real start of a bounce header is missed because of the earlier
794
-	 * false positive
795 786
 	 */
796
-	if(line) {
787
+	if(line && *line) {
797 788
 		if((m->encoding == NULL) &&
798 789
 		   (strncasecmp(line, encoding, sizeof(encoding) - 1) == 0) &&
799 790
 		   (strstr(line, "7bit") == NULL))
800 791
 			m->encoding = m->body_last;
801 792
 		else if((m->bounce == NULL) &&
802
-			(strncmp(line, "X-", 2) != 0) &&	/*!!*/
803 793
 			(cli_filetype(line, strlen(line)) == CL_MAILFILE))
804 794
 				m->bounce = m->body_last;
805 795
 		else if((m->binhex == NULL) &&