I asked on the mailing list if this was expected behavior, and it turns out that POSIX only requires EXIT to run on a clean shutdown; to catch interruptions, add more signals.
trap 'eval $(ssh-agent -k)' EXIT INT ABRT KILL TERMI asked on the mailing list if this was expected behavior, and it turns out that POSIX only requires EXIT to run on a clean shutdown; to catch interruptions, add more signals.
trap 'eval $(ssh-agent -k)' EXIT INT ABRT KILL TERMhttps://www.spinics.net/lists/dash/msg02208.html
"Signal terminations are not caught by EXIT. It only catches normal exits. Unfortunately, the EXIT condition is not well-defined by POSIX, so it's left to interpretation."
...
POSIX is making it unspecified what happens here:
https://austingroupbugs.net/view.php?id=621
The EXIT condition shall occur when the shell terminates normally (exits), and may occur when the shell terminates abnormally as a result of delivery of a signal (other than SIGKILL) whose trap action is the default.
In basic terms for my purposes these respectively account for a clean exit, the terminal emulator being closed, ctrl-c, the kill command (edit: the default SIGTERM kill -15, not the SIGKILL kill -9)
And SIGKILL can't be handled, so that is indeed pointless.
I'm guessing this is some relic from 80s Unix systems where SIGKILL behaved different, or perhaps just an inconsistency/oversight.
The debugger could then allow the signal to go through to the process as is, or to replace it with another signal, or have it be ignored.
I made a program that looked like sh in ps, but actually was a simple debugger that just ran a specific other processes of mine. When that process hit a signal the debugger poked the signal number into a fixed location in the process' memory, then changed the signal to something innocuous like SIGALRM, and let that be delivered.
The process' SIGALRM handler would get the original signal number that the debugger had poked in, and print some obnoxious message like "Stupid sysadmin...your wimpy SIGxxx cannot hurt me!" where xxx was whatever signal someone had tried to send it.
I then told my fellow admins I had a stuck process that I could not kill, and asked them to kill it.
It took quite a while before someone got suspicious enough to suspect that the sh that was the parent of the "stuck" process wasn't actually a normal shell and try killing it.
trap 'ssh-agent -k' EXIT INT TERM
I don't see any reason for the eval as "ssh-agent -k" doesn't return anything useful you want the shell to evaluate.The "ssh-agent -k" command will emit shell commands that the shell must then execute which will kill the agent daemon and unset the socket environment variable.
Does it really? I've executed it here and it just runs kill, doesn't emit any bash. Running just ssh-agent (without any args) does that though, which is what's probably causing the confusion.
$ eval $(ssh-agent)
Agent pid 56785
$ ssh-agent -k
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
echo Agent pid 56785 killed;
The correct processing of that output requires an eval.Did you have any other questions?
$(ssh-agent)
won’t substitute that with the stdout and run that?
While redirecting to /dev/null will certainly work, the agent is holding sensitive credentials (by design), and confirmation of shutdown has a tangible security benefit.
$ ssh-agent
SSH_AUTH_SOCK=/var/folders/8p/_pwq997168s7vdwwdg_qr1j40000gn/T//ssh-DE0IoJfU5rrM/agent.15015; export SSH_AUTH_SOCK;
SSH_AGENT_PID=15016; export SSH_AGENT_PID;
echo Agent pid 15016;
$ SSH_AGENT_PID=15016; export SSH_AGENT_PID;
$ ssh-agent -k
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
echo Agent pid 15016 killed;
That said, it doesn't hurt to eval it, so I overstated my case in my original comment.It's a very common mistake with trap.