Improved handling of file descriptors, other minor changes

This commit is contained in:
Jesse Gardner 2021-07-11 14:14:04 -07:00
parent 94d3727614
commit aac58857e5
1 changed files with 22 additions and 8 deletions

View File

@ -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