| 1 |
/* The regexp object. |
| 2 |
* |
| 3 |
* Module begun 2008-03-05 by Rainer Gerhards, based on some code |
| 4 |
* from syslogd.c |
| 5 |
* |
| 6 |
* Copyright 2008-2012 Adiscon GmbH. |
| 7 |
* |
| 8 |
* This file is part of the rsyslog runtime library. |
| 9 |
* |
| 10 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 |
* you may not use this file except in compliance with the License. |
| 12 |
* You may obtain a copy of the License at |
| 13 |
* |
| 14 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 15 |
* -or- |
| 16 |
* see COPYING.ASL20 in the source distribution |
| 17 |
* |
| 18 |
* Unless required by applicable law or agreed to in writing, software |
| 19 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 20 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 |
* See the License for the specific language governing permissions and |
| 22 |
* limitations under the License. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "config.h" |
| 26 |
#include <pthread.h> |
| 27 |
#include <regex.h> |
| 28 |
#include <string.h> |
| 29 |
#include <stdint.h> |
| 30 |
#include <assert.h> |
| 31 |
|
| 32 |
#include "rsyslog.h" |
| 33 |
#include "module-template.h" |
| 34 |
#include "obj.h" |
| 35 |
#include "regexp.h" |
| 36 |
#include "errmsg.h" |
| 37 |
#include "hashtable.h" |
| 38 |
#include "hashtable_itr.h" |
| 39 |
|
| 40 |
MODULE_TYPE_LIB |
| 41 |
MODULE_TYPE_NOKEEP; |
| 42 |
|
| 43 |
/* static data */ |
| 44 |
DEFobjStaticHelpers; |
| 45 |
|
| 46 |
/* When using glibc, we enable per-thread regex to avoid lock contention. |
| 47 |
* See: |
| 48 |
* - https://github.com/rsyslog/rsyslog/issues/2759 |
| 49 |
* - https://github.com/rsyslog/rsyslog/pull/2786 |
| 50 |
* - https://sourceware.org/bugzilla/show_bug.cgi?id=11159 |
| 51 |
* |
| 52 |
* This should not affect BSD as they don't seem to take a lock in regexec. |
| 53 |
*/ |
| 54 |
#ifdef __GLIBC__ |
| 55 |
#define USE_PERTHREAD_REGEX 1 |
| 56 |
#else |
| 57 |
#define USE_PERTHREAD_REGEX 0 |
| 58 |
#endif |
| 59 |
|
| 60 |
static pthread_mutex_t mut_regexp; |
| 61 |
|
| 62 |
// Map a regex_t to its associated uncompiled parameters. |
| 63 |
static struct hashtable *regex_to_uncomp = NULL; |
| 64 |
|
| 65 |
// Map a (regexp_t, pthead_t) to a perthread_regex. |
| 66 |
static struct hashtable *perthread_regexs = NULL; |
| 67 |
|
| 68 |
|
| 69 |
/* |
| 70 |
* This stores un-compiled regex to allow further |
| 71 |
* call to regexec to re-compile a new regex dedicated |
| 72 |
* to the calling thread. |
| 73 |
*/ |
| 74 |
typedef struct uncomp_regex { |
| 75 |
char *regex; |
| 76 |
int cflags; |
| 77 |
regex_t *preg; |
| 78 |
} uncomp_regex_t; |
| 79 |
|
| 80 |
/* |
| 81 |
* This stores a regex dedicated to a single thread. |
| 82 |
*/ |
| 83 |
typedef struct perthread_regex { |
| 84 |
const regex_t *original_preg; |
| 85 |
regex_t preg; |
| 86 |
int ret; |
| 87 |
pthread_mutex_t lock; |
| 88 |
pthread_t thread; |
| 89 |
} perthread_regex_t; |
| 90 |
|
| 91 |
|
| 92 |
static unsigned __attribute__((nonnull(1))) int hash_from_regex(void *k) { |
| 93 |
return (uintptr_t) * (regex_t **)k; |
| 94 |
} |
| 95 |
|
| 96 |
static int key_equals_regex(void *key1, void *key2) { |
| 97 |
return *(regex_t **)key1 == *(regex_t **)key2; |
| 98 |
} |
| 99 |
|
| 100 |
static unsigned __attribute__((nonnull(1))) int hash_from_tregex(void *k) { |
| 101 |
perthread_regex_t *entry = k; |
| 102 |
// Cast to (void*) is ok here because already used in other parts of the code. |
| 103 |
uintptr_t thread_id = (uintptr_t)(void *)entry->thread; |
| 104 |
|
| 105 |
return thread_id ^ (uintptr_t)entry->original_preg; |
| 106 |
} |
| 107 |
|
| 108 |
static int key_equals_tregex(void *key1, void *key2) { |
| 109 |
perthread_regex_t *entry1 = key1; |
| 110 |
perthread_regex_t *entry2 = key2; |
| 111 |
|
| 112 |
return (pthread_equal(entry1->thread, entry2->thread) && entry1->original_preg == entry2->original_preg); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
/* ------------------------------ methods ------------------------------ */ |
| 117 |
|
| 118 |
|
| 119 |
// Create a copy of preg to be used by this thread only. |
| 120 |
static perthread_regex_t *create_perthread_regex(const regex_t *preg, uncomp_regex_t *uncomp) { |
| 121 |
perthread_regex_t *entry = NULL; |
| 122 |
|
| 123 |
if (Debug) { |
| 124 |
DBGPRINTF("Creating new regex_t for thread %p original regexp_t %p (pattern: %s, cflags: %x)\n", |
| 125 |
(void *)pthread_self(), preg, uncomp->regex, uncomp->cflags); |
| 126 |
} |
| 127 |
entry = calloc(1, sizeof(*entry)); |
| 128 |
if (!entry) return entry; |
| 129 |
entry->original_preg = preg; |
| 130 |
DBGPRINTF("regexp: regcomp %p %p\n", entry, &entry->preg); |
| 131 |
entry->ret = regcomp(&entry->preg, uncomp->regex, uncomp->cflags); |
| 132 |
pthread_mutex_init(&entry->lock, NULL); |
| 133 |
entry->thread = pthread_self(); |
| 134 |
return entry; |
| 135 |
} |
| 136 |
|
| 137 |
// Get (or create) a regex_t to be used by the current thread. |
| 138 |
static perthread_regex_t *get_perthread_regex(const regex_t *preg) { |
| 139 |
perthread_regex_t *entry = NULL; |
| 140 |
perthread_regex_t key = {.original_preg = preg, .thread = pthread_self()}; |
| 141 |
|
| 142 |
pthread_mutex_lock(&mut_regexp); |
| 143 |
entry = hashtable_search(perthread_regexs, (void *)&key); |
| 144 |
if (!entry) { |
| 145 |
uncomp_regex_t *uncomp = hashtable_search(regex_to_uncomp, (void *)&preg); |
| 146 |
|
| 147 |
if (uncomp) { |
| 148 |
entry = create_perthread_regex(preg, uncomp); |
| 149 |
if (!hashtable_insert(perthread_regexs, (void *)entry, entry)) { |
| 150 |
LogError(0, RS_RET_INTERNAL_ERROR, |
| 151 |
"error trying to insert thread-regexp into hash-table - things " |
| 152 |
"will not work 100%% correctly (mostly probably out of memory issue)"); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
if (entry) { |
| 157 |
pthread_mutex_lock(&entry->lock); |
| 158 |
} |
| 159 |
pthread_mutex_unlock(&mut_regexp); |
| 160 |
return entry; |
| 161 |
} |
| 162 |
|
| 163 |
static void remove_uncomp_regexp(regex_t *preg) { |
| 164 |
uncomp_regex_t *uncomp = NULL; |
| 165 |
|
| 166 |
pthread_mutex_lock(&mut_regexp); |
| 167 |
uncomp = hashtable_remove(regex_to_uncomp, (void *)&preg); |
| 168 |
|
| 169 |
if (uncomp) { |
| 170 |
if (Debug) { |
| 171 |
DBGPRINTF("Removing everything linked to regexp_t %p (pattern: %s, cflags: %x)\n", preg, uncomp->regex, |
| 172 |
uncomp->cflags); |
| 173 |
} |
| 174 |
free(uncomp->regex); |
| 175 |
free(uncomp); |
| 176 |
} |
| 177 |
pthread_mutex_unlock(&mut_regexp); |
| 178 |
} |
| 179 |
|
| 180 |
static void _regfree(regex_t *preg) { |
| 181 |
int ret = 0; |
| 182 |
struct hashtable_itr *itr = NULL; |
| 183 |
|
| 184 |
if (!preg) return; |
| 185 |
|
| 186 |
regfree(preg); |
| 187 |
remove_uncomp_regexp(preg); |
| 188 |
|
| 189 |
pthread_mutex_lock(&mut_regexp); |
| 190 |
if (!hashtable_count(perthread_regexs)) { |
| 191 |
pthread_mutex_unlock(&mut_regexp); |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
// This can be long to iterate other all regexps, but regfree doesn't get called |
| 196 |
// a lot during processing. |
| 197 |
itr = hashtable_iterator(perthread_regexs); |
| 198 |
do { |
| 199 |
perthread_regex_t *entry = (perthread_regex_t *)hashtable_iterator_value(itr); |
| 200 |
|
| 201 |
// Do it before freeing the entry. |
| 202 |
ret = hashtable_iterator_advance(itr); |
| 203 |
|
| 204 |
if (entry->original_preg == preg) { |
| 205 |
// This allows us to avoid freeing this while somebody is still using it. |
| 206 |
pthread_mutex_lock(&entry->lock); |
| 207 |
// We can unlock immediately after because mut_regexp is locked. |
| 208 |
pthread_mutex_unlock(&entry->lock); |
| 209 |
pthread_mutex_destroy(&entry->lock); |
| 210 |
regfree(&entry->preg); |
| 211 |
|
| 212 |
// Do it last because it will free entry. |
| 213 |
hashtable_remove(perthread_regexs, (void *)entry); |
| 214 |
} |
| 215 |
} while (ret); |
| 216 |
free(itr); |
| 217 |
|
| 218 |
pthread_mutex_unlock(&mut_regexp); |
| 219 |
} |
| 220 |
|
| 221 |
static int _regcomp(regex_t *preg, const char *regex, int cflags) { |
| 222 |
int ret = 0; |
| 223 |
regex_t **ppreg = NULL; |
| 224 |
uncomp_regex_t *uncomp; |
| 225 |
|
| 226 |
// Remove previous data if caller forgot to call regfree(). |
| 227 |
remove_uncomp_regexp(preg); |
| 228 |
|
| 229 |
// Make sure preg itself it correctly initalized. |
| 230 |
ret = regcomp(preg, regex, cflags); |
| 231 |
if (ret != 0) return ret; |
| 232 |
|
| 233 |
uncomp = calloc(1, sizeof(*uncomp)); |
| 234 |
if (!uncomp) return REG_ESPACE; |
| 235 |
|
| 236 |
uncomp->preg = preg; |
| 237 |
uncomp->regex = strdup(regex); |
| 238 |
uncomp->cflags = cflags; |
| 239 |
pthread_mutex_lock(&mut_regexp); |
| 240 |
|
| 241 |
// We need to allocate the key because hashtable will free it on remove. |
| 242 |
ppreg = malloc(sizeof(regex_t *)); |
| 243 |
*ppreg = preg; |
| 244 |
ret = hashtable_insert(regex_to_uncomp, (void *)ppreg, uncomp); |
| 245 |
pthread_mutex_unlock(&mut_regexp); |
| 246 |
if (ret == 0) { |
| 247 |
free(uncomp->regex); |
| 248 |
free(uncomp); |
| 249 |
return REG_ESPACE; |
| 250 |
} |
| 251 |
|
| 252 |
perthread_regex_t *entry = get_perthread_regex(preg); |
| 253 |
if (entry) { |
| 254 |
ret = entry->ret; |
| 255 |
pthread_mutex_unlock(&entry->lock); |
| 256 |
} else { |
| 257 |
ret = REG_ESPACE; |
| 258 |
} |
| 259 |
return ret; |
| 260 |
} |
| 261 |
|
| 262 |
static int _regexec(const regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags) { |
| 263 |
perthread_regex_t *entry = get_perthread_regex(preg); |
| 264 |
int ret = REG_NOMATCH; |
| 265 |
if (entry != NULL) { |
| 266 |
ret = regexec(&entry->preg, string, nmatch, pmatch, eflags); |
| 267 |
pthread_mutex_unlock(&entry->lock); |
| 268 |
} |
| 269 |
return ret; |
| 270 |
} |
| 271 |
|
| 272 |
static size_t _regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size) { |
| 273 |
perthread_regex_t *entry = get_perthread_regex(preg); |
| 274 |
|
| 275 |
if (entry) preg = &entry->preg; |
| 276 |
|
| 277 |
size_t ret = regerror(errcode, preg, errbuf, errbuf_size); |
| 278 |
|
| 279 |
if (entry) pthread_mutex_unlock(&entry->lock); |
| 280 |
|
| 281 |
return ret; |
| 282 |
} |
| 283 |
|
| 284 |
/* queryInterface function |
| 285 |
* rgerhards, 2008-03-05 |
| 286 |
*/ |
| 287 |
BEGINobjQueryInterface(regexp) |
| 288 |
CODESTARTobjQueryInterface(regexp); |
| 289 |
if (pIf->ifVersion != regexpCURR_IF_VERSION) { /* check for current version, increment on each change */ |
| 290 |
ABORT_FINALIZE(RS_RET_INTERFACE_NOT_SUPPORTED); |
| 291 |
} |
| 292 |
|
| 293 |
/* ok, we have the right interface, so let's fill it |
| 294 |
* Please note that we may also do some backwards-compatibility |
| 295 |
* work here (if we can support an older interface version - that, |
| 296 |
* of course, also affects the "if" above). |
| 297 |
*/ |
| 298 |
if (USE_PERTHREAD_REGEX) { |
| 299 |
pIf->regcomp = _regcomp; |
| 300 |
pIf->regexec = _regexec; |
| 301 |
pIf->regerror = _regerror; |
| 302 |
pIf->regfree = _regfree; |
| 303 |
} else { |
| 304 |
pIf->regcomp = regcomp; |
| 305 |
pIf->regexec = regexec; |
| 306 |
pIf->regerror = regerror; |
| 307 |
pIf->regfree = regfree; |
| 308 |
} |
| 309 |
|
| 310 |
finalize_it: |
| 311 |
ENDobjQueryInterface(regexp) |
| 312 |
|
| 313 |
|
| 314 |
/* Initialize the regexp class. Must be called as the very first method |
| 315 |
* before anything else is called inside this class. |
| 316 |
* rgerhards, 2008-02-19 |
| 317 |
*/ |
| 318 |
BEGINAbstractObjClassInit(regexp, 1, OBJ_IS_LOADABLE_MODULE) /* class, version */ |
| 319 |
/* request objects we use */ |
| 320 |
|
| 321 |
if (USE_PERTHREAD_REGEX) { |
| 322 |
pthread_mutex_init(&mut_regexp, NULL); |
| 323 |
|
| 324 |
regex_to_uncomp = create_hashtable(100, hash_from_regex, key_equals_regex, NULL); |
| 325 |
perthread_regexs = create_hashtable(100, hash_from_tregex, key_equals_tregex, NULL); |
| 326 |
if (regex_to_uncomp == NULL || perthread_regexs == NULL) { |
| 327 |
LogError(0, RS_RET_INTERNAL_ERROR, |
| 328 |
"error trying to initialize hash-table " |
| 329 |
"for regexp table. regexp will be disabled."); |
| 330 |
if (regex_to_uncomp) hashtable_destroy(regex_to_uncomp, 1); |
| 331 |
if (perthread_regexs) hashtable_destroy(perthread_regexs, 1); |
| 332 |
regex_to_uncomp = NULL; |
| 333 |
perthread_regexs = NULL; |
| 334 |
ABORT_FINALIZE(RS_RET_INTERNAL_ERROR); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
ENDObjClassInit(regexp) |
| 339 |
|
| 340 |
|
| 341 |
/* Exit the class. |
| 342 |
*/ |
| 343 |
BEGINObjClassExit(regexp, OBJ_IS_LOADABLE_MODULE) /* class, version */ |
| 344 |
if (USE_PERTHREAD_REGEX) { |
| 345 |
/* release objects we no longer need */ |
| 346 |
pthread_mutex_destroy(&mut_regexp); |
| 347 |
if (regex_to_uncomp) hashtable_destroy(regex_to_uncomp, 1); |
| 348 |
if (perthread_regexs) hashtable_destroy(perthread_regexs, 1); |
| 349 |
} |
| 350 |
ENDObjClassExit(regexp) |
| 351 |
|
| 352 |
|
| 353 |
/* --------------- here now comes the plumbing that makes as a library module --------------- */ |
| 354 |
|
| 355 |
|
| 356 |
BEGINmodExit |
| 357 |
CODESTARTmodExit; |
| 358 |
regexpClassExit(); |
| 359 |
ENDmodExit |
| 360 |
|
| 361 |
|
| 362 |
BEGINqueryEtryPt |
| 363 |
CODESTARTqueryEtryPt; |
| 364 |
CODEqueryEtryPt_STD_LIB_QUERIES; |
| 365 |
ENDqueryEtryPt |
| 366 |
|
| 367 |
|
| 368 |
BEGINmodInit() |
| 369 |
CODESTARTmodInit; |
| 370 |
*ipIFVersProvided = CURR_MOD_IF_VERSION; /* we only support the current interface specification */ |
| 371 |
|
| 372 |
CHKiRet(regexpClassInit(pModInfo)); /* must be done after tcps_sess, as we use it */ |
| 373 |
/* Initialize all classes that are in our module - this includes ourselfs */ |
| 374 |
ENDmodInit |
| 375 |
/* vi:set ai: |
| 376 |
*/ |