@cryptotaxi247 / netdata-1 / commits / 40ab14d0d

Assorted systemd detection fixes (#19345)

Assorted systemd detection fixes: - Properly scope `set -e` changes to only affect `systemctl is-system-running` invocation. - Skip checking for hard-coded unit file paths. Other paths may technically be used for unit files instead of what we hard-code, and the presence or abscence of any specific unit file path _does not_ reliably indicate the presence or abscence of systemd.

Austin S. Hemmelgarn committed Jan 8, 2025 at 08:37 UTC 40ab14d0d533dbeec4efdd1742149757236760d4
4 files changed +13 -32
packaging/installer/functions.sh
-5
@@ -597,11 +597,6 @@ issystemd() {
597 ns=''
598 systemctl=''
599
600 - # if the directory /lib/systemd/system OR /usr/lib/systemd/system (SLES 12.x) does not exit, it is not systemd
601 - if [ ! -d /lib/systemd/system ] && [ ! -d /usr/lib/systemd/system ]; then
602 - return 1
603 - fi
604 -
600 # if there is no systemctl command, it is not systemd
601 systemctl=$(command -v systemctl 2> /dev/null)
602 if [ -z "${systemctl}" ] || [ ! -x "${systemctl}" ]; then
packaging/installer/netdata-uninstaller.sh
-5
@@ -463,11 +463,6 @@ issystemd() {
463 ns=''
464 systemctl=''
465
466 - # if the directory /lib/systemd/system OR /usr/lib/systemd/system (SLES 12.x) does not exit, it is not systemd
467 - if [ ! -d /lib/systemd/system ] && [ ! -d /usr/lib/systemd/system ]; then
468 - return 1
469 - fi
470 -
466 # if there is no systemctl command, it is not systemd
467 systemctl=$(command -v systemctl 2> /dev/null)
468 if [ -z "${systemctl}" ] || [ ! -x "${systemctl}" ]; then
packaging/installer/netdata-updater.sh
+6 -8
@@ -126,11 +126,6 @@ safe_pidof() {
126 }
127
128 issystemd() {
129 - # if the directory /lib/systemd/system OR /usr/lib/systemd/system (SLES 12.x) does not exit, it is not systemd
130 - if [ ! -d /lib/systemd/system ] && [ ! -d /usr/lib/systemd/system ]; then
131 - return 1
132 - fi
133 -
129 # if there is no systemctl command, it is not systemd
130 systemctl=$(command -v systemctl 2> /dev/null)
131 if [ -z "${systemctl}" ] || [ ! -x "${systemctl}" ]; then
@@ -144,15 +139,18 @@ issystemd() {
139 # anything else, it is systemd.
140 #
141 # This may return a non-zero exit status in cases when it actually
147 - # succeeded for our purposes, so we need to toggle set -e off here.
142 + # succeeded for our purposes (notably, if the state is `degraded`),
143 + # so we need to toggle set -e off here.
144 set +e
149 - case "$(systemctl is-system-running)" in
145 + systemd_state="$(systemctl is-system-running)"
146 + set -e
147 +
148 + case "${systemd_state}" in
149 offline) return 1 ;;
150 unknown) : ;;
151 "") : ;;
152 *) return 0 ;;
153 esac
155 - set -e
154
155 # if pid 1 is systemd, it is systemd
156 [ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "systemd" ] && return 0
system/install-service.sh.in
+7 -14
@@ -185,11 +185,6 @@ _check_systemd() {
185 myns=''
186 ns=''
187
188 - # if the directory /lib/systemd/system OR /usr/lib/systemd/system (SLES 12.x) does not exit, it is not systemd
189 - if [ ! -d /lib/systemd/system ] && [ ! -d /usr/lib/systemd/system ]; then
190 - echo "NO" && return 0
191 - fi
192 -
188 # if there is no systemctl command, it is not systemd
189 [ -z "$(command -v systemctl 2>/dev/null || true)" ] && echo "NO" && return 0
190
@@ -198,22 +193,19 @@ _check_systemd() {
193 # This may return a non-zero exit status in cases when it actually
194 # succeeded for our purposes, so we need to toggle set -e off here.
195 set +e
201 - case "$(systemctl is-system-running)" in
196 + systemd_state="$(systemctl is-system-running)"
197 + set -e
198 +
199 + case "${systemd_state}" in
200 offline) echo "OFFLINE" && return 0 ;;
201 unknown) : ;;
202 "") : ;;
203 *) echo "YES" && return 0 ;;
204 esac
207 - set -e
205
206 # if pid 1 is systemd, it is systemd
207 [ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "systemd" ] && echo "YES" && return 0
208
212 - # it ‘is’ systemd at this point, but systemd might not be running
213 - # if not, return 2 to indicate ‘systemd, but not running’
214 - pids=$(safe_pidof systemd 2> /dev/null)
215 - [ -z "${pids}" ] && echo "OFFLINE" && return 0
216 -
209 # check if the running systemd processes are not in our namespace
210 myns="$(readlink /proc/self/ns/pid 2> /dev/null)"
211 for p in ${pids}; do
@@ -223,8 +215,9 @@ _check_systemd() {
215 [ -n "${myns}" ] && [ "${myns}" = "${ns}" ] && echo "YES" && return 0
216 done
217
226 - # else, it is not systemd
227 - echo "NO"
218 + # At this point, we know it’s a systemd system because systemctl
219 + # exists, but systemd does not appear to be running, so indicate as such
220 + echo "OFFLINE"
221 }
222
223 check_systemd() {