Browse code

minor cleanup

git-svn: trunk@2271

Tomasz Kojm authored on 2006/09/15 07:43:31
Showing 2 changed files
... ...
@@ -1,3 +1,7 @@
1
+Fri Sep 15 00:42:27 CEST 2006 (tk)
2
+----------------------------------
3
+  * clamd/server-th.c: minor cleanup
4
+
1 5
 Fri Sep 15 00:07:02 CEST 2006 (tk)
2 6
 ----------------------------------
3 7
   * shared/cfgparser.c: add support for OPT_QUOTESTR and use it for file
... ...
@@ -70,6 +70,7 @@ int reload = 0;
70 70
 time_t reloaded_time = 0;
71 71
 pthread_mutex_t reload_mutex;
72 72
 int sighup = 0;
73
+static struct cl_stat *dbstat = NULL;
73 74
 
74 75
 typedef struct client_conn_tag {
75 76
     int sd;
... ...
@@ -160,11 +161,6 @@ void sighandler_th(int sig)
160 160
 	    progexit = 1;
161 161
 	    break;
162 162
 
163
-	case SIGSEGV:
164
-	    logg("Segmentation fault :-( Bye..\n");
165
-	    _exit(11); /* probably not reached at all */
166
-	    break; /* not reached */
167
-
168 163
 #ifdef	SIGHUP
169 164
 	case SIGHUP:
170 165
 	    sighup = 1;
... ...
@@ -182,13 +178,13 @@ void sighandler_th(int sig)
182 182
     }
183 183
 }
184 184
 
185
-static struct cl_node *reload_db(struct cl_node *root, unsigned int dboptions, const struct cfgstruct *copt, int do_check)
185
+static struct cl_node *reload_db(struct cl_node *root, unsigned int dboptions, const struct cfgstruct *copt, int do_check, int *ret)
186 186
 {
187 187
 	const char *dbdir;
188 188
 	int retval;
189 189
 	unsigned int sigs = 0;
190
-	static struct cl_stat *dbstat=NULL;
191 190
 
191
+    *ret = 0;
192 192
     if(do_check) {
193 193
 	if(dbstat == NULL) {
194 194
 	    logg("No stats for Database check - forcing reload\n");
... ...
@@ -215,6 +211,11 @@ static struct cl_node *reload_db(struct cl_node *root, unsigned int dboptions, c
215 215
 
216 216
     if(dbstat == NULL) {
217 217
 	dbstat = (struct cl_stat *) mmalloc(sizeof(struct cl_stat));
218
+	if(!dbstat) {
219
+	    logg("!Can't allocate memory for dbstat\n");
220
+	    *ret = 1;
221
+	    return NULL;
222
+	}
218 223
     } else {
219 224
 	cl_statfree(dbstat);
220 225
     }
... ...
@@ -224,18 +225,20 @@ static struct cl_node *reload_db(struct cl_node *root, unsigned int dboptions, c
224 224
 
225 225
     if((retval = cl_load(dbdir, &root, &sigs, dboptions))) {
226 226
 	logg("!reload db failed: %s\n", cl_strerror(retval));
227
-	exit(-1);
227
+	*ret = 1;
228
+	return NULL;
228 229
     }
229 230
 
230 231
     if(!root) {
231
-	logg("!load db failed: %s\n", cl_strerror(retval));
232
-	exit(-1);
232
+	logg("!reload db failed: %s\n", cl_strerror(retval));
233
+	*ret = 1;
234
+	return NULL;
233 235
     }
234 236
 
235 237
     if((retval = cl_build(root)) != 0) {
236
-	logg("!Database initialization error: can't build engine: %s\n",
237
-	cl_strerror(retval));
238
-	exit(-1);
238
+	logg("!Database initialization error: can't build engine: %s\n", cl_strerror(retval));
239
+	*ret = 1;
240
+	return NULL;
239 241
     }
240 242
     logg("Database correctly reloaded (%d signatures)\n", sigs);
241 243
 
... ...
@@ -244,7 +247,7 @@ static struct cl_node *reload_db(struct cl_node *root, unsigned int dboptions, c
244 244
 
245 245
 int acceptloop_th(int *socketds, int nsockets, struct cl_node *root, unsigned int dboptions, const struct cfgstruct *copt)
246 246
 {
247
-	int new_sd, max_threads, i;
247
+	int new_sd, max_threads, i, ret;
248 248
 	unsigned int options = 0;
249 249
 	threadpool_t *thr_pool;
250 250
 #ifndef	C_WINDOWS
... ...
@@ -551,7 +554,7 @@ int acceptloop_th(int *socketds, int nsockets, struct cl_node *root, unsigned in
551 551
 	if(selfchk) {
552 552
 	    time(&current_time);
553 553
 	    if((current_time - start_time) > (time_t)selfchk) {
554
-		if(reload_db(root, dboptions, copt, TRUE)) {
554
+		if(reload_db(root, dboptions, copt, TRUE, &ret)) {
555 555
 		    pthread_mutex_lock(&reload_mutex);
556 556
 		    reload = 1;
557 557
 		    pthread_mutex_unlock(&reload_mutex);
... ...
@@ -563,7 +566,13 @@ int acceptloop_th(int *socketds, int nsockets, struct cl_node *root, unsigned in
563 563
 	pthread_mutex_lock(&reload_mutex);
564 564
 	if(reload) {
565 565
 	    pthread_mutex_unlock(&reload_mutex);
566
-	    root = reload_db(root, dboptions, copt, FALSE);
566
+	    root = reload_db(root, dboptions, copt, FALSE, &ret);
567
+	    if(ret) {
568
+		logg("Terminating because of a fatal error.");
569
+		if(new_sd >= 0)
570
+		    close(new_sd);
571
+		break;
572
+	    }
567 573
 	    pthread_mutex_lock(&reload_mutex);
568 574
 	    reload = 0;
569 575
 	    time(&reloaded_time);
... ...
@@ -593,7 +602,11 @@ int acceptloop_th(int *socketds, int nsockets, struct cl_node *root, unsigned in
593 593
 	pthread_join(clamuko_pid, NULL);
594 594
     }
595 595
 #endif
596
-    cl_free(root);
596
+    if(root)
597
+	cl_free(root);
598
+
599
+    if(dbstat)
600
+	cl_statfree(dbstat);
597 601
     logg("*Shutting down the main socket%s.\n", (nsockets > 1) ? "s" : "");
598 602
     for (i = 0; i < nsockets; i++)
599 603
 	shutdown(socketds[i], 2);
... ...
@@ -616,7 +629,6 @@ int acceptloop_th(int *socketds, int nsockets, struct cl_node *root, unsigned in
616 616
 	    logg("Pid file removed.\n");
617 617
     }
618 618
 
619
-    logg("Exiting (clean)\n");
620 619
     time(&current_time);
621 620
     logg("--- Stopped at %s", ctime(&current_time));
622 621