Browse code

clamd: revert patch from bb#1028 (bb#1113)

git-svn: trunk@4029

Tomasz Kojm authored on 2008/07/30 06:35:58
Showing 4 changed files
... ...
@@ -1,3 +1,7 @@
1
+Tue Jul 29 23:18:23 CEST 2008 (tk)
2
+----------------------------------
3
+  * clamd: revert patch from bb#1028 (bb#1113)
4
+
1 5
 Tue Jul 29 13:18:24 EEST 2008 (edwin)
2 6
 ------------------------------------
3 7
   * libclamav/regex_*.[ch]: handle multiple matches (bb #1110)
... ...
@@ -83,16 +83,6 @@ typedef struct client_conn_tag {
83 83
     int nsockets;
84 84
 } client_conn_t;
85 85
 
86
-static void scanner_thread_cleanup(void *arg)
87
-{
88
-	client_conn_t *conn = (client_conn_t *) arg;
89
-
90
-    shutdown(conn->sd, 2);
91
-    closesocket(conn->sd);
92
-    cl_free(conn->engine);
93
-    free(conn);
94
-}
95
-
96 86
 static void scanner_thread(void *arg)
97 87
 {
98 88
 	client_conn_t *conn = (client_conn_t *) arg;
... ...
@@ -120,9 +110,6 @@ static void scanner_thread(void *arg)
120 120
     if(!timeout)
121 121
     	timeout = -1;
122 122
 
123
-    /* register cleanup procedure in this thread */
124
-    pthread_cleanup_push(scanner_thread_cleanup, arg);
125
-   
126 123
     do {
127 124
     	ret = command(conn->sd, conn->engine, conn->limits, conn->options, conn->copt, timeout);
128 125
 	if (ret < 0) {
... ...
@@ -168,7 +155,10 @@ static void scanner_thread(void *arg)
168 168
 	}
169 169
     } while (session);
170 170
 
171
-    pthread_cleanup_pop(1);
171
+    shutdown(conn->sd, 2);
172
+    closesocket(conn->sd);
173
+    cl_free(conn->engine);
174
+    free(conn);
172 175
     return;
173 176
 }
174 177
 
... ...
@@ -644,10 +634,6 @@ int acceptloop_th(int *socketds, int nsockets, struct cl_engine *engine, unsigne
644 644
 
645 645
 	pthread_mutex_lock(&reload_mutex);
646 646
 	if(reload) {
647
-#ifdef OPTIMIZE_MEMORY_FOOTPRINT
648
-	    /* Signal all worker threads to STOP, wait till no more acive threads */
649
-	    thrmgr_worker_stop_wait(thr_pool);
650
-#endif	    
651 647
 	    pthread_mutex_unlock(&reload_mutex);
652 648
 	    engine = reload_db(engine, dboptions, copt, FALSE, &ret);
653 649
 	    if(ret) {
... ...
@@ -665,10 +651,6 @@ int acceptloop_th(int *socketds, int nsockets, struct cl_engine *engine, unsigne
665 665
 	    reload = 0;
666 666
 	    time(&reloaded_time);
667 667
 	    pthread_mutex_unlock(&reload_mutex);
668
-#ifdef OPTIMIZE_MEMORY_FOOTPRINT    
669
-	    /* Resume thread pool worker threads */
670
-	    thrmgr_setstate(thr_pool, POOL_VALID);
671
-#endif
672 668
 #ifdef CLAMUKO
673 669
 	    if(cfgopt(copt, "ClamukoScanOnAccess")->enabled) {
674 670
 		logg("Stopping and restarting Clamuko.\n");
... ...
@@ -215,98 +215,6 @@ threadpool_t *thrmgr_new(int max_threads, int idle_timeout, void (*handler)(void
215 215
 	return threadpool;
216 216
 }
217 217
 
218
-#ifdef OPTIMIZE_MEMORY_FOOTPRINT
219
-/**
220
- * thrmgr_worker_stop_wait : set state to POOL_STOP, wake all thread worker, wait for them
221
- * to exit before continuing.
222
- */
223
-void thrmgr_worker_stop_wait(threadpool_t * const threadpool)
224
-{
225
-	struct timespec timeout;
226
-	int ret_cond;
227
-	int loop = 2;
228
-	
229
-	if (!threadpool || (threadpool->state != POOL_VALID)) {
230
-		return;
231
-	}
232
-  	if (pthread_mutex_lock(&threadpool->pool_mutex) != 0) {
233
-   		logg("!Mutex lock failed\n");
234
-    		exit(-1);
235
-	}
236
-	threadpool->state = POOL_STOP;
237
-	
238
-	/* wait for threads to exit */
239
-	if (threadpool->thr_alive > 0) {
240
-#ifdef CL_DEBUG
241
-		logg("*%u active threads: waking them and entering wait loop\n", threadpool->thr_alive);
242
-#endif
243
-		if (pthread_cond_broadcast(&(threadpool->pool_cond)) != 0) {
244
-			pthread_mutex_unlock(&threadpool->pool_mutex);
245
-			logg("!Fatal: failed in cond broadcast 'pool_cond'\n");
246
-			return;
247
-		}
248
-	}
249
-	/* now, wait for the threads to exit, make 'loop' number of tries,  */
250
-	while (threadpool->thr_alive > 0 && loop--) {		
251
-#ifdef CL_DEBUG
252
-		logg("*%u active threads. Waiting.\n", threadpool->thr_alive);
253
-#endif
254
-		timeout.tv_sec = time(NULL) + (threadpool->idle_timeout/2) + 10L;
255
-		timeout.tv_nsec = 0;
256
-		ret_cond = pthread_cond_timedwait (&threadpool->pool_cond, &threadpool->pool_mutex, &timeout);
257
-		if (ret_cond == ETIMEDOUT) {
258
-#ifdef CL_DEBUG
259
-			logg("*%u active threads. Continue to wait.\n", threadpool->thr_alive);
260
-#endif
261
-		} else if (ret_cond == 0) {
262
-#ifdef CL_DEBUG
263
-			logg("*Received signal. %u active threads.\n", threadpool->thr_alive);
264
-#endif
265
-		}
266
-	}
267
-  	if (pthread_mutex_unlock(&threadpool->pool_mutex) != 0) {
268
-    		logg("!Mutex unlock failed\n");
269
-    		exit(-1);
270
-  	}
271
-}
272
-#endif
273
-#ifdef OPTIMIZE_MEMORY_FOOTPRINT
274
-void thrmgr_setstate(threadpool_t * const threadpool, pool_state_t state )
275
-{
276
-  	if (pthread_mutex_lock(&threadpool->pool_mutex) != 0) {
277
-   		logg("!Mutex lock failed\n");
278
-    		exit(-1);
279
-	}
280
-	threadpool->state = state;
281
-  	if (pthread_mutex_unlock(&threadpool->pool_mutex) != 0) {
282
-    		logg("!Mutex unlock failed\n");
283
-    		exit(-1);
284
-  	}
285
-}
286
-#endif
287
-
288
-static void *thrmgr_worker_cleanup(void *arg)
289
-{
290
-	threadpool_t *threadpool = (threadpool_t *) arg;
291
-	
292
-	if (pthread_mutex_lock(&(threadpool->pool_mutex)) != 0) {
293
-		/* Fatal error */
294
-		logg("!Fatal: mutex lock failed\n");
295
-		exit(-2);
296
-	}
297
-	(threadpool->thr_alive) && threadpool->thr_alive--;
298
-	/* logg("*Thread clean up, %u active threads.", threadpool->thr_alive); */
299
-	if (threadpool->thr_alive == 0) {
300
-		/* signal that all threads are finished */
301
-		pthread_cond_broadcast(&threadpool->pool_cond);
302
-	}
303
-	if (pthread_mutex_unlock(&(threadpool->pool_mutex)) != 0) {
304
-		/* Fatal error */
305
-		logg("!Fatal: mutex unlock failed\n");
306
-		exit(-2);
307
-	}
308
-}
309
-
310 218
 static void *thrmgr_worker(void *arg)
311 219
 {
312 220
 	threadpool_t *threadpool = (threadpool_t *) arg;
... ...
@@ -314,9 +222,6 @@ static void *thrmgr_worker(void *arg)
314 314
 	int retval, must_exit = FALSE;
315 315
 	struct timespec timeout;
316 316
 	
317
-	/* Register cleanup procedure for worker in current thread */
318
-	pthread_cleanup_push(thrmgr_worker_cleanup, arg);
319
-	
320 317
 	/* loop looking for work */
321 318
 	for (;;) {
322 319
 		if (pthread_mutex_lock(&(threadpool->pool_mutex)) != 0) {
... ...
@@ -326,15 +231,16 @@ static void *thrmgr_worker(void *arg)
326 326
 		timeout.tv_sec = time(NULL) + threadpool->idle_timeout;
327 327
 		timeout.tv_nsec = 0;
328 328
 		threadpool->thr_idle++;
329
-		while ( must_exit == FALSE 
330
-				&& ((job_data = work_queue_pop(threadpool->queue)) == NULL)
329
+		while (((job_data=work_queue_pop(threadpool->queue)) == NULL)
331 330
 				&& (threadpool->state != POOL_EXIT)) {
332 331
 			/* Sleep, awaiting wakeup */
333 332
 			pthread_cond_signal(&threadpool->idle_cond);
334 333
 			retval = pthread_cond_timedwait(&(threadpool->pool_cond),
335 334
 				&(threadpool->pool_mutex), &timeout);
336
-			if (retval == ETIMEDOUT)
335
+			if (retval == ETIMEDOUT) {
337 336
 				must_exit = TRUE;
337
+				break;
338
+			}
338 339
 		}
339 340
 		threadpool->thr_idle--;
340 341
 		if (threadpool->state == POOL_EXIT) {
... ...
@@ -345,17 +251,27 @@ static void *thrmgr_worker(void *arg)
345 345
 			logg("!Fatal: mutex unlock failed\n");
346 346
 			exit(-2);
347 347
 		}
348
-		if (must_exit) break;
349
-		if (job_data) threadpool->handler(job_data);
350
-		if (threadpool->state == POOL_STOP) break;
348
+		if (job_data) {
349
+			threadpool->handler(job_data);
350
+		} else if (must_exit) {
351
+			break;
352
+		}
353
+	}
354
+	if (pthread_mutex_lock(&(threadpool->pool_mutex)) != 0) {
355
+		/* Fatal error */
356
+		logg("!Fatal: mutex lock failed\n");
357
+		exit(-2);
358
+	}
359
+	threadpool->thr_alive--;
360
+	if (threadpool->thr_alive == 0) {
361
+		/* signal that all threads are finished */
362
+		pthread_cond_broadcast(&threadpool->pool_cond);
363
+	}
364
+	if (pthread_mutex_unlock(&(threadpool->pool_mutex)) != 0) {
365
+		/* Fatal error */
366
+		logg("!Fatal: mutex unlock failed\n");
367
+		exit(-2);
351 368
 	}
352
-
353
-#ifdef HAVE_PTHREAD_YIELD
354
-	pthread_yield(); /* do not remove on premptive kernel e.g linux 2.6 */
355
-#elif HAVE_SCHED_YIELD
356
-	sched_yield();
357
-#endif
358
-	pthread_cleanup_pop(1);
359 369
 	return NULL;
360 370
 }
361 371
 
... ...
@@ -26,13 +26,6 @@
26 26
 #include <sys/time.h>
27 27
 #endif
28 28
 
29
-/**
30
- * OPTIMIZE_MEMORY_FOOTPRINT : #ifdef, it will force all worker threads to terminate
31
- * before switching to new database, thereby, avoiding fragmenting memory due which
32
- * will cause swelling resident memory during run-time.
33
- */
34
-#define OPTIMIZE_MEMORY_FOOTPRINT
35
-
36 29
 typedef struct work_item_tag {
37 30
 	struct work_item_tag *next;
38 31
 	void *data;
... ...
@@ -48,7 +41,6 @@ typedef struct work_queue_tag {
48 48
 typedef enum {
49 49
 	POOL_INVALID,
50 50
 	POOL_VALID,
51
-	POOL_STOP,	/* All worker threads should exit */
52 51
 	POOL_EXIT
53 52
 } pool_state_t;
54 53
 
... ...
@@ -74,13 +66,4 @@ threadpool_t *thrmgr_new(int max_threads, int idle_timeout, void (*handler)(void
74 74
 void thrmgr_destroy(threadpool_t *threadpool);
75 75
 int thrmgr_dispatch(threadpool_t *threadpool, void *user_data);
76 76
 
77
-#ifdef OPTIMIZE_MEMORY_FOOTPRINT
78
-/**
79
- * thrmgr_worker_stop_wait : set state to POOL_STOP, wake all thread worker, wait for them
80
- * to exit before continuing.
81
- */
82
-void thrmgr_worker_stop_wait(threadpool_t * const threadpool);
83
-void thrmgr_setstate(threadpool_t * const threadpool, pool_state_t state);
84
-#endif
85
-
86 77
 #endif