zsh-users
 help / color / mirror / Atom feed
* callback?
@ 2020-11-02  2:39 Matt Zagrabelny
  2020-11-02  8:52 ` callback? Oliver Kiddle
  0 siblings, 1 reply; 2+ messages in thread
From: Matt Zagrabelny @ 2020-11-02  2:39 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 575 bytes --]

Greetings!

I am using tmux with zsh.

I use ssh agent forwarding and I ssh to a remote system that is running
tmux. I then detach from that terminal and my SSH_AUTH_SOCK environment
variable becomes "stale".

I would like to have zsh execute something to refresh the SSH_AUTH_SOCK
environment variable when I "tmux attach" to the aforementioned detached
tmux session.

Does anyone know of a way to register a callback or something for zsh so
when a particular command gets run (in my case, tmux attach) my zsh
environment variable can get updated?

Thanks for the help!

-m

[-- Attachment #2: Type: text/html, Size: 776 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: callback?
  2020-11-02  2:39 callback? Matt Zagrabelny
@ 2020-11-02  8:52 ` Oliver Kiddle
  0 siblings, 0 replies; 2+ messages in thread
From: Oliver Kiddle @ 2020-11-02  8:52 UTC (permalink / raw)
  To: Matt Zagrabelny; +Cc: zsh-users

Matt Zagrabelny wrote:
>
> I am using tmux with zsh.
>
> I use ssh agent forwarding and I ssh to a remote system that is running tmux. I
> then detach from that terminal and my SSH_AUTH_SOCK environment variable
> becomes "stale".
>
> I would like to have zsh execute something to refresh the SSH_AUTH_SOCK
> environment variable when I "tmux attach" to the aforementioned detached tmux
> session.
>
> Does anyone know of a way to register a callback or something for zsh so when a
> particular command gets run (in my case, tmux attach) my zsh environment
> variable can get updated?

When "tmux attach" is run, it'll be run by a different instance of zsh
to those within the tmux session. It is tmux's child processes that need
to receive a callback but I don't think there's any way for tmux to send
callbacks. Nor can I think of a particularly sane way that could be
implemented in tmux.

tmux does provide access to the environment for the current session so
you can run something like the following to import a variable into the
running zsh:

  eval $(tmux showenv -s SSH_AUTH_SOCK)

You might even set an alias, e.g:

  alias fixssh='eval $(tmux showenv -s)'

In the absence of a direct tmux callback, the easiest may be to detect a
stale ssh socket in an existing zsh callback function such as preexec().
There appears to be a TMUX environment variable which can be used to
detect a shell that is a a child of a tmux process. So something like
the following may work well enough:

if [[ $+TMUX ]]; then
  tmuxrefreshenv() {
    [[ -r $SSH_AUTH_SOCK ]] || eval $(tmux showenv -s)
  }
  add-zsh-hook preexec tmuxrefreshenv
fi

For something more direct than preexec, you could also try trapping
SIGWINCH. tmux will send this to child processes if the pane is resized
as a result of tmux attach - which is fairly likely. But it is also sent
when the pane is resized for other reasons and may not be sent if you're
consistent in your window sizing.

Any approach is going to have limitations - it won't pass through
subsequent layers of child processes using su, ssh and similar.

Another quite different approach would be to use the IdentityAgent
option in ~/.ssh/config to ensure a consistent path to the agent socket
so that the value remains valid instead of becoming stale.

Oliver


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-11-02  8:52 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-02  2:39 callback? Matt Zagrabelny
2020-11-02  8:52 ` callback? Oliver Kiddle

zsh-users

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/zsh-users

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 zsh-users zsh-users/ http://inbox.vuxu.org/zsh-users \
		zsh-users@zsh.org
	public-inbox-index zsh-users

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.users


code repositories for the project(s) associated with this inbox:

	https://git.vuxu.org/mirror/zsh/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git