Commit Graph
178 Commits
Author SHA1 Message Date
Isaac Connor 77df32bf04 Merge pull request #6915 from singhharsh1708/fix/c20-zmdc-server-dbh
fix: zmdc server status update uses the connection start() reopens
2026-11-19 22:40:48 -05:00
singhharsh1708 91dd9d69aa fix: zmdc.pl shutdown waits for daemons that are only stopped by KILL 2026-11-20 01:11:50 +05:30
singhharsh1708 205743d9e0 fix: zmdc server status update uses the connection start() reopens 2026-11-20 00:51:39 +05:30
Jash 15372fea65 fix: end the zmdc stop unknown-process message with a newline refs #6586 2026-11-17 12:08:40 +05:30
Jash 9d9acc16e9 fix: log crashed daemons as errors and let zmpkg restart of a stopped system exit 0 refs #6411 2026-11-16 14:50:27 +05:30
Jash ab7ddc2c88 fix: do not apply the crash restart backoff to a requested zmdc restart refs #5930 2026-11-15 09:46:56 +05:30
Jash b9cb8e02de perf: import only the POSIX names zmdc.pl uses and none where nothing is used refs #5547 2026-11-11 23:16:30 +05:30
Isaac ConnorandClaude Opus 5.5 a3c9d5cf70 fix: start zmdc's daemons with zmdc's signal mask and log their signals correctly
start() blocks SIGCHLD (and since #5199 TERM, INT and ABRT) across the fork.
The child unblocks the stop signals, but SIGCHLD stayed blocked through exec,
so every daemon zmdc started ran with it blocked (SigBlk 0x10000 in
/proc/<pid>/status for zmc, zmfilter, zmstats and zmwatch). Nothing depends
on it today, but a daemon that waits for SIGCHLD would never get it. Restore
the pre-fork mask just before exec.

The reaper took the signal from a wait status as $status & 0xfe, dropping its
low bit, so TERM was logged as signal 14, KILL as 8 and SEGV as 10, and the
core dump flag as $status & 0x01. Use 0x7f and 0x80.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 22:39:00 -04:00
nicandClaude Opus 5.5 86b593dded fix: keep zmwatch from restarting a starting zmc and stop zmdc losing the stop (#5199)
* fix: give a monitor's new zmc a grace period in zmwatch before restarting it

zmwatch's startup grace counted from zmwatch's own start, so a zmc started
later (a monitor added or saved through the web UI or API, a zmdc restart)
had none. When a zmwatch pass landed in the seconds between that zmc being
forked and it mapping its shared memory, zmwatch logged "shared data not
valid" and asked zmdc to restart it, replacing a zmc that was about to come
up.

Count ZM_WATCH_MAX_DELAY per monitor from the pass that first found its
shared data missing or invalid, and forget it once the data is valid again
or after the restart. The zmwatch-start grace and the heartbeat checks are
unchanged, so a zmc that hangs after mapping its memory is restarted as
before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: don't lose a stop that zmdc sends to a daemon before it has exec'd

zmdc forks each daemon and, in the child, connects to the database, closes
every descriptor up to _SC_OPEN_MAX and only then resets TERM, INT and ABRT
to their defaults before exec. Until then the child runs zmdc's own
shutdown handler, which only sets $zm_terminate in the child, so a TERM sent
in that window is lost. The daemon then starts as if nothing happened, zmdc
waits KILL_DELAY (30 s) and KILLs it, and an event that zmc opened in the
meantime is left with no EndDateTime.

zmwatch's restart of a zmc that hasn't mapped its shared memory yet lands
in this window. Closing descriptors one by one takes about 1.7 s with a
1048576 open-file limit (Docker's default), which makes the window wide.

Block TERM, INT and ABRT across the fork along with SIGCHLD, and in the
child reset them to their defaults and unblock them first thing, so a stop
sent before exec ends the child and the reaper restarts or forgets it as
asked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf: close only open descriptors in zmdc's child before exec

The child closed every descriptor number from 3 up to _SC_OPEN_MAX one by
one. With a 1048576 open-file limit (Docker's default) that is about 1.7 s
of close() calls before each daemon starts, and it is the window in which
zmwatch can find a just-started zmc without shared memory.

Close the descriptors /proc/self/fd lists instead, and keep the old loop
where /proc isn't available.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 22:38:14 -04:00
Steve GilvarryandClaude Opus 5 ba49bbb33c fix: make the Perl scripts' taint-safe PATH configurable
The scripts run under -T, so they cannot trust the caller's PATH and set
their own. Sixteen of them hardcoded /bin:/usr/bin:/usr/local/bin, which
assumes everything they shell out to lives under /usr or /usr/local.

ZoneMinder::General::findDbCommand looks for a database client on that
PATH, and zmupdate.pl and zmcamtool.pl run what it finds. So an install
whose client sits anywhere else cannot apply schema changes:

  sh: mysql: command not found
  Command 'mysql -u'zmuser' ... ' exited with status: 127

even with the client on the caller's PATH. Homebrew on Apple Silicon is
the case that surfaced it - the client is in /opt/homebrew/bin - but a
--prefix=/opt install on Linux has the same shape, as does anything that
keeps its database client outside the FHS locations.

Replaced the literal with @ZM_SCRIPT_PATH@, defaulting to the same three
directories plus wherever cmake actually found a client, and overridable
for packagers who want to pin it. Warn at configure time when no client
is found at all, since that failure otherwise appears much later and
says something unrelated.

Nothing changes for an install whose client is already under /usr/bin:
the directory is only appended when it is not in the list, so the
default stays exactly as it was.

Memory.pm is deliberately left alone. It has a narrower PATH of
/bin:/usr/bin, and the only command it runs is uname through an absolute
path from ZM_PATH_UNAME, so it does not need widening.

Verified on macOS across the three cases: with the client in
/opt/homebrew/bin the directory is appended; -DZM_SCRIPT_PATH= is
respected verbatim; and pointing detection at /usr/bin/mariadb leaves
the default untouched. Build clean, suite 146 cases.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B5KL9Xbi7K5aGsauLtd8tG
2026-09-13 13:04:59 +10:00
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.8 51ae0ca5d9 feat: re-assert zm.pid in zmdc main loop so systemd keeps tracking refs #4874
zmdc wrote ZM_PID only once at startup and removed it only at shutdown, so
if the pid file was removed out-of-band while the daemon kept running (a
partial/aborted stop that orphaned it, or /run tmpfiles cleanup), systemd
(Type=forking, PIDFile=) lost its handle: the service showed dead/failed,
stop no-op'd the orphaned tree, and start failed because ZM was already up.

Add ensure_pid(), called each iteration of the main server loop, which
rewrites ZM_PID only when it is missing or its contents do not match our
pid. Shutdown-safe: the shutdown unlink runs only after the loop exits.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-03 17:30:00 -04:00
Isaac ConnorandClaude Opus 4.6 fcd925900b fix: redirect stdin/stdout/stderr to /dev/null instead of closing them
Bug 376 (2006) closed all FDs starting from 0 when daemonizing. This
caused FD reuse problems: libx264 writing to a reused stderr FD led to
memory corruption (fixed in child spawn by 66f11435b, but not in the
parent's run()). It also meant children inherited closed FDs 0-2, so
any Perl die/warn output was silently lost, making daemon crashes
impossible to diagnose.

Redirect 0-2 to /dev/null (standard daemon practice) and close only
FDs 3+ for inherited sockets/DB connections. Children now inherit valid
FDs that won't crash or corrupt on write.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-03 11:45:32 -04:00
Isaac Connor bc65f57473 return early if daemon is invalid 2026-01-26 17:22:14 -05:00
Isaac Connor 45695211c3 main:: specificaiton for daemon_patt is wrong 2026-01-26 16:44:34 -05:00
Isaac ConnorandClaude Opus 4.5 5ebf40568c Fix duplicate restart race condition in zmdc.pl
- Always update term_sent_at when sending stop signal, not just when
  unset. This ensures the KILL_DELAY countdown resets properly when
  multiple stop/restart commands are issued.

- Add duplicate restart detection: if a process is already terminating
  with keepalive=true (restart scheduled), ignore subsequent restart
  requests. This prevents race conditions with zmwatch.pl which may
  detect stale heartbeat/shared memory during the restart window and
  send duplicate restart commands.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-26 14:03:42 -05:00
Isaac ConnorandClaude Opus 4.5 97e5c65fa9 Fix multiple bugs in zmdc.pl daemon control script
- Fix undefined $saddr variable in ZMServer package by using
  sockaddr_un(main::SOCK_FILE) directly
- Fix hash in scalar context that returned bucket ratio instead of
  count, use scalar(keys %terminating_processes)
- Replace die calls with Fatal() for consistent error handling
- Change confusing keepalive=>!undef idiom to keepalive=>1
- Remove unused $win and $ein variables
- Fix typo in comment: processess -> processes
- Add explicit $main:: package qualifier for daemon_patt variable
- Add 30-second timeout when reading server response to prevent
  client from hanging indefinitely

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-26 12:44:05 -05:00
Isaac Connor 25297e440c Put locking around updating server records 2025-11-13 15:45:51 -05:00
Isaac Connor 30408df4ed Add saving Cpuload when setting run status of server 2024-03-20 10:38:21 -04:00
Isaac Connor 00155dc841 Fix CpuSysPercent to CpuSystemPercent 2024-02-22 16:06:27 -05:00
Isaac Connor 68e2b19440 Update to use Server module updates and store CpuUsage stats instead of just load 2024-02-20 13:13:27 -05:00
Isaac Connor b618030b7a Increase time between TERM and KILL because ffmpeg connect can take way more than 10 secs 2023-12-14 09:29:03 -05:00
Isaac Connor b6bd1c93ad Sort output of status by pid. 2023-10-31 09:59:02 -04:00
ovargasp 6753a04e91 Latest version of zmAlarmServer 2022-08-05 16:46:11 -06:00
Isaac Connor 7d9525e28b WARNING should be a DEBUG when restarting a process 2022-04-28 10:36:06 -04:00
Isaac Connor ffdb0f9824 If we are starting a process that is waiting to term, mark it to get started by the reaper. Fixes case where zmdc thought the process was still running and so didn't start it. We never noticed because zmwatch would eventually notice. The result is instant restart. 2021-11-23 11:05:39 -05:00
Isaac Connor b70cd0bc76 Add zm_rtsp_server to list of daemons we can start 2021-03-01 16:47:18 -05:00
Isaac Connor 0b0df96271 Merge branch 'master' of github.com:ZoneMinder/zoneminder 2021-02-18 16:05:10 -05:00
Peter Keresztes Schmidt 687ba03eb4 Drop relicts of zma 2021-02-10 13:26:25 +01:00
Isaac Connor fab7efa003 Fix PrimeCapture on local cams. We need to be able to call it for each monitor 2021-02-09 20:29:37 -05:00
Isaac Connor 8a99665311 better variable names and code documentation 2021-02-03 09:50:13 -05:00
Isaac Connor 284b9f963f Merge branch 'master' into zma_to_thread 2020-12-29 12:18:26 -05:00
Isaac Connor 869860bb26 Merge branch 'master' into zma_to_thread 2020-07-21 17:49:39 -04:00
Isaac Connor c022cae7ef Sleep 10 secs if db connection fails to reduce log spamming and load 2020-06-30 18:59:27 -04:00
Isaac Connor e618966e08 spacing, improve error message when can't connect. 2020-06-23 10:23:17 -04:00
Isaac Connor 09fcbad666 Merge branch 'master' into zma_to_thread 2019-11-30 14:59:39 -05:00
Isaac Connor 6c997a0bd6 quotes 2019-11-22 10:03:09 -05:00
Isaac Connor f1ec455665 Add some debug to logrot 2019-11-21 10:49:23 -05:00
Isaac Connor 19d4a2287f When restarting, if the process was already dead, we won't restart it. So add a start call. Fixes #2757 2019-11-20 14:22:12 -05:00
Isaac Connor 766a59884d Don't hard code perl path. Use PERL_EXECUTABLE cmake var 2019-09-23 12:54:27 -04:00
Isaac Connor 96b7d30d7d Merge branch 'master' into zma_to_thread 2019-04-15 10:47:26 -04:00
Andrew Bauer 2bb15c4f90 daemonize zmcontrol (#2563) 2019-04-01 17:26:24 -04:00
Isaac Connor 28dafd5931 Merge branch 'storageareas' into zma_to_thread 2018-12-26 12:02:00 -05:00
Isaac Connor 6af8f0d419 Logging and whitespace 2018-12-20 13:55:18 -05:00
Isaac Connor b16cd7ec84 Merge branch 'h265' into zma_to_thread 2018-11-20 12:38:17 -05:00
Isaac Connor 62e511cfd1 Merge branch 'master' into h265 2018-11-17 10:41:20 -05:00
Andrew Bauer b4f77077e9 Update zmdc.pl.in
temporary fix to #2292
2018-11-16 18:33:54 -06:00
Isaac Connor a8329465fb Merge branch 'master' into h265 2018-10-22 10:30:01 -04:00
Isaac Connor 5ab1126260 Merge branch 'storageareas' into zma_to_thread 2018-10-20 17:31:14 -04:00
Isaac Connor e0dc8a5f6b Merge branch 'master' into storageareas 2018-09-27 13:54:53 -04:00