git-svn-id: file:///var/lib/svn/clamav-devel/trunk/clamav-devel@2514 77e5149b-7576-45b1-b177-96237e5ba77b
Nigel Horne authored on 2006/11/23 18:44:30... | ... |
@@ -29,7 +29,7 @@ |
29 | 29 |
* TODO: Check the NGS code for vulnerabilities, leaks etc. |
30 | 30 |
* TODO: Check the NGS code is thread safe |
31 | 31 |
*/ |
32 |
-static char const rcsid[] = "$Id: jscript.c,v 1.8 2006/11/21 10:46:00 njh Exp $"; |
|
32 |
+static char const rcsid[] = "$Id: jscript.c,v 1.9 2006/11/23 09:44:30 njh Exp $"; |
|
33 | 33 |
|
34 | 34 |
#if HAVE_CONFIG_H |
35 | 35 |
#include "clamav-config.h" |
... | ... |
@@ -288,13 +288,6 @@ struct args { |
288 | 288 |
int result; |
289 | 289 |
}; |
290 | 290 |
|
291 |
-static void |
|
292 |
-sigrecv(int sig) |
|
293 |
-{ |
|
294 |
- /* pthread_cond_broadcast(&cond); */ |
|
295 |
- pthread_exit(NULL); /* FIXME: interp isn't destroyed - mem leak? */ |
|
296 |
-} |
|
297 |
- |
|
298 | 291 |
static void * |
299 | 292 |
js_thread(void *a) |
300 | 293 |
{ |
... | ... |
@@ -303,6 +296,7 @@ js_thread(void *a) |
303 | 303 |
struct args *args = (struct args *)a; |
304 | 304 |
const char *dir = args->dir; |
305 | 305 |
const char *filename = args->filename; |
306 |
+ int otype; |
|
306 | 307 |
|
307 | 308 |
cli_dbgmsg("run_js(%s)\n", filename); |
308 | 309 |
|
... | ... |
@@ -332,11 +326,17 @@ js_thread(void *a) |
332 | 332 |
|
333 | 333 |
args->result = CL_EIO; /* TODO: CL_TIMEOUT */ |
334 | 334 |
|
335 |
+ pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &otype); |
|
336 |
+ |
|
335 | 337 |
if(!js_eval_file(interp, filename)) { |
336 | 338 |
cli_warnmsg("JS failed: %s\n", js_error_message(interp)); |
337 | 339 |
/*rc = CL_EIO;*/ |
338 | 340 |
} |
339 | 341 |
|
342 |
+ /* |
|
343 |
+ * If a pthread_cancel() is issued exactly here, js_destroy_interp() |
|
344 |
+ * wouldn't be called, leading to a memory leak |
|
345 |
+ */ |
|
340 | 346 |
if(pthread_cond_broadcast(args->cond) < 0) |
341 | 347 |
perror("pthread_cond_broadcast"); |
342 | 348 |
|
... | ... |
@@ -357,7 +357,6 @@ run_js(const char *filename, const char *dir) |
357 | 357 |
struct timeval tp; |
358 | 358 |
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
359 | 359 |
pthread_cond_t cond = PTHREAD_COND_INITIALIZER; |
360 |
- void (*oldkill)(int); |
|
361 | 360 |
|
362 | 361 |
args.filename = filename; |
363 | 362 |
args.dir = dir; |
... | ... |
@@ -370,17 +369,16 @@ run_js(const char *filename, const char *dir) |
370 | 370 |
ts.tv_sec = tp.tv_sec + VM_TIMEOUT; |
371 | 371 |
ts.tv_nsec = tp.tv_usec * 1000; |
372 | 372 |
|
373 |
- oldkill = signal(SIGUSR1, sigrecv); |
|
374 | 373 |
pthread_mutex_lock(&mutex); |
375 | 374 |
if(pthread_cond_timedwait(&cond, &mutex, &ts) == ETIMEDOUT) { |
376 | 375 |
cli_warnmsg("Run away javascript stopped after %d seconds\n", |
377 | 376 |
VM_TIMEOUT); |
378 |
- /* FIXME: should use pthread_cancel? */ |
|
379 |
- pthread_kill(tid, SIGUSR1); |
|
377 |
+ /*pthread_kill(tid, SIGUSR1);*/ |
|
378 |
+ if(pthread_cancel(tid) < 0) |
|
379 |
+ perror("pthread_cancel"); |
|
380 | 380 |
} |
381 | 381 |
pthread_mutex_unlock(&mutex); |
382 | 382 |
pthread_join(tid, NULL); |
383 |
- signal(SIGUSR1, oldkill); |
|
384 | 383 |
|
385 | 384 |
return args.result; |
386 | 385 |
} |