From: Martin Schwenke Date: Mon, 11 Jul 2016 10:53:56 +0000 (+1000) Subject: ctdb-scripts: Avoid shellcheck warning SC2094 (read/write same file) X-Git-Tag: tdb-1.3.10~262 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=524eac92347a8adc6745f0cca04358a6f3fb581f;p=thirdparty%2Fsamba.git ctdb-scripts: Avoid shellcheck warning SC2094 (read/write same file) 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 Reviewed-by: Amitay Isaacs --- diff --git a/ctdb/config/events.d/13.per_ip_routing b/ctdb/config/events.d/13.per_ip_routing index cfb8348078e..f0e4609a01d 100755 --- a/ctdb/config/events.d/13.per_ip_routing +++ b/ctdb/config/events.d/13.per_ip_routing @@ -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" } ###################################################################### diff --git a/ctdb/config/functions b/ctdb/config/functions index 603cbf0a490..3b9d1dbbfa2 100755 --- a/ctdb/config/functions +++ b/ctdb/config/functions @@ -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 ()