4

I open multiple shell tabs when I start KDE, and I've just added keychain to my ~/.shellrc the problem is that all the tabs prompt for key passwords when I login. This is quite annoying to do this. Is there any good solution for this so that all the tabs simply start, and once I've logged into one tab, all of them have the keys loaded?

xenoterracide
  • 59,188
  • 74
  • 187
  • 252

1 Answers1

2

Here are two methods:

You can ensure that keychain only opens on one tab like this:

if mkdir /tmp/keychain.lock; then
  eval `keychain --eval --agents ssh id_dsa`
  rm -r /tmp/keychain.lock
fi

But it may not be on the first tab you land on - you might have to hunt for it, which could be just as annoying. This works because mkdir is an atomic operation - only one script will succeed, and that one will display the prompt.

Another way will display the prompt on all the tabs, but will quit them once you respond on any one of them. You can poll a file or use inotify-tools like this:

file=/tmp/keychain-wait
touch $file
inotifywait -e delete_self $file |\
while read file event; do 
 if [ "$event" = "DELETE_SELF" ]; then
   pkill keychain
 fi
done &

keychain
rm $file

This one presents the prompt, but first it starts a watcher to see if a file is deleted. After the prompt is satisfied, the file is deleted, and the watcher will kill any other prompts that are waiting. inotifywait is from inotify-tools; inotify is a Linux API. There may be a similar API on other Unices, but if not, you only need a loop that polls to see if the file is deleted.

Shawn J. Goff
  • 46,081
  • I changed your code a little you can see it here: https://github.com/xenoterracide/dot_etc/blob/master/rc.d/keychain . I don't think any of my changes would account for my problem... but... I occasionally get 2 prompts for passwords... does my code have a problem? or is there something else causing a race condition. – xenoterracide Dec 20 '10 at 03:11
  • 1
    Yes - There is time between the test -e $file and touch $file. This is why I used the mkdir in my example. You can read more about atomic operations here: http://unix.stackexchange.com/questions/70/what-unix-commands-can-be-used-as-a-semaphore-lock – Shawn J. Goff Dec 20 '10 at 20:50