| 1 |
/* lookup.c |
| 2 |
* Support for lookup tables in RainerScript. |
| 3 |
* |
| 4 |
* Copyright 2013-2023 Adiscon GmbH. |
| 5 |
* |
| 6 |
* This file is part of the rsyslog runtime library. |
| 7 |
* |
| 8 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 |
* you may not use this file except in compliance with the License. |
| 10 |
* You may obtain a copy of the License at |
| 11 |
* |
| 12 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
* -or- |
| 14 |
* see COPYING.ASL20 in the source distribution |
| 15 |
* |
| 16 |
* Unless required by applicable law or agreed to in writing, software |
| 17 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 18 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 |
* See the License for the specific language governing permissions and |
| 20 |
* limitations under the License. |
| 21 |
*/ |
| 22 |
#include "config.h" |
| 23 |
#include <stdlib.h> |
| 24 |
#include <string.h> |
| 25 |
#include <errno.h> |
| 26 |
#include <sys/types.h> |
| 27 |
#include <sys/stat.h> |
| 28 |
#include <fcntl.h> |
| 29 |
#include <unistd.h> |
| 30 |
#include <json.h> |
| 31 |
#include <assert.h> |
| 32 |
|
| 33 |
#include "rsyslog.h" |
| 34 |
#include "srUtils.h" |
| 35 |
#include "errmsg.h" |
| 36 |
#include "lookup.h" |
| 37 |
#include "msg.h" |
| 38 |
#include "rsconf.h" |
| 39 |
#include "dirty.h" |
| 40 |
#include "unicode-helper.h" |
| 41 |
#include "regexp.h" |
| 42 |
|
| 43 |
PRAGMA_IGNORE_Wdeprecated_declarations |
| 44 |
/* definitions for objects we access */ |
| 45 |
DEFobjStaticHelpers; |
| 46 |
DEFobjCurrIf(glbl) |
| 47 |
#ifdef FEATURE_REGEXP |
| 48 |
DEFobjCurrIf(regexp) |
| 49 |
#endif |
| 50 |
|
| 51 |
/* forward definitions */ |
| 52 |
static rsRetVal lookupReadFile(lookup_t *pThis, const uchar *name, const uchar *filename); |
| 53 |
static void lookupDestruct(lookup_t *pThis); |
| 54 |
|
| 55 |
/* static data */ |
| 56 |
/* tables for interfacing with the v6 config system (as far as we need to) */ |
| 57 |
static struct cnfparamdescr modpdescr[] = {{"name", eCmdHdlrString, CNFPARAM_REQUIRED}, |
| 58 |
{"file", eCmdHdlrString, CNFPARAM_REQUIRED}, |
| 59 |
{"reloadOnHUP", eCmdHdlrBinary, 0}}; |
| 60 |
static struct cnfparamblk modpblk = {CNFPARAMBLK_VERSION, sizeof(modpdescr) / sizeof(struct cnfparamdescr), modpdescr}; |
| 61 |
|
| 62 |
/* internal data-types */ |
| 63 |
typedef struct uint32_index_val_s { |
| 64 |
uint32_t index; |
| 65 |
uchar *val; |
| 66 |
} uint32_index_val_t; |
| 67 |
|
| 68 |
const char *reloader_prefix = "lkp_tbl_reloader:"; |
| 69 |
|
| 70 |
static void *lookupTableReloader(void *self); |
| 71 |
|
| 72 |
static void lookupStopReloader(lookup_ref_t *pThis); |
| 73 |
|
| 74 |
|
| 75 |
/* create a new lookup table object AND include it in our list of |
| 76 |
* lookup tables. |
| 77 |
*/ |
| 78 |
static rsRetVal lookupNew(lookup_ref_t **ppThis) { |
| 79 |
lookup_ref_t *pThis = NULL; |
| 80 |
lookup_t *t = NULL; |
| 81 |
DEFiRet; |
| 82 |
|
| 83 |
CHKmalloc(pThis = calloc(1, sizeof(lookup_ref_t))); |
| 84 |
CHKmalloc(t = calloc(1, sizeof(lookup_t))); |
| 85 |
pThis->do_reload = pThis->do_stop = 0; |
| 86 |
pThis->reload_on_hup = 1; /*DO reload on HUP (default)*/ |
| 87 |
|
| 88 |
pThis->next = NULL; |
| 89 |
if (loadConf->lu_tabs.root == NULL) { |
| 90 |
loadConf->lu_tabs.root = pThis; |
| 91 |
} else { |
| 92 |
loadConf->lu_tabs.last->next = pThis; |
| 93 |
} |
| 94 |
loadConf->lu_tabs.last = pThis; |
| 95 |
|
| 96 |
pThis->self = t; |
| 97 |
|
| 98 |
*ppThis = pThis; |
| 99 |
finalize_it: |
| 100 |
if (iRet != RS_RET_OK) { |
| 101 |
LogError(errno, iRet, "a lookup table could not be initialized"); |
| 102 |
free(t); |
| 103 |
free(pThis); |
| 104 |
} |
| 105 |
RETiRet; |
| 106 |
} |
| 107 |
|
| 108 |
/* activate a lookup table entry once rsyslog is ready to do so */ |
| 109 |
static rsRetVal lookupActivateTable(lookup_ref_t *pThis) { |
| 110 |
DEFiRet; |
| 111 |
int initialized = 0; |
| 112 |
|
| 113 |
DBGPRINTF("lookupActivateTable called\n"); |
| 114 |
CHKiConcCtrl(pthread_rwlock_init(&pThis->rwlock, NULL)); |
| 115 |
initialized++; /*1*/ |
| 116 |
CHKiConcCtrl(pthread_mutex_init(&pThis->reloader_mut, NULL)); |
| 117 |
initialized++; /*2*/ |
| 118 |
CHKiConcCtrl(pthread_cond_init(&pThis->run_reloader, NULL)); |
| 119 |
initialized++; /*3*/ |
| 120 |
CHKiConcCtrl(pthread_attr_init(&pThis->reloader_thd_attr)); |
| 121 |
initialized++; /*4*/ |
| 122 |
pThis->do_reload = pThis->do_stop = 0; |
| 123 |
CHKiConcCtrl(pthread_create(&pThis->reloader, &pThis->reloader_thd_attr, lookupTableReloader, pThis)); |
| 124 |
initialized++; /*5*/ |
| 125 |
|
| 126 |
|
| 127 |
finalize_it: |
| 128 |
if (iRet != RS_RET_OK) { |
| 129 |
LogError(errno, iRet, |
| 130 |
"a lookup table could not be activated: " |
| 131 |
"failed at init-step %d (please enable debug logs for details)", |
| 132 |
initialized); |
| 133 |
/* Can not happen with current code, but might occur in the future when |
| 134 |
* an error-condition as added after step 5. If we leave it in, Coverity |
| 135 |
* scan complains. So we comment it out but do not remove the code. |
| 136 |
* Triggered by CID 185426 |
| 137 |
if (initialized > 4) lookupStopReloader(pThis); |
| 138 |
*/ |
| 139 |
if (initialized > 3) pthread_attr_destroy(&pThis->reloader_thd_attr); |
| 140 |
if (initialized > 2) pthread_cond_destroy(&pThis->run_reloader); |
| 141 |
if (initialized > 1) pthread_mutex_destroy(&pThis->reloader_mut); |
| 142 |
if (initialized > 0) pthread_rwlock_destroy(&pThis->rwlock); |
| 143 |
} |
| 144 |
RETiRet; |
| 145 |
} |
| 146 |
|
| 147 |
/*must be called with reloader_mut acquired*/ |
| 148 |
static void ATTR_NONNULL() freeStubValueForReloadFailure(lookup_ref_t *const pThis) { |
| 149 |
if (pThis->stub_value_for_reload_failure != NULL) { |
| 150 |
free(pThis->stub_value_for_reload_failure); |
| 151 |
pThis->stub_value_for_reload_failure = NULL; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
static void lookupStopReloader(lookup_ref_t *pThis) { |
| 156 |
pthread_mutex_lock(&pThis->reloader_mut); |
| 157 |
freeStubValueForReloadFailure(pThis); |
| 158 |
pThis->do_reload = 0; |
| 159 |
pThis->do_stop = 1; |
| 160 |
pthread_cond_signal(&pThis->run_reloader); |
| 161 |
pthread_mutex_unlock(&pThis->reloader_mut); |
| 162 |
pthread_join(pThis->reloader, NULL); |
| 163 |
} |
| 164 |
|
| 165 |
static void lookupRefDestruct(lookup_ref_t *pThis) { |
| 166 |
lookupStopReloader(pThis); |
| 167 |
pthread_mutex_destroy(&pThis->reloader_mut); |
| 168 |
pthread_cond_destroy(&pThis->run_reloader); |
| 169 |
pthread_attr_destroy(&pThis->reloader_thd_attr); |
| 170 |
|
| 171 |
pthread_rwlock_destroy(&pThis->rwlock); |
| 172 |
lookupDestruct(pThis->self); |
| 173 |
free(pThis->name); |
| 174 |
free(pThis->filename); |
| 175 |
free(pThis); |
| 176 |
} |
| 177 |
|
| 178 |
static void destructTable_str(lookup_t *pThis) { |
| 179 |
uint32_t i = 0; |
| 180 |
lookup_string_tab_entry_t *entries = pThis->table.str->entries; |
| 181 |
for (i = 0; i < pThis->nmemb; i++) { |
| 182 |
free(entries[i].key); |
| 183 |
} |
| 184 |
free(entries); |
| 185 |
free(pThis->table.str); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
static void destructTable_arr(lookup_t *pThis) { |
| 190 |
free(pThis->table.arr->interned_val_refs); |
| 191 |
free(pThis->table.arr); |
| 192 |
} |
| 193 |
|
| 194 |
static void destructTable_sparseArr(lookup_t *pThis) { |
| 195 |
free(pThis->table.sprsArr->entries); |
| 196 |
free(pThis->table.sprsArr); |
| 197 |
} |
| 198 |
|
| 199 |
#ifdef FEATURE_REGEXP |
| 200 |
static void destructTable_regex(lookup_t *pThis) { |
| 201 |
uint32_t i; |
| 202 |
int regfree_available = 0; |
| 203 |
lookup_regex_tab_entry_t *entries; |
| 204 |
|
| 205 |
if (pThis == NULL || pThis->table.regex == NULL) return; |
| 206 |
|
| 207 |
entries = pThis->table.regex->entries; |
| 208 |
if (objUse(regexp, LM_REGEXP_FILENAME) == RS_RET_OK) regfree_available = 1; |
| 209 |
|
| 210 |
if (entries != NULL) { |
| 211 |
for (i = 0; i < pThis->nmemb; ++i) { |
| 212 |
if (entries[i].is_compiled) { |
| 213 |
if (regfree_available) { |
| 214 |
regexp.regfree(&entries[i].regex); |
| 215 |
} else { |
| 216 |
LogError(0, RS_RET_INTERNAL_ERROR, "regexp module missing; compiled regex cannot be freed"); |
| 217 |
} |
| 218 |
} |
| 219 |
free(entries[i].regex_str); |
| 220 |
entries[i].regex_str = NULL; |
| 221 |
} |
| 222 |
} |
| 223 |
free(entries); |
| 224 |
free(pThis->table.regex); |
| 225 |
} |
| 226 |
#endif |
| 227 |
|
| 228 |
static void lookupDestruct(lookup_t *pThis) { |
| 229 |
uint32_t i; |
| 230 |
|
| 231 |
if (pThis == NULL) return; |
| 232 |
|
| 233 |
if (pThis->type == STRING_LOOKUP_TABLE) { |
| 234 |
destructTable_str(pThis); |
| 235 |
} else if (pThis->type == ARRAY_LOOKUP_TABLE) { |
| 236 |
destructTable_arr(pThis); |
| 237 |
} else if (pThis->type == SPARSE_ARRAY_LOOKUP_TABLE) { |
| 238 |
destructTable_sparseArr(pThis); |
| 239 |
#ifdef FEATURE_REGEXP |
| 240 |
} else if (pThis->type == REGEX_LOOKUP_TABLE) { |
| 241 |
destructTable_regex(pThis); |
| 242 |
#endif |
| 243 |
} else if (pThis->type == STUBBED_LOOKUP_TABLE) { |
| 244 |
/*nothing to be done*/ |
| 245 |
} |
| 246 |
|
| 247 |
for (i = 0; i < pThis->interned_val_count; i++) { |
| 248 |
free(pThis->interned_vals[i]); |
| 249 |
} |
| 250 |
free(pThis->interned_vals); |
| 251 |
free(pThis->nomatch); |
| 252 |
free(pThis); |
| 253 |
} |
| 254 |
|
| 255 |
void lookupInitCnf(lookup_tables_t *lu_tabs) { |
| 256 |
lu_tabs->root = NULL; |
| 257 |
lu_tabs->last = NULL; |
| 258 |
} |
| 259 |
|
| 260 |
void lookupDestroyCnf(void) { |
| 261 |
lookup_ref_t *luref, *luref_next; |
| 262 |
for (luref = runConf->lu_tabs.root; luref != NULL;) { |
| 263 |
luref_next = luref->next; |
| 264 |
lookupRefDestruct(luref); |
| 265 |
luref = luref_next; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
/* comparison function for qsort() */ |
| 270 |
static int qs_arrcmp_strtab(const void *s1, const void *s2) { |
| 271 |
return ustrcmp(((lookup_string_tab_entry_t *)s1)->key, ((lookup_string_tab_entry_t *)s2)->key); |
| 272 |
} |
| 273 |
|
| 274 |
static int qs_arrcmp_ustrs(const void *s1, const void *s2) { |
| 275 |
return ustrcmp(*(uchar **)s1, *(uchar **)s2); |
| 276 |
} |
| 277 |
|
| 278 |
static int qs_arrcmp_uint32_index_val(const void *s1, const void *s2) { |
| 279 |
uint32_t first_value = ((uint32_index_val_t *)s1)->index; |
| 280 |
uint32_t second_value = ((uint32_index_val_t *)s2)->index; |
| 281 |
if (first_value < second_value) { |
| 282 |
return -1; |
| 283 |
} |
| 284 |
if (first_value > second_value) { |
| 285 |
return 1; |
| 286 |
} |
| 287 |
return 0; |
| 288 |
} |
| 289 |
|
| 290 |
static int qs_arrcmp_sprsArrtab(const void *s1, const void *s2) { |
| 291 |
uint32_t first_value = ((lookup_sparseArray_tab_entry_t *)s1)->key; |
| 292 |
uint32_t second_value = ((lookup_sparseArray_tab_entry_t *)s2)->key; |
| 293 |
if (first_value < second_value) { |
| 294 |
return -1; |
| 295 |
} |
| 296 |
if (first_value > second_value) { |
| 297 |
return 1; |
| 298 |
} |
| 299 |
return 0; |
| 300 |
} |
| 301 |
|
| 302 |
/* comparison function for bsearch() and string array compare |
| 303 |
* this is for the string lookup table type |
| 304 |
*/ |
| 305 |
static int bs_arrcmp_strtab(const void *s1, const void *s2) { |
| 306 |
return strcmp((char *)s1, (char *)((lookup_string_tab_entry_t *)s2)->key); |
| 307 |
} |
| 308 |
|
| 309 |
static int bs_arrcmp_str(const void *s1, const void *s2) { |
| 310 |
return ustrcmp((uchar *)s1, *(uchar **)s2); |
| 311 |
} |
| 312 |
|
| 313 |
static int bs_arrcmp_sprsArrtab(const void *s1, const void *s2) { |
| 314 |
uint32_t key = *(uint32_t *)s1; |
| 315 |
uint32_t array_member_value = ((lookup_sparseArray_tab_entry_t *)s2)->key; |
| 316 |
if (key < array_member_value) { |
| 317 |
return -1; |
| 318 |
} |
| 319 |
if (key > array_member_value) { |
| 320 |
return 1; |
| 321 |
} |
| 322 |
return 0; |
| 323 |
} |
| 324 |
|
| 325 |
static inline const char *defaultVal(lookup_t *pThis) { |
| 326 |
return (pThis->nomatch == NULL) ? "" : (const char *)pThis->nomatch; |
| 327 |
} |
| 328 |
|
| 329 |
/* lookup_fn for different types of tables */ |
| 330 |
static es_str_t *lookupKey_stub(lookup_t *pThis, lookup_key_t __attribute__((unused)) key) { |
| 331 |
return es_newStrFromCStr((char *)pThis->nomatch, ustrlen(pThis->nomatch)); |
| 332 |
} |
| 333 |
|
| 334 |
static es_str_t *lookupKey_str(lookup_t *pThis, lookup_key_t key) { |
| 335 |
lookup_string_tab_entry_t *entry; |
| 336 |
const char *r; |
| 337 |
if (pThis->nmemb == 0) { |
| 338 |
entry = NULL; |
| 339 |
} else { |
| 340 |
assert(pThis->table.str->entries); |
| 341 |
entry = bsearch(key.k_str, pThis->table.str->entries, pThis->nmemb, sizeof(lookup_string_tab_entry_t), |
| 342 |
bs_arrcmp_strtab); |
| 343 |
} |
| 344 |
if (entry == NULL) { |
| 345 |
r = defaultVal(pThis); |
| 346 |
} else { |
| 347 |
r = (const char *)entry->interned_val_ref; |
| 348 |
} |
| 349 |
return es_newStrFromCStr(r, strlen(r)); |
| 350 |
} |
| 351 |
|
| 352 |
static es_str_t *lookupKey_arr(lookup_t *pThis, lookup_key_t key) { |
| 353 |
const char *r; |
| 354 |
uint32_t uint_key = key.k_uint; |
| 355 |
if ((pThis->nmemb == 0) || (uint_key < pThis->table.arr->first_key)) { |
| 356 |
r = defaultVal(pThis); |
| 357 |
} else { |
| 358 |
uint32_t idx = uint_key - pThis->table.arr->first_key; |
| 359 |
if (idx >= pThis->nmemb) { |
| 360 |
r = defaultVal(pThis); |
| 361 |
} else { |
| 362 |
r = (char *)pThis->table.arr->interned_val_refs[idx]; |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
return es_newStrFromCStr(r, strlen(r)); |
| 367 |
} |
| 368 |
|
| 369 |
typedef int(comp_fn_t)(const void *s1, const void *s2); |
| 370 |
|
| 371 |
static void *bsearch_lte(const void *key, const void *base, size_t nmemb, size_t size, comp_fn_t *comp_fn) { |
| 372 |
size_t l, u, idx; |
| 373 |
const void *p; |
| 374 |
int comparison; |
| 375 |
|
| 376 |
l = 0; |
| 377 |
u = nmemb; |
| 378 |
if (l == u) { |
| 379 |
return NULL; |
| 380 |
} |
| 381 |
while (l < u) { |
| 382 |
idx = (l + u) / 2; |
| 383 |
p = (void *)(((const char *)base) + (idx * size)); |
| 384 |
comparison = (*comp_fn)(key, p); |
| 385 |
if (comparison < 0) |
| 386 |
u = idx; |
| 387 |
else if (comparison > 0) |
| 388 |
l = idx + 1; |
| 389 |
else |
| 390 |
return (void *)p; |
| 391 |
} |
| 392 |
if (comparison < 0) { |
| 393 |
if (idx == 0) { |
| 394 |
return NULL; |
| 395 |
} |
| 396 |
idx--; |
| 397 |
} |
| 398 |
return (void *)(((const char *)base) + (idx * size)); |
| 399 |
} |
| 400 |
|
| 401 |
static es_str_t *lookupKey_sprsArr(lookup_t *pThis, lookup_key_t key) { |
| 402 |
lookup_sparseArray_tab_entry_t *entry; |
| 403 |
const char *r; |
| 404 |
if (pThis->nmemb == 0) { |
| 405 |
entry = NULL; |
| 406 |
} else { |
| 407 |
entry = bsearch_lte(&key.k_uint, pThis->table.sprsArr->entries, pThis->nmemb, |
| 408 |
sizeof(lookup_sparseArray_tab_entry_t), bs_arrcmp_sprsArrtab); |
| 409 |
} |
| 410 |
|
| 411 |
if (entry == NULL) { |
| 412 |
r = defaultVal(pThis); |
| 413 |
} else { |
| 414 |
r = (const char *)entry->interned_val_ref; |
| 415 |
} |
| 416 |
return es_newStrFromCStr(r, strlen(r)); |
| 417 |
} |
| 418 |
|
| 419 |
#ifdef FEATURE_REGEXP |
| 420 |
static es_str_t *lookupKey_regex(lookup_t *pThis, lookup_key_t key) { |
| 421 |
const char *r = defaultVal(pThis); |
| 422 |
|
| 423 |
for (uint32_t i = 0; i < pThis->nmemb; ++i) { |
| 424 |
if (regexp.regexec(&pThis->table.regex->entries[i].regex, (char *)key.k_str, 0, NULL, 0) == 0) { |
| 425 |
r = (const char *)pThis->table.regex->entries[i].interned_val_ref; |
| 426 |
break; |
| 427 |
} |
| 428 |
} |
| 429 |
return es_newStrFromCStr(r, strlen(r)); |
| 430 |
} |
| 431 |
#endif |
| 432 |
|
| 433 |
/* builders for different table-types */ |
| 434 |
|
| 435 |
#define NO_INDEX_ERROR(type, name) \ |
| 436 |
LogError(0, RS_RET_INVALID_VALUE, \ |
| 437 |
"'%s' lookup table named: '%s' has record(s) without 'index' " \ |
| 438 |
"field", \ |
| 439 |
type, name); \ |
| 440 |
ABORT_FINALIZE(RS_RET_INVALID_VALUE); |
| 441 |
|
| 442 |
static rsRetVal build_StringTable(lookup_t *pThis, struct json_object *jtab, const uchar *name) { |
| 443 |
uint32_t i; |
| 444 |
struct json_object *jrow, *jindex, *jvalue; |
| 445 |
uchar *value, *canonicalValueRef; |
| 446 |
DEFiRet; |
| 447 |
|
| 448 |
pThis->table.str = NULL; |
| 449 |
CHKmalloc(pThis->table.str = calloc(1, sizeof(lookup_string_tab_t))); |
| 450 |
if (pThis->nmemb > 0) { |
| 451 |
CHKmalloc(pThis->table.str->entries = calloc(pThis->nmemb, sizeof(lookup_string_tab_entry_t))); |
| 452 |
|
| 453 |
for (i = 0; i < pThis->nmemb; i++) { |
| 454 |
jrow = json_object_array_get_idx(jtab, i); |
| 455 |
fjson_object_object_get_ex(jrow, "index", &jindex); |
| 456 |
fjson_object_object_get_ex(jrow, "value", &jvalue); |
| 457 |
if (jindex == NULL || json_object_is_type(jindex, json_type_null)) { |
| 458 |
NO_INDEX_ERROR("string", name); |
| 459 |
} |
| 460 |
CHKmalloc(pThis->table.str->entries[i].key = ustrdup((uchar *)json_object_get_string(jindex))); |
| 461 |
value = (uchar *)json_object_get_string(jvalue); |
| 462 |
uchar **found = (uchar **)bsearch(value, pThis->interned_vals, pThis->interned_val_count, sizeof(uchar *), |
| 463 |
bs_arrcmp_str); |
| 464 |
if (found == NULL) { |
| 465 |
LogError(0, RS_RET_INTERNAL_ERROR, |
| 466 |
"lookup.c:build_StringTable(): " |
| 467 |
"internal error, bsearch returned NULL for '%s'", |
| 468 |
value); |
| 469 |
ABORT_FINALIZE(RS_RET_INTERNAL_ERROR); |
| 470 |
} |
| 471 |
// I give up, I see no way to remove false positive -- rgerhards, 2017-10-24 |
| 472 |
#ifndef __clang_analyzer__ |
| 473 |
canonicalValueRef = *found; |
| 474 |
if (canonicalValueRef == NULL) { |
| 475 |
LogError(0, RS_RET_INTERNAL_ERROR, |
| 476 |
"lookup.c:build_StringTable(): " |
| 477 |
"internal error, canonicalValueRef returned from bsearch " |
| 478 |
"is NULL for '%s'", |
| 479 |
value); |
| 480 |
ABORT_FINALIZE(RS_RET_INTERNAL_ERROR); |
| 481 |
} |
| 482 |
pThis->table.str->entries[i].interned_val_ref = canonicalValueRef; |
| 483 |
#endif |
| 484 |
} |
| 485 |
qsort(pThis->table.str->entries, pThis->nmemb, sizeof(lookup_string_tab_entry_t), qs_arrcmp_strtab); |
| 486 |
} |
| 487 |
|
| 488 |
pThis->lookup = lookupKey_str; |
| 489 |
pThis->key_type = LOOKUP_KEY_TYPE_STRING; |
| 490 |
finalize_it: |
| 491 |
RETiRet; |
| 492 |
} |
| 493 |
|
| 494 |
static rsRetVal build_ArrayTable(lookup_t *pThis, struct json_object *jtab, const uchar *name) { |
| 495 |
uint32_t i; |
| 496 |
struct json_object *jrow, *jindex, *jvalue; |
| 497 |
uchar *canonicalValueRef; |
| 498 |
uint32_t prev_index, _index; |
| 499 |
uint8_t prev_index_set; |
| 500 |
uint32_index_val_t *indexes = NULL; |
| 501 |
DEFiRet; |
| 502 |
|
| 503 |
prev_index_set = 0; |
| 504 |
|
| 505 |
pThis->table.arr = NULL; |
| 506 |
CHKmalloc(pThis->table.arr = calloc(1, sizeof(lookup_array_tab_t))); |
| 507 |
if (pThis->nmemb > 0) { |
| 508 |
CHKmalloc(indexes = calloc(pThis->nmemb, sizeof(uint32_index_val_t))); |
| 509 |
CHKmalloc(pThis->table.arr->interned_val_refs = calloc(pThis->nmemb, sizeof(uchar *))); |
| 510 |
|
| 511 |
for (i = 0; i < pThis->nmemb; i++) { |
| 512 |
jrow = json_object_array_get_idx(jtab, i); |
| 513 |
fjson_object_object_get_ex(jrow, "index", &jindex); |
| 514 |
fjson_object_object_get_ex(jrow, "value", &jvalue); |
| 515 |
if (jindex == NULL || json_object_is_type(jindex, json_type_null)) { |
| 516 |
NO_INDEX_ERROR("array", name); |
| 517 |
} |
| 518 |
int64_t index_val = json_object_get_int64(jindex); |
| 519 |
if (index_val < 0 || index_val > UINT32_MAX) { |
| 520 |
LogError(0, RS_RET_INVALID_VALUE, |
| 521 |
"'array' lookup table name: '%s' has index '%lld' outside uint32 range", name, |
| 522 |
(long long)index_val); |
| 523 |
ABORT_FINALIZE(RS_RET_INVALID_VALUE); |
| 524 |
} |
| 525 |
indexes[i].index = (uint32_t)index_val; |
| 526 |
indexes[i].val = (uchar *)json_object_get_string(jvalue); |
| 527 |
} |
| 528 |
qsort(indexes, pThis->nmemb, sizeof(uint32_index_val_t), qs_arrcmp_uint32_index_val); |
| 529 |
for (i = 0; i < pThis->nmemb; i++) { |
| 530 |
_index = indexes[i].index; |
| 531 |
if (prev_index_set == 0) { |
| 532 |
prev_index = _index; |
| 533 |
prev_index_set = 1; |
| 534 |
pThis->table.arr->first_key = _index; |
| 535 |
} else { |
| 536 |
if (_index != ++prev_index) { |
| 537 |
LogError(0, RS_RET_INVALID_VALUE, |
| 538 |
"'array' lookup table name: '%s' " |
| 539 |
"has non-contiguous members between index '%d' and '%d'", |
| 540 |
name, prev_index, _index); |
| 541 |
ABORT_FINALIZE(RS_RET_INVALID_VALUE); |
| 542 |
} |
| 543 |
} |
| 544 |
uchar *const *const canonicalValueRef_ptr = bsearch( |
| 545 |
indexes[i].val, pThis->interned_vals, pThis->interned_val_count, sizeof(uchar *), bs_arrcmp_str); |
| 546 |
if (canonicalValueRef_ptr == NULL) { |
| 547 |
LogError(0, RS_RET_ERR, |
| 548 |
"BUG: canonicalValueRef not found in " |
| 549 |
"build_ArrayTable(), %s:%d", |
| 550 |
__FILE__, __LINE__); |
| 551 |
ABORT_FINALIZE(RS_RET_ERR); |
| 552 |
} |
| 553 |
canonicalValueRef = *canonicalValueRef_ptr; |
| 554 |
assert(canonicalValueRef != NULL); |
| 555 |
pThis->table.arr->interned_val_refs[i] = canonicalValueRef; |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
pThis->lookup = lookupKey_arr; |
| 560 |
pThis->key_type = LOOKUP_KEY_TYPE_UINT; |
| 561 |
|
| 562 |
finalize_it: |
| 563 |
free(indexes); |
| 564 |
RETiRet; |
| 565 |
} |
| 566 |
|
| 567 |
static rsRetVal build_SparseArrayTable(lookup_t *pThis, struct json_object *jtab, const uchar *name) { |
| 568 |
uint32_t i; |
| 569 |
struct json_object *jrow, *jindex, *jvalue; |
| 570 |
uchar *value, *canonicalValueRef; |
| 571 |
DEFiRet; |
| 572 |
|
| 573 |
pThis->table.str = NULL; |
| 574 |
CHKmalloc(pThis->table.sprsArr = calloc(1, sizeof(lookup_sparseArray_tab_t))); |
| 575 |
if (pThis->nmemb > 0) { |
| 576 |
CHKmalloc(pThis->table.sprsArr->entries = calloc(pThis->nmemb, sizeof(lookup_sparseArray_tab_entry_t))); |
| 577 |
|
| 578 |
for (i = 0; i < pThis->nmemb; i++) { |
| 579 |
jrow = json_object_array_get_idx(jtab, i); |
| 580 |
fjson_object_object_get_ex(jrow, "index", &jindex); |
| 581 |
fjson_object_object_get_ex(jrow, "value", &jvalue); |
| 582 |
if (jindex == NULL || json_object_is_type(jindex, json_type_null)) { |
| 583 |
NO_INDEX_ERROR("sparseArray", name); |
| 584 |
} |
| 585 |
int64_t index_val = json_object_get_int64(jindex); |
| 586 |
if (index_val < 0 || index_val > UINT32_MAX) { |
| 587 |
LogError(0, RS_RET_INVALID_VALUE, |
| 588 |
"'sparseArray' lookup table name: '%s' has index '%lld' outside uint32 range", name, |
| 589 |
(long long)index_val); |
| 590 |
ABORT_FINALIZE(RS_RET_INVALID_VALUE); |
| 591 |
} |
| 592 |
pThis->table.sprsArr->entries[i].key = (uint32_t)index_val; |
| 593 |
value = (uchar *)json_object_get_string(jvalue); |
| 594 |
uchar *const *const canonicalValueRef_ptr = |
| 595 |
bsearch(value, pThis->interned_vals, pThis->interned_val_count, sizeof(uchar *), bs_arrcmp_str); |
| 596 |
if (canonicalValueRef_ptr == NULL) { |
| 597 |
LogError(0, RS_RET_ERR, |
| 598 |
"BUG: canonicalValueRef not found in " |
| 599 |
"build_SparseArrayTable(), %s:%d", |
| 600 |
__FILE__, __LINE__); |
| 601 |
ABORT_FINALIZE(RS_RET_ERR); |
| 602 |
} |
| 603 |
canonicalValueRef = *canonicalValueRef_ptr; |
| 604 |
assert(canonicalValueRef != NULL); |
| 605 |
pThis->table.sprsArr->entries[i].interned_val_ref = canonicalValueRef; |
| 606 |
} |
| 607 |
qsort(pThis->table.sprsArr->entries, pThis->nmemb, sizeof(lookup_sparseArray_tab_entry_t), |
| 608 |
qs_arrcmp_sprsArrtab); |
| 609 |
} |
| 610 |
|
| 611 |
pThis->lookup = lookupKey_sprsArr; |
| 612 |
pThis->key_type = LOOKUP_KEY_TYPE_UINT; |
| 613 |
|
| 614 |
finalize_it: |
| 615 |
RETiRet; |
| 616 |
} |
| 617 |
|
| 618 |
#ifdef FEATURE_REGEXP |
| 619 |
static rsRetVal build_RegexTable(lookup_t *pThis, struct json_object *jtab, const uchar *name) { |
| 620 |
uint32_t i; |
| 621 |
struct json_object *jrow, *jregex, *jtag; |
| 622 |
uchar *value, *canonicalValueRef; |
| 623 |
const char *regex_str; |
| 624 |
DEFiRet; |
| 625 |
|
| 626 |
pThis->table.regex = NULL; |
| 627 |
CHKmalloc(pThis->table.regex = calloc(1, sizeof(lookup_regex_tab_t))); |
| 628 |
if (pThis->nmemb > 0) { |
| 629 |
CHKmalloc(pThis->table.regex->entries = calloc(pThis->nmemb, sizeof(lookup_regex_tab_entry_t))); |
| 630 |
|
| 631 |
for (i = 0; i < pThis->nmemb; i++) { |
| 632 |
jrow = json_object_array_get_idx(jtab, i); |
| 633 |
fjson_object_object_get_ex(jrow, "regex", &jregex); |
| 634 |
fjson_object_object_get_ex(jrow, "tag", &jtag); |
| 635 |
if (jregex == NULL || jtag == NULL || json_object_is_type(jregex, json_type_null) || |
| 636 |
json_object_is_type(jtag, json_type_null)) { |
| 637 |
LogError(0, RS_RET_INVALID_VALUE, |
| 638 |
"'regex' lookup table named: '%s' has record(s) without required " |
| 639 |
"fields", |
| 640 |
name); |
| 641 |
ABORT_FINALIZE(RS_RET_INVALID_VALUE); |
| 642 |
} |
| 643 |
regex_str = json_object_get_string(jregex); |
| 644 |
CHKmalloc(pThis->table.regex->entries[i].regex_str = ustrdup((const uchar *)regex_str)); |
| 645 |
value = (uchar *)json_object_get_string(jtag); |
| 646 |
uchar *const *const canonicalValueRef_ptr = |
| 647 |
bsearch(value, pThis->interned_vals, pThis->interned_val_count, sizeof(uchar *), bs_arrcmp_str); |
| 648 |
if (canonicalValueRef_ptr == NULL) { |
| 649 |
LogError(0, RS_RET_ERR, "BUG: canonicalValueRef not found in build_RegexTable(), %s:%d", __FILE__, |
| 650 |
__LINE__); |
| 651 |
ABORT_FINALIZE(RS_RET_ERR); |
| 652 |
} |
| 653 |
canonicalValueRef = *canonicalValueRef_ptr; |
| 654 |
assert(canonicalValueRef != NULL); |
| 655 |
pThis->table.regex->entries[i].interned_val_ref = canonicalValueRef; |
| 656 |
|
| 657 |
int err; |
| 658 |
if (objUse(regexp, LM_REGEXP_FILENAME) == RS_RET_OK) { |
| 659 |
if ((err = regexp.regcomp(&pThis->table.regex->entries[i].regex, regex_str, |
| 660 |
REG_EXTENDED | REG_NOSUB)) != 0) { |
| 661 |
char errbuf[256]; |
| 662 |
regexp.regerror(err, &pThis->table.regex->entries[i].regex, errbuf, sizeof(errbuf)); |
| 663 |
LogError(0, RS_RET_INVALID_VALUE, "error compiling regex '%s' in lookup table '%s': %s", regex_str, |
| 664 |
name, errbuf); |
| 665 |
ABORT_FINALIZE(RS_RET_INVALID_VALUE); |
| 666 |
} else { |
| 667 |
pThis->table.regex->entries[i].is_compiled = 1; |
| 668 |
} |
| 669 |
} else { |
| 670 |
LogError(0, RS_RET_INTERNAL_ERROR, "regex library could not be loaded"); |
| 671 |
ABORT_FINALIZE(RS_RET_INTERNAL_ERROR); |
| 672 |
} |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
pThis->lookup = lookupKey_regex; |
| 677 |
pThis->key_type = LOOKUP_KEY_TYPE_STRING; |
| 678 |
|
| 679 |
finalize_it: |
| 680 |
RETiRet; |
| 681 |
} |
| 682 |
#endif |
| 683 |
|
| 684 |
static rsRetVal lookupBuildStubbedTable(lookup_t *pThis, const uchar *stub_val) { |
| 685 |
DEFiRet; |
| 686 |
|
| 687 |
CHKmalloc(pThis->nomatch = ustrdup(stub_val)); |
| 688 |
pThis->lookup = lookupKey_stub; |
| 689 |
pThis->type = STUBBED_LOOKUP_TABLE; |
| 690 |
pThis->key_type = LOOKUP_KEY_TYPE_NONE; |
| 691 |
|
| 692 |
finalize_it: |
| 693 |
RETiRet; |
| 694 |
} |
| 695 |
|
| 696 |
static rsRetVal lookupBuildTable_v1(lookup_t *pThis, struct json_object *jroot, const uchar *name) { |
| 697 |
struct json_object *jnomatch, *jtype, *jtab; |
| 698 |
struct json_object *jrow, *jvalue; |
| 699 |
const char *table_type, *nomatch_value; |
| 700 |
const char *value_key; |
| 701 |
const uchar **all_values; |
| 702 |
const uchar *curr, *prev; |
| 703 |
uint32_t i, j; |
| 704 |
uint32_t uniq_values; |
| 705 |
|
| 706 |
DEFiRet; |
| 707 |
all_values = NULL; |
| 708 |
|
| 709 |
fjson_object_object_get_ex(jroot, "nomatch", &jnomatch); |
| 710 |
fjson_object_object_get_ex(jroot, "type", &jtype); |
| 711 |
fjson_object_object_get_ex(jroot, "table", &jtab); |
| 712 |
if (jtab == NULL || !json_object_is_type(jtab, json_type_array)) { |
| 713 |
LogError(0, RS_RET_INVALID_VALUE, "lookup table named: '%s' has invalid table definition", name); |
| 714 |
ABORT_FINALIZE(RS_RET_INVALID_VALUE); |
| 715 |
} |
| 716 |
pThis->nmemb = json_object_array_length(jtab); |
| 717 |
table_type = json_object_get_string(jtype); |
| 718 |
if (table_type == NULL) { |
| 719 |
table_type = "string"; |
| 720 |
} |
| 721 |
value_key = (strcmp(table_type, "regex") == 0) ? "tag" : "value"; |
| 722 |
|
| 723 |
CHKmalloc(all_values = malloc(pThis->nmemb * sizeof(uchar *))); |
| 724 |
|
| 725 |
/* before actual table can be loaded, prepare all-value list and remove duplicates*/ |
| 726 |
for (i = 0; i < pThis->nmemb; i++) { |
| 727 |
jrow = json_object_array_get_idx(jtab, i); |
| 728 |
fjson_object_object_get_ex(jrow, value_key, &jvalue); |
| 729 |
if (jvalue == NULL || json_object_is_type(jvalue, json_type_null)) { |
| 730 |
LogError(0, RS_RET_INVALID_VALUE, |
| 731 |
"'%s' lookup table named: '%s' has record(s) " |
| 732 |
"without '%s' field", |
| 733 |
table_type, name, value_key); |
| 734 |
ABORT_FINALIZE(RS_RET_INVALID_VALUE); |
| 735 |
} |
| 736 |
all_values[i] = (const uchar *)json_object_get_string(jvalue); |
| 737 |
} |
| 738 |
qsort(all_values, pThis->nmemb, sizeof(uchar *), qs_arrcmp_ustrs); |
| 739 |
uniq_values = 1; |
| 740 |
for (i = 1; i < pThis->nmemb; i++) { |
| 741 |
curr = all_values[i]; |
| 742 |
prev = all_values[i - 1]; |
| 743 |
if (ustrcmp(prev, curr) != 0) { |
| 744 |
uniq_values++; |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
if (pThis->nmemb > 0) { |
| 749 |
CHKmalloc(pThis->interned_vals = malloc(uniq_values * sizeof(uchar *))); |
| 750 |
j = 0; |
| 751 |
CHKmalloc(pThis->interned_vals[j++] = ustrdup(all_values[0])); |
| 752 |
for (i = 1; i < pThis->nmemb; ++i) { |
| 753 |
curr = all_values[i]; |
| 754 |
prev = all_values[i - 1]; |
| 755 |
if (ustrcmp(prev, curr) != 0) { |
| 756 |
CHKmalloc(pThis->interned_vals[j++] = ustrdup(all_values[i])); |
| 757 |
} |
| 758 |
} |
| 759 |
pThis->interned_val_count = uniq_values; |
| 760 |
} |
| 761 |
/* uniq values captured (sorted) */ |
| 762 |
|
| 763 |
nomatch_value = json_object_get_string(jnomatch); |
| 764 |
if (nomatch_value != NULL) { |
| 765 |
CHKmalloc(pThis->nomatch = (uchar *)strdup(nomatch_value)); |
| 766 |
} |
| 767 |
|
| 768 |
if (strcmp(table_type, "array") == 0) { |
| 769 |
pThis->type = ARRAY_LOOKUP_TABLE; |
| 770 |
CHKiRet(build_ArrayTable(pThis, jtab, name)); |
| 771 |
} else if (strcmp(table_type, "sparseArray") == 0) { |
| 772 |
pThis->type = SPARSE_ARRAY_LOOKUP_TABLE; |
| 773 |
CHKiRet(build_SparseArrayTable(pThis, jtab, name)); |
| 774 |
#ifdef FEATURE_REGEXP |
| 775 |
} else if (strcmp(table_type, "regex") == 0) { |
| 776 |
pThis->type = REGEX_LOOKUP_TABLE; |
| 777 |
CHKiRet(build_RegexTable(pThis, jtab, name)); |
| 778 |
#endif |
| 779 |
} else if (strcmp(table_type, "string") == 0) { |
| 780 |
pThis->type = STRING_LOOKUP_TABLE; |
| 781 |
CHKiRet(build_StringTable(pThis, jtab, name)); |
| 782 |
} else { |
| 783 |
LogError(0, RS_RET_INVALID_VALUE, |
| 784 |
"lookup table named: '%s' uses unupported " |
| 785 |
"type: '%s'", |
| 786 |
name, table_type); |
| 787 |
ABORT_FINALIZE(RS_RET_INVALID_VALUE); |
| 788 |
} |
| 789 |
finalize_it: |
| 790 |
if (all_values != NULL) free(all_values); |
| 791 |
RETiRet; |
| 792 |
} |
| 793 |
|
| 794 |
static rsRetVal lookupBuildTable(lookup_t *pThis, struct json_object *jroot, const uchar *name) { |
| 795 |
struct json_object *jversion; |
| 796 |
int version = 1; |
| 797 |
|
| 798 |
DEFiRet; |
| 799 |
|
| 800 |
fjson_object_object_get_ex(jroot, "version", &jversion); |
| 801 |
if (jversion != NULL && !json_object_is_type(jversion, json_type_null)) { |
| 802 |
version = json_object_get_int(jversion); |
| 803 |
} else { |
| 804 |
LogError(0, RS_RET_INVALID_VALUE, |
| 805 |
"lookup table named: '%s' doesn't specify version " |
| 806 |
"(will use default value: %d)", |
| 807 |
name, version); |
| 808 |
} |
| 809 |
if (version == 1) { |
| 810 |
CHKiRet(lookupBuildTable_v1(pThis, jroot, name)); |
| 811 |
} else { |
| 812 |
LogError(0, RS_RET_INVALID_VALUE, |
| 813 |
"lookup table named: '%s' uses unsupported " |
| 814 |
"version: %d", |
| 815 |
name, version); |
| 816 |
ABORT_FINALIZE(RS_RET_INVALID_VALUE); |
| 817 |
} |
| 818 |
|
| 819 |
finalize_it: |
| 820 |
RETiRet; |
| 821 |
} |
| 822 |
|
| 823 |
|
| 824 |
/* find a lookup table. This is a naive O(n) algo, but this really |
| 825 |
* doesn't matter as it is called only a few times during config |
| 826 |
* load. The function returns either a pointer to the requested |
| 827 |
* table or NULL, if not found. |
| 828 |
*/ |
| 829 |
lookup_ref_t *ATTR_NONNULL() lookupFindTable(uchar *name) { |
| 830 |
lookup_ref_t *curr; |
| 831 |
|
| 832 |
for (curr = loadConf->lu_tabs.root; curr != NULL; curr = curr->next) { |
| 833 |
if (!ustrcmp(curr->name, name)) break; |
| 834 |
} |
| 835 |
return curr; |
| 836 |
} |
| 837 |
|
| 838 |
|
| 839 |
/* this reloads a lookup table. This is done while the engine is running, |
| 840 |
* as such the function must ensure proper locking and proper order of |
| 841 |
* operations (so that nothing can interfere). If the table cannot be loaded, |
| 842 |
* the old table is continued to be used. |
| 843 |
*/ |
| 844 |
static rsRetVal lookupReloadOrStub(lookup_ref_t *pThis, const uchar *stub_val) { |
| 845 |
lookup_t *newlu, *oldlu; /* dummy to be able to use support functions without |
| 846 |
affecting current settings. */ |
| 847 |
DEFiRet; |
| 848 |
|
| 849 |
oldlu = pThis->self; |
| 850 |
newlu = NULL; |
| 851 |
|
| 852 |
DBGPRINTF("reload requested for lookup table '%s'\n", pThis->name); |
| 853 |
CHKmalloc(newlu = calloc(1, sizeof(lookup_t))); |
| 854 |
if (stub_val == NULL) { |
| 855 |
CHKiRet(lookupReadFile(newlu, pThis->name, pThis->filename)); |
| 856 |
} else { |
| 857 |
CHKiRet(lookupBuildStubbedTable(newlu, stub_val)); |
| 858 |
} |
| 859 |
/* all went well, copy over data members */ |
| 860 |
pthread_rwlock_wrlock(&pThis->rwlock); |
| 861 |
pThis->self = newlu; |
| 862 |
pthread_rwlock_unlock(&pThis->rwlock); |
| 863 |
finalize_it: |
| 864 |
if (iRet != RS_RET_OK) { |
| 865 |
if (stub_val == NULL) { |
| 866 |
LogError(0, RS_RET_INTERNAL_ERROR, "lookup table '%s' could not be reloaded from file '%s'", pThis->name, |
| 867 |
pThis->filename); |
| 868 |
} else { |
| 869 |
LogError(0, RS_RET_INTERNAL_ERROR, "lookup table '%s' could not be stubbed with value '%s'", pThis->name, |
| 870 |
stub_val); |
| 871 |
} |
| 872 |
lookupDestruct(newlu); |
| 873 |
} else { |
| 874 |
if (stub_val == NULL) { |
| 875 |
LogMsg(0, RS_RET_OK, LOG_INFO, "lookup table '%s' reloaded from file '%s'", pThis->name, pThis->filename); |
| 876 |
} else { |
| 877 |
LogError(0, RS_RET_OK, "lookup table '%s' stubbed with value '%s'", pThis->name, stub_val); |
| 878 |
} |
| 879 |
lookupDestruct(oldlu); |
| 880 |
} |
| 881 |
RETiRet; |
| 882 |
} |
| 883 |
|
| 884 |
static rsRetVal lookupDoStub(lookup_ref_t *pThis, const uchar *stub_val) { |
| 885 |
int already_stubbed = 0; |
| 886 |
DEFiRet; |
| 887 |
pthread_rwlock_rdlock(&pThis->rwlock); |
| 888 |
if (pThis->self->type == STUBBED_LOOKUP_TABLE && ustrcmp(pThis->self->nomatch, stub_val) == 0) already_stubbed = 1; |
| 889 |
pthread_rwlock_unlock(&pThis->rwlock); |
| 890 |
if (!already_stubbed) { |
| 891 |
LogError(0, RS_RET_OK, "stubbing lookup table '%s' with value '%s'", pThis->name, stub_val); |
| 892 |
CHKiRet(lookupReloadOrStub(pThis, stub_val)); |
| 893 |
} else { |
| 894 |
LogError(0, RS_RET_OK, "lookup table '%s' is already stubbed with value '%s'", pThis->name, stub_val); |
| 895 |
} |
| 896 |
finalize_it: |
| 897 |
RETiRet; |
| 898 |
} |
| 899 |
|
| 900 |
static uint8_t lookupIsReloadPending(lookup_ref_t *pThis) { |
| 901 |
uint8_t reload_pending; |
| 902 |
pthread_mutex_lock(&pThis->reloader_mut); |
| 903 |
reload_pending = pThis->do_reload; |
| 904 |
pthread_mutex_unlock(&pThis->reloader_mut); |
| 905 |
return reload_pending; |
| 906 |
} |
| 907 |
|
| 908 |
/* note: stub_val_if_reload_fails may or may not be NULL */ |
| 909 |
rsRetVal ATTR_NONNULL(1) lookupReload(lookup_ref_t *const pThis, const uchar *const stub_val_if_reload_fails) { |
| 910 |
uint8_t locked = 0; |
| 911 |
int lock_errno = 0; |
| 912 |
DEFiRet; |
| 913 |
assert(pThis != NULL); |
| 914 |
if ((lock_errno = pthread_mutex_trylock(&pThis->reloader_mut)) == 0) { |
| 915 |
locked = 1; |
| 916 |
/*so it doesn't leak memory in situation where 2 reload requests are issued back to back*/ |
| 917 |
freeStubValueForReloadFailure(pThis); |
| 918 |
if (stub_val_if_reload_fails != NULL) { |
| 919 |
CHKmalloc(pThis->stub_value_for_reload_failure = ustrdup(stub_val_if_reload_fails)); |
| 920 |
} |
| 921 |
pThis->do_reload = 1; |
| 922 |
pthread_cond_signal(&pThis->run_reloader); |
| 923 |
} else { |
| 924 |
LogError(lock_errno, RS_RET_INTERNAL_ERROR, |
| 925 |
"attempt to trigger " |
| 926 |
"reload of lookup table '%s' failed (not stubbing)", |
| 927 |
pThis->name); |
| 928 |
ABORT_FINALIZE(RS_RET_INTERNAL_ERROR); |
| 929 |
/* we can choose to stub the table here, but it'll hurt because |
| 930 |
the table reloader may take time to complete the reload |
| 931 |
and stubbing because of a concurrent reload message may |
| 932 |
not be desirable (except in very tightly controled environments |
| 933 |
where reload-triggering messages pushed are timed accurately |
| 934 |
and an idempotency-filter is used to reject re-deliveries) */ |
| 935 |
} |
| 936 |
finalize_it: |
| 937 |
if (locked) { |
| 938 |
pthread_mutex_unlock(&pThis->reloader_mut); |
| 939 |
} |
| 940 |
RETiRet; |
| 941 |
} |
| 942 |
|
| 943 |
static rsRetVal ATTR_NONNULL(1) lookupDoReload(lookup_ref_t *pThis, uchar *stub_val) { |
| 944 |
DEFiRet; |
| 945 |
iRet = lookupReloadOrStub(pThis, NULL); |
| 946 |
if ((iRet != RS_RET_OK) && (stub_val != NULL)) { |
| 947 |
iRet = lookupDoStub(pThis, stub_val); |
| 948 |
} |
| 949 |
free(stub_val); |
| 950 |
RETiRet; |
| 951 |
} |
| 952 |
|
| 953 |
void *lookupTableReloader(void *self) { |
| 954 |
lookup_ref_t *pThis = (lookup_ref_t *)self; |
| 955 |
pthread_mutex_lock(&pThis->reloader_mut); |
| 956 |
while (1) { |
| 957 |
if (pThis->do_stop) { |
| 958 |
break; |
| 959 |
} else if (pThis->do_reload) { |
| 960 |
uchar *stub_val = pThis->stub_value_for_reload_failure; |
| 961 |
pThis->stub_value_for_reload_failure = NULL; |
| 962 |
pThis->do_reload = 0; |
| 963 |
pthread_mutex_unlock(&pThis->reloader_mut); |
| 964 |
lookupDoReload(pThis, stub_val); |
| 965 |
pthread_mutex_lock(&pThis->reloader_mut); |
| 966 |
} else { |
| 967 |
pthread_cond_wait(&pThis->run_reloader, &pThis->reloader_mut); |
| 968 |
} |
| 969 |
} |
| 970 |
pthread_mutex_unlock(&pThis->reloader_mut); |
| 971 |
return NULL; |
| 972 |
} |
| 973 |
|
| 974 |
/* reload all lookup tables on HUP */ |
| 975 |
void lookupDoHUP(void) { |
| 976 |
lookup_ref_t *luref; |
| 977 |
for (luref = runConf->lu_tabs.root; luref != NULL; luref = luref->next) { |
| 978 |
if (luref->reload_on_hup) { |
| 979 |
lookupReload(luref, NULL); |
| 980 |
} |
| 981 |
} |
| 982 |
} |
| 983 |
|
| 984 |
/* activate lookup table system config |
| 985 |
* most importantly, this means tarting the lookup table reloader thread in the |
| 986 |
* right process space - it is a difference if we fork or not! |
| 987 |
*/ |
| 988 |
void lookupActivateConf(void) { |
| 989 |
DBGPRINTF("lookup tables: activate config \n"); |
| 990 |
lookup_ref_t *luref; |
| 991 |
for (luref = runConf->lu_tabs.root; luref != NULL; luref = luref->next) { |
| 992 |
DBGPRINTF("lookup actiate: processing %p\n", luref); |
| 993 |
lookupActivateTable(luref); |
| 994 |
} |
| 995 |
DBGPRINTF("lookup tables: activate done\n"); |
| 996 |
} |
| 997 |
|
| 998 |
uint lookupPendingReloadCount(void) { |
| 999 |
uint pending_reload_count = 0; |
| 1000 |
lookup_ref_t *luref; |
| 1001 |
for (luref = runConf->lu_tabs.root; luref != NULL; luref = luref->next) { |
| 1002 |
if (lookupIsReloadPending(luref)) { |
| 1003 |
pending_reload_count++; |
| 1004 |
} |
| 1005 |
} |
| 1006 |
return pending_reload_count; |
| 1007 |
} |
| 1008 |
|
| 1009 |
|
| 1010 |
/* returns either a pointer to the value (read only!) or NULL |
| 1011 |
* if either the key could not be found or an error occurred. |
| 1012 |
* Note that an estr_t object is returned. The caller is |
| 1013 |
* responsible for freeing it. |
| 1014 |
*/ |
| 1015 |
es_str_t *lookupKey(lookup_ref_t *pThis, lookup_key_t key) { |
| 1016 |
es_str_t *estr; |
| 1017 |
lookup_t *t; |
| 1018 |
pthread_rwlock_rdlock(&pThis->rwlock); |
| 1019 |
t = pThis->self; |
| 1020 |
estr = t->lookup(t, key); |
| 1021 |
pthread_rwlock_unlock(&pThis->rwlock); |
| 1022 |
return estr; |
| 1023 |
} |
| 1024 |
|
| 1025 |
|
| 1026 |
/* note: widely-deployed json_c 0.9 does NOT support incremental |
| 1027 |
* parsing. In order to keep compatible with e.g. Ubuntu 12.04LTS, |
| 1028 |
* we read the file into one big memory buffer and parse it at once. |
| 1029 |
* While this is not very elegant, it will not pose any real issue |
| 1030 |
* for "reasonable" lookup tables (and "unreasonably" large ones |
| 1031 |
* will probably have other issues as well...). |
| 1032 |
*/ |
| 1033 |
static rsRetVal ATTR_NONNULL() |
| 1034 |
lookupReadFile(lookup_t *const pThis, const uchar *const name, const uchar *const filename) { |
| 1035 |
struct json_tokener *tokener = NULL; |
| 1036 |
struct json_object *json = NULL; |
| 1037 |
char *iobuf = NULL; |
| 1038 |
int fd = -1; |
| 1039 |
ssize_t nread; |
| 1040 |
struct stat sb; |
| 1041 |
DEFiRet; |
| 1042 |
|
| 1043 |
|
| 1044 |
if ((fd = open((const char *)filename, O_RDONLY)) == -1) { |
| 1045 |
LogError(errno, RS_RET_FILE_NOT_FOUND, "lookup table file '%s' could not be opened", filename); |
| 1046 |
ABORT_FINALIZE(RS_RET_FILE_NOT_FOUND); |
| 1047 |
} |
| 1048 |
|
| 1049 |
if (fstat(fd, &sb) == -1) { |
| 1050 |
LogError(errno, RS_RET_FILE_NOT_FOUND, "lookup table file '%s' stat failed", filename); |
| 1051 |
ABORT_FINALIZE(RS_RET_FILE_NOT_FOUND); |
| 1052 |
} |
| 1053 |
|
| 1054 |
if (sb.st_size == 0) { |
| 1055 |
LogError(0, RS_RET_JSON_PARSE_ERR, "lookup table file '%s' is empty", filename); |
| 1056 |
ABORT_FINALIZE(RS_RET_JSON_PARSE_ERR); |
| 1057 |
} |
| 1058 |
|
| 1059 |
CHKmalloc(iobuf = malloc(sb.st_size)); |
| 1060 |
|
| 1061 |
tokener = json_tokener_new(); |
| 1062 |
nread = read(fd, iobuf, sb.st_size); |
| 1063 |
if (nread != (ssize_t)sb.st_size) { |
| 1064 |
LogError(errno, RS_RET_READ_ERR, "lookup table file '%s' read error", filename); |
| 1065 |
ABORT_FINALIZE(RS_RET_READ_ERR); |
| 1066 |
} |
| 1067 |
|
| 1068 |
json = json_tokener_parse_ex(tokener, iobuf, sb.st_size); |
| 1069 |
if (json == NULL) { |
| 1070 |
LogError(0, RS_RET_JSON_PARSE_ERR, "lookup table file '%s' json parsing error", filename); |
| 1071 |
ABORT_FINALIZE(RS_RET_JSON_PARSE_ERR); |
| 1072 |
} |
| 1073 |
free(iobuf); /* early free to sever resources*/ |
| 1074 |
iobuf = NULL; /* make sure no double-free */ |
| 1075 |
|
| 1076 |
/* got json object, now populate our own in-memory structure */ |
| 1077 |
CHKiRet(lookupBuildTable(pThis, json, name)); |
| 1078 |
|
| 1079 |
finalize_it: |
| 1080 |
if (fd != -1) { |
| 1081 |
close(fd); |
| 1082 |
} |
| 1083 |
free(iobuf); |
| 1084 |
if (tokener != NULL) json_tokener_free(tokener); |
| 1085 |
if (json != NULL) json_object_put(json); |
| 1086 |
RETiRet; |
| 1087 |
} |
| 1088 |
|
| 1089 |
|
| 1090 |
rsRetVal lookupTableDefProcessCnf(struct cnfobj *o) { |
| 1091 |
struct cnfparamvals *pvals; |
| 1092 |
lookup_ref_t *lu; |
| 1093 |
short i; |
| 1094 |
#ifdef HAVE_PTHREAD_SETNAME_NP |
| 1095 |
char *reloader_thd_name = NULL; |
| 1096 |
int thd_name_len = 0; |
| 1097 |
#endif |
| 1098 |
DEFiRet; |
| 1099 |
lu = NULL; |
| 1100 |
|
| 1101 |
pvals = nvlstGetParams(o->nvlst, &modpblk, NULL); |
| 1102 |
if (pvals == NULL) { |
| 1103 |
ABORT_FINALIZE(RS_RET_MISSING_CNFPARAMS); |
| 1104 |
} |
| 1105 |
DBGPRINTF("lookupTableDefProcessCnf params:\n"); |
| 1106 |
cnfparamsPrint(&modpblk, pvals); |
| 1107 |
|
| 1108 |
CHKiRet(lookupNew(&lu)); |
| 1109 |
|
| 1110 |
for (i = 0; i < modpblk.nParams; ++i) { |
| 1111 |
if (!pvals[i].bUsed) continue; |
| 1112 |
if (!strcmp(modpblk.descr[i].name, "file")) { |
| 1113 |
CHKmalloc(lu->filename = (uchar *)es_str2cstr(pvals[i].val.d.estr, NULL)); |
| 1114 |
} else if (!strcmp(modpblk.descr[i].name, "name")) { |
| 1115 |
CHKmalloc(lu->name = (uchar *)es_str2cstr(pvals[i].val.d.estr, NULL)); |
| 1116 |
} else if (!strcmp(modpblk.descr[i].name, "reloadOnHUP")) { |
| 1117 |
lu->reload_on_hup = (pvals[i].val.d.n != 0); |
| 1118 |
} else { |
| 1119 |
dbgprintf( |
| 1120 |
"lookup_table: program error, non-handled " |
| 1121 |
"param '%s'\n", |
| 1122 |
modpblk.descr[i].name); |
| 1123 |
} |
| 1124 |
} |
| 1125 |
const uchar *const lu_name = lu->name; /* we need a const to keep TSAN happy :-( */ |
| 1126 |
const uchar *const lu_filename = lu->filename; /* we need a const to keep TSAN happy :-( */ |
| 1127 |
if (lu_name == NULL || lu_filename == NULL) { |
| 1128 |
iRet = RS_RET_INTERNAL_ERROR; |
| 1129 |
LogError(0, iRet, "internal error: lookup table name not set albeit being mandatory"); |
| 1130 |
ABORT_FINALIZE(iRet); |
| 1131 |
} |
| 1132 |
#ifdef HAVE_PTHREAD_SETNAME_NP |
| 1133 |
thd_name_len = ustrlen(lu_name) + strlen(reloader_prefix) + 1; |
| 1134 |
CHKmalloc(reloader_thd_name = malloc(thd_name_len)); |
| 1135 |
strcpy(reloader_thd_name, reloader_prefix); |
| 1136 |
strcpy(reloader_thd_name + strlen(reloader_prefix), (char *)lu_name); |
| 1137 |
reloader_thd_name[thd_name_len - 1] = '\0'; |
| 1138 |
#if defined(__NetBSD__) |
| 1139 |
pthread_setname_np(lu->reloader, "%s", reloader_thd_name); |
| 1140 |
#elif defined(__APPLE__) |
| 1141 |
pthread_setname_np(reloader_thd_name); // must check |
| 1142 |
#else |
| 1143 |
pthread_setname_np(lu->reloader, reloader_thd_name); |
| 1144 |
#endif |
| 1145 |
#endif |
| 1146 |
CHKiRet(lookupReadFile(lu->self, lu_name, lu_filename)); |
| 1147 |
LogMsg(0, RS_RET_OK, LOG_INFO, "lookup table '%s' loaded from file '%s'", lu_name, lu->filename); |
| 1148 |
|
| 1149 |
finalize_it: |
| 1150 |
#ifdef HAVE_PTHREAD_SETNAME_NP |
| 1151 |
free(reloader_thd_name); |
| 1152 |
#endif |
| 1153 |
cnfparamvalsDestruct(pvals, &modpblk); |
| 1154 |
if (iRet != RS_RET_OK) { |
| 1155 |
if (lu != NULL) { |
| 1156 |
lookupDestruct(lu->self); |
| 1157 |
lu->self = NULL; |
| 1158 |
} |
| 1159 |
} |
| 1160 |
RETiRet; |
| 1161 |
} |
| 1162 |
|
| 1163 |
void lookupClassExit(void) { |
| 1164 |
objRelease(glbl, CORE_COMPONENT); |
| 1165 |
} |
| 1166 |
|
| 1167 |
rsRetVal lookupClassInit(void) { |
| 1168 |
DEFiRet; |
| 1169 |
CHKiRet(objGetObjInterface(&obj)); |
| 1170 |
CHKiRet(objUse(glbl, CORE_COMPONENT)); |
| 1171 |
finalize_it: |
| 1172 |
RETiRet; |
| 1173 |
} |