changeset 545:566ecdd9e73b

When analyzing the output of "mount -p" check for proper parsing capability: no improper mixing of tabs and spaces. Suggest (and mostly use) "jq" in other cases.
author Franz Glasner <fzglas.hg@dom66.de>
date Tue, 10 Sep 2024 10:13:59 +0200
parents 56317c9226ba
children d28733400b18
files sbin/ftjail sbin/fzfs share/local-bsdtools/common.subr
diffstat 3 files changed, 43 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/sbin/ftjail	Tue Sep 10 01:54:42 2024 +0200
+++ b/sbin/ftjail	Tue Sep 10 10:13:59 2024 +0200
@@ -537,16 +537,20 @@
     _rootds_mountpoint="$(zfs list -H -o mountpoint -t filesystem "${_dsname}")" || \
         { echo "ERROR: dataset not found" >&2; return 1; }
 
+    # Check for unexpected spaces
+    if ! check_for_proper_fstab; then
+        fatal 1 "Unexpected spaces in fstab. Please install \`${JQ}'."
+    fi
     /sbin/mount -t zfs -p \
-    | /usr/bin/grep -E "^${_dsname}(/|\s)" \
-    | sort -n -r \
+    | LC_ALL=C /usr/bin/grep -E "^${_dsname}(/|\s)" \
+    | LC_ALL=C /usr/bin/sort -n -r \
     | {
         while IFS=' '$'\t' read -r _name _mp _rest ; do
             echo "Umounting ${_name} on ${_mp}"
             /sbin/umount "${_mp}" || return 1
         done
         return 0
-    }
+      }
 }
 
 
--- a/sbin/fzfs	Tue Sep 10 01:54:42 2024 +0200
+++ b/sbin/fzfs	Tue Sep 10 10:13:59 2024 +0200
@@ -322,6 +322,10 @@
             fi
           done
     else
+        # Check for unexpected spaces
+        if ! check_for_proper_fstab; then
+            fatal 1 "Unexpected spaces in fstab. Please install \`${JQ}'."
+        fi
         /sbin/mount -t zfs -p \
         | LC_ALL=C /usr/bin/grep -E "^${_dsname}(/|\s)" \
         | LC_ALL=C /usr/bin/sort -n -r \
--- a/share/local-bsdtools/common.subr	Tue Sep 10 01:54:42 2024 +0200
+++ b/share/local-bsdtools/common.subr	Tue Sep 10 10:13:59 2024 +0200
@@ -236,6 +236,10 @@
             return 1
           }
     else
+        # Check for unexpected spaces
+        if ! check_for_proper_fstab; then
+            fatal 1 "Unexpected spaces in fstab. Please install \`${JQ}'."
+        fi
         /sbin/mount -t zfs -p \
         | {
             while IFS=' '$'\t' read -r _ds _mount _rest ; do
@@ -391,7 +395,10 @@
         | LC_ALL=C /usr/bin/awk -F $'\\t+' -v OFS=$'\t' -v ds1="${_directory}" -v ds2="${_directory}/" $'{ if (($2 == ds1) || (index($2, ds2) == 1)) { print; } }' \
         | LC_ALL=C /usr/bin/sort -t $'\t' -k1
     else
-
+        # Check for unexpected spaces
+        if ! check_for_proper_fstab; then
+            fatal 1 "Unexpected spaces in fstab. Please install \`${JQ}'."
+        fi
         _fstab="$(/sbin/mount -p | awk -v pa1="^${_directory}\$" -v pa2="^${_directory}/" '($2 ~ pa1) || ($2 ~ pa2 ) { print; }' | sort -k3)"
         echo "${_fstab}"
     fi
@@ -449,6 +456,30 @@
 
 
 #:
+#: Check that the current fstab as returned by :command:`mount -p` does not
+#: contain any spaces at improper -- and therefore unhandled -- locations.
+#:
+#: Returns:
+#:   int: 0 (truish) if :command:`mount -p` contains no spaces,
+#:        1 (falsy) otherwise
+#:
+check_for_proper_fstab() {
+    local _dev _mp _fstype _opts _dump _pass _rest
+
+    /sbin/mount -p \
+    | {
+        while IFS=$' \t' read -r _dev _mp _fstype _opts _dump _pass _rest; do
+            if [ -n "${_rest}" ]; then
+                return 1
+            fi
+            # XXX TBD: Check that _dump and _pass are numbers proper
+        done
+        return 0
+      }
+}
+
+
+#:
 #: Clean the current process environment somewhat
 #:
 reset_environment() {