Commit Graph
144 Commits
Author SHA1 Message Date
Isaac ConnorandClaude Opus 5 bde61e7af6 fix: rotate logs on SIGWINCH instead of SIGHUP fixes #5063
SIGHUP means reload. zmc responds by closing its events, disconnecting the
camera and reconnecting; the perl daemons respond by exiting so zmdc restarts
them. zmdc.pl logrot hupped every managed process, so the nightly logrotate
run cost about 8 seconds of capture on a default install.

Rotating a log file only needs the daemon to drop its file handle, so use a
separate signal for it. SIGWINCH is otherwise unused, is ignored by default and
exists on every supported platform.

- Logger (C++) installs a SIGWINCH handler beside its USR1/USR2 handler. The
  handler only sets a flag; the next logPrint closes the file and the write
  reopens it at the original path. This covers every C++ binary without
  touching any daemon's main loop.
- Logger.pm registers WINCH alongside HUP in logSetSignal, which logInit
  already calls, so the scripts that install their own HUP handler still
  rotate.
- zmdc.pl logrot sends WINCH. The logrotate config is unchanged - it still
  calls zmpkg.pl logrot.

Filter.pm and FilterTerm.php justified MAX_EVENT_DAYS by events not outliving
the nightly HUP, which is no longer what bounds them; cite SectionLength.

Tests: tests/zm_logger_rotate.cpp and tests/perl/test_log_rotate_signal.pl both
log, rename the file out from under the process, confirm writes still land in
the renamed file, signal WINCH and confirm the original path is written again.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NDhTBPj9xEaT52pRmAufaP
2026-08-22 15:30:46 -04:00
Isaac ConnorandClaude Opus 4.6 e6ace6fcf4 feat: add AUDIT logging level for tracking administrative changes
Add a new AUDIT logging level (-5) between PANIC (-4) and NOLOG (shifted
to -6) across C++, PHP, and Perl loggers. AUDIT entries use code 'AUD'
and syslog priority LOG_NOTICE. They record who changed what, from where,
for monitors, filters, users, config, roles, groups, zones, states,
servers, storage, events, snapshots, control caps, and login/logout.

AUDIT entries have their own retention period (ZM_LOG_AUDIT_DATABASE_LIMIT,
default 1 year) separate from regular log pruning. The log pruning in
zmstats.pl and zmaudit.pl now excludes AUDIT rows from regular pruning
and prunes them independently.

Critical safety: the C++ termination logic is changed from
'if (level <= FATAL)' to 'if (level == FATAL || level == PANIC)' to
prevent AUDIT-level log calls from killing the process.

Includes db migration zm_update-1.39.1.sql to shift any stored NOLOG
config values from -5 to -6.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 18:19:20 -05:00
Isaac Connor 94a33b2716 Merge pull request #4518 from ZoneMinder/copilot/fix-time-t-format-specifier
Fix time_t format specifiers for ILP32 platforms with 64-bit time_t
2026-01-05 18:30:08 -05:00
Isaac Connor 4a121dcdb4 Fix hang on Fatal 2026-01-05 16:37:50 -05:00
copilot-swe-agent[bot]andSteveGilvarry 2c2047c343 Replace C-style casts with C++ static_cast for intmax_t
Co-authored-by: SteveGilvarry <7613276+SteveGilvarry@users.noreply.github.com>
2026-01-05 13:36:16 +00:00
copilot-swe-agent[bot]andSteveGilvarry 89e0a4918c Fix %ld format specifiers for time_t on ILP32 platforms
Co-authored-by: SteveGilvarry <7613276+SteveGilvarry@users.noreply.github.com>
2026-01-05 10:36:23 +00:00
Isaac Connor 4f3bad3e75 Add Options for TERM log out 2025-02-25 14:31:19 -05:00
Isaac Connor 4af6e6eee0 Do not output anything to stdout when logging is happening when the db is closed. 2024-09-23 11:43:55 -04:00
Aaron Kling c4683d90a9 Format code using astyle google format
Commands used:
astyle --style=google --indent=spaces=2 --keep-one-line-blocks src/*.cpp
astyle --style=google --indent=spaces=2 --keep-one-line-blocks src/*.h
2024-03-26 13:43:58 -05:00
Alejandro Criado-Pérez 543683aa4c Corrections on cpp and h files 2023-08-13 01:27:19 +02:00
Isaac Connor 35c55a79e0 Use correct format, fix build warnings 2022-08-26 09:54:05 -04:00
Isaac Connor fa39983b69 Merge branch 'master' into add_mqtt 2022-08-25 18:41:59 -04:00
Isaac Connor 29750a56e7 Merge branch 'master' into add_mqtt 2022-08-25 18:37:54 -04:00
Doug Nazar 8c37846c6c Check result of snprintf for errors and truncation. 2022-07-31 16:38:23 -04:00
Doug Nazar 3b69a51ff8 Try to fix warning again that printf might exceed buffer.
The last fix worked on GCC 11 & 12 but broke GCC 8.
2022-07-22 12:00:42 -04:00
Doug Nazar 0db4a18ff6 Switch to unsigned int for loop to restrict size of printf.
GCC 12 incorrectly detects a format truncation.

src/zm_logger.cpp:86:39: warning: ‘%d’ directive output may be truncated writing between 1 and 11 bytes into a region of size 2 [-Wformat-truncation=]
   86 |       snprintf(code, sizeof(code), "DB%d", i);
      |                                       ^~
src/zm_logger.cpp:86:36: note: directive argument in the range [-2147483647, 9]
   86 |       snprintf(code, sizeof(code), "DB%d", i);
      |                                    ^~~~~~
2022-07-16 03:21:25 -04:00
Isaac Connor 64e930e7f4 Print out mLogFile.size as well hopefully to figure out why it tries to open an empty file 2022-03-21 16:12:49 -04:00
Isaac Connor 133553e630 Revert "Merge pull request #3119 from Carbenium/fmt"
This reverts commit 90d930c7c3, reversing
changes made to 0bcb9d276f.
2022-02-27 14:05:14 -05:00
Isaac Connor a2ae63cefe Merge branch 'master' into fmt 2021-11-16 15:10:45 -05:00
Isaac Connor 883772295d spacing and log the new log level string as well as number 2021-11-11 13:51:17 -05:00
Isaac Connor b0cf3a4732 Merge pull request #3314 from Carbenium/path-max
Fix Wformat for stringtf and convert path buffers depending on PATH_MAX to std::string
2021-07-07 11:34:03 -04:00
Peter Keresztes Schmidt eaf2e51b0c utils: Make sure the compiler can emit format warnings for stringtf
Unfortunately the compilers can't emit Wformat warnings for variadic templates
and those can't be annotated with the format attribute.
Use a variadic function which can be annotated and thus warns on format string-args mismatches.

Ref 0796a2262e
2021-07-06 10:33:17 +02:00
Peter Keresztes Schmidt cf9c47149f db: Add helper for escaping strings and use it 2021-07-06 10:20:45 +02:00
Peter Keresztes Schmidt b0b2f5fc16 Logger: Convert internals to std::chrono 2021-06-08 19:45:23 +02:00
Peter Keresztes Schmidt e881e97c06 Logger: Fix DB log messages not being populated
We need to `resize` the buffer string since `reserve` does not guarantee that the buffer is
actually available.

Follow-up on 298415fff3
2021-05-22 10:52:57 +02:00
Peter Keresztes Schmidt 298415fff3 Remove remaining usages of VLAs 2021-05-17 22:12:04 +02:00
Isaac Connor 8f27db5d6f Do not stop dbQueue in logTerm. dbQueue is not just for logging. 2021-05-04 14:22:02 -04:00
Isaac Connor 59b1927ad6 do dbQUeue.stop in logTerm so we don't have to have an extra line in every executable 2021-05-01 14:49:33 -04:00
Peter Keresztes Schmidt c3dd8d8a47 Logger: Annotate logPrint with format attribute
This allows the compiler to check for mismatches in the format string and the passed parameters.
2021-04-29 17:12:44 +02:00
Isaac Connor c0c5c07ecf Quiet cppcheck issues 2021-04-20 12:59:59 -04:00
Peter Keresztes Schmidt e330f8553d utils: cleanup Split and Join 2021-04-04 00:30:18 +02:00
Peter Keresztes Schmidt 4e8c7d1f7c Eliminate non-thread-safe calls to localtime
localtime uses an internal static storage to which a pointer is given as return value.
Due to this it is not safe to call localtime from multiple threads since the same static storage is used.

Use localtime_r instead which allows to pass in a tm struct.

Fixes:
https://github.com/ZoneMinder/zoneminder/security/code-scanning/24
https://github.com/ZoneMinder/zoneminder/security/code-scanning/25
https://github.com/ZoneMinder/zoneminder/security/code-scanning/26
https://github.com/ZoneMinder/zoneminder/security/code-scanning/27
https://github.com/ZoneMinder/zoneminder/security/code-scanning/28
https://github.com/ZoneMinder/zoneminder/security/code-scanning/30
https://github.com/ZoneMinder/zoneminder/security/code-scanning/31
https://github.com/ZoneMinder/zoneminder/security/code-scanning/33
https://github.com/ZoneMinder/zoneminder/security/code-scanning/58
https://github.com/ZoneMinder/zoneminder/security/code-scanning/59
https://github.com/ZoneMinder/zoneminder/security/code-scanning/63
https://github.com/ZoneMinder/zoneminder/security/code-scanning/64
https://github.com/ZoneMinder/zoneminder/security/code-scanning/65
2021-03-21 21:42:02 +01:00
Isaac Connor ebf1b7cbdc Only output to stdout if mTerminalLevel is something. zms for example SHOULD not output to stdout, ever except maybe when running from terminal to debug 2021-03-16 13:26:06 -04:00
Isaac Connor 86a26ef3b1 nvsnprintf won't exceed the buffer but it can hit the end of it so adding the ending ]\n can overflow. Test and prevent. 2021-03-11 13:16:32 -05:00
Isaac Connor f99b8896ec Don't unlock the mutex around openFile. It is a recursive mutex so we should be able to stay locked 2021-03-05 14:13:10 -05:00
Peter Keresztes Schmidt d9568a98c0 Drop zm_thread which has been replaced by STL implementations 2021-03-04 10:55:46 +01:00
Peter Keresztes Schmidt e18750bb79 logger: Pass std::string as ID during logInit 2021-03-01 22:43:02 +01:00
Peter Keresztes Schmidt 3dd52a92eb db: Make sure to bind only rvalues when pushing to zmDbQueue
With this we can ensure that no copy-construction of the SQL string takes place.
Regarding calling semantics: The pushed SQL string will be moved and cannot be reused.
2021-02-25 23:08:43 +01:00
Isaac Connor 8aeb4ab758 Switch db_mutex to a std::mutex. Use modern locking with it. Use zmDbDo or dbQueue.push where appropriate. code cleanup. 2021-02-25 12:26:26 -05:00
Isaac Connor fdf515ca10 rough in a db queue thread. Use it in zm_logger so that we don't have to aquire the db lock 2021-02-24 19:59:55 -05:00
Isaac Connor 6f89eba154 add missing () 2021-02-24 13:48:59 -05:00
Isaac Connor 778a4f1d84 We cannot wait for the db lock. Do not wait. 2021-02-24 13:48:36 -05:00
Isaac Connor 9959464ef7 Fix ]\n being included in the db Logs entry. Also removes a strlen so performance should improve 2021-02-24 12:37:09 -05:00
Isaac Connor 58e3d96f8c Instead of using basename and doing a strdup etc, just use strrchr because the filepath is going to one of the sources files in zm. 2021-02-24 12:23:02 -05:00
Isaac Connor 6c013f0e65 Put back includes needed on FreeBSD. Fixes #3165 2021-02-22 08:03:10 -05:00
Peter Keresztes Schmidt 7e7b1a0b1f Logger: Try harder to obtain the DB lock
Since we are now multi-threaded it can happen quite easily that a log message should be written to the DB on one thread while the other thread executes another query. Don't bail out immediately in the logging code, instead try to obtain the lock within 1s.
2021-02-08 19:07:01 +01:00
Isaac Connor 583779438b Don't unlock db_mutex if we don't get the lock in the first place 2021-02-08 11:09:44 -05:00
Peter Keresztes Schmidt cc6ea04afe Reformat touched code 2021-02-07 13:55:08 +01:00
Peter Keresztes Schmidt aec4dbc6ff DB: Make connection initialization more predictable and avoid double-initialization
Remove calls to zmDBConnect from various places to avoid possible side-effects/double initialization.
The function should be called once from the main thread of the daemon.

Also split config loading into 2 steps: static and DB config loading. Load the static config before zmDBConnect is called so it has a chance to succeed.
2021-02-07 13:44:41 +01:00
Peter Keresztes Schmidt 0dbc39ee25 Cleanup and reorganize includes
With this commit a unified structure for includes is introduced.
The general rules:
 * Only include what you need
 * Include wherever possible in the cpp and forward-declare in the header

 The includes are sorted in a local to global fashion. This means for the include order:
  0. If cpp file: The corresponding h file and an empty line
  1. Includes from the project sorted alphabetically
  2. System/library includes sorted alphabetically
  3. Conditional includes
2021-02-04 18:02:01 +01:00