mirror of
https://github.com/ZoneMinder/zoneminder.git
synced 2026-11-30 04:04:03 -05:00
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>
48 lines
1.4 KiB
C++
48 lines
1.4 KiB
C++
#include "zm_analysis_thread.h"
|
|
|
|
#include "zm_monitor.h"
|
|
#include "zm_signal.h"
|
|
#include "zm_time.h"
|
|
|
|
AnalysisThread::AnalysisThread(Monitor *monitor) :
|
|
monitor_(monitor), terminate_(false) {
|
|
thread_ = std::thread(&AnalysisThread::Run, this);
|
|
}
|
|
|
|
AnalysisThread::~AnalysisThread() {
|
|
Stop();
|
|
if (thread_.joinable()) thread_.join();
|
|
}
|
|
|
|
void AnalysisThread::Start() {
|
|
Stop(); // Signal any running thread to terminate first
|
|
if (thread_.joinable()) thread_.join();
|
|
terminate_ = false;
|
|
Debug(3, "Starting analysis thread");
|
|
thread_ = std::thread(&AnalysisThread::Run, this);
|
|
}
|
|
|
|
void AnalysisThread::Stop() {
|
|
terminate_ = true;
|
|
}
|
|
void AnalysisThread::Join() {
|
|
if (thread_.joinable()) thread_.join();
|
|
}
|
|
|
|
void AnalysisThread::Run() {
|
|
// Report a stack overflow on this thread like one in main.
|
|
zmSetThreadAltStack();
|
|
while (!(terminate_ or zm_terminate)) {
|
|
// Some periodic updates are required for variable capturing framerate
|
|
if (!monitor_->Analyse()) {
|
|
if (!(terminate_ or zm_terminate)) {
|
|
// We wait on the packetqueue condition variable instead of sleeping.
|
|
// This allows us to wake up immediately when decoding completes.
|
|
Microseconds wait_for = monitor_->Active() ? Microseconds(ZM_SAMPLE_RATE) : Microseconds(ZM_SUSPENDED_RATE);
|
|
Debug(5, "Waiting for %" PRId64 "us", int64(wait_for.count()));
|
|
monitor_->GetPacketQueue()->wait_for(wait_for);
|
|
}
|
|
}
|
|
}
|
|
}
|