| 1 |
/* janitor.c - rsyslog's janitor |
| 2 |
* |
| 3 |
* The rsyslog janitor can be used to periodically clean out |
| 4 |
* resources. It was initially developed to close files that |
| 5 |
* were not written to for some time (omfile plugin), but has |
| 6 |
* a generic interface that can be used for all similar tasks. |
| 7 |
* |
| 8 |
* Module begun 2014-05-15 by Rainer Gerhards |
| 9 |
* |
| 10 |
* Copyright (C) 2014-2015 by Rainer Gerhards and Adiscon GmbH. |
| 11 |
* |
| 12 |
* This file is part of the rsyslog runtime library. |
| 13 |
* |
| 14 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 15 |
* you may not use this file except in compliance with the License. |
| 16 |
* You may obtain a copy of the License at |
| 17 |
* |
| 18 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 19 |
* -or- |
| 20 |
* see COPYING.ASL20 in the source distribution |
| 21 |
* |
| 22 |
* Unless required by applicable law or agreed to in writing, software |
| 23 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 24 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 |
* See the License for the specific language governing permissions and |
| 26 |
* limitations under the License. |
| 27 |
*/ |
| 28 |
|
| 29 |
#include "config.h" |
| 30 |
#include <stdlib.h> |
| 31 |
#include <assert.h> |
| 32 |
#include <string.h> |
| 33 |
#include <pthread.h> |
| 34 |
|
| 35 |
#include "rsyslog.h" |
| 36 |
#include "janitor.h" |
| 37 |
|
| 38 |
static struct janitorEtry *janitorRoot = NULL; |
| 39 |
static pthread_mutex_t janitorMut = PTHREAD_MUTEX_INITIALIZER; |
| 40 |
|
| 41 |
rsRetVal janitorAddEtry(void (*cb)(void *), const char *id, void *pUsr) { |
| 42 |
struct janitorEtry *etry = NULL; |
| 43 |
DEFiRet; |
| 44 |
CHKmalloc(etry = malloc(sizeof(struct janitorEtry))); |
| 45 |
CHKmalloc(etry->id = strdup(id)); |
| 46 |
etry->pUsr = pUsr; |
| 47 |
etry->cb = cb; |
| 48 |
etry->next = janitorRoot; |
| 49 |
pthread_mutex_lock(&janitorMut); |
| 50 |
janitorRoot = etry; |
| 51 |
pthread_mutex_unlock(&janitorMut); |
| 52 |
DBGPRINTF("janitor: entry %p, id '%s' added\n", etry, id); |
| 53 |
finalize_it: |
| 54 |
if (iRet != RS_RET_OK && etry != NULL) free(etry); |
| 55 |
RETiRet; |
| 56 |
} |
| 57 |
|
| 58 |
rsRetVal janitorDelEtry(const char *__restrict__ const id) { |
| 59 |
struct janitorEtry *curr, *prev = NULL; |
| 60 |
DEFiRet; |
| 61 |
|
| 62 |
pthread_mutex_lock(&janitorMut); |
| 63 |
for (curr = janitorRoot; curr != NULL; curr = curr->next) { |
| 64 |
if (!strcmp(curr->id, id)) { |
| 65 |
if (prev == NULL) { |
| 66 |
janitorRoot = curr->next; |
| 67 |
} else { |
| 68 |
prev->next = curr->next; |
| 69 |
} |
| 70 |
free(curr->id); |
| 71 |
free(curr); |
| 72 |
DBGPRINTF("janitor: deleted entry '%s'\n", id); |
| 73 |
ABORT_FINALIZE(RS_RET_OK); |
| 74 |
} |
| 75 |
prev = curr; |
| 76 |
} |
| 77 |
DBGPRINTF("janitor: to be deleted entry '%s' not found\n", id); |
| 78 |
iRet = RS_RET_NOT_FOUND; |
| 79 |
finalize_it: |
| 80 |
pthread_mutex_unlock(&janitorMut); |
| 81 |
RETiRet; |
| 82 |
} |
| 83 |
|
| 84 |
/* run the janitor; all entries are processed */ |
| 85 |
void janitorRun(void) { |
| 86 |
struct janitorEtry *curr; |
| 87 |
|
| 88 |
dbgprintf("janitorRun() called\n"); |
| 89 |
pthread_mutex_lock(&janitorMut); |
| 90 |
for (curr = janitorRoot; curr != NULL; curr = curr->next) { |
| 91 |
DBGPRINTF("janitor: processing entry %p, id '%s'\n", curr, curr->id); |
| 92 |
curr->cb(curr->pUsr); |
| 93 |
} |
| 94 |
pthread_mutex_unlock(&janitorMut); |
| 95 |
} |