75 Commits

Author SHA1 Message Date
Rainer Gerhards
da0b5b8baa
lookup: report active table reloads as pending
Why:
The de-flake campaign exposed lookup-table reload tests that could resume
after HUP while the reload worker was still installing the replacement
table. The wait helper saw no pending reload and injected messages against
stale or stub lookup state.

Impact:
Lookup reload waiters now observe the full reload lifecycle.

Before/After:
Before, only queued reload requests were pending; after, an active reload
also remains pending until the table swap completes.

Technical Overview:
Track the interval after the reloader consumes do_reload but before
lookupDoReload() returns. lookupPendingReloadCount() now treats that
interval as pending, so imdiag's AwaitLookupTableReload command cannot
return while a reload is still applying. Initialize and clear the new state
alongside the existing reloader flags to keep startup, activation, and
shutdown state consistent.

Validation:
- ./autogen.sh --enable-debug --enable-testbench --enable-imdiag --enable-omstdout
- make -j$(nproc) check TESTS=""
- ./tests/array_lookup_table.sh
- ./tests/lookup_table_bad_configs.sh
- git diff --check
- Ubuntu 26.04 dev container focused lookup reload run, 9/9 pass:
  array_lookup_table.sh array_lookup_table-vg.sh
  array_lookup_table_misuse-vg.sh lookup_table_bad_configs.sh
  lookup_table_bad_configs-vg.sh lookup_table_rscript_reload.sh
  lookup_table_rscript_reload-vg.sh
  lookup_table_rscript_reload_without_stub.sh
  lookup_table_rscript_reload_without_stub-vg.sh

With the help of AI-Agents: OpenAI Codex
2026-05-27 16:16:54 +02:00
Rainer Gerhards
f838a285a8 lookup: harden shutdown lifecycle
Why: old crash/hang reports showed lookup-table shutdown could
touch pthread state that had not been fully activated.

Impact: daemonized lookup-table shutdown now has focused
regression coverage.

Before/After: reloader state was assumed; destruction now
follows explicit initialized and started flags.

Technical Overview:
Track initialized pthread primitives and the reloader thread
start state in lookup_ref_t. Only join the reloader and
destroy synchronization objects when activation created them.
Move lookup reloader thread naming into the running thread via
pthread_self(), avoiding use of an uninitialized pthread_t.
Add lookupKeyLocked() so RainerScript lookup() keeps table and
key-type consistency without recursively acquiring the rwlock.
Add a daemon-mode shutdown regression with lookup_table() and
lookup().

closes https://github.com/rsyslog/rsyslog/issues/1071

closes https://github.com/rsyslog/rsyslog/issues/5301

With the help of AI-Agents: Codex, Copernicus
2026-05-21 11:43:08 +02:00
Rainer Gerhards
0ac9caa3d1 lookup: reject duplicate table names
Why: duplicate lookup_table names create unreachable table entries and can
exhaust reloader resources before reporting the real configuration problem.

Impact: configurations with duplicate lookup_table names now fail validation
with a clear diagnostic. Unique lookup tables and reload recovery for
initially invalid table files remain unchanged.

Before and after: duplicates were accepted and only the first table was usable.
Now duplicate names are rejected before allocating another table.

Technical Overview:
- Parse lookup_table name, file, and reloadOnHUP before creating the table
  object.
- Check the current config set for an existing lookup table name.
- Report duplicate names as RS_RET_CONFIG_ERROR with a stable message.
- Keep existing registration behavior for initially invalid table files so
  later HUP reload can recover them.
- Add RainerScript and YAML regression tests for duplicate lookup table names.

closes https://github.com/rsyslog/rsyslog/issues/5316

With the help of AI-Agents: Codex
2026-05-20 18:23:48 +02:00
Rainer Gerhards
1d16992563 core: harden string allocation and copy handling
Why:
Older platforms need consistent formatted string allocation, and the
remaining copy helpers kept triggering review noise around classic C
string APIs.

A major motivation is to avoid very common AI review false positives:
those tools often do not understand the actual scope and safety checks,
and then mechanically flag strcpy-style APIs despite the surrounding
bounds and initialization logic being correct.

Impact: string allocation and bounded copy paths are now explicit and
portable across the tree.

Before/After: ad hoc unsafe string helpers remained; now allocation and
bounded copies follow one portable pattern.

Technical Overview:
Add a complete asprintf and vasprintf compatibility layer with shared
prototypes so older libc variants build without local wrappers.

Replace repo-wide strcpy, strcat, strncat, sprintf, and direct strncpy
uses with explicit memcpy-based bounded copies or exact-width byte
copies as appropriate for each destination.

Add rsCStrAppendParts() for incremental string assembly so callers can
build pre-sized buffers without repeated snprintf return handling.

Update the unicode helper copy routine so existing ustrncpy() call sites
no longer route to libc strncpy semantics.

This also removes a broad class of review distractions from automated AI
reviewers that key off banned function names without understanding the
actual copy contract at the call site.

Extend the stringbuf unit coverage for the new append helper and the
formatted-allocation compatibility path.

With the help of AI-Agents: Codex
2026-04-22 14:45:45 +02:00
Rainer Gerhards
d0d84a824d runtime: cleanup zero-size alloc handling
Why
Guard obvious zero-size allocation edges and remove a couple of
stale or misleading notes in the imtcp/tcpsrv path so future
reviews see the intended behavior directly in code.

Impact
Empty key and lookup-table files now fail before zero-size
allocations; OpenSSL accepted sessions use per-session callback
context.

Before/After
Before, empty files could flow into malloc(0)-style paths and the
OpenSSL accept path stored listener-owned callback context.
After, those runtime paths fail early and accepted OpenSSL sessions
carry their own callback context.

Technical Overview
Reject empty key files in the libgcry and OpenSSL runtime helpers
before allocating buffers from sb.st_size.
Reject empty lookup-table files before allocating and parsing the
JSON payload.
Update accepted OpenSSL sessions to store pNew-owned callback state
in SSL ex-data instead of the listener object's pointers.
Remove a stale TODO in the imtcp octet-count parser because the
zero-count case already falls into the framing-error path.
Add a short comment that maxReads==0 is intentional and documented
behavior that disables starvation protection.

With the help of AI-Agents: Codex
2026-04-12 17:38:02 +02:00
Rainer Gerhards
8cfd636484
Fix lookup table reload race condition (Device or resource busy)
Previously, the `lookupTableReloader` thread held the `reloader_mut` mutex
for the entire duration of the reload operation (`lookupDoReload`). Since
reloading a lookup table involves file I/O and parsing, it can take a
significant amount of time. If a SIGHUP (or other reload trigger) occurred
while a reload was in progress, the main thread (executing `lookupReload`)
would fail to acquire the mutex via `pthread_mutex_trylock`, resulting in
the error "Device or resource busy" and the reload request being dropped.

This commit modifies `lookupTableReloader` to release the mutex before
calling `lookupDoReload` and re-acquire it afterwards. This allows
`lookupReload` to successfully acquire the mutex, queue a new reload request
(by setting `do_reload = 1`), and update the stub value if necessary.

To ensure thread safety, `lookupDoReload` now accepts the `stub_val` as an
argument (instead of accessing the shared `stub_value_for_reload_failure`
member directly) and takes ownership of it (freeing it upon completion).
`lookupTableReloader` extracts the stub value to a local variable while
holding the lock before passing it to `lookupDoReload`.

The `ATTR_NONNULL` attribute on `lookupDoReload` was updated to `ATTR_NONNULL(1)`
to allow the `stub_val` argument to be NULL, fixing a build failure with
`-Werror`.

Fixes #5628

Co-authored-by: rgerhards <1482123+rgerhards@users.noreply.github.com>
2026-02-03 13:18:37 +01:00
Janmejay Singh
895450fadc Fix incorrect coercing of json-object int to uint32.
This aims to fix broken int-max case described
 https://github.com/rsyslog/rsyslog/issues/4490 and
 https://github.com/rsyslog/rsyslog/issues/4490.
2026-01-04 11:23:54 +01:00
Rainer Gerhards
b326c76f45 style: normalize C source formatting via clang-format (PoC)
This commit applies the new canonical formatting style using `clang-format` with custom settings (notably 4-space indentation), as part of our shift toward automated formatting normalization.

⚠️ No functional changes are included — only whitespace and layout modifications as produced by `clang-format`.

This change is part of the formatting modernization strategy discussed in:
https://github.com/rsyslog/rsyslog/issues/5747

Key context:
- Formatting is now treated as a disposable view, normalized via tooling.
- The `.clang-format` file defines the canonical style.
- A fixup script (`devtools/format-code.sh`) handles remaining edge cases.
- Formatting commits are added to `.git-blame-ignore-revs` to reduce noise.
- Developers remain free to format code however they prefer locally.
2025-07-16 13:56:21 +02:00
Rainer Gerhards
7225999b77 refactor: modernize macro definitions to support formatting and clarity
This commit performs a broad modernization of widely used rsyslog
macros to align with modern C practices and support automated
formatting tools like clang-format. The changes focus on improving
syntactic regularity, readability, and tooling compatibility — without
altering behavior.

Macros refactored in this commit now follow a consistent,
statement-like form with explicit trailing semicolons. Where
applicable, macro blocks that define module interfaces (`queryEtryPt`)
have been updated to use simple `if` statements instead of `else if`
chains. While this slightly increases evaluation time, the affected
functions are only called once per module during load time to register
supported interfaces — making the performance cost irrelevant in
practice.

These improvements serve multiple purposes:
- Enable reliable clang-format usage without mangling macro logic
- Simplify reasoning about macro-expanded code for human readers
- Reduce style drift and merge conflicts
- Facilitate development for contributors using assistive tools
- Support future formatting pipelines using:
  1. `clang-format`
  2. a post-fixup normalization script

Refactored macros:
- MODULE_TYPE_NOKEEP
- MODULE_TYPE_KEEP
- MODULE_TYPE_INPUT
- MODULE_TYPE_OUTPUT
- MODULE_TYPE_FUNCTION
- MODULE_TYPE_PARSER
- MODULE_TYPE_LIB
- DEF_IMOD_STATIC_DATA
- DEF_OMOD_STATIC_DATA
- DEF_PMOD_STATIC_DATA
- DEF_FMOD_STATIC_DATA
- DEFobjStaticHelpers
- SIMP_PROP(...)

And all `queryEtryPt()` dispatch macros:
- CODEqueryEtryPt_STD_MOD_QUERIES
- CODEqueryEtryPt_STD_OMOD_QUERIES
- CODEqueryEtryPt_STD_OMODTX_QUERIES
- CODEqueryEtryPt_STD_OMOD8_QUERIES
- CODEqueryEtryPt_TXIF_OMOD_QUERIES
- CODEqueryEtryPt_IsCompatibleWithFeature_IF_OMOD_QUERIES
- CODEqueryEtryPt_STD_IMOD_QUERIES
- CODEqueryEtryPt_STD_CONF2_QUERIES
- CODEqueryEtryPt_STD_CONF2_setModCnf_QUERIES
- CODEqueryEtryPt_STD_CONF2_OMOD_QUERIES
- CODEqueryEtryPt_STD_CONF2_IMOD_QUERIES
- CODEqueryEtryPt_STD_CONF2_PREPRIVDROP_QUERIES
- CODEqueryEtryPt_STD_CONF2_CNFNAME_QUERIES
- CODEqueryEtryPt_STD_PMOD_QUERIES
- CODEqueryEtryPt_STD_PMOD2_QUERIES
- CODEqueryEtryPt_STD_FMOD_QUERIES
- CODEqueryEtryPt_STD_SMOD_QUERIES
- CODEqueryEtryPt_doHUPWrkr
- CODEqueryEtryPt_doHUP

This general modernization reduces macro misuse, improves DX, and
lays the foundation for a robust, automated style normalization
system.

See also: https://github.com/rsyslog/rsyslog/issues/5747
2025-07-15 08:25:58 +02:00
Rainer Gerhards
d6d340aaad
omfwd regression fix: avoid false active target change log message
Commit ffaf6dc4620da added proper variable sync, but dropped the check
if active count had actually changed. As such, the output was always
generated, which could pollute the log heavily.

also fixes some codestyle issues introduced by earlier commits.

With the help of AI-Agent: Codex 2025-06
2025-06-09 17:41:52 +02:00
Rainer Gerhards
611f7844c9
lookup: add new table type for regex-matches
While we do not like rexeg-matches for performance reasons, they are
well known and appreciated by users. With the new table type, we
add a lookup capability for partial matches, but at the price
of much higher ressource use. It still is useful, e.g. to classify
events as "noise" events in a simple manner.
2025-06-08 17:16:27 +02:00
alakatos
04de03b2f2 Replace deprecated json_object_object_get 2024-08-15 13:30:03 +02:00
Rainer Gerhards
8d36cbd29d
lookup tables: fix static analyzer issue
If something goes really wrong, a lookup table's name would not
be set. That could lead to a NULL pointer access. HOWEVER, this
would require serious bugs in config parameter parsing, as the
lookup table name is a required parameter and the parser will
error out if not set.

So the bug is mostly cosmetic - but it does not hurt to handle
this case, of course.
2023-08-02 15:02:05 +02:00
Rainer Gerhards
b6b4f25eda
lookup tables bugfix: reload on HUP did not work when backgrounded
Lookup tables were only reloaded on HUP if the -n option was given
and rsyslog no backgrounded. This patch fixes the issue.

closes: https://github.com/rsyslog/rsyslog/issues/4813
2023-08-02 15:01:31 +02:00
Michael Biebl
6569133c75
Typo fixes (#4801)
* typo fix: ambigious -> ambiguous

* typo fix: aquire -> acquire

* typo fix: assgined -> assigned

* typo fix: cancelation -> cancellation

* typo fix: childs -> children

* typo fix: configuraton -> configuration

* typo fix: delemiter -> delimiter

* typo fix: forwardig -> forwarding

* typo fix: initializiation -> initialization

* typo fix: intializing -> initializing

* typo fix: lengh -> length

* typo fix: mesage -> message

* typo fix: occured -> occurred

* typo fix: occurence -> occurrence

* typo fix: paramter -> parameter

* typo fix: remaing -> remaining

* typo fix: resetted -> reset

* typo fix: suppored -> supported

* typo fix: Sytem -> System

* typo fix: uncommited -> uncommitted

* typo fix: depricated -> deprecated

* typo fix: stoping -> stopping

* type fix: allow to -> allow one to
2022-02-17 10:54:12 +01:00
alakatos
66bcd91196 Clarify meaning of loadConf and RunConf 2021-12-15 10:26:52 +01:00
Rainer Gerhards
683c9d6b07
emit info-level success message on lookup table load (startup)
closes https://github.com/rsyslog/rsyslog/issues/3639
2019-05-08 15:41:19 +02:00
Philippe Duveau
c8d8871aea
Merge branch 'AIX_Port_step2' into master 2019-02-21 09:23:40 +01:00
Rainer Gerhards
00f09d8723
cleanup: use less verbose C11 method to use pragmas 2019-02-20 17:13:28 +01:00
Philippe Duveau
9ad7324dfa AIX_port: second phase 2019-02-14 14:36:05 +01:00
PascalWithopf
19133327cc correct codestyle in rsyslog 2018-07-31 09:44:27 +02:00
Rainer Gerhards
029a44c6d8 lookup tables: fix potential misadressing
We have not seen this occur in practice. It was detected by
gcc 8.

closes https://github.com/rsyslog/rsyslog/issues/2842
2018-07-16 14:39:10 +02:00
Rainer Gerhards
a08387a699
fix or work around some more gcc 8 "issues" 2018-07-16 11:28:09 +02:00
Adam Chalkley
6dbc857508 lookup table reload: log level from error to info
Based on discussion, the error level is being adjusted to reflect
that the action (reloading the lookup table) is normal/expected
and not an actual error condition.

refs rsyslog/rsyslog#2713
2018-05-17 09:30:29 -05:00
Rainer Gerhards
b6216c9246 lookup: fix dead code (caused no harm)
Detected by Coverity scan, CID 185407
2018-01-15 17:57:45 +01:00
Janmejay Singh
c205dcba26 Fix lookup-table unsigned underflow in array-table lookup and empty-table handling across array and sparse-array tables (first one was identified in #2295 after first set of identified issues were fixed) + single-line cosmetic changes that kill trailing spaces/tabs 2017-12-27 19:01:23 +05:30
Rainer Gerhards
29533f53dd
Merge pull request #2300 from jgerhards/errmsg-lookup
lookup: use new errmsg interface
2017-12-27 12:11:09 +01:00
Janmejay Singh
66bdb31377 Fix lookup-table unsigned underflow in quick-sort and binary-search comparators (identified in #2295) 2017-12-27 13:20:29 +05:30
Jan Gerhards
0134348e15 lookup: use new errmsg interface
see also https://github.com/rsyslog/rsyslog/issues/1684
2017-12-26 17:15:39 +01:00
Rainer Gerhards
4f13c27d5a lookup tables: fix undefined behaviour
bsearch is not permitted to be called with NULL ptr for array.

Detectedb by LLVM UBSan.

see also https://github.com/rsyslog/rsyslog/issues/2295
2017-12-25 16:20:32 +01:00
BSD Patch
3b50c7c6db lookup: import BSD project patch 2017-12-18 13:23:24 +01:00
Rainer Gerhards
3a8d71961d lookup: "fix" cosmetic Coverity issue CID 185371 2017-12-01 08:17:57 +01:00
Rainer Gerhards
047624147c some primarily cosmetic changes 2017-11-22 12:33:03 +01:00
PascalWithopf
e485c5c5bc codestyle: shorten lines to max 130 chars 2017-11-15 09:04:42 +01:00
Rainer Gerhards
c0c23e258e
Merge pull request #2022 from rgerhards/cid-185407
lookup: improve code in regard to err detection at compile time
2017-11-14 17:06:03 +01:00
Rainer Gerhards
31bd05dfce remove unnecessary code 2017-11-14 14:27:11 +01:00
Rainer Gerhards
1f109c53ca lookup: improve code in regard to err detection at compile time
Trying to understand CID 185407, which looks like a false positive. On
the way to this, add attributation which could increase correctness of
compiler code unerstanding and trigger different set of error messages.
2017-11-14 14:27:05 +01:00
Rainer Gerhards
824a1c9746 lookup: "fix" cosmetic Coverity scan issue CID 185426 2017-11-12 11:26:13 +01:00
Rainer Gerhards
bc53205a1b lookup table bugfix: potential infinite loop
lookup table could loop if error in lookupDoStub() occurs

also fixes coverty scan CID 185315 (IDENTICAL_BRANCHES due to
CHKiRet() immediately followed by finalize_it)
2017-10-26 11:00:04 +02:00
Rainer Gerhards
f9f33c7056 core/lookup: fix potential misadressing
found by clang 5.0 static analyzer. The situation is now reported
via an error message indicating internal program error.
2017-10-24 11:23:33 +02:00
Rupert
a6a24abf13 portability: fixes needed to build on osx
Thanks to github user hdatma
2017-05-15 09:05:05 +02:00
Pascal Withopf
b63a6b9010 codestyle: line length adjusted 2017-01-17 08:54:37 +01:00
purnima
d45daa2af0 Rebase,redefine msgDestruct() as smsg_t is used 2016-11-22 14:24:25 +05:30
Rainer Gerhards
448f16495c remove "inline" attribute from excessivly long functions 2016-10-26 11:50:15 +02:00
Janmejay Singh
d9a182589e error check lookup table initialization (improves initialization error checking rigour) 2016-10-14 02:08:41 +05:30
Rainer Gerhards
8a986092c6 fix old-style definitions (missing prototype in func def) 2016-06-03 10:08:57 +02:00
Rainer Gerhards
8a8675b223 fix compiler warnings 2016-06-02 10:18:05 +02:00
Janmejay Singh
3d2dc0e115 [BUG-FIX] close files after loading lookup table 2016-05-11 16:28:42 +05:30
Rainer Gerhards
eb603584a4 add missing header (build broke on some platforms) 2016-04-25 08:05:39 +02:00
Janmejay Singh
1ac753761a added reloadOnHUP(defaults to on) parameter in lookup-table object (again, somehow overlooked this during proposal impl) 2016-02-11 23:23:22 +05:30