clamav/clamonacc/scan/queue.c

305 lines
7.6 KiB
C
Raw Normal View History

/*
2020-01-03 15:44:07 -05:00
* Copyright (C) 2019-2020 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
*
* Authors: Mickey Sola
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <pthread.h>
#include <string.h>
#include "../misc/utils.h"
#include "libclamav/clamav.h"
#include "shared/optparser.h"
#include "shared/output.h"
#include "../c-thread-pool/thpool.h"
#include "thread.h"
#include "queue.h"
2019-05-16 13:05:48 -07:00
2019-07-25 12:42:08 -04:00
static void onas_scan_queue_exit(void *arg);
2019-05-16 13:05:48 -07:00
static int onas_consume_event(threadpool thpool);
static cl_error_t onas_new_event_queue_node(struct onas_event_queue_node **node);
static void onas_destroy_event_queue_node(struct onas_event_queue_node *node);
static pthread_mutex_t onas_queue_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t onas_scan_queue_loop = PTHREAD_MUTEX_INITIALIZER;
2019-07-25 12:42:08 -04:00
pthread_cond_t onas_scan_queue_empty_cond = PTHREAD_COND_INITIALIZER;
extern pthread_t scan_queue_pid;
static threadpool g_thpool;
static struct onas_event_queue_node *g_onas_event_queue_head = NULL;
static struct onas_event_queue_node *g_onas_event_queue_tail = NULL;
2019-05-16 13:05:48 -07:00
static struct onas_event_queue g_onas_event_queue;
2019-07-25 12:42:08 -04:00
static cl_error_t onas_new_event_queue_node(struct onas_event_queue_node **node)
{
2019-07-25 12:42:08 -04:00
*node = malloc(sizeof(struct onas_event_queue_node));
if (NULL == *node) {
return CL_EMEM;
}
2019-07-25 12:42:08 -04:00
**node = (struct onas_event_queue_node){
.next = NULL,
.prev = NULL,
2019-07-25 12:42:08 -04:00
.data = NULL};
2019-07-25 12:42:08 -04:00
return CL_SUCCESS;
}
2019-07-25 12:42:08 -04:00
static void *onas_init_event_queue()
{
2019-07-25 12:42:08 -04:00
if (CL_EMEM == onas_new_event_queue_node(&g_onas_event_queue_head)) {
return NULL;
}
2019-07-25 12:42:08 -04:00
if (CL_EMEM == onas_new_event_queue_node(&g_onas_event_queue_tail)) {
return NULL;
}
2019-07-25 12:42:08 -04:00
g_onas_event_queue_tail->prev = g_onas_event_queue_head;
g_onas_event_queue_head->next = g_onas_event_queue_tail;
2019-07-25 12:42:08 -04:00
g_onas_event_queue = (struct onas_event_queue){
.head = g_onas_event_queue_head,
.tail = g_onas_event_queue_tail,
2019-07-25 12:42:08 -04:00
.size = 0};
2019-07-25 12:42:08 -04:00
return &g_onas_event_queue;
}
2019-07-25 12:42:08 -04:00
static void onas_destroy_event_queue_node(struct onas_event_queue_node *node)
{
2019-07-25 12:42:08 -04:00
if (NULL == node) {
return;
}
2019-07-25 12:42:08 -04:00
node->next = NULL;
node->prev = NULL;
node->data = NULL;
2019-07-25 12:42:08 -04:00
free(node);
node = NULL;
2019-07-25 12:42:08 -04:00
return;
}
2019-07-25 12:42:08 -04:00
static void onas_destroy_event_queue()
{
2019-07-25 12:42:08 -04:00
if (NULL == g_onas_event_queue_head) {
return;
}
2019-07-25 12:42:08 -04:00
struct onas_event_queue_node *curr = g_onas_event_queue_head;
struct onas_event_queue_node *next = curr->next;
2019-07-25 12:42:08 -04:00
do {
onas_destroy_event_queue_node(curr);
curr = next;
if (curr) {
next = curr->next;
}
} while (curr);
2019-07-25 12:42:08 -04:00
return;
}
2019-07-25 12:42:08 -04:00
void *onas_scan_queue_th(void *arg)
{
2019-07-25 12:42:08 -04:00
/* not a ton of use for context right now, but perhaps in the future we can pass in more options */
struct onas_context *ctx = (struct onas_context *)arg;
sigset_t sigset;
struct sigaction act;
const struct optstruct *pt;
int ret, len, idx;
2019-07-25 12:42:08 -04:00
cl_error_t err;
2019-07-25 12:42:08 -04:00
/* ignore all signals except SIGUSR2 */
sigfillset(&sigset);
sigdelset(&sigset, SIGUSR2);
/* The behavior of a process is undefined after it ignores a
* SIGFPE, SIGILL, SIGSEGV, or SIGBUS signal */
2019-07-25 12:42:08 -04:00
sigdelset(&sigset, SIGFPE);
sigdelset(&sigset, SIGILL);
sigdelset(&sigset, SIGSEGV);
sigdelset(&sigset, SIGTERM);
sigdelset(&sigset, SIGINT);
#ifdef SIGBUS
2019-07-25 12:42:08 -04:00
sigdelset(&sigset, SIGBUS);
#endif
2019-07-25 12:42:08 -04:00
logg("*ClamScanQueue: initializing event queue consumer ... (%d) threads in thread pool\n", ctx->maxthreads);
onas_init_event_queue();
threadpool thpool = thpool_init(ctx->maxthreads);
g_thpool = thpool;
/* loop w/ onas_consume_event until we die */
pthread_cleanup_push(onas_scan_queue_exit, NULL);
logg("*ClamScanQueue: waiting to consume events ...\n");
do {
pthread_mutex_lock(&onas_scan_queue_loop);
pthread_cond_wait(&onas_scan_queue_empty_cond, &onas_scan_queue_loop);
/* run 'till we're empty */
do {
pthread_testcancel();
ret = onas_consume_event(thpool);
} while (1 != ret);
pthread_mutex_unlock(&onas_scan_queue_loop);
} while (1);
pthread_cleanup_pop(1);
}
2019-07-25 12:42:08 -04:00
static int onas_queue_is_b_empty()
{
2019-05-16 13:05:48 -07:00
if (g_onas_event_queue.head->next == g_onas_event_queue.tail) {
return 1;
}
return 0;
}
2019-07-25 12:42:08 -04:00
static int onas_consume_event(threadpool thpool)
{
pthread_mutex_lock(&onas_queue_lock);
struct onas_event_queue_node *popped_node = g_onas_event_queue_head->next;
if (onas_queue_is_b_empty()) {
pthread_mutex_unlock(&onas_queue_lock);
return 1;
}
#ifdef ONAS_DEBUG
logg("*ClamScanQueue: consuming event!\n");
#endif
2019-07-25 12:42:08 -04:00
thpool_add_work(thpool, (void *)onas_scan_worker, (void *)popped_node->data);
2019-07-25 12:42:08 -04:00
g_onas_event_queue_head->next = g_onas_event_queue_head->next->next;
2019-05-16 13:05:48 -07:00
g_onas_event_queue_head->next->prev = g_onas_event_queue_head;
onas_destroy_event_queue_node(popped_node);
2019-05-16 13:05:48 -07:00
g_onas_event_queue.size--;
pthread_mutex_unlock(&onas_queue_lock);
return 0;
}
2019-07-25 12:42:08 -04:00
cl_error_t onas_queue_event(struct onas_scan_event *event_data)
{
pthread_mutex_lock(&onas_queue_lock);
struct onas_event_queue_node *node = NULL;
#ifdef ONAS_DEBUG
logg("*ClamScanQueue: queueing event!\n");
#endif
if (CL_EMEM == onas_new_event_queue_node(&node)) {
2019-07-25 12:42:08 -04:00
return CL_EMEM;
}
node->next = g_onas_event_queue_tail;
node->prev = g_onas_event_queue_tail->prev;
node->data = event_data;
/* tail will always have a .prev */
2019-07-25 12:42:08 -04:00
((struct onas_event_queue_node *)g_onas_event_queue_tail->prev)->next = node;
g_onas_event_queue_tail->prev = node;
2019-05-16 13:05:48 -07:00
g_onas_event_queue.size++;
pthread_mutex_unlock(&onas_queue_lock);
pthread_cond_signal(&onas_scan_queue_empty_cond);
return CL_SUCCESS;
}
2019-07-25 12:42:08 -04:00
cl_error_t onas_scan_queue_start(struct onas_context **ctx)
{
2019-07-25 12:42:08 -04:00
pthread_attr_t scan_queue_attr;
int32_t thread_started = 1;
2019-07-25 12:42:08 -04:00
if (!ctx || !*ctx) {
logg("*ClamScanQueue: unable to start clamonacc. (bad context)\n");
return CL_EARG;
}
2019-07-25 12:42:08 -04:00
if (pthread_attr_init(&scan_queue_attr)) {
return CL_BREAK;
}
pthread_attr_setdetachstate(&scan_queue_attr, PTHREAD_CREATE_JOINABLE);
thread_started = pthread_create(&scan_queue_pid, &scan_queue_attr, onas_scan_queue_th, *ctx);
2019-07-25 12:42:08 -04:00
if (0 != thread_started) {
/* Failed to create thread */
logg("*ClamScanQueue: Unable to start event consumer queue thread ... \n");
return CL_ECREAT;
}
2019-07-25 12:42:08 -04:00
return CL_SUCCESS;
}
2019-07-25 12:42:08 -04:00
static void onas_scan_queue_exit(void *arg)
{
2019-07-25 12:42:08 -04:00
logg("*ClamScanQueue: onas_scan_queue_exit()\n");
2019-07-25 12:42:08 -04:00
pthread_mutex_lock(&onas_queue_lock);
onas_destroy_event_queue();
2019-07-25 12:42:08 -04:00
if (g_thpool) {
thpool_destroy(g_thpool);
}
g_thpool = NULL;
pthread_mutex_unlock(&onas_queue_lock);
2019-07-25 12:42:08 -04:00
logg("ClamScanQueue: stopped\n");
}