Skip to content

Commit

Permalink
detect: free all tenant detect engines
Browse files Browse the repository at this point in the history
Free all tenants registered in the master.
  • Loading branch information
victorjulien committed Aug 11, 2023
1 parent 228caa6 commit a4d80bc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
35 changes: 28 additions & 7 deletions src/detect-engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -4492,14 +4492,10 @@ int DetectEngineAddToMaster(DetectEngineCtx *de_ctx)
return r;
}

int DetectEngineMoveToFreeList(DetectEngineCtx *de_ctx)
static int DetectEngineMoveToFreeListNoLock(DetectEngineMasterCtx *master, DetectEngineCtx *de_ctx)
{
DetectEngineMasterCtx *master = &g_master_de_ctx;

SCMutexLock(&master->lock);
DetectEngineCtx *instance = master->list;
if (instance == NULL) {
SCMutexUnlock(&master->lock);
return -1;
}

Expand All @@ -4522,7 +4518,6 @@ int DetectEngineMoveToFreeList(DetectEngineCtx *de_ctx)
instance = next;
}
if (instance == NULL) {
SCMutexUnlock(&master->lock);
return -1;
}
}
Expand All @@ -4538,9 +4533,17 @@ int DetectEngineMoveToFreeList(DetectEngineCtx *de_ctx)
master->free_list = instance;
}
SCLogDebug("detect engine %p moved to free list (%u refs)", de_ctx, de_ctx->ref_cnt);
return 0;
}

int DetectEngineMoveToFreeList(DetectEngineCtx *de_ctx)
{
int ret = 0;
DetectEngineMasterCtx *master = &g_master_de_ctx;
SCMutexLock(&master->lock);
ret = DetectEngineMoveToFreeListNoLock(master, de_ctx);
SCMutexUnlock(&master->lock);
return 0;
return ret;
}

void DetectEnginePruneFreeList(void)
Expand Down Expand Up @@ -4573,6 +4576,24 @@ void DetectEnginePruneFreeList(void)
SCMutexUnlock(&master->lock);
}

void DetectEngineClearMaster(void)
{
DetectEngineMasterCtx *master = &g_master_de_ctx;
SCMutexLock(&master->lock);

DetectEngineCtx *instance = master->list;
while (instance) {
DetectEngineCtx *next = instance->next;
DEBUG_VALIDATE_BUG_ON(instance->ref_cnt);
SCLogDebug("detect engine %p has %u ref(s)", instance, instance->ref_cnt);
instance->ref_cnt = 0;
DetectEngineMoveToFreeListNoLock(master, instance);
instance = next;
}
SCMutexUnlock(&master->lock);
DetectEnginePruneFreeList();
}

static int reloads = 0;

/** \brief Reload the detection engine
Expand Down
1 change: 1 addition & 0 deletions src/detect-engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ DetectEngineCtx *DetectEngineGetCurrent(void);
DetectEngineCtx *DetectEngineGetByTenantId(uint32_t tenant_id);
void DetectEnginePruneFreeList(void);
int DetectEngineMoveToFreeList(DetectEngineCtx *de_ctx);
void DetectEngineClearMaster(void);
DetectEngineCtx *DetectEngineReference(DetectEngineCtx *);
void DetectEngineDeReference(DetectEngineCtx **de_ctx);
int DetectEngineReload(const SCInstance *suri);
Expand Down
2 changes: 1 addition & 1 deletion src/suricata.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ static void GlobalsDestroy(SCInstance *suri)
DetectEngineMoveToFreeList(de_ctx);
DetectEngineDeReference(&de_ctx);
}
DetectEnginePruneFreeList();
DetectEngineClearMaster();

AppLayerDeSetup();
DatasetsSave();
Expand Down

0 comments on commit a4d80bc

Please sign in to comment.