Skip to content

Commit

Permalink
Improve anacron detection in updater. (netdata#17862)
Browse files Browse the repository at this point in the history
* Improve anacron detection in updater.

Instead of only checking the parent and grandparent processes, walk up
the process tree until we either find anacron or find init (or hit the
recursion limit for the function, which is currently 50 iterations).
This is needed because at least on some systems there is more than one
intermediate process between the script and anacron.

* Fix PID selection.
  • Loading branch information
Ferroin authored Jun 12, 2024
1 parent 847844f commit 9b77777
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions packaging/installer/netdata-updater.sh
Original file line number Diff line number Diff line change
Expand Up @@ -151,24 +151,35 @@ issystemd() {

# shellcheck disable=SC2009
running_under_anacron() {
if [ "$(uname -s)" = "Linux" ] && [ -r "/proc/$$/stat" ]; then
ppid="$(cut -f 4 -d ' ' "/proc/$$/stat")"
if [ -n "${ppid}" ] && [ -r "/proc/${ppid}/comm" ]; then
pid="${1:-$$}"
iter="${2:-0}"

[ "${iter}" -gt 50 ] && return 1

if [ "$(uname -s)" = "Linux" ] && [ -r "/proc/${pid}/stat" ]; then
ppid="$(cut -f 4 -d ' ' "/proc/${pid}/stat")"
if [ -n "${ppid}" ]; then
# The below case accounts for the hidepid mount option for procfs, as well as setups with LSM
[ ! -r "/proc/${ppid}/comm" ] && return 1

[ "${ppid}" -eq "${pid}" ] && return 1

grep -q anacron "/proc/${ppid}/comm" && return 0

ppid2="$(cut -f 4 -d ' ' "/proc/${ppid}/stat")"
running_under_anacron "${ppid}" "$((iter + 1))"

if [ -n "${ppid2}" ] && [ -r "/proc/${ppid2}/comm" ]; then
grep -q anacron "/proc/${ppid2}/comm" 2>/dev/null && return 0
fi
return "$?"
fi
else
ppid="$(ps -o pid= -o ppid= 2>/dev/null | grep -e "^ *$$" | xargs | cut -f 2 -d ' ')"
ppid="$(ps -o pid= -o ppid= 2>/dev/null | grep -e "^ *${pid}" | xargs | cut -f 2 -d ' ')"
if [ -n "${ppid}" ]; then
[ "${ppid}" -eq "${pid}" ] && return 1

ps -o pid= -o command= 2>/dev/null | grep -e "^ *${ppid}" | grep -q anacron && return 0

ppid2="$(ps -o pid= -o ppid= 2>/dev/null | grep -e "^ *${ppid}" | xargs | cut -f 2 -d ' ')"
ps -o pid= -o command= 2>/dev/null | grep -e "^ *${ppid2}" | grep -q anacron && return 0
running_under_anacron "${ppid}" "$((iter + 1))"

return "$?"
fi
fi

Expand Down

0 comments on commit 9b77777

Please sign in to comment.