From aac58857e5ff2b7545ef494f8fd211969d74ed73 Mon Sep 17 00:00:00 2001 From: Jesse Gardner Date: Sun, 11 Jul 2021 14:14:04 -0700 Subject: [PATCH] Improved handling of file descriptors, other minor changes --- qq2clone | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/qq2clone b/qq2clone index 38370ba..d1e65b3 100755 --- a/qq2clone +++ b/qq2clone @@ -15,7 +15,7 @@ E_template=13 # Problem with a template E_extcom=14 # An external command failed E_xml=15 # Malformed XML or issue processing XML E_libvirt=16 # Invokation of a libvirt tool was unsuccesful -E_timeout=17 # Timeout was exceeded before spice connection to clone +E_timeout=17 # Timeout was exceeded before spice connection to VM # was established E_file=18 # Expected file does not exist or is of wrong type/format E_unexpected=19 # Probably a bug in qq2clone @@ -89,6 +89,7 @@ while IFS= read -r line <&3; do echo "$match" (($#)) && (($1 == 1)) && write_pipe 0 "$match" else + [[ "$line" == "EOF" ]] || unexpected_error "read_pipe" break fi done @@ -107,9 +108,10 @@ write_pipe () # $2 and on: If $1 is 0, write_pipe will write the remaining parameters #=========================================================================# { -# We put a + at the beginning of every line to let read_pipe work in a -# non-blocking manner +# + is put at the beginning of every line echoed to the pipe, so that +# read_pipe can operate in a non-blocking manner local line +{ [[ "$1" == "0" ]] || [[ "$1" == "1" ]]; } || unexpected_error write_pipe if (($1)); then while IFS= read -r line; do echo "+$line" >&3 @@ -129,6 +131,12 @@ return 0 #@@@@@@@@@@@@@@@@@@@# #-------------------# +# lv_api_do is accessed in the background because it allows for only one +# subshell to be invoked when using lv_api_do repeatedly. Makes qq2clone +# more efficient (significantly, in some cases) but makes opening and +# closing lv_api_do into something that must be managed manually by the +# coder + #=========================================================================# lv_api_do_close () # DESCRIPTION: Tell lv_api_do to exit and close the extra pipe @@ -138,8 +146,7 @@ lv_api_do_close () #=========================================================================# { echo "exit" >&4 - exec 4>&- - exec 4<&- + exec 4>&- 4<&- rm -rf "${lv_api_temp:?}" return 0 } @@ -147,12 +154,15 @@ lv_api_do_close () lv_api_do_comm () # DESCRIPTION: Issue a command to lv_api_do # INPUT: The command -# OUTPUT: Return 0 on success, lv_api_do output can be accessed with -# read_pipe. Return 1 on failure. Exit and error message if lv_api_do -# encounters a fatal error +# OUTPUT: Return 0/1 on success/failure. lv_api_do output can be accessed +# with read_pipe. Exit and error message if lv_api_do encounters +# a fatal error # PARAMETERS: $@: command string to lv_api_do #=========================================================================# { +# Ensure lv_api_do is open +( : >&4 ; ) &>/dev/null || unexpected_error lv_api_do_comm + echo "$*" >&4 local check read -r check <&3 @@ -3118,6 +3128,10 @@ return 0 #-----------------# if ! ((QQ2_NOEXECUTE)); then + + #Ensure needed fds are not in use + exec 3>&- 3<&- 4>&- 4<&-; + open_pipe if [[ -n "$QQ2_DIR" ]]; then : # If already set, use existing value