Fix locking and command status output
authorBryn M. Reeves <bmr@redhat.com>
Fri, 1 Feb 2013 17:37:28 +0000 (17:37 +0000)
committerBryn M. Reeves <bmr@redhat.com>
Fri, 1 Feb 2013 17:37:28 +0000 (17:37 +0000)
Fix flock usage to ensure the proper file descriptor is used and
propagate and check exit statuses when using locking and unlocking
functions.

blkdevalias

index 3704876..85912f2 100755 (executable)
@@ -402,16 +402,24 @@ ba_deletedisk () {
         _error "\"$_ALIAS\" does not exist"
         ba_usage "deletedisk <alias>"
     fi
-    echo -n "Removing wwid map disk \"$_ALIAS\":"
     # generate device path for refresh call
     _DEVPATH="/dev/$BA_DEV_PATH/${WWIDMAP[$_WWID]}"
     _TARGET=$($BA_READLINK -f $_DEVPATH)
+    if [ "$_DEVPATH" == "$_TARGET" ]; then
+        _warn "device at $_DEVPATH not found;" \
+                "will not update device mappings"
+    fi
+    _debug "$FUNCNAME: removing wwid mapping for" \
+            "WWID=\"$WWID\" DEVPATH=\"$_DEVPATH\" TARGET=\"$_TARGET\""
+    echo -n "Removing wwid map disk \"$_ALIAS\":"
     ba_del_wwid_mapping $_WWID \
     && if [ "$BA_DEBUG" != "yes" ]; then
         echo "                                  [  OK  ]"
     fi
     ba_store_wwid_map
-    ba_refresh_device $_TARGET
+    if [ "$_TARGET" != "$_DEVPATH" ]; then
+        ba_refresh_device $_TARGET
+    fi
 }
 
 # ba_list_disks:
@@ -518,12 +526,19 @@ ba_lock_map () {
     fi
     _debug "$FUNCNAME: locking $BA_LOCK_PATH on" \
         "fd $BA_LOCK_FD with mode: $_MODE"
-    $BA_FLOCK -n $BA_LOCK_FD -x &>/dev/null
+    $BA_FLOCK $_FLAG -n $BA_LOCK_FD
+    _STATUS="$?"
+    _debug "$FUNCNAME: flock exited with status $?"
+    return $_STATUS
 }
 
 ba_unlock_map () {
+    local _STATUS
     _debug "$FUNCNAME: unlocking $BA_LOCK_PATH on fd $BA_LOCK_FD"
-    $BA_FLOCK -n $BA_LOCK_FD -u &>/dev/null
+    $BA_FLOCK -u -n $BA_LOCK_FD
+    _STATUS="$?"
+    _debug "$FUNCNAME: flock exited with status $_STATUS"
+    return $_STATUS
 }
 
 # no point optimizing this out for configure
@@ -536,7 +551,8 @@ case "$1" in
     create*)
         (
             # lock exclusive
-            ba_lock_map exclusive
+            ba_lock_map exclusive || _error "could not acquire read lock" \
+                 "on $BA_LOCK_PATH"
             ba_createdisk "$2" "$3"
             ba_unlock_map
         ) 9> $BA_LOCK_PATH
@@ -544,7 +560,8 @@ case "$1" in
     delete*)
         (
             # lock exclusive
-            ba_lock_map exclusive
+            ba_lock_map exclusive || _error "could not acquire read lock" \
+                 "on $BA_LOCK_PATH"
             ba_deletedisk "$2"
             ba_unlock_map
         ) 9> $BA_LOCK_PATH
@@ -552,7 +569,8 @@ case "$1" in
     list*)
         (
             # lock shared
-            ba_lock_map shared
+            ba_lock_map shared || _error "could not acquire read lock" \
+                 "on $BA_LOCK_PATH"
             ba_list_disks
             ba_unlock_map
         ) 9> $BA_LOCK_PATH
@@ -560,7 +578,8 @@ case "$1" in
     query*)
         (
             # lock shared
-            ba_lock_map shared
+            ba_lock_map shared || _error "could not acquire read lock" \
+                 "on $BA_LOCK_PATH"
             ba_querydisk "$2" "$3"
             ba_unlock_map
         ) 9> $BA_LOCK_PATH
@@ -568,7 +587,8 @@ case "$1" in
     scan*)
         (
             # lock shared
-            ba_lock_map shared
+            ba_lock_map shared || _error "could not acquire read lock" \
+                 "on $BA_LOCK_PATH"
             ba_scandisks "$2"
             ba_unlock_map
         ) 9> $BA_LOCK_PATH
@@ -576,7 +596,8 @@ case "$1" in
     refresh*)
         (
             # lock shared
-            ba_lock_map shared
+            ba_lock_map shared || _error "could not acquire read lock" \
+                 "on $BA_LOCK_PATH"
             ba_refresh_alias "$2"
             ba_unlock_map
         ) 9> $BA_LOCK_PATH
@@ -584,7 +605,8 @@ case "$1" in
     map)
         (
             # lock shared
-            ba_lock_map shared
+            ba_lock_map shared || _error "could not acquire read lock" \
+                "on $BA_LOCK_PATH"
             ba_map "$2"
             ba_unlock_map
         ) 9> $BA_LOCK_PATH
@@ -593,3 +615,9 @@ case "$1" in
         ba_usage "\n{configure|createdisk|deletedisk|listdisks|scandisks|querydisk|map}"
 esac
 
+_STATUS="$?"
+if [ "$_STATUS" != 0 ]; then
+    _warn "ba_main: reached end of main function with non-zero status ($?)"
+fi
+# catch non-zero exits
+exit 0