@cryptotaxi247 / netdata-1 / commits / 9b7777703

Improve anacron detection in updater. (#17862)

* 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.

Austin S. Hemmelgarn committed Jun 12, 2024 at 11:41 UTC 9b7777703cc6bb563786344a005542d4b6adce02
1 file changed +21 -10
packaging/installer/netdata-updater.sh
+21 -10
@@ -151,24 +151,35 @@ issystemd() {
151
152 # shellcheck disable=SC2009
153 running_under_anacron() {
154 - if [ "$(uname -s)" = "Linux" ] && [ -r "/proc/$$/stat" ]; then
155 - ppid="$(cut -f 4 -d ' ' "/proc/$$/stat")"
156 - if [ -n "${ppid}" ] && [ -r "/proc/${ppid}/comm" ]; then
154 + pid="${1:-$$}"
155 + iter="${2:-0}"
156 +
157 + [ "${iter}" -gt 50 ] && return 1
158 +
159 + if [ "$(uname -s)" = "Linux" ] && [ -r "/proc/${pid}/stat" ]; then
160 + ppid="$(cut -f 4 -d ' ' "/proc/${pid}/stat")"
161 + if [ -n "${ppid}" ]; then
162 + # The below case accounts for the hidepid mount option for procfs, as well as setups with LSM
163 + [ ! -r "/proc/${ppid}/comm" ] && return 1
164 +
165 + [ "${ppid}" -eq "${pid}" ] && return 1
166 +
167 grep -q anacron "/proc/${ppid}/comm" && return 0
168
159 - ppid2="$(cut -f 4 -d ' ' "/proc/${ppid}/stat")"
169 + running_under_anacron "${ppid}" "$((iter + 1))"
170
161 - if [ -n "${ppid2}" ] && [ -r "/proc/${ppid2}/comm" ]; then
162 - grep -q anacron "/proc/${ppid2}/comm" 2>/dev/null && return 0
163 - fi
171 + return "$?"
172 fi
173 else
166 - ppid="$(ps -o pid= -o ppid= 2>/dev/null | grep -e "^ *$$" | xargs | cut -f 2 -d ' ')"
174 + ppid="$(ps -o pid= -o ppid= 2>/dev/null | grep -e "^ *${pid}" | xargs | cut -f 2 -d ' ')"
175 if [ -n "${ppid}" ]; then
176 + [ "${ppid}" -eq "${pid}" ] && return 1
177 +
178 ps -o pid= -o command= 2>/dev/null | grep -e "^ *${ppid}" | grep -q anacron && return 0
179
170 - ppid2="$(ps -o pid= -o ppid= 2>/dev/null | grep -e "^ *${ppid}" | xargs | cut -f 2 -d ' ')"
171 - ps -o pid= -o command= 2>/dev/null | grep -e "^ *${ppid2}" | grep -q anacron && return 0
180 + running_under_anacron "${ppid}" "$((iter + 1))"
181 +
182 + return "$?"
183 fi
184 fi
185