51 Commits

Author SHA1 Message Date
Rainer Gerhards
18b12d9f6e runtime: harden atomic helper fallbacks
Why:
The atomic helper header had minor cleanup issues and two no-atomics
fallbacks did not match the contracts of the GCC builtins they replace.
Keeping these semantics aligned matters for portability builds.

Impact:
No behavior change when compiler atomic builtins are available. Builds
without atomic builtins now get matching OR and CAS return semantics.

Before/After:
Before, the fallback OR helper assigned the value, and CAS_VAL returned
the value after the operation. After, they match the builtin behavior.

Technical Overview:
Fix comment and URL typos left in runtime/atomic.h and remove a stray
semicolon from the ATOMIC_CAS_VAL builtin macro. Parenthesize data in the
atomic CAS builtin wrappers for macro consistency. In the mutex fallback,
make ATOMIC_OR_INT_TO_INT apply bitwise OR and make ATOMIC_CAS_VAL save
and return the previous value before any successful replacement.

With the help of AI-Agents: Codex
2026-05-11 20:26:02 +02:00
Rainer Gerhards
954b431d21
Apply suggested fix to runtime/atomic.h from Copilot Autofix
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
2026-05-11 09:15:54 +02:00
Rainer Gerhards
78c88f4828 [HARDENING] imjournal: harden disk usage stats update
Use the uint64 atomic helper path when updating the journal disk usage
stat from reader threads. The value remains a best-effort statistic.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-10 14:26:10 +02:00
Rainer Gerhards
9250101223 runtime: clarify atomic helper usage
Why: Make atomic helper expectations explicit for maintainers and tooling.

Impact: Documentation only; no behavior change.

Before/After: Before: header lacked usage guidance. After: added a usage section that clarifies constraints.

Technical Overview:

- Document helper mutex expectations across atomic/non-atomic builds.

- Explain best-effort PREFER_* semantics and when they are safe.

- Emphasize word-sized constraints and safe argument usage.

- Warn against mixing atomic helpers with plain accesses.

With the help of AI-Agents: ChatGPT
2026-01-29 17:17:38 +01:00
Rainer Gerhards
a4a37b5b71
action: guard worker instance check on non-atomic builds
Motivation: CI surfaced TSAN-related flakes suggesting a threading edge
case. This change keeps the atomic fast path efficient and makes the
non-atomic slow path portable and safe. No real-world bug reports.

Impact: Non-atomic builds avoid a nested-lock scenario in worker init.
Atomic builds retain the lock-free fast path. No API/ABI change.

Before/After: non-atomic builds could check instance existence via a
mutex-backed fallback while holding the same mutex / the check happens
under a single mutex (or via atomics), preventing self-deadlock.

Technical: when HAVE_ATOMIC_BUILTINS is set, perform a fast pointer read
and re-check under mutWrkrDataTable (double-checked locking). On
non-atomic toolchains, take mutWrkrDataTable once and read actWrkrData
directly, avoiding fallback atomics that would re-lock the same mutex.
Provide pointer atomics for both __sync-based and mutex-backed paths, and
return type fix for ATOMIC_FETCH_32BIT_unsigned. Also reverts a recent
fix-attempt from earlier this week that introduced the nested-lock risk
on non-atomic builds. Add ommysql README with TSAN context and extend
tsan-rt.supp for libmysqlclient. Minor whitespace tweaks in diag.sh.

Tests/Docs: ommysql/README.md added (TSAN notes), tsan-rt.supp extended.

With the help of AI-Agents: Claude (Anthropic), Codex
2026-01-12 10:23:23 +01:00
Rainer Gerhards
9af1cd6f34
runtime: fix critical data race in wtpSetState()
Fixed a critical race condition where wtpSetState() used a plain store
while readers (wtpChkStopWrkr) used atomic loads. This violated the C11
memory model and created undefined behavior, causing shutdown delays and
potential message loss.

The fix introduces ATOMIC_STORE_INT macro and updates wtpSetState() to
use atomic stores, ensuring proper memory barriers and visibility across
all worker threads on all architectures.

Root cause: Mixed synchronization - plain store on writer side, atomic
load on reader side creates data race per C11 §5.1.2.4.

Impact:
- Ensures SHUTDOWN_IMMEDIATE signal is immediately visible
- Prevents shutdown timeouts and forced termination
- Provides proper memory ordering on ARM and weak-memory architectures
- No performance regression (atomic operations are hardware-optimized)

Testing:
- Builds successfully on x86_64
- Passes smoke test (imtcp-basic.sh)
- Verified synchronization consistency between reader/writer

With the help of AI Agents: GitHub Copilot
2026-01-07 10:19:50 +01:00
Rainer Gerhards
7d3ff39e88 atomic: remove trailing semicolons from helper macros
Trailing semicolons in atomic helper macros caused double
semicolons when the macros expanded, producing build warnings.
Macros now omit semicolons and call sites add them explicitly.
STATSCOUNTER_DEF updated to terminate the generated mutex line.

AI-Agent: ChatGPT
2025-08-19 16:06:44 +02: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
bb7db923ad
iminternal bugfix: race on termination 2019-08-03 14:33:54 +02:00
Rich Megginson
8b92ea1bd9 mmkubernetes - support for metadata cache expiration
New parameters for mmkubernetes (module and action):

* `cacheexpireinterval`
If `cacheexpireinterval` is -1, then do not check for cache expiration.
If `cacheexpireinterval` is 0, then check for cache expiration.
If `cacheexpireinterval` is greater than 0, check for cache expiration
if the last time we checked was more than this many seconds ago.

* `cacheentryttl` - maximum age in seconds for cache entries

New statistics counters:

* `podcachenumentries` - the number of entries in the pod metadata cache.
* `namespacecachenumentries` - the number of entries in the namespace
  metadata cache.
* `podcachehits` - the number of times a requested entry was found in the
  pod metadata cache.
* `namespacecachehits` - the number of times a requested entry was found
  in the namespace metadata cache.
* `podcachemisses` - the number of times a requested entry was not found
  in the pod metadata cache, and had to be requested from Kubernetes.
* `namespacecachemisses` - the number of times a requested entry was not
  found in the namespace metadata cache, and had to be requested from
  Kubernetes.
2019-04-01 11:53:29 -06:00
PascalWithopf
19133327cc correct codestyle in rsyslog 2018-07-31 09:44:27 +02:00
Janmejay Singh
93bfc583df Create support for unsigned 32-bit atomic arithmetic and migrate existing dynstats usage to it 2018-04-03 00:35:12 +05:30
Rainer Gerhards
dd9b98986d core bugfix: inconsitent atomic fetch function interface
macro used unsigned, inline function int. This could lead to an unsigned
overflow if macro version was used. That could potentially lead to malfunction
on some platforms (not seen nor expected in practice, though).

Detected by clang UBSAN.
2017-12-15 09:28:05 +01:00
Rainer Gerhards
3701e7ec91 core/wrkr threads: fix race condition
During e.g. shutdown it could happen that a worker thread was started
and immediately be requested to terminate. In such situations there was
a race the prevented proper initialization. This could lead to follow-on
problems.

We believe (but could not proof) that this also could lead to a hang of
the termination process. Thus we claim to close an issue tracker down
here below, but are not 100% certain it really closes it (we hope for
user feedback on this). In any case, the thread debuger showed invalid
operations and this has been fixed, so it is useful in any case.

closes https://github.com/rsyslog/rsyslog/issues/1959
2017-11-10 19:03:09 +01:00
Rainer Gerhards
6d89c9cdbe imdiag: fix cosmetic race in termination processing
This is not a real problem because imdiag intentionally does multiple
tries to validate predicate validity. However, this is reported in
clang thread sanitizer and so we need to fix it.
2017-11-05 15:57:38 +01:00
Janmejay Singh
d94abbf49f created a control to bump stats counter for batch-operations in one shot + some asthetic space->tab conversion 2015-11-17 16:16:24 +05:30
Rainer Gerhards
09ce215f66 fix build problem on platforms without atomic instructions
Thanks to Michael Biebl for his very helpful analysis of the bug.
2014-08-19 16:04:52 +02:00
Rainer Gerhards
6d668c915f cosmetic: fix some gcc --pedantic messages 2014-07-13 12:43:07 +02:00
Rainer Gerhards
6757ec6dd4 make rsyslogd -v output work in regard to 64 bit atomic buildin info 2014-06-04 15:25:29 +02:00
Rainer Gerhards
18e60f46a1 Merge branch 'v4-stable' into v5-stable
Conflicts:
	runtime/debug.h
	runtime/obj.c
	runtime/parser.h
	runtime/wti.h
2012-01-16 16:14:30 +01:00
Rainer Gerhards
f6dea36394 runtime license change 2012-01-16 12:39:01 +01:00
Rainer Gerhards
1bfb97e576 bugfix: one type of 64bit atomics was enabled when 32bit atomics were supported
also cleaned up some minor things
2010-12-02 07:28:04 +01:00
Rainer Gerhards
f8769ca19d bugfix: fixed build problems on some platforms
namely those that have 32bit atomic operations but not 64 bit ones
2010-12-01 18:13:40 +01:00
Rainer Gerhards
a3c81f500a bugfix: replacements for atomic operations for non-int sized types had problems.
At least one instance of that problem could potentially lead
to abort (inside omfile).
2010-11-25 17:20:55 +01:00
Rainer Gerhards
0d721450eb added missing support for systems without atomic instructions 2010-06-24 14:01:05 +02:00
Rainer Gerhards
11bd517465 added support for high-performance action queue submission if not all mark messages should be logged
this was previously not properly handeld. This is also the first occurence of
a (real) CAS loop inside rsyslog. Note that the performance is now very well
in the default configuration, and mark message directives are still correctly
being handled. So this code looks close to final, but needs to have some
bug cleanup as the testsuite shows.
2010-06-08 15:20:33 +02:00
Rainer Gerhards
220c57e7eb bugfix: regression caused more locking action in msg.c than necessary
also: bugfix: mutexes used to similate atomic instructions were not destructed
2010-06-08 08:25:56 +02:00
Rainer Gerhards
c26f830825 fixed recent regression: did not compile on systems with atomic instructions 2010-04-29 11:56:33 +02:00
Rainer Gerhards
dd76d96d67 adapted new atomic instruction emulation to v5 engine
code did not compile after merge from v4
2010-04-27 18:26:09 +02:00
Rainer Gerhards
4a5a3196fb Merge branch 'v4-devel' into master
Conflicts:
	runtime/Makefile.am
	runtime/atomic.h
	runtime/queue.c
	runtime/queue.h
	runtime/wti.c
	runtime/wti.h
	runtime/wtp.c
	runtime/wtp.h
2010-04-27 17:49:06 +02:00
Rainer Gerhards
cbe2e3d444 bugfix: problems with atomic operations emulation
replaced atomic operation emulation with new code. The previous code
seemed to have some issue and also limited concurrency severely. The
whole atomic operation emulation has been rewritten.
2010-04-27 17:31:28 +02:00
Rainer Gerhards
24ad305375 Merge branch 'v4-devel' into master
Conflicts:
	ChangeLog
	configure.ac
	doc/manual.html
2010-04-23 13:02:41 +01:00
Rainer Gerhards
57eb123abe minor fix: invalid duplicated include of config.h 2010-04-23 12:59:11 +01:00
Rainer Gerhards
072fc663a8 added replacements for atomic instructions on systems that do not support them.
[backport of Stefen Sledz' patch for v5]
2010-03-23 15:04:24 +01:00
Rainer Gerhards
c1e3a032b4 Merge branch 'master-solaris' 2010-03-23 12:38:44 +01:00
Steffen Sledz
a61e164a97 emulate missing atomic builtins with posix semaphores if available
Signed-off-by: Steffen Sledz <sledz@dresearch.de>
Acked-by: Thilo Fromm <t.fromm@dresearch.de>
2010-03-23 11:11:30 +01:00
Rainer Gerhards
40be6cb6e7 Merge branch 'v4-stable-solaris' into master-solaris
Conflicts:
	ChangeLog
	configure.ac
	doc/manual.html
	tests/rt-init.c
2010-03-05 09:54:42 +01:00
Rainer Gerhards
396e211e5c enabled compilation by using "racy" replacements for atomic instructions
... but this is not considered a real solution. For some of the
uses, it may acutally be sufficient, but the implications need
to be analyzed in detail.
2010-03-05 08:27:26 +01:00
Rainer Gerhards
ac186f1c3d removed mutex locks
... by utilizing that we need to modify a state variable only in
a sequential way during shutdown.
2009-07-17 19:59:22 +02:00
Rainer Gerhards
12dc91a157 backported "clean" increment of memory trim counter (not protected by mutex) 2009-06-25 17:09:51 +02:00
Rainer Gerhards
4818b0081d bugfix: subtle synchronization issue
This may have caused a segfault under strange circumstances (but if
we just run long enough with a high enough message volume, even the
strangest circumstances will occur...)
2009-06-25 15:21:29 +02:00
Rainer Gerhards
d12b9e0c67 some memory accesses are now explicitely atomic
... as far as I think this mostly is to keep the thread debuggers
happy
2009-06-25 12:03:14 +02:00
Rainer Gerhards
0d69059091 Merge branch 'beta' 2009-01-30 15:41:29 +01:00
Rainer Gerhards
25245cc682 added info if atomic ops are available to rsyslogd -v output 2009-01-30 15:40:53 +01:00
Rainer Gerhards
2cfaf5f86a bugfix: inconsistent use of mutex/atomic operations could cause segfault
details are too many, for full analysis see blog post at:
http://blog.gerhards.net/2009/01/rsyslog-data-race-analysis.html
2009-01-30 13:49:41 +01:00
Rainer Gerhards
14d5cc7f55 fixed atomic operations 2009-01-29 11:58:26 +01:00
Rainer Gerhards
bc70a73019 bugfix: proper synchronization on message destruction
The code was potentially race, at least on systems where
a memory barrier was needed. Fix not fully tested yet.
2008-09-19 17:41:11 +02:00
Rainer Gerhards
4c96ebdcfe bugfix: potential race condition when adding messages to queue
There was a wrong order of mutex lock operations. It is hard to
believe that really caused problems, but in theory it could and with
threading we often see that theory becomes practice if something is only
used long enough on a fast enough machine with enough CPUs ;)
2008-09-18 12:19:33 +02:00
Rainer Gerhards
988989e49e re-enabled gcc builtin atomic operations and added a proper ./configure check 2008-09-18 10:49:16 +02:00
Rainer Gerhards
8f8f65abb6 moved files to the runtime
there are still some files left which could go into the
runtime, but I think we will delete most of them once we
are done with the full modularization.
2008-04-16 10:26:54 +02:00