clamd/server-th.c
e3aaff8e
 /*
81837459
  *  Copyright (C) 2002 - 2005 Tomasz Kojm <tkojm@clamav.net>
c238ac42
  *			      Trog <trog@clamav.net>
e3aaff8e
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 2 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
48b7b4a7
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
e3aaff8e
  */
 
67118e92
 #ifdef	_MSC_VER
 #include <winsock.h>
 #endif
 
98ac8d19
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
e3aaff8e
 #include <pthread.h>
c238ac42
 #include <errno.h>
e3aaff8e
 #include <signal.h>
c238ac42
 #include <stdio.h>
a9ebff44
 #include <string.h>
c238ac42
 #include <time.h>
a9ebff44
 #include <sys/types.h>
67118e92
 #ifndef	C_WINDOWS
a9ebff44
 #include <sys/socket.h>
67118e92
 #endif
 #ifdef	HAVE_UNISTD_H
520cf7eb
 #include <unistd.h>
67118e92
 #endif
bd8603aa
 
 #include "libclamav/clamav.h"
 
 #include "shared/output.h"
e3aaff8e
 
 #include "server.h"
c238ac42
 #include "thrmgr.h"
81131381
 #include "session.h"
c238ac42
 #include "clamuko.h"
 #include "others.h"
afb48b28
 #include "shared.h"
e3aaff8e
 
67118e92
 #ifndef	C_WINDOWS
 #define	closesocket(s)	close(s)
 #endif
 
c238ac42
 #define BUFFSIZE 1024
67118e92
 #ifndef	FALSE
c238ac42
 #define FALSE (0)
67118e92
 #endif
 #ifndef	TRUE
c238ac42
 #define TRUE (1)
67118e92
 #endif
e3aaff8e
 
ae12e285
 int progexit = 0;
 pthread_mutex_t exit_mutex;
 int reload = 0;
 time_t reloaded_time = 0;
 pthread_mutex_t reload_mutex;
 int sighup = 0;
2dab0a15
 static struct cl_stat *dbstat = NULL;
e8c9ccdb
 
c238ac42
 typedef struct client_conn_tag {
     int sd;
a57e3d41
     unsigned int options;
c238ac42
     const struct cfgstruct *copt;
a57e3d41
     struct cl_engine *engine;
     time_t engine_timestamp;
c238ac42
     const struct cl_limits *limits;
57358cc8
     int *socketds;
     int nsockets;
c238ac42
 } client_conn_t;
81131381
 
fc83da82
 static void scanner_thread(void *arg)
e3aaff8e
 {
c238ac42
 	client_conn_t *conn = (client_conn_t *) arg;
67118e92
 #ifndef	C_WINDOWS
e3aaff8e
 	sigset_t sigset;
67118e92
 #endif
57358cc8
 	int ret, timeout, i, session=FALSE;
e3aaff8e
 
c238ac42
 
67118e92
 #ifndef	C_WINDOWS
e3aaff8e
     /* ignore all signals */
     sigfillset(&sigset);
     pthread_sigmask(SIG_SETMASK, &sigset, NULL);
67118e92
 #endif
e3aaff8e
 
81837459
     timeout = cfgopt(conn->copt, "ReadTimeout")->numarg;
e1fdb94b
     if(!timeout)
     	timeout = -1;
 
45905a4a
     do {
a57e3d41
     	ret = command(conn->sd, conn->engine, conn->limits, conn->options, conn->copt, timeout);
1d8b9788
 	if (ret < 0) {
45905a4a
 		break;
c238ac42
 	}
e3aaff8e
 
45905a4a
 	switch(ret) {
 	    case COMMAND_SHUTDOWN:
 		pthread_mutex_lock(&exit_mutex);
 		progexit = 1;
57358cc8
 		for(i = 0; i < conn->nsockets; i++) {
 		    shutdown(conn->socketds[i], 2);
67118e92
 		    closesocket(conn->socketds[i]);
57358cc8
 		}
45905a4a
 		pthread_mutex_unlock(&exit_mutex);
 		break;
 
 	    case COMMAND_RELOAD:
 		pthread_mutex_lock(&reload_mutex);
 		reload = 1;
 		pthread_mutex_unlock(&reload_mutex);
 		break;
 
 	    case COMMAND_SESSION:
ae12e285
 		session = TRUE;
45905a4a
 		break;
 
 	    case COMMAND_END:
ae12e285
 		session = FALSE;
45905a4a
 		break;
9a7bd935
 	}
 	if (session) {
 	    pthread_mutex_lock(&exit_mutex);
 	    if(progexit) {
 		session = FALSE;
 	    }
 	    pthread_mutex_unlock(&exit_mutex);
 	    pthread_mutex_lock(&reload_mutex);
a57e3d41
 	    if (conn->engine_timestamp != reloaded_time) {
9a7bd935
 		session = FALSE;
 	    }
 	    pthread_mutex_unlock(&reload_mutex);
 	}
45905a4a
     } while (session);
 
67118e92
     closesocket(conn->sd);
a57e3d41
     cl_free(conn->engine);
c238ac42
     free(conn);
     return;
e3aaff8e
 }
 
c238ac42
 void sighandler_th(int sig)
e3aaff8e
 {
c238ac42
     switch(sig) {
 	case SIGINT:
 	case SIGTERM:
 	    progexit = 1;
520cf7eb
 	    break;
e3aaff8e
 
67118e92
 #ifdef	SIGHUP
520cf7eb
 	case SIGHUP:
 	    sighup = 1;
 	    break;
67118e92
 #endif
d056f4d6
 
67118e92
 #ifdef	SIGUSR2
ae203685
 	case SIGUSR2:
 	    reload = 1;
 	    break;
67118e92
 #endif
ae203685
 
d056f4d6
 	default:
 	    break; /* Take no action on other signals - e.g. SIGPIPE */
c238ac42
     }
 }
e3aaff8e
 
a57e3d41
 static struct cl_engine *reload_db(struct cl_engine *engine, unsigned int dboptions, const struct cfgstruct *copt, int do_check, int *ret)
c238ac42
 {
fb787a06
 	const char *dbdir;
bd8603aa
 	int retval;
120f3c85
 	unsigned int sigs = 0, try = 1;
9e431a95
 
2dab0a15
     *ret = 0;
c238ac42
     if(do_check) {
 	if(dbstat == NULL) {
 	    logg("No stats for Database check - forcing reload\n");
a57e3d41
 	    return engine;
e3aaff8e
 	}
 
c238ac42
 	if(cl_statchkdir(dbstat) == 1) {
 	    logg("SelfCheck: Database modification detected. Forcing reload.\n");
a57e3d41
 	    return engine;
c238ac42
 	} else {
 	    logg("SelfCheck: Database status OK.\n");
 	    return NULL;
e3aaff8e
 	}
c238ac42
     }
e3aaff8e
 
c238ac42
     /* release old structure */
a57e3d41
     if(engine) {
 	cl_free(engine);
 	engine = NULL;
c238ac42
     }
e3aaff8e
 
81837459
     dbdir = cfgopt(copt, "DatabaseDirectory")->strarg;
c238ac42
     logg("Reading databases from %s\n", dbdir);
e3aaff8e
 
c238ac42
     if(dbstat == NULL) {
8ca8a18e
 	dbstat = (struct cl_stat *) malloc(sizeof(struct cl_stat));
2dab0a15
 	if(!dbstat) {
 	    logg("!Can't allocate memory for dbstat\n");
 	    *ret = 1;
 	    return NULL;
 	}
c238ac42
     } else {
 	cl_statfree(dbstat);
     }
46c2e927
 
c238ac42
     memset(dbstat, 0, sizeof(struct cl_stat));
cd69cf20
     if((retval = cl_statinidir(dbdir, dbstat))) {
 	logg("!cl_statinidir() failed: %s\n", cl_strerror(retval));
 	*ret = 1;
 	return NULL;
     }
d6449522
 
120f3c85
     while((retval = cl_load(dbdir, &engine, &sigs, dboptions)) == CL_ELOCKDB) {
 	logg("!reload db failed: %s (try %u)\n", cl_strerror(retval), try);
 	if(++try > 3)
 	    break;
     }
 
     if(retval) {
c238ac42
 	logg("!reload db failed: %s\n", cl_strerror(retval));
2dab0a15
 	*ret = 1;
 	return NULL;
c238ac42
     }
46c2e927
 
a57e3d41
     if(!engine) {
2dab0a15
 	logg("!reload db failed: %s\n", cl_strerror(retval));
 	*ret = 1;
 	return NULL;
c238ac42
     }
e3aaff8e
 
a57e3d41
     if((retval = cl_build(engine)) != 0) {
2dab0a15
 	logg("!Database initialization error: can't build engine: %s\n", cl_strerror(retval));
 	*ret = 1;
 	return NULL;
e3aaff8e
     }
1095156a
     logg("Database correctly reloaded (%u signatures)\n", sigs);
e3aaff8e
 
a57e3d41
     return engine;
e3aaff8e
 }
 
a57e3d41
 int acceptloop_th(int *socketds, int nsockets, struct cl_engine *engine, unsigned int dboptions, const struct cfgstruct *copt)
e3aaff8e
 {
f22f13d9
 	int max_threads, i, ret = 0;
c6dbfbcb
 	unsigned int options = 0;
52e8d3c6
 	threadpool_t *thr_pool;
67118e92
 #ifndef	C_WINDOWS
c238ac42
 	struct sigaction sigact;
67118e92
 #endif
c238ac42
 	mode_t old_umask;
e3aaff8e
 	struct cl_limits limits;
67118e92
 #ifndef	C_WINDOWS
e3aaff8e
 	sigset_t sigset;
67118e92
 #endif
c238ac42
 	client_conn_t *client_conn;
1095156a
 	const struct cfgstruct *cpt;
abf06850
 #ifdef HAVE_STRERROR_R
 	char buff[BUFFSIZE + 1];
 #endif
c238ac42
 	unsigned int selfchk;
 	time_t start_time, current_time;
6b8aa2d0
 	pid_t mainpid;
02b4b0c7
 	int idletimeout;
6b8aa2d0
 
e3aaff8e
 #if defined(C_BIGSTACK) || defined(C_BSD)
c238ac42
         size_t stacksize;
e3aaff8e
 #endif
 
c238ac42
 #ifdef CLAMUKO
 	pthread_t clamuko_pid;
 	pthread_attr_t clamuko_attr;
afb48b28
 	struct thrarg *tharg = NULL; /* shut up gcc */
c238ac42
 #endif
67118e92
 
 #ifndef	C_WINDOWS
c238ac42
 	memset(&sigact, 0, sizeof(struct sigaction));
67118e92
 #endif
e3aaff8e
 
     /* save the PID */
6b8aa2d0
     mainpid = getpid();
81837459
     if((cpt = cfgopt(copt, "PidFile"))->enabled) {
e3aaff8e
 	    FILE *fd;
c238ac42
 	old_umask = umask(0006);
e3aaff8e
 	if((fd = fopen(cpt->strarg, "w")) == NULL) {
 	    logg("!Can't save PID in file %s\n", cpt->strarg);
 	} else {
1095156a
 	    fprintf(fd, "%u", (unsigned int) mainpid);
e3aaff8e
 	    fclose(fd);
 	}
 	umask(old_umask);
     }
 
1095156a
     logg("*Listening daemon: PID: %u\n", (unsigned int) mainpid);
81837459
     max_threads = cfgopt(copt, "MaxThreads")->numarg;
e3aaff8e
 
9549d94b
     if(cfgopt(copt, "ScanArchive")->enabled) {
e3aaff8e
 
 	/* set up limits */
8adf0608
 	memset(&limits, 0, sizeof(struct cl_limits));
e3aaff8e
 
81837459
 	if((limits.maxfilesize = cfgopt(copt, "ArchiveMaxFileSize")->numarg)) {
1095156a
 	    logg("Archive: Archived file size limit set to %lu bytes.\n", limits.maxfilesize);
81837459
 	} else {
 	    logg("^Archive: File size limit protection disabled.\n");
e3aaff8e
 	}
 
81837459
 	if((limits.maxreclevel = cfgopt(copt, "ArchiveMaxRecursion")->numarg)) {
1095156a
 	    logg("Archive: Recursion level limit set to %u.\n", limits.maxreclevel);
81837459
 	} else {
 	    logg("^Archive: Recursion level limit protection disabled.\n");
e3aaff8e
 	}
 
81837459
 	if((limits.maxfiles = cfgopt(copt, "ArchiveMaxFiles")->numarg)) {
1095156a
 	    logg("Archive: Files limit set to %u.\n", limits.maxfiles);
81837459
 	} else {
 	    logg("^Archive: Files limit protection disabled.\n");
e3aaff8e
 	}
 
81837459
 	if((limits.maxratio = cfgopt(copt, "ArchiveMaxCompressionRatio")->numarg)) {
1095156a
 	    logg("Archive: Compression ratio limit set to %u.\n", limits.maxratio);
81837459
 	} else {
 	    logg("^Archive: Compression ratio limit disabled.\n");
a6945b5d
 	}
 
81837459
 	if(cfgopt(copt, "ArchiveLimitMemoryUsage")->enabled) {
e3aaff8e
 	    limits.archivememlim = 1;
9e431a95
 	    logg("Archive: Limited memory usage.\n");
c238ac42
 	} else {
e3aaff8e
 	    limits.archivememlim = 0;
c238ac42
 	}
e3aaff8e
     }
 
81837459
     if(cfgopt(copt, "ScanArchive")->enabled) {
e3aaff8e
 	logg("Archive support enabled.\n");
3805ebcb
 	options |= CL_SCAN_ARCHIVE;
8139fd99
 
81837459
 	if(cfgopt(copt, "ArchiveBlockEncrypted")->enabled) {
d272908a
 	    logg("Archive: Blocking encrypted archives.\n");
08d6b1e3
 	    options |= CL_SCAN_BLOCKENCRYPTED;
0f34221a
 	}
 
81837459
 	if(cfgopt(copt, "ArchiveBlockMax")->enabled) {
d272908a
 	    logg("Archive: Blocking archives that exceed limits.\n");
3805ebcb
 	    options |= CL_SCAN_BLOCKMAX;
d272908a
 	}
 
e3aaff8e
     } else {
 	logg("Archive support disabled.\n");
     }
 
47138a98
     if(cfgopt(copt, "AlgorithmicDetection")->enabled) {
1b661cef
 	logg("Algorithmic detection enabled.\n");
6fd2fb47
 	options |= CL_SCAN_ALGORITHMIC;
1b661cef
     } else {
 	logg("Algorithmic detection disabled.\n");
     }
 
81837459
     if(cfgopt(copt, "ScanPE")->enabled) {
a9082ea2
 	logg("Portable Executable support enabled.\n");
3805ebcb
 	options |= CL_SCAN_PE;
3f97a1e7
     } else {
 	logg("Portable Executable support disabled.\n");
     }
20c3d44d
 
3f97a1e7
     if(cfgopt(copt, "ScanELF")->enabled) {
 	logg("ELF support enabled.\n");
 	options |= CL_SCAN_ELF;
     } else {
 	logg("ELF support disabled.\n");
     }
 
     if(cfgopt(copt, "ScanPE")->enabled || cfgopt(copt, "ScanELF")->enabled) {
81837459
 	if(cfgopt(copt, "DetectBrokenExecutables")->enabled) {
20c3d44d
 	    logg("Detection of broken executables enabled.\n");
453581ae
 	    options |= CL_SCAN_BLOCKBROKEN;
20c3d44d
 	}
a9082ea2
     }
 
81837459
     if(cfgopt(copt, "ScanMail")->enabled) {
e3aaff8e
 	logg("Mail files support enabled.\n");
3805ebcb
 	options |= CL_SCAN_MAIL;
a36e6e5c
 
81837459
 	if(cfgopt(copt, "MailFollowURLs")->enabled) {
a36e6e5c
 	    logg("Mail: URL scanning enabled.\n");
3805ebcb
 	    options |= CL_SCAN_MAILURL;
a36e6e5c
 	}
 
dab42957
 	if((limits.maxmailrec = cfgopt(copt, "MailMaxRecursion")->numarg)) {
 	    logg("Mail: Recursion level limit set to %u.\n", limits.maxmailrec);
 	} else {
 	    logg("^Mail: Recursion level limit protection disabled.\n");
 	}
 
e3aaff8e
     } else {
 	logg("Mail files support disabled.\n");
     }
 
81837459
     if(cfgopt(copt, "ScanOLE2")->enabled) {
e8c9ccdb
 	logg("OLE2 support enabled.\n");
3805ebcb
 	options |= CL_SCAN_OLE2;
e8c9ccdb
     } else {
 	logg("OLE2 support disabled.\n");
     }
 
c5107e70
     if(cfgopt(copt, "ScanPDF")->enabled) {
 	logg("PDF support enabled.\n");
 	options |= CL_SCAN_PDF;
     } else {
 	logg("PDF support disabled.\n");
     }
 
81837459
     if(cfgopt(copt, "ScanHTML")->enabled) {
888f5794
 	logg("HTML support enabled.\n");
3805ebcb
 	options |= CL_SCAN_HTML;
888f5794
     } else {
 	logg("HTML support disabled.\n");
     }
 
19b3e182
     if(cfgopt(copt,"PhishingScanURLs")->enabled) {
 
 	if(cfgopt(copt,"PhishingRestrictedScan")->enabled) {
 	    /* we don't scan urls from all domains, just those listed in
 	     * .pdb file. This is the safe default
 	     */
 	    options |= CL_SCAN_PHISHING_DOMAINLIST;
 	} else {
 	    /* This is a false positive prone option, since newsletters, etc.
 	     * often contain links that will be classified as phishing attempts,
 	     * even though the site they link to isn't a phish site.
 	     */
 	    logg("Phishing: Checking all URLs, regardless of domain (FP prone).\n");
 	}
 
 	if(cfgopt(copt,"PhishingAlwaysBlockCloak")->enabled) {
 	    options |= CL_SCAN_PHISHING_BLOCKCLOAK; 
 	    logg("Phishing: Always checking for cloaked urls\n");
 	}
 
 	if(cfgopt(copt,"PhishingAlwaysBlockSSLMismatch")->enabled) {
 	    options |= CL_SCAN_PHISHING_BLOCKSSL;
 	    logg("Phishing: Always checking for ssl mismatches\n");
 	}
     }
 
81837459
     selfchk = cfgopt(copt, "SelfCheck")->numarg;
c238ac42
     if(!selfchk) {
 	logg("Self checking disabled.\n");
     } else {
1095156a
 	logg("Self checking every %u seconds.\n", selfchk);
c238ac42
     }
e3aaff8e
 
81837459
     if(cfgopt(copt, "ClamukoScanOnAccess")->enabled)
e3aaff8e
 #ifdef CLAMUKO
32fc1d7b
     {
 	pthread_attr_init(&clamuko_attr);
 	pthread_attr_setdetachstate(&clamuko_attr, PTHREAD_CREATE_JOINABLE);
c238ac42
 
8ca8a18e
 	tharg = (struct thrarg *) malloc(sizeof(struct thrarg));
32fc1d7b
 	tharg->copt = copt;
a57e3d41
 	tharg->engine = engine;
32fc1d7b
 	tharg->limits = &limits;
 	tharg->options = options;
c238ac42
 
32fc1d7b
 	pthread_create(&clamuko_pid, &clamuko_attr, clamukoth, tharg);
     }
e3aaff8e
 #else
a3e7b8a1
 	logg("Clamuko is not available.\n");
e3aaff8e
 #endif
 
67118e92
 #ifndef	C_WINDOWS
e3aaff8e
     /* set up signal handling */
     sigfillset(&sigset);
     sigdelset(&sigset, SIGINT);
     sigdelset(&sigset, SIGTERM);
b4912e71
     sigdelset(&sigset, SIGSEGV);
f456f28e
     sigdelset(&sigset, SIGHUP);
d056f4d6
     sigdelset(&sigset, SIGPIPE);
ae203685
     sigdelset(&sigset, SIGUSR2);
e3aaff8e
     sigprocmask(SIG_SETMASK, &sigset, NULL);
c238ac42
  
b4912e71
     /* SIGINT, SIGTERM, SIGSEGV */
8ac7b1ce
     sigact.sa_handler = sighandler_th;
e3aaff8e
     sigemptyset(&sigact.sa_mask);
     sigaddset(&sigact.sa_mask, SIGINT);
     sigaddset(&sigact.sa_mask, SIGTERM);
f456f28e
     sigaddset(&sigact.sa_mask, SIGHUP);
d056f4d6
     sigaddset(&sigact.sa_mask, SIGPIPE);
ae203685
     sigaddset(&sigact.sa_mask, SIGUSR2);
e3aaff8e
     sigaction(SIGINT, &sigact, NULL);
     sigaction(SIGTERM, &sigact, NULL);
d056f4d6
     sigaction(SIGHUP, &sigact, NULL);
     sigaction(SIGPIPE, &sigact, NULL);
ae203685
     sigaction(SIGUSR2, &sigact, NULL);
67118e92
 #endif
c238ac42
 
     pthread_mutex_init(&exit_mutex, NULL);
     pthread_mutex_init(&reload_mutex, NULL);
e3aaff8e
 
81837459
     idletimeout = cfgopt(copt, "IdleTimeout")->numarg;
02b4b0c7
 
ae12e285
     if((thr_pool=thrmgr_new(max_threads, idletimeout, scanner_thread)) == NULL) {
10b04232
 	logg("!thrmgr_new failed\n");
c238ac42
 	exit(-1);
     }
e3aaff8e
 
c238ac42
     time(&start_time);
e3aaff8e
 
ae12e285
     for(;;) {				
477cfb97
 #if !defined(C_WINDOWS) && !defined(C_BEOS)
 	    struct stat st_buf;
 #endif
57358cc8
     	int socketd = socketds[0];
f22f13d9
 	int new_sd = 0;
 
57358cc8
     	if(nsockets > 1) {
f22f13d9
 	    int pollret = poll_fds(socketds, nsockets, -1, 1);
57358cc8
     	    if(pollret > 0) {
     		socketd = socketds[pollret - 1];
     	    } else {
f22f13d9
 		new_sd = -1;
57358cc8
     	    }
477cfb97
     	}
 #if !defined(C_WINDOWS) && !defined(C_BEOS)
f22f13d9
 	if(new_sd != -1 && fstat(socketd, &st_buf) == -1) {
4b3a6d83
 	    logg("!fstat(): socket descriptor gone\n");
 	    memmove(socketds, socketds + 1, sizeof(socketds[0]) * nsockets);
 	    nsockets--;
 	    if(!nsockets) {
 		logg("!Main socket gone: fatal\n");
 		break;
 	    }
 	}
477cfb97
 #endif
f22f13d9
 	if (new_sd != -1)
 	    new_sd = accept(socketd, NULL, NULL);
c238ac42
 	if((new_sd == -1) && (errno != EINTR)) {
57358cc8
 	    if(progexit) {
 	    	break;
 	    }
c238ac42
 	    /* very bad - need to exit or restart */
abf06850
 #ifdef HAVE_STRERROR_R
4b3a6d83
 	    strerror_r(errno, buff, BUFFSIZE);
 	    logg("!accept() failed: %s\n", buff);
abf06850
 #else
ae12e285
 	    logg("!accept() failed\n");
abf06850
 #endif
e3aaff8e
 	    continue;
 	}
 
506a6176
 	if (sighup) {
 		logg("SIGHUP caught: re-opening log file.\n");
 		logg_close();
 		sighup = 0;
81837459
 		if(!logg_file && (cpt = cfgopt(copt, "LogFile"))->enabled)
f9593781
 		    logg_file = cpt->strarg;
506a6176
 	}
 
07d33096
 	if (!progexit && new_sd >= 0) {
8ca8a18e
 		client_conn = (client_conn_t *) malloc(sizeof(struct client_conn_tag));
506a6176
 		client_conn->sd = new_sd;
 		client_conn->options = options;
 		client_conn->copt = copt;
a57e3d41
 		client_conn->engine = cl_dup(engine);
 		client_conn->engine_timestamp = reloaded_time;
506a6176
 		client_conn->limits = &limits;
57358cc8
 		client_conn->socketds = socketds;
 		client_conn->nsockets = nsockets;
e0f90556
 		if (!thrmgr_dispatch(thr_pool, client_conn)) {
 		    close(client_conn->sd);
 		    free(client_conn);
10b04232
 		    logg("!thread dispatch failed\n");
e0f90556
 		}
506a6176
 	}
c238ac42
 
 	pthread_mutex_lock(&exit_mutex);
 	if(progexit) {
07d33096
 	    if (new_sd >= 0) {
 		close(new_sd);
 	    }
c238ac42
 	    pthread_mutex_unlock(&exit_mutex);
 	    break;
e3aaff8e
 	}
c238ac42
 	pthread_mutex_unlock(&exit_mutex);
e3aaff8e
 
c238ac42
 	if(selfchk) {
 	    time(&current_time);
9d3c38ba
 	    if((current_time - start_time) > (time_t)selfchk) {
a57e3d41
 		if(reload_db(engine, dboptions, copt, TRUE, &ret)) {
c238ac42
 		    pthread_mutex_lock(&reload_mutex);
 		    reload = 1;
 		    pthread_mutex_unlock(&reload_mutex);
 		}
 		time(&start_time);
 	    }
0f387b1b
 	}
e3aaff8e
 
c238ac42
 	pthread_mutex_lock(&reload_mutex);
3e42a62e
 	if(reload && cfgopt(copt, "NodalCoreAcceleration")->enabled) {
 	    logg("^RELOAD is not available in hardware accelerated mode (yet).\n");
 	    logg("^Please restart the daemon manually.\n");
 	    reload = 0;
 	}
c238ac42
 	if(reload) {
 	    pthread_mutex_unlock(&reload_mutex);
a57e3d41
 	    engine = reload_db(engine, dboptions, copt, FALSE, &ret);
2dab0a15
 	    if(ret) {
b6e75665
 		logg("Terminating because of a fatal error.\n");
2dab0a15
 		if(new_sd >= 0)
 		    close(new_sd);
 		break;
 	    }
45905a4a
 	    pthread_mutex_lock(&reload_mutex);
 	    reload = 0;
ae12e285
 	    time(&reloaded_time);
45905a4a
 	    pthread_mutex_unlock(&reload_mutex);
c238ac42
 #ifdef CLAMUKO
81837459
 	    if(cfgopt(copt, "ClamukoScanOnAccess")->enabled) {
32fc1d7b
 		logg("Stopping and restarting Clamuko.\n");
 		pthread_kill(clamuko_pid, SIGUSR1);
 		pthread_join(clamuko_pid, NULL);
a57e3d41
 		tharg->engine = engine;
32fc1d7b
 		pthread_create(&clamuko_pid, &clamuko_attr, clamukoth, tharg);
 	    }
2d70a403
 #endif
c238ac42
 	} else {
 	    pthread_mutex_unlock(&reload_mutex);
 	}
     }
e3aaff8e
 
40107990
     /* Destroy the thread manager.
      * This waits for all current tasks to end
      */
     thrmgr_destroy(thr_pool);
c238ac42
 #ifdef CLAMUKO
81837459
     if(cfgopt(copt, "ClamukoScanOnAccess")->enabled) {
32fc1d7b
 	logg("Stopping Clamuko.\n");
 	pthread_kill(clamuko_pid, SIGUSR1);
 	pthread_join(clamuko_pid, NULL);
     }
6a2532ca
 #endif
a57e3d41
     if(engine)
 	cl_free(engine);
2dab0a15
 
     if(dbstat)
 	cl_statfree(dbstat);
57358cc8
     logg("*Shutting down the main socket%s.\n", (nsockets > 1) ? "s" : "");
     for (i = 0; i < nsockets; i++)
 	shutdown(socketds[i], 2);
     logg("*Closing the main socket%s.\n", (nsockets > 1) ? "s" : "");
     for (i = 0; i < nsockets; i++)
67118e92
 	closesocket(socketds[i]);
75ccac9f
 #ifndef C_OS2
81837459
     if((cpt = cfgopt(copt, "LocalSocket"))->enabled) {
a3e7b8a1
 	if(unlink(cpt->strarg) == -1)
 	    logg("!Can't unlink the socket file %s\n", cpt->strarg);
 	else
 	     logg("Socket file removed.\n");
75ccac9f
     }
 #endif
a3e7b8a1
 
81837459
     if((cpt = cfgopt(copt, "PidFile"))->enabled) {
a3e7b8a1
 	if(unlink(cpt->strarg) == -1)
 	    logg("!Can't unlink the pid file %s\n", cpt->strarg);
 	else
 	    logg("Pid file removed.\n");
     }
 
520cf7eb
     time(&current_time);
     logg("--- Stopped at %s", ctime(&current_time));
 
c95ab85e
     return ret;
b4912e71
 }