majestic: make S95majestic init script independent of a pidfile - #2250
majestic: make S95majestic init script independent of a pidfile#2250zavaruev wants to merge 2 commits into
Conversation
PR Summary by QodoMajestic: make S95majestic start/stop reliable without pidfile (watchdog-safe)
AI Description
Diagram
High-Level Assessment
Files changed (1)
|
Code Review by Qodo
1.
|
|
An example script that handles the task if needed.
|
Summary
Fix the
S95majesticinit script so a camera whosemajesticdied (OOM-kill,kill -9, power glitch) can actually be revived by the common crond watchdog line (* * * * * pgrep majestic || /etc/init.d/S95majestic start) and by repeatedS95majestic startcalls in general.Problem
On an OpenIPC SSC335DE camera (52 MB RAM)
majesticwas OOM-killed and stayed dead for 13+ minutes even though crond executed the watchdog every minute. The camera went fully offline (RTSP dead, no audio, no restart).Reproduction with the stock script:
kill -9a healthymajesticmajesticstays dead, whilecron.infoin the log shows the watchdog line executing every minute/var/run/majestic.pidends up containing pids that never correspond to a livemajestic(observed 456, 1355, 1843, 1856, 1870 vs. real daemon pids 4032, 1156, 1546, ...)Root cause
start()/stop()rely onstart-stop-daemon -b -m -S -p $PIDFILE -x majestic, which has two problems on busybox 1.36 (seedebianutils/start_stop_daemon.c):The pidfile becomes the single source of truth about "is it running" -- and it gets corrupt. With
-b, busyboxstart-stop-daemondouble-forks and the pidfile is written by the final child right beforeexec(); if the daemon later dies uncleanly, the pidfile keeps a stale pid. With-pgiven,do_procinit()checks only the pidfile pid and never scans/proc, so a stale pidfile silently breaks bothstartandstop(stopsignals nothing, itsrm -fnever runs).-x majestic(bare name) can only match by luck.pid_is_exec()compares the pattern against the full/proc/PID/exesymlink (e.g./usr/bin/majestic) and then againstargv[0]from/proc/PID/cmdline; a baremajesticnever matches the exe link, so detection is fragile.A liveness pre-check in the crontab (
pgrep majestic) is extra state that can also go stale -- e.g. a lingering zombie whosecommis stillmajesticmakespgrepsucceed while no usable daemon exists.Fix
/usr/bin/majesticasDAEMONso busybox-xmatches via/proc/PID/exe.start=-b -S -q -x "$DAEMON",stop=-K -q -x "$DAEMON". Without-p,start-stop-daemonscans/procitself, so there is no stale state to poison it. Repeatedstartcalls are idempotent: busybox refuses with a non-zero exit when the daemon is already running and does not touch the live instance.This also makes the script safe for an unconditional crond watchdog (
* * * * * /etc/init.d/S95majestic start): the daemon manager itself decides whether a copy is already running, with nopgrep/pidofpre-check to go stale.Verification (on device, OpenIPC SSC335DE, busybox 1.36.1)
kill -9->majesticstill dead at t+75 s and t+135 s while cron runs the line every minute; pidfile holds a wrong pid.kill -9-> the next cron tick restartsmajestic(RTSP:554listening, stable across a 3-minute observation window); while healthy, the per-minutestartis refused cleanly (FAIL (already running), rc=1) and the running daemon is untouched.kill -9-> restart cycles with the fixed invocation all succeeded;start,stopandrestartactions verified individually.Notes
general/overlay/etc/init.d/S60cronduses the same-b -m -ppattern; it did not fail on the test hardware, so it is intentionally left untouched.majesticcan refuse to re-initialize the sensor/VENC HAL for a couple of minutes (silent exit, no kernel messages). A per-minute watchdog absorbs this by simply retrying; a one-shot restart does not.