macOS delivers a SIGSEGV sent with kill() with si_code SEGV_ACCERR and si_pid
and si_uid 0, so the die handler cannot tell it from a fault and reports it as
one. The test's expectation holds on Linux only; it failed the macOS CI job.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
For a signal sent with kill(), raise() or sigqueue() rather than raised by
a fault, si_addr shares its storage with the sender's pid and uid, so the
crash report printed "unknown segfault code (signal code 0)" and a fault
address that was really those two numbers. The report now says
"Sent by process <pid> (uid <uid>)" and gives only the instruction
pointer for SI_USER, SI_QUEUE and, where defined, SI_TKILL. Reports of
real faults are unchanged; a test checks that a write to address 0x10
still reports that address.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A zero RLIMIT_CORE stops the kernel writing a core file, but when
core_pattern pipes to a program such as systemd-coredump or apport the
kernel hands it the dump anyway and leaves the limit to that program.
With ZM_DUMP_CORES off, the die handler now also calls
prctl(PR_SET_DUMPABLE, 0) when it starts reporting, so the kernel skips
the dump unless fs.suid_dumpable is 2, in which case the limit still
applies. It is done only at crash time because it also stops ptrace and
makes root the owner of the process's /proc files. Linux only.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The die handler's 5 second report timeout replaced the child's alarm()
watchdog and ends a stuck report on the original signal, so a test whose
report stalled still passed. run_crashing_child() now waits in the parent
with its own deadline, 4 seconds by default, and kills the child and
fails the test once it passes. The blocked-stderr test, which is meant to
reach the timeout, gets 8 seconds; the other-thread test, which ends
through its 3 second wait, gets 4.5.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The die handler is installed only when ZM_DUMP_CORES is off. It used to end
the process with exit(), so no core was written; it now re-raises the fault
with the default action, which dumps core wherever the core size limit
allows. zmSetDefaultDieHandler() now lowers the soft RLIMIT_CORE to 0 in
that case. The hard limit is left as it was, and with ZM_DUMP_CORES on
nothing changes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Only zms handles SIGPIPE; in zmc, zma and zmu it is fatal. A crash report
written to a stderr pipe whose reader had gone raised SIGPIPE, which killed
the process before the report reached the log file or syslog and replaced
the fault as the cause of death. SIGPIPE is now blocked while the die
handler runs, so the write fails with EPIPE and the report goes on.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A write in the die handler that never completes, such as to a stderr pipe
nobody reads when a daemon is run by hand, left the process hung with the
fault never reaching zmdc. The handler now sets a 5 second alarm when it
starts reporting; if it fires, a SIGALRM handler ends the process on the
signal that was being reported. alarm() and sigaction() are
async-signal-safe and nothing else in ZoneMinder uses SIGALRM.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The crash socket was blocking, so a full syslog queue could leave the
crashed process waiting in send() instead of dying. It is now non-blocking
and a line that does not fit is dropped.
- zmSetCrashSyslog() rewrote the ident in place while another thread could
be reading it in the handler. It now publishes a new, never-modified
target (socket path and ident) through an atomic pointer.
- The socket was connected once, so a syslog daemon that restarted and made
a new socket no longer got reports. It is left unconnected and each line
is sent with sendto() to the stored path.
- The default path was /dev/log everywhere; it is now _PATH_LOG, which is
/var/run/syslog on macOS and /var/run/log on FreeBSD.
Tests: the fork helper fails a test whose child the watchdog had to kill;
new cases cover a recreated syslog socket and a full syslog queue (filled
from several senders, as Linux caps what one sender may queue). The
worker-thread overflow test accepts SIGBUS, which is what macOS raises.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review follow-ups to the async-signal-safe die handler:
- The log file descriptor the handler wrote to could be closed by a log
rotation after the handler loaded it, and its number reused by an
unrelated file or socket. zmSetCrashLogFile() now keeps one descriptor
that is never closed and dup2()s each new log file onto it, so it only
ever refers to a log file.
- Lines were formatted with snprintf(), which is not async-signal-safe. A
small fixed-buffer CrashLine appends strings, decimals and hex instead.
- glibc backtrace() loads libgcc_s on first use, which allocates. It is now
called once when the handler is installed, and the comment that called it
safe is corrected.
- zmdc points daemons' stderr at /dev/null, so with file logging off the
report went nowhere. The handler now also sends each line to syslog over
its own datagram socket, connected by zmSetCrashSyslog() when the Logger
opens syslog, without calling syslog(3).
- A second signal arriving during the report was blocked when re-raised, so
the process exited 255 instead of dying of it. Both paths now go through
die_of(), which unblocks the signal.
- The shared volatile flag did not stop two threads that fault together from
both reporting. A lock-free atomic_flag picks the reporting thread; a fault
on another thread waits for it to end the process, and a fault inside the
handler on the same thread dies at once.
- sigaltstack() only covers the calling thread. zmSetThreadAltStack() gives
a thread its own stack, freed when it exits; main, the decoder and the
analysis threads call it.
Tests fork a child per case: report formatting, the rotated log file, the
syslog datagram, a second signal mid-report, a fault on another thread
mid-report, and a stack overflow on a worker thread.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
zm_die_handler logged through Error()/Fatal() and, on the second signal,
called Fatal() -> logPrint(), which from inside the handler stopped the
database queue, closed the MySQL handle, deleted the Logger and called
exit(): it joined and freed objects the decoder, analysis and stream
threads were still using, ran atexit handlers and static destructors (the
libcrypto/MariaDB signal-unsafe-call floods seen under ThreadSanitizer),
and could deadlock on the log mutex. The crash report it wrote to the Logs
table was sometimes cut short or lost, and the process could hang instead
of exiting.
Emit the crash report (signal, fault reason, fault and instruction
address, backtrace) with async-signal-safe write(2) only, to stderr and,
when open, the per-process log file, then restore the default disposition
and re-raise the signal so the process dies of the actual fault and can
leave a core dump. Nothing touches the database, the log queue or the
Logger from the handler.
- The Logger publishes its log-file descriptor in zm_crash_log_fd when it
opens the file and clears it on close, so the handler can write to the
log file without going through the Logger.
- The glibc path uses backtrace_symbols_fd(), which does not allocate,
instead of backtrace_symbols(); the libunwind path writes fixed buffers.
- Install the handler on a dedicated sigaltstack so a stack-overflow fault
can still be reported, and guard against faulting again while handling.
The crash detail now lands in the log file and syslog rather than the Logs
table; zmdc restarts the daemon and records the abnormal exit.
tests/zm_signal.cpp forks a child, installs the handler, raises SIGSEGV
and checks the process dies of the signal rather than exiting from the
handler, and that the report reached stderr and the published log fd.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>