janitor: add base plumbing

This commit is contained in:
Rainer Gerhards 2014-05-15 18:36:40 +02:00
parent 3805ff1712
commit 394d54987d
2 changed files with 39 additions and 0 deletions

View File

@ -30,12 +30,44 @@
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <pthread.h>
#include "rsyslog.h"
#include "janitor.h"
static struct janitorEtry *janitorRoot = NULL; /* TODO: move to runConf? */
static pthread_mutex_t janitorMut = PTHREAD_MUTEX_INITIALIZER;
rsRetVal
janitorAddEtry(void (*cb)(void*), const char *id, void *pUsr)
{
struct janitorEtry *etry;
DEFiRet;
CHKmalloc(etry = malloc(sizeof(struct janitorEtry)));
CHKmalloc(etry->id = strdup(id));
etry->pUsr = pUsr;
etry->cb = cb;
etry->next = janitorRoot;
pthread_mutex_lock(&janitorMut);
janitorRoot = etry;
pthread_mutex_unlock(&janitorMut);
finalize_it:
RETiRet;
}
/* run the janitor; all entries are processed */
void
janitorRun(void)
{
struct janitorEtry *curr;
dbgprintf("janitorRun() called\n");
pthread_mutex_lock(&janitorMut);
for(curr = janitorRoot ; curr != NULL ; curr = curr->next) {
dbgprintf("janitor: processing entry %p, id '%s'\n",
curr, curr->id);
curr->cb(curr->pUsr);
}
pthread_mutex_unlock(&janitorMut);
}

View File

@ -21,7 +21,14 @@
#ifndef INCLUDED_JANITOR_H
#define INCLUDED_JANITOR_H
struct janitorEtry {
struct janitorEtry *next;
char *id; /* ID used to remove entry */
void (*cb)(void *pUsr);
void *pUsr; /* user-settable pointer (passed to callback) */
};
rsRetVal janitorAddEtry(void (*cb)(void*), const char *id, void *pUsr);
void janitorRun(void);
#endif /* #ifndef INCLUDED_JANITOR_H */