#1076 Issue closed: setting EXIT_CODE for Error functions is useless because rear kills itself that results exit code 143

Labels: cleanup, fixed / solved / done, minor bug

jsmeix opened issue at 2016-11-22 10:49:

While working on
https://github.com/rear/rear/pull/1075
I noticed that code like

Error 99 "error messsage"

does not let rear exit with exit code 99.

The reason seems that EXIT_CODE can be set
but that variable is nowhere evaluated:

$ find usr/share/rear/* | xargs grep 'EXIT_CODE'

usr/share/rear/layout/compare/default/500_compare_layout.sh:    EXIT_CODE=1
usr/share/rear/layout/compare/default/510_compare_files.sh:        EXIT_CODE=1
usr/share/rear/layout/save/NSR/default/650_check_iso_recoverable.sh:   EXIT_CODE=1
usr/share/rear/lib/_input-output-functions.sh:          EXIT_CODE=$1
usr/share/rear/lib/_input-output-functions.sh:          EXIT_CODE=1
usr/share/rear/lib/_input-output-functions.sh:    EXIT_CODE=1
usr/share/rear/lib/_input-output-functions.sh:        EXIT_CODE=$1
usr/share/rear/lib/_input-output-functions.sh:    # Call Error explicitly with the above set EXIT_CODE:
usr/share/rear/lib/_input-output-functions.sh:    Error $EXIT_CODE "
usr/share/rear/lib/help-workflow.sh:EXIT_CODE=1

I.e. there is nowhere a $EXIT_CODE except my recent change in
https://github.com/rear/rear/pull/1075
where I call "Error $EXIT_CODE"
but that does not let rear exit with that EXIT_CODE value.

jsmeix commented at 2016-11-22 10:52:

$ git log -p --follow usr/share/rear/lib/_input-output-functions.sh

shows that EXIT_CODE was initially introduced
by Dag Wieers.

@dagwieers
can you tell me how EXIT_CODE is meant to be used?

jsmeix commented at 2016-11-22 14:44:

I found where $EXIT_CODE is used:
In usr/sbin/rear there is the very last line

exit $EXIT_CODE

but somehow that does not work - at least not via e.g.

Error 99 "error messsage"

jsmeix commented at 2016-11-24 15:32:

Found the reason:

Using in a script

EXIT_CODE=99

works as expected:
rear exits with that exit code.

In contrast using in a script

Error 99 "test error 99"

exits rear always with exit code 143 which means
usually the program caught a SIGTERM signal
i.e. the process was killed.
For rear it is USR1 that kills it via
usr/share/rear/lib/_input-output-functions.sh

# activate the trap function
builtin trap "DoExitTasks" 0
# keep PID of main process
readonly MASTER_PID=$$
# duplication STDOUT to fd7 to use for Print
exec 7>&1
QuietAddExitTask "exec 7>&-"
# USR1 is used to abort on errors, not using Print to always print to the original STDOUT, even if quiet
builtin trap "echo 'Aborting due to an error, check $LOGFILE for details' >&7 ; kill $MASTER_PID" USR1
...
Error() {
    ...
        kill -USR1 $MASTER_PID # make sure that Error exits the master process, even if called from child processes :-)
}

so that setting EXIT_CODE for Error functions is useless.

jsmeix commented at 2016-11-24 15:40:

I think I will simply remove the EXIT_CODE stuff
from the Error functions.

The only usage of EXIT_CODE in Error functions is

$ find usr/sbin/rear usr/share/rear/* | xargs grep 'Error [0-9][0-9]* '
usr/share/rear/prep/BACULA/default/500_check_BACULA_bconsole_results.sh:StopIfError 1 "Bacula client status unknown on director."
usr/share/rear/prep/BAREOS/default/500_check_BAREOS_bconsole_results.sh:StopIfError 1 "Bareos client status unknown on director."
usr/share/rear/verify/BACULA/default/050_check_requirements.sh:   StopIfError 1  "Bacula configuration file (bconsole.conf) missing"
usr/share/rear/verify/BAREOS/default/050_check_requirements.sh:   StopIfError 1  "Bareos configuration file (bconsole.conf) missing"

that I can easily adapt.

jsmeix commented at 2016-11-24 17:40:

Fixed via https://github.com/rear/rear/pull/1084


[Export of Github issue for rear/rear.]