zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.stephenson@samsung.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Re: How to detect that Zsh startup is result of exec zsh?
Date: Tue, 13 Sep 2016 14:30:42 +0100	[thread overview]
Message-ID: <20160913143042.3907c2b3@pwslap01u.europe.root.pri> (raw)
In-Reply-To: <CAKc7PVAGqeJK=kdpQ9hSBcgpJmn639X94BU0CoNR2m6L2VBfcg@mail.gmail.com>

On Tue, 13 Sep 2016 15:19:03 +0200
Sebastian Gniazdowski <sgniazdowski@gmail.com> wrote:
> I'm assigning unique ID to every Zsh. If one runs Zsh in side Zsh,
> either by exec or by new Zsh, the ID is inherited. However, there's
> Tmux. There, ID is not inherited in both cases – person opening new
> pane gets new Zsh, with new ID. But the exec case could still inherit
> ID – replacing Zsh inside Tmux is fine to inherit ID. That's why I
> need to differentiate the two

But that's not a difference between the two case in a subshell.  In both

(exec zsh)
(zsh)

the parent shell still exists.  The exec in a subshell doesn't replace
the parent shell, unlike exec in the parent shell itself.

So what you actually need to distinguish is the existence of the
subshell.

You can do that by storng the PID of each process and associating it
with the ID.  If the PID is the same as that of the parent shell (which
is what happens with exec in the parent shell) the ID can stay the same;
in any other case it will be different.  This works generally,
regardless of any fork / exec behaviour, with the usual proviso that
once the PID has exited the number my be reused.

It sounds like you might be in danger of reniventing UNIX process
management...

BTW, telling you're in a subshell when you're still in the parent shell
is actually easy:

zmodload zsh/system
if (( $$ ==  $sysparams[pid] )); then
  print "I'm in the parent shell"
else
  print "I'm in a subshell"
fi

However, that's too early for you --- you need to know at the start of
the new zsh.  I think some external mapping to PIDs is the only reliable
way.

pws


  reply	other threads:[~2016-09-13 13:30 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20160913122504eucas1p121867b112b7f08e125a324230d8296df@eucas1p1.samsung.com>
2016-09-13 12:23 ` Sebastian Gniazdowski
2016-09-13 13:06   ` Peter Stephenson
2016-09-13 13:19     ` Sebastian Gniazdowski
2016-09-13 13:30       ` Peter Stephenson [this message]
2016-09-15  6:31         ` Sebastian Gniazdowski
2016-09-15 17:54           ` Lawrence Velázquez
2016-09-15 18:02             ` Sebastian Gniazdowski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160913143042.3907c2b3@pwslap01u.europe.root.pri \
    --to=p.stephenson@samsung.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).