funtoo / keychain

keychain ssh-agent front-end
http://www.funtoo.org
GNU General Public License v2.0
750 stars 104 forks source link

Exit status of commands #122

Open HaleTom opened 4 years ago

HaleTom commented 4 years ago

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 commented 2 years ago

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.

https://github.com/funtoo/keychain/blob/7e37e4b63529f4a1caae9ca5e6db42b10a6fd2f3/keychain.sh#L1225-L1235

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 (https://github.com/funtoo/keychain/pull/139) but the project does not seem to be actively maintained.

mrl5 commented 2 years ago

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