]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
ctdb-scripts: Avoid shellcheck warning SC2094 (read/write same file)
authorMartin Schwenke <martin@meltin.net>
Mon, 11 Jul 2016 10:53:56 +0000 (20:53 +1000)
committerAmitay Isaacs <amitay@samba.org>
Thu, 21 Jul 2016 00:24:26 +0000 (02:24 +0200)
SC2094: Make sure not to read and write the same file in the same pipeline.

The semantics here are unclear, so use a separate flock file in each
case for clarity.

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
ctdb/config/events.d/13.per_ip_routing
ctdb/config/functions

index cfb8348078e41bc7d21bb396a222e849ed79f059..f0e4609a01dcde3e9ddfa0eb61602116648c4f56 100755 (executable)
@@ -36,6 +36,8 @@ if ! have_link_local_config && [ ! -r "$CTDB_PER_IP_ROUTING_CONF" ] ; then
     die "error: CTDB_PER_IP_ROUTING_CONF=$CTDB_PER_IP_ROUTING_CONF file not found"
 fi
 
+service_state_dir=$(ctdb_setup_service_state_dir) || exit $?
+
 ######################################################################
 
 ipv4_is_valid_addr()
@@ -103,6 +105,7 @@ ipv4_host_addr_to_net ()
 ensure_rt_tables ()
 {
     rt_tables="$CTDB_SYS_ETCDIR/iproute2/rt_tables"
+    rt_tables_lock="${service_state_dir}/rt_tables_lock"
 
     # This file should always exist.  Even if this didn't exist on the
     # system, adding a route will have created it.  What if we startup
@@ -131,7 +134,7 @@ ensure_table_id_for_ip ()
     # range).
     (
        # Note that die() just gets us out of the subshell...
-       flock --timeout 30 0 || \
+       flock --timeout 30 9 || \
            die "ensure_table_id_for_ip: failed to lock file $rt_tables"
 
        _new="$CTDB_PER_IP_ROUTING_TABLE_ID_LOW"
@@ -150,7 +153,7 @@ ensure_table_id_for_ip ()
                "$_t" -le "$CTDB_PER_IP_ROUTING_TABLE_ID_HIGH" ] 2>/dev/null ; then
                _new=$((_t + 1))
            fi
-       done
+       done <"$rt_tables"
 
        # If the new table id is legal then add it to the file and
        # print it.
@@ -160,7 +163,7 @@ ensure_table_id_for_ip ()
        else
            return 1
        fi
-    ) <"$rt_tables"
+    ) 9>"$rt_tables_lock"
 }
 
 # Clean up all the table ids that we might own.
@@ -170,7 +173,7 @@ clean_up_table_ids ()
 
     (
        # Note that die() just gets us out of the subshell...
-       flock --timeout 30 0 || \
+       flock --timeout 30 9 || \
            die "clean_up_table_ids: failed to lock file $rt_tables"
 
        # Delete any items from the file that have a table id in our
@@ -185,8 +188,7 @@ clean_up_table_ids ()
             { print $0 }' "$rt_tables" >"$_tmp"
 
        mv "$_tmp" "$rt_tables"
-       # The lock is gone - don't do anything else here
-    ) <"$rt_tables"
+    ) 9>"$rt_tables_lock"
 }
 
 ######################################################################
index 603cbf0a4908f2f8e95543d58d28c82831d78eb7..3b9d1dbbfa2fbf974d5d3954d383d5db0ec1ed62 100755 (executable)
@@ -864,29 +864,32 @@ service_reconfigure ()
 
 ctdb_reconfigure_take_lock ()
 {
-    _ctdb_service_reconfigure_common
-    _lock="${_d}/reconfigure_lock"
-    mkdir -p "${_lock%/*}" # dirname
-    touch "$_lock"
-
-    (
-       flock 0
-       # This is overkill but will work if we need to extend this to
-       # allow certain events to run multiple times in parallel
-       # (e.g. takeip) and write multiple PIDs to the file.
-       read _locker_event 
-       if [ -n "$_locker_event" ] ; then
-           while read _pid ; do
-               if [ -n "$_pid" -a "$_pid" != $$ ] && \
-                   kill -0 "$_pid" 2>/dev/null ; then
-                   exit 1
-               fi
-           done
-       fi
+       _ctdb_service_reconfigure_common
+       _lock="${_d}/reconfigure_lock"
+       mkdir -p "${_lock%/*}" # dirname
+       touch "$_lock"
 
-       printf "%s\n%s\n" "$event_name" $$ >"$_lock"
-       exit 0
-    ) <"$_lock"
+       (
+               flock 9
+               # This is overkill but will work if we need to extend
+               # this to allow certain events to run multiple times
+               # in parallel (e.g. takeip) and write multiple PIDs to
+               # the file.
+               {
+                       read _locker_event
+                       if [ -n "$_locker_event" ] ; then
+                               while read _pid ; do
+                                       if [ -n "$_pid" -a "$_pid" != $$ ] && \
+                                          kill -0 "$_pid" 2>/dev/null ; then
+                                               exit 1
+                                       fi
+                               done
+                       fi
+               } <"$_lock"
+
+               printf "%s\n%s\n" "$event_name" $$ >"$_lock"
+               exit 0
+    ) 9>"${_lock}.flock"
 }
 
 ctdb_reconfigure_release_lock ()