Skip to content

Commit

Permalink
Merge pull request rear#3072 from rear/jsmeix-USER_INPUT_UNATTENDED_T…
Browse files Browse the repository at this point in the history
…IMEOUT

New USER_INPUT_UNATTENDED_TIMEOUT config variable
to specify the timeout in seconds (by default 3)
for how long UserInput() waits for user input
when ReaR is run in 'unattended' or 'non-interactive' mode.
  • Loading branch information
jsmeix authored Nov 10, 2023
2 parents 6fa0df5 + 76e03ac commit e035d18
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
17 changes: 15 additions & 2 deletions usr/share/rear/conf/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,18 @@ PROGRESS_WAIT_SECONDS="1"
#
# USER_INPUT_TIMEOUT
# USER_INPUT_INTERRUPT_TIMEOUT
# USER_INPUT_UNATTENDED_TIMEOUT
# USER_INPUT_PROMPT
# USER_INPUT_MAX_CHARS
# USER_INPUT_user_input_ID
#
# All timeout values must be at least '1' (i.e. one second timeout).
# The reason is that 'read -t 0' returns immediately without trying to read any data
# so when 'read' should time out the minimum timeout value is '1'.
# That the 'read' timeout can be a fractional number requires bash 4.x
# see https://github.com/rear/rear/issues/2866#issuecomment-1254908270
# but in general ReaR should still work with bash 3.x so we use '-t 1'.
#
# USER_INPUT_TIMEOUT specifies the default timeout in seconds
# after that UserInput() automatically proceeds with a default value.
# That timeout interrupts a possibly ongoing user input
Expand All @@ -312,18 +320,23 @@ PROGRESS_WAIT_SECONDS="1"
# his particular input when the default is not the right one.
USER_INPUT_TIMEOUT="${USER_INPUT_TIMEOUT:-300}"
#
# USER_INPUT_INTERRUPT_TIMEOUT specifies the default timeout in seconds
# USER_INPUT_INTERRUPT_TIMEOUT specifies the timeout in seconds
# for how long UserInput() waits for the user to interrupt an automated input
# when a predefined input value is specified for a particular UserInput() call
# via a matching USER_INPUT_user_input_ID variable (see below).
# The minimum waiting time to interrupt an automated input is one second.
# The default interrupt timeout must be sufficiently long for the user
# to read and understand the possibly unexpected UserInput() message
# and then some more time to make a decision whether or not
# the automated action is actually the right one and
# if not a bit more time to hit a key to interrupt.
USER_INPUT_INTERRUPT_TIMEOUT="${USER_INPUT_INTERRUPT_TIMEOUT:-30}"
#
# USER_INPUT_UNATTENDED_TIMEOUT specifies the timeout in seconds
# for how long UserInput() waits for user input
# when ReaR is run in 'unattended' or 'non-interactive' mode
# i.e. when there is (usually) no user who would or could input something.
USER_INPUT_UNATTENDED_TIMEOUT="${USER_INPUT_UNATTENDED_TIMEOUT:-3}"
#
# USER_INPUT_PROMPT specifies the default prompt text that is shown
# if no prompt was specified for a particular UserInput() call.
USER_INPUT_PROMPT="${USER_INPUT_PROMPT:-enter your input}"
Expand Down
26 changes: 16 additions & 10 deletions usr/share/rear/lib/_input-output-functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1131,11 +1131,15 @@ function UserInput () {
# that would (without the delay) run in a tight loop that wastes resources (CPU, diskspace, and memory)
# and fills up the ReaR log file (and the disk - which is a ramdisk for 'rear recover')
# with some KiB data each second that may let 'rear recover' fail with 'out of diskspace/memory'.
# The default automated input interrupt timeout is 10 seconds to give the user a reasonable chance
# The default automated input interrupt timeout is 30 seconds to give the user a reasonable chance
# to recognize the right automated input on his screen and interrupt it when needed:
local automated_input_interrupt_timeout=10
local automated_input_interrupt_timeout=30
# Avoid stderr if USER_INPUT_INTERRUPT_TIMEOUT is not set or empty and ignore wrong USER_INPUT_INTERRUPT_TIMEOUT:
test "$USER_INPUT_INTERRUPT_TIMEOUT" -ge 1 2>/dev/null && automated_input_interrupt_timeout=$USER_INPUT_INTERRUPT_TIMEOUT
# Have at least one second timeout when ReaR runs unattended (in particular in non-interactive mode)
# because 'read -t 0' would return immediately without trying to read any data:
local unattended_timeout=3
test "$USER_INPUT_UNATTENDED_TIMEOUT" -ge 1 2>/dev/null && unattended_timeout=$USER_INPUT_UNATTENDED_TIMEOUT
local default_prompt="enter your input"
local prompt="$default_prompt"
# Avoid stderr if USER_INPUT_PROMPT is not set or empty:
Expand Down Expand Up @@ -1208,14 +1212,10 @@ function UserInput () {
test $user_input_ID || BugError "UserInput: Option '-I user_input_ID' required"
test "$( echo $user_input_ID | tr -c -d '[:lower:]' )" && BugError "UserInput: Option '-I' argument '$user_input_ID' must not contain lower case letters"
declare $user_input_ID="dummy" 2>/dev/null || BugError "UserInput: Option '-I' argument '$user_input_ID' not a valid variable name"
# Check the non-interactive mode and throw an error if default_input was not set
# In non-interactive mode use a short timeout (by default 3 seconds):
if is_true "$NON_INTERACTIVE" ; then
if IsInArray "$user_input_ID" "${USER_INPUT_SEEN_WITH_TIMEOUT[@]}" 2>/dev/null; then
Error "UserInput: non-interactive mode and repeat input for '$user_input_ID' requested while the previous attempt was answered with the default or timed out"
fi
# set timeouts to low but acceptable 3 seconds for non-interactive mode:
timeout=3
automated_input_interrupt_timeout=3
timeout=$unattended_timeout
automated_input_interrupt_timeout=$unattended_timeout
fi
# Shift away the options and arguments:
shift "$(( OPTIND - 1 ))"
Expand Down Expand Up @@ -1318,7 +1318,13 @@ function UserInput () {
if test "$default_and_timeout" ; then
is_true "$confidential_mode" && UserOutput "($default_and_timeout)" || LogUserOutput "($default_and_timeout)"
fi
# Prepare the 'read' call:
# In non-interactive mode error out if things do not proceed with default or automated input or without input and timeout:
if is_true "$NON_INTERACTIVE" ; then
if IsInArray "$user_input_ID" "${USER_INPUT_SEEN_WITH_TIMEOUT[@]}" 2>/dev/null ; then
Error "UserInput: non-interactive mode and repeat input request for '$user_input_ID' (previous attempt got default or automated input or timed out)"
fi
fi
# Prepare the 'read $read_options_and_arguments ...' call:
local read_options_and_arguments=""
is_true "$raw_input" && read_options_and_arguments+=" -r"
is_true "$silent_input" && read_options_and_arguments+=" -s"
Expand Down

0 comments on commit e035d18

Please sign in to comment.