Browse code

c4w: new error codes.

CL_ELOCK, CL_EBUSY, CL_ESTATE.

Török Edvin authored on 2010/12/08 18:23:43
Showing 3 changed files
... ...
@@ -69,6 +69,11 @@ typedef enum {
69 69
     CL_EBYTECODE,/* may be reported in testmode */
70 70
     CL_EBYTECODE_TESTFAIL, /* may be reported in testmode */
71 71
 
72
+    /* c4w error codes */
73
+    CL_ELOCK,
74
+    CL_EBUSY,
75
+    CL_ESTATE,
76
+
72 77
     /* no error codes below this line please */
73 78
     CL_ELAST_ERROR
74 79
 } cl_error_t;
... ...
@@ -251,6 +251,12 @@ const char *cl_strerror(int clerror)
251 251
 	    return "Error during bytecode execution";
252 252
 	case CL_EBYTECODE_TESTFAIL:
253 253
 	    return "Failure in bytecode testmode";
254
+	case CL_ELOCK:
255
+	    return "Mutex lock failed";
256
+	case CL_EBUSY:
257
+	    return "Scanner still active";
258
+	case CL_ESTATE:
259
+	    return "Bad state (engine not initialized, or already initialized)";
254 260
 	default:
255 261
 	    return "Unknown error code";
256 262
     }
... ...
@@ -193,7 +193,7 @@ static int del_instance(instance *inst) {
193 193
     INFN();
194 194
     if(lock_instances()) {
195 195
 	logg("!del_instance: failed to lock instances\n");
196
-	return 1;
196
+	return CL_ELOCK;
197 197
     }
198 198
     for(i=0; i<ninsts_total; i++) {
199 199
 	if(instances[i].inst != inst)
... ...
@@ -201,18 +201,18 @@ static int del_instance(instance *inst) {
201 201
 	if(instances[i].refcnt) {
202 202
 	    logg("!del_instance: attempted to free instance with %d active scanners\n", instances[i].refcnt);
203 203
 	    unlock_instances();
204
-	    return 1;
204
+	    return CL_EBUSY;
205 205
 	}
206 206
 	instances[i].inst = NULL;
207 207
 	instances[i].refcnt = 0;
208 208
 	ninsts_avail++;
209 209
 	logg("del_instance: %u / %u instances now available\n", ninsts_avail, ninsts_total);
210 210
 	unlock_instances();
211
-	return 0;
211
+	return CL_SUCCESS;
212 212
     }
213 213
     logg("!del_instances: instance not found\n");
214 214
     unlock_instances();
215
-    return 1;
215
+    return CL_EARG;
216 216
 }
217 217
 
218 218
 /* To be called with the instances locked */
... ...
@@ -342,10 +342,10 @@ int CLAMAPI Scan_Initialize(const wchar_t *pEnginesFolder, const wchar_t *pTempR
342 342
     if(!pTempRoot)
343 343
 	FAIL(CL_ENULLARG, "pTempRoot is NULL");
344 344
     if(lock_engine())
345
-	FAIL(CL_EMEM, "failed to lock engine");
345
+	FAIL(CL_ELOCK, "failed to lock engine");
346 346
     if(engine) {
347 347
 	unlock_engine();
348
-	FAIL(CL_EARG, "Already initialized");
348
+	FAIL(CL_ESTATE, "Already initialized");
349 349
     }
350 350
 
351 351
     if(!(engine = cl_engine_new())) {
... ...
@@ -393,27 +393,27 @@ int CLAMAPI Scan_Uninitialize(void) {
393 393
     uninitialize_called = 1;
394 394
     INFN();
395 395
     if(lock_engine())
396
-	FAIL(CL_EMEM, "failed to lock engine");
396
+	FAIL(CL_ELOCK, "failed to lock engine");
397 397
     if(!engine) {
398 398
 	unlock_engine();
399
-	FAIL(CL_EARG, "attempted to uninit a NULL engine");
399
+	FAIL(CL_ESTATE, "attempted to uninit a NULL engine");
400 400
     }
401 401
    if(lock_instances()) {
402 402
 	unlock_engine();
403
-	FAIL(CL_EMEM, "failed to lock instances");
403
+	FAIL(CL_ELOCK, "failed to lock instances");
404 404
     }
405 405
     if(ninsts_avail != ninsts_total) {
406 406
 	volatile unsigned int refcnt = ninsts_total - ninsts_avail;
407 407
 	unlock_instances();
408 408
 	unlock_engine();
409
-	FAIL(CL_EARG, "Attempted to uninit the engine with %u active instances", refcnt);
409
+	FAIL(CL_EBUSY, "Attempted to uninit the engine with %u active instances", refcnt);
410 410
     }
411 411
     unlock_instances();
412 412
     free_engine_and_unlock();
413 413
 
414 414
     if(monitor_hdl) {
415 415
 	SetEvent(monitor_event);
416
-	if(WaitForSingleObject(monitor_hdl, 60000) != WAIT_OBJECT_0) {
416
+	if(WaitForSingleObject(monitor_hdl, 5000) != WAIT_OBJECT_0) {
417 417
 	    logg("Scan_Uninitialize: forcibly terminating monitor thread after 60 seconds\n");
418 418
 	    TerminateThread(monitor_hdl, 0);
419 419
 	}
... ...
@@ -433,12 +433,12 @@ int CLAMAPI Scan_CreateInstance(CClamAVScanner **ppScanner) {
433 433
 	FAIL(CL_EMEM, "CreateInstance: OOM");
434 434
     if(lock_engine()) {
435 435
 	free(inst);
436
-	FAIL(CL_EMEM, "Failed to lock engine");
436
+	FAIL(CL_ELOCK, "Failed to lock engine");
437 437
     }
438 438
     if(!engine) {
439 439
 	free(inst);
440 440
 	unlock_engine();
441
-	FAIL(CL_ENULLARG, "Create instance called with no engine");
441
+	FAIL(CL_ESTATE, "Create instance called with no engine");
442 442
     }
443 443
     if(add_instance(inst)) {
444 444
 	free(inst);
... ...
@@ -453,11 +453,12 @@ int CLAMAPI Scan_CreateInstance(CClamAVScanner **ppScanner) {
453 453
 }
454 454
 
455 455
 int CLAMAPI Scan_DestroyInstance(CClamAVScanner *pScanner) {
456
+    int rc;
456 457
     INFN();
457 458
     if(!pScanner)
458 459
 	FAIL(CL_ENULLARG, "NULL pScanner");
459
-    if(del_instance((instance *)pScanner))
460
-	FAIL(CL_EMEM, "del_instance failed for %p", pScanner);
460
+    if((rc = del_instance((instance *)pScanner)))
461
+	FAIL(rc, "del_instance failed for %p", pScanner);
461 462
     free(pScanner);
462 463
     logg("in Scan_DestroyInstance: Instance %p destroyed\n", pScanner);
463 464
     WIN();
... ...
@@ -470,7 +471,7 @@ int CLAMAPI Scan_SetScanCallback(CClamAVScanner *pScanner, CLAM_SCAN_CALLBACK pf
470 470
     if(!pScanner)
471 471
 	FAIL(CL_ENULLARG, "NULL pScanner");
472 472
     if(lock_instances())
473
-	FAIL(CL_EMEM, "failed to lock instances for instance %p", pScanner);
473
+	FAIL(CL_ELOCK, "failed to lock instances for instance %p", pScanner);
474 474
 
475 475
     inst = (instance *)pScanner;
476 476
     if(is_instance(inst)) {
... ...
@@ -493,7 +494,7 @@ int CLAMAPI Scan_SetOption(CClamAVScanner *pScanner, int option, void *value, un
493 493
     if(!value)
494 494
 	FAIL(CL_ENULLARG, "NULL value");
495 495
     if(lock_instances())
496
-	FAIL(CL_EMEM, "failed to lock instances");
496
+	FAIL(CL_ELOCK, "failed to lock instances");
497 497
 
498 498
     inst = (instance *)pScanner;
499 499
     if(!is_instance(inst)) {
... ...
@@ -557,7 +558,7 @@ int CLAMAPI Scan_GetOption(CClamAVScanner *pScanner, int option, void *value, un
557 557
     if(!value || !inputLength)
558 558
 	FAIL(CL_ENULLARG, "NULL value");
559 559
     if(lock_instances())
560
-	FAIL(CL_EMEM, "failed to lock instances");
560
+	FAIL(CL_ELOCK, "failed to lock instances");
561 561
 
562 562
     inst = (instance *)pScanner;
563 563
     if(!is_instance(inst)) {
... ...
@@ -607,10 +608,10 @@ int CLAMAPI Scan_GetLimit(int option, unsigned int *value) {
607 607
 
608 608
     INFN();
609 609
     if(lock_engine())
610
-	FAIL(CL_EMEM, "Failed to lock engine");
610
+	FAIL(CL_ELOCK, "Failed to lock engine");
611 611
     if(!engine) {
612 612
 	unlock_engine();
613
-	FAIL(CL_EARG, "Engine is NULL");
613
+	FAIL(CL_ESTATE, "Engine is NULL");
614 614
     }
615 615
     switch((enum CLAM_LIMIT_TYPE)option) {
616 616
     case CLAM_LIMIT_FILESIZE:
... ...
@@ -646,10 +647,10 @@ int CLAMAPI Scan_SetLimit(int option, unsigned int value) {
646 646
 
647 647
     INFN();
648 648
     if(lock_engine())
649
-	FAIL(CL_EMEM, "Failed to lock engine");
649
+	FAIL(CL_ELOCK, "Failed to lock engine");
650 650
     if(!engine) {
651 651
 	unlock_engine();
652
-	FAIL(CL_EARG, "Engine is NULL");
652
+	FAIL(CL_ESTATE, "Engine is NULL");
653 653
     }
654 654
     switch((enum CLAM_LIMIT_TYPE)option) {
655 655
     case CLAM_LIMIT_FILESIZE:
... ...
@@ -721,7 +722,7 @@ int CLAMAPI Scan_ScanObjectByHandle(CClamAVScanner *pScanner, HANDLE object, int
721 721
 
722 722
     if(lock_instances()) {
723 723
 	close(fd);
724
-	FAIL(CL_EMEM, "failed to lock instances for instance %p", pScanner);
724
+	FAIL(CL_ELOCK, "failed to lock instances for instance %p", pScanner);
725 725
     }
726 726
     inst = (instance *)pScanner;
727 727
     for(i=0; i<ninsts_total; i++) {
... ...
@@ -780,7 +781,7 @@ int CLAMAPI Scan_ScanObjectByHandle(CClamAVScanner *pScanner, HANDLE object, int
780 780
     logg("Scan_ScanObjectByHandle (instance %p): cl_scandesc returned %d in %u ms\n", inst, res, perf);
781 781
 
782 782
     if(lock_instances())
783
-	FAIL(CL_EMEM, "failed to lock instances for instance %p", pScanner);
783
+	FAIL(CL_ELOCK, "failed to lock instances for instance %p", pScanner);
784 784
     instances[i].refcnt--;
785 785
     if(!instances[i].refcnt)
786 786
 	SetEvent(reload_event);