[pass] [PATCH] Do not race on clipping.

Jason A. Donenfeld Jason at zx2c4.com
Thu Mar 20 07:02:10 CET 2014


We use a simple pid lock file, and if another pass instance is going to
restore the clipboard, we simply restore to blank.
---
This is untested. Before I spend the time debugging this, what do
you think of the approach? I know there were some opinions on the
list about just restoring the clipboard to blank, always, but
perhaps this patch is a decent compromise for those who like it.
Is this too complex? Too error prone?

Soliciting feedback before I start debugging it.

 src/password-store.sh | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

diff --git a/src/password-store.sh b/src/password-store.sh
index 9cfae7f..7eafd88 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -114,14 +114,26 @@ clip() {
 	# we're going with this for now.
 
 	before="$(xclip -o -selection clipboard | base64)"
+	restore_file="${TMPDIR:-/tmp}/password-store-clipboard-restore.${DISPLAY}.pid"
+	restore_pid=""
+	[[ -f $restore_file ]] && restore_pid="$(< "$restore_file")"
+	other_restore_active=0
+	[[ $restore_pid =~ ^[0-9]+$ && $(readlink -f /proc/self/exe 2>/dev/null) == "$(readlink -f "/proc/$restore_pid/exe" 2>/dev/null)" ]] && other_restore_active=1
 	echo -n "$1" | xclip -selection clipboard
 	(
+		if [[ $other_restore_active -eq 0 ]]; then
+			old_umask=$(umask)
+			umask 0077
+			echo $$ > "$restore_file"
+			umask $old_umask
+		fi
 		sleep 45
-		now="$(xclip -o -selection clipboard | base64)"
-		if [[ $now != $(echo -n "$1" | base64) ]]; then
-			before="$now"
+		if [[ $other_restore_active -eq 0 ]]; then
+			now="$(xclip -o -selection clipboard | base64)"
+			[[ $now != $(echo -n "$1" | base64) ]] && before="$now"
+		else
+			before=""
 		fi
-
 		# It might be nice to programatically check to see if klipper exists,
 		# as well as checking for other common clipboard managers. But for now,
 		# this works fine -- if qdbus isn't there or if klipper isn't running,
@@ -132,6 +144,7 @@ clip() {
 		qdbus org.kde.klipper /klipper org.kde.klipper.klipper.clearClipboardHistory &>/dev/null
 
 		echo "$before" | base64 -d | xclip -selection clipboard
+		[[ $other_restore_active -eq 0 ]] && rm "$restore_file"
 	) & disown
 	echo "Copied $2 to clipboard. Will clear in 45 seconds."
 }
-- 
1.9.0



More information about the Password-Store mailing list