grml-hwinfo: Warn the user if grml-hwinfo is running without X server
authorDarshaka Pathirana <dpat@syn-net.org>
Fri, 22 Sep 2023 16:03:08 +0000 (18:03 +0200)
committerDarshaka Pathirana <dpat@syn-net.org>
Fri, 22 Sep 2023 16:07:48 +0000 (18:07 +0200)
Not all data is collected when grml-hwinfo is run without an X server.
The user is now warned.

Also improved the check if the X server is not running.

So far, we relied on the existence of the $DISPLAY variable to detect if
the X server is running.

If the variable is set but the X server is not running, we use xset
quietly to see if we can connect to the X server.

Inspired by: https://stackoverflow.com/a/11965765/2142030

grml-hwinfo

index 4ba0a62..97ad572 100755 (executable)
@@ -172,6 +172,22 @@ get_network_devices() {
   done
 }
 
+# Check if X server is running
+#
+# If xset is missing, we rely on the existence of the $DISPLAY variable.
+NO_DISPLAY=0
+if exectest xset ; then
+  if ! timeout 1s xset q &>/dev/null ; then
+    NO_DISPLAY=1
+  fi
+elif [ -z "${DISPLAY}" ] ; then
+    NO_DISPLAY=1
+fi
+
+if [ "${NO_DISPLAY}" -eq 1 ] ; then
+  $_opt_quiet || echo "W: Running without X server. Not all data will be collected."
+fi
+
 cd "${OUTDIR}" || exit 1
 (
   if ! $_opt_quiet ; then
@@ -324,7 +340,7 @@ cd "${OUTDIR}" || exit 1
   dpkg -S "/boot/vmlinuz-$(uname -r)" >> running_kernel 2>>running_kernel.error
 
   # X stuff
-  if [ -n "${DISPLAY}" ] ; then
+  if [ "${NO_DISPLAY}" -eq 0 ] ; then
     exectest xviddetect  && xviddetect         > ./xviddetect
     exectest xvidtune    && xvidtune -show     > ./xdivtune
     exectest xrandr      && xrandr             > ./xrandr