onas: improving handling of thread exit cleanup and db reload.

pull/27/head
Mickey Sola 10 years ago
parent 214d750e97
commit abbe4c4bc3
  1. 27
      clamd/onaccess_ddd.c
  2. 20
      clamd/onaccess_fan.c
  3. 11
      clamd/server-th.c

@ -58,6 +58,7 @@
static struct onas_ht *ddd_ht;
static char **wdlt;
static uint32_t wdlt_len;
static int onas_in_fd;
static int onas_ddd_init_ht(uint32_t ht_size) {
@ -266,7 +267,6 @@ void *onas_ddd_th(void *arg) {
int sizelimit = 0, extinfo;
STATBUF sb;
uint64_t in_mask = IN_ONLYDIR | IN_MOVE | IN_DELETE | IN_CREATE;
int in_fd;
fd_set rfds;
char buf[4096];
ssize_t bread;
@ -291,9 +291,8 @@ void *onas_ddd_th(void *arg) {
sigaction(SIGUSR1, &act, NULL);
sigaction(SIGSEGV, &act, NULL);
in_fd = inotify_init1(IN_NONBLOCK);
//in_fd = inotify_init();
if (in_fd == -1) {
onas_in_fd = inotify_init1(IN_NONBLOCK);
if (onas_in_fd == -1) {
logg("!ScanOnAccess: Could not init inotify.");
return NULL;
}
@ -341,7 +340,7 @@ void *onas_ddd_th(void *arg) {
while(pt) {
size_t ptlen = strlen(pt->strarg);
if(onas_ht_get(ddd_ht, pt->strarg, ptlen, NULL) == CL_SUCCESS) {
if(onas_ddd_watch(pt->strarg, tharg->fan_fd, tharg->fan_mask, in_fd, in_mask)) {
if(onas_ddd_watch(pt->strarg, tharg->fan_fd, tharg->fan_mask, onas_in_fd, in_mask)) {
logg("!ScanOnAccess: Could not watch path '%s', %s\n", pt->strarg, strerror(errno));
return NULL;
}
@ -352,14 +351,14 @@ void *onas_ddd_th(void *arg) {
FD_ZERO(&rfds);
FD_SET(in_fd, &rfds);
FD_SET(onas_in_fd, &rfds);
while (1) {
do {
ret = select(in_fd + 1, &rfds, NULL, NULL, NULL);
ret = select(onas_in_fd + 1, &rfds, NULL, NULL, NULL);
} while(ret == -1 && errno == EINTR);
while((bread = read(in_fd, buf, sizeof(buf))) > 0) {
while((bread = read(onas_in_fd, buf, sizeof(buf))) > 0) {
/* Handle events. */
int wd;
@ -387,23 +386,23 @@ void *onas_ddd_th(void *arg) {
if (event->mask & IN_DELETE) {
logg("*ddd: DELETE - Removing %s from %s with wd:%d\n", child_path, path, wd);
onas_ddd_unwatch(child_path, tharg->fan_fd, in_fd);
onas_ddd_unwatch(child_path, tharg->fan_fd, onas_in_fd);
onas_ht_rm_hierarchy(ddd_ht, child_path, strlen(child_path), 0);
} else if (event->mask & IN_MOVED_FROM) {
logg("*ddd: MOVED_FROM - Removing %s from %s with wd:%d\n", child_path, path, wd);
onas_ddd_unwatch(child_path, tharg->fan_fd, in_fd);
onas_ddd_unwatch(child_path, tharg->fan_fd, onas_in_fd);
onas_ht_rm_hierarchy(ddd_ht, child_path, strlen(child_path), 0);
} else if (event->mask & IN_CREATE) {
logg("*ddd: CREATE - Adding %s to %s with wd:%d\n", child_path, path, wd);
onas_ht_add_hierarchy(ddd_ht, child_path);
onas_ddd_watch(child_path, tharg->fan_fd, tharg->fan_mask, in_fd, in_mask);
onas_ddd_watch(child_path, tharg->fan_fd, tharg->fan_mask, onas_in_fd, in_mask);
} else if (event->mask & IN_MOVED_TO) {
logg("*ddd: MOVED_TO - Adding %s to %s with wd:%d\n", child_path, path, wd);
onas_ht_add_hierarchy(ddd_ht, child_path);
onas_ddd_watch(child_path, tharg->fan_fd, tharg->fan_mask, in_fd, in_mask);
onas_ddd_watch(child_path, tharg->fan_fd, tharg->fan_mask, onas_in_fd, in_mask);
}
}
}
@ -414,8 +413,12 @@ void *onas_ddd_th(void *arg) {
static void onas_ddd_exit(int sig) {
logg("*ScanOnAccess: onas_ddd_exit(), signal %d\n", sig);
close(onas_in_fd);
onas_free_ht(ddd_ht);
free(wdlt);
pthread_exit(NULL);
logg("ScanOnAccess: stopped\n");
}

@ -51,10 +51,13 @@
#include "onaccess_ddd.h"
static pthread_t ddd_pid;
static int onas_fan_fd;
static void onas_fan_exit(int sig)
{
logg("*ScanOnAccess: onas_fan_exit(), signal %d\n", sig);
close(onas_fan_fd);
pthread_kill(ddd_pid, SIGUSR1);
pthread_join(ddd_pid, NULL);
@ -103,7 +106,6 @@ void *onas_fan_th(void *arg)
int sizelimit = 0, extinfo;
STATBUF sb;
uint64_t fan_mask = FAN_OPEN_PERM | FAN_ACCESS_PERM | FAN_EVENT_ON_CHILD;
int fan_fd;
fd_set rfds;
char buf[4096];
ssize_t bread;
@ -134,8 +136,8 @@ void *onas_fan_th(void *arg)
sigaction(SIGSEGV, &act, NULL);
/* Initialize fanotify */
fan_fd = fanotify_init(FAN_CLASS_CONTENT | FAN_UNLIMITED_QUEUE | FAN_UNLIMITED_MARKS, O_RDONLY);
if(fan_fd < 0) {
onas_fan_fd = fanotify_init(FAN_CLASS_CONTENT | FAN_UNLIMITED_QUEUE | FAN_UNLIMITED_MARKS, O_RDONLY);
if(onas_fan_fd < 0) {
logg("!ScanOnAccess: fanotify_init failed: %s\n", cli_strerror(errno, err, sizeof(err)));
if(errno == EPERM)
logg("ScanOnAccess: clamd must be started by root\n");
@ -148,7 +150,7 @@ void *onas_fan_th(void *arg)
if(!(ddd_tharg = (struct ddd_thrarg *) malloc(sizeof(struct ddd_thrarg)))) break;
ddd_tharg->fan_fd = fan_fd;
ddd_tharg->fan_fd = onas_fan_fd;
ddd_tharg->fan_mask = fan_mask;
ddd_tharg->opts = tharg->opts;
ddd_tharg->engine = tharg->engine;
@ -171,12 +173,12 @@ void *onas_fan_th(void *arg)
extinfo = optget(tharg->opts, "ExtendedDetectionInfo")->enabled;
FD_ZERO(&rfds);
FD_SET(fan_fd, &rfds);
FD_SET(onas_fan_fd, &rfds);
do {
ret = select(fan_fd + 1, &rfds, NULL, NULL, NULL);
ret = select(onas_fan_fd + 1, &rfds, NULL, NULL, NULL);
} while(ret == -1 && errno == EINTR);
while((bread = read(fan_fd, buf, sizeof(buf))) > 0) {
while((bread = read(onas_fan_fd, buf, sizeof(buf))) > 0) {
fmd = (struct fanotify_event_metadata *) buf;
while(FAN_EVENT_OK(fmd, bread)) {
scan = 1;
@ -202,7 +204,7 @@ void *onas_fan_th(void *arg)
}
}
if(onas_fan_scanfile(fan_fd, fname, fmd, scan, extinfo, tharg) == -1) {
if(onas_fan_scanfile(onas_fan_fd, fname, fmd, scan, extinfo, tharg) == -1) {
close(fmd->fd);
return NULL;
}
@ -216,7 +218,7 @@ void *onas_fan_th(void *arg)
fmd = FAN_EVENT_NEXT(fmd, bread);
}
do {
ret = select(fan_fd + 1, &rfds, NULL, NULL, NULL);
ret = select(onas_fan_fd + 1, &rfds, NULL, NULL, NULL);
} while(ret == -1 && errno == EINTR);
}

@ -1426,15 +1426,7 @@ int recvloop_th(int *socketds, unsigned nsockets, struct cl_engine *engine, unsi
pthread_mutex_lock(&reload_mutex);
if(reload) {
pthread_mutex_unlock(&reload_mutex);
#if defined(FANOTIFY) || defined(CLAMAUTH)
if(optget(opts, "ScanOnAccess")->enabled && tharg) {
logg("Restarting on-access scan\n");
pthread_mutex_lock(&logg_mutex);
pthread_kill(fan_pid, SIGUSR1);
pthread_mutex_unlock(&logg_mutex);
pthread_join(fan_pid, NULL);
}
#endif
engine = reload_db(engine, dboptions, opts, FALSE, &ret);
if(ret) {
logg("Terminating because of a fatal error.\n");
@ -1451,7 +1443,6 @@ int recvloop_th(int *socketds, unsigned nsockets, struct cl_engine *engine, unsi
#if defined(FANOTIFY) || defined(CLAMAUTH)
if(optget(opts, "ScanOnAccess")->enabled && tharg) {
tharg->engine = engine;
pthread_create(&fan_pid, &fan_attr, onas_fan_th, tharg);
}
#endif
time(&start_time);

Loading…
Cancel
Save