# XXX: only call after relevant vars have been set up, defined early so that
# below code can utilize it after they do so
function _sev_zcleanup {
- local x p y
+ local x y
+ function _sev_checkpid {
+ # return 1 if pid is a zsh process that isn't us
+ [[ $$ -eq $1 ]] || { ! kill -0 $1 2>/dev/null ||
+ [[ $(ps -aopid=,comm= | awk "\$1 == $1 { print \$2 }") != zsh ]] }
+ }
# gpg forwarding
if [[ -d $_sev_gpg_forward_dir && ( -z $1 || $1 == 'gpg-forward' ) ]] {
# clean up forward dirs if its session is dead or we ask for it
find $_sev_gpg_forward_dir -mindepth 1 -maxdepth 1 -type d |
while {read -r x} {
- # NOTE: the only way we can get here is if we have not been
- # forwarded before, if the user asks for it, or during
- # logout. if our own pid already has a dir, it is most likely
- # stale, the user wants it removed, or something is very
- # broken—in all 3 of these cases the best choice is remove it.
- p=$(basename $x)
- if {[[ -v _sev_gpg_forward_clean || $$ == $p ]] ||
- ! kill -0 $p 2>/dev/null} {
+ # NOTE: called before setup and on logout: remove the dir we
+ # will be using (it's stale) or the dir we did use, and any
+ # dead sessions if present
+ if (_sev_checkpid ${x:t}) {
find $x -mindepth 1 -maxdepth 1 | while {read -r y} {
# XXX: real dirs will stop unlink, consider it a feature
unlink $y
# after this function is called
if [[ -d $_sev_tmp && ( -z $1 || $1 == 'tmp' ) ]] {
# clean up tmp dirs if its session is dead or we ask for it
- find $_sev_tmp -mindepth 1 -maxdepth 1 -name '.session.*' -type d |
+ find -L $_sev_tmp -mindepth 1 -maxdepth 1 -name '.session.*' -type d |
while {read -r x} {
# NOTE: same rationale as above
- p=${$(basename $x)#.session.}
- if {[[ -v _sev_tmp_clean || $$ == $p ]] ||
- ! kill -0 $p 2>/dev/null} {
+ if (_sev_checkpid ${${x:t}#.session.}) {
rm -rf $x
}
}
}
+
+ unfunction _sev_checkpid
}
function _sev_setpath {
_sev_zcleanup tmp
# finally create our subdir for this session
_t=$_sev_tmp/.session.$$
- if ! mkdir -m700 $_t 2>/dev/null; then
+ # XXX: we probably shouldn't allow the use of an existing dir—tmp dir for
+ # current pid should have been removed by zcleanup
+ if ([[ ! -d $_t ]] && ! mkdir -m700 $_t 2>/dev/null) {
[[ -o interactive ]] &&
print -P "%F{red}!!! Can't create session tmp subdir $_t, using $_sev_tmp%f"
_t=$_sev_tmp
- fi
+ }
export _sev_tmp TMPDIR=$_t TEMPDIR=$_t TEMP=$_t TMP=$_t TMPPREFIX=$_t/zsh
unset _t
}