Patch: password-store-1.7.3: ignore SIGHUP for non-interactive shells.

RVP rvp at SDF.ORG
Mon Oct 26 07:06:50 CET 2020


We need to explicitly ignore SIGHUP in non-interactive shells, otherwise
bash will terminate us when, let's say, the xterm we're running in closes.
The built-in disown command won't do the job in a non-interactive shell.
(A bit of reading-between-the-lines of the man-page is needed here, as
this is not explicitly stated in bash(1).)

Test scripts:
1. This script, which relies on disown, will exit prematurely:
---
#!/usr/bin/env bash
#
# Run as: xterm -fn 10x20 -fg Ivory -bg Black -e ./sleep-disown.sh
#
# This version doesn't work because disown only works in an interactive
# shell. In non-interactive shells, like above, disown doesn't protect
# backgrounded jobs from termination on shell exit.

me=${0##*/}
(
  	# SIGHUP will be delivered; sleep is interrupted; script aborts.
  	trap '	echo "Start: $(date)" >> /tmp/$me.$$.txt
  		echo SIGHUP >> /tmp/$me.$$.txt
  	' HUP
  	sleep 60
  	echo "End: $(date)" >> /tmp/$me.$$.txt
) &
disown && echo disowned
echo 'Background job will not sleep for 60 secs if xterm exits.
Press RETURN to exit xterm.'
read x
---

2. This script, which ignores SIGHUP, will sleep for the correct length
of time:
---
#!/usr/bin/env bash
#
# Run as: xterm -fn 10x20 -fg Ivory -bg Black -e ./sleep-nohup.sh
#
# This works because background the job is protected from termination
# by "trap '' HUP". You can also use nohup(1) instead.

me=${0##*/}
(
  	trap '' HUP
  	echo "Start: $(date)" >> /tmp/$me.$$.txt
  	sleep 60
  	echo "End $(date)" >> /tmp/$me.$$.txt
) &
echo 'Background job will sleep for 60 secs even if xterm exits.
Press RETURN to exit xterm.'
read x
---

Patch for password-store-1.7.3 follows:

---START
--- pass.orig	2020-07-17 13:45:22.000000000 +0000
+++ pass	2020-10-06 23:08:41.159955217 +0000
@@ -157,6 +157,12 @@
   	local sleep_argv0="password store sleep on display $DISPLAY"
   	pkill -f "^$sleep_argv0" 2>/dev/null && sleep 0.5
   	local before="$(xclip -o -selection "$X_SELECTION" 2>/dev/null | $BASE64)"
+	# Explicitly ignore SIGHUP because "disown" only works in interactive shells.
+	# Otherwise, in non-interactive cases like "xterm -e passmenu" or "xterm -e pass",
+	# which are needed to supply a terminal for "pinentry-curses" (like when "pass"
+	# is called from "dmenu"), bash will kill xclip when the script finishes--ie.
+	# before $CLIP_TIME has elapsed.
+	trap '' HUP
   	echo -n "$1" | xclip -selection "$X_SELECTION" || die "Error: Could not copy data to the clipboard"
   	(
   		( exec -a "$sleep_argv0" bash <<<"trap 'kill %1' TERM; sleep '$CLIP_TIME' & wait" )
@@ -173,7 +179,7 @@
   		qdbus org.kde.klipper /klipper org.kde.klipper.klipper.clearClipboardHistory &>/dev/null

   		echo "$before" | $BASE64 -d | xclip -selection "$X_SELECTION"
-	) >/dev/null 2>&1 & disown
+	) >/dev/null 2>&1 &
   	echo "Copied $2 to clipboard. Will clear in $CLIP_TIME seconds."
   }

---END

Thanks,
RVP


More information about the Password-Store mailing list