Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exit status of commands #122

Open
HaleTom opened this issue Aug 29, 2020 · 2 comments
Open

Exit status of commands #122

HaleTom opened this issue Aug 29, 2020 · 2 comments

Comments

@HaleTom
Copy link

HaleTom commented Aug 29, 2020

I was surprised to see:

% keychain -l
Error connecting to agent: No such file or directory
% echo $?                            
0

Whereas:

% ssh-add -l        
Error connecting to agent: No such file or directory
% echo $?                                                                                          
2

Can keychain return useful exit statuses? (I note there's no EXIT STATUS section in the man page, also)

@manolis-andr
Copy link

Problem

keychain always exits with success code 0, even on failure.

Cause

Upon errors, the script calls die() which performs an exit 1, so one would expect to exit with a correct exit code.

However, the problem is that initially the script uses the trap shell builtin to cleanup some lock files when the scripts exits.

keychain/keychain.sh

Lines 1225 to 1235 in 7e37e4b

# Set up traps
# Don't use signal names because they don't work on Cygwin.
if $clearopt; then
trap '' 2 # disallow ^C until we've had a chance to --clear
trap 'droplock; exit 1' 1 15 # drop the lock on signal
trap 'droplock; exit 0' 0 # drop the lock on exit
else
# Don't use signal names because they don't work on Cygwin.
trap 'droplock; exit 1' 1 2 15 # drop the lock on signal
trap 'droplock; exit 0' 0 # drop the lock on exit
fi

The trap syntax to run some bash commands upon a signal delivery or on exit is: trap <action> <signal>. To run on shell-exit the <sigspec> should be set to 0 or EXIT. This applies both for dash and bash.

However, the code apart from droplock also issues another exit 0 that masks the previous exit code (see trap 'droplock; exit 0' 0 in the snippet above). So that is why the script always exits with a zero exit code.

Solution

Bourne shell should be able to properly return the previously specified exit code after trap runs the given commands, so to preserve the exit code we should simply remove the exit command from the trap syntax:

- trap 'droplock; exit 0' 0
+ trap 'droplock' 0.

I have sent a patch (#139) but the project does not seem to be actively maintained.

@mrl5
Copy link

mrl5 commented Jul 16, 2022

hello @HaleTom - if you'd like to report a bug kindly use https://bugs.funtoo.org/

you can also reach us on Discord - for more info check https://www.funtoo.org/Welcome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants