zsh-workers
 help / color / mirror / code / Atom feed
* $PPID not updated when the PPID changes (parent killed)
@ 2021-05-16 15:24 Vincent Lefevre
  2021-05-16 18:37 ` Bart Schaefer
  0 siblings, 1 reply; 23+ messages in thread
From: Vincent Lefevre @ 2021-05-16 15:24 UTC (permalink / raw)
  To: zsh-workers

Consider the following script:

#!/usr/bin/env zsh
echo $ZSH_VERSION
f() { grep PPid /proc/$$/status; echo $PPID }
f
kill -9 $PPID
sleep 1
f

Under Linux, I get:

zira% sh -c ./tst.zsh; sleep 2
5.8
PPid:   39867
39867
zsh: killed     sh -c ./tst.zsh
PPid:   1
39867
zira% 

i.e. $PPID is not updated when the PPID changes, i.e. the parent
process is killed.

The zshparam(1) man page says:

  PPID <S>
      The process ID of the parent of the shell.  As for $$, the value
      indicates the parent of the original shell and does  not  change
      in subshells.

This implies that it should be updated when the process ID of the
parent of the shell changes.

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


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

* Re: $PPID not updated when the PPID changes (parent killed)
  2021-05-16 15:24 $PPID not updated when the PPID changes (parent killed) Vincent Lefevre
@ 2021-05-16 18:37 ` Bart Schaefer
  2021-05-17 20:26   ` Vincent Lefevre
  0 siblings, 1 reply; 23+ messages in thread
From: Bart Schaefer @ 2021-05-16 18:37 UTC (permalink / raw)
  To: Zsh hackers list

On Sun, May 16, 2021 at 8:24 AM Vincent Lefevre <vincent@vinc17.net> wrote:
>
> i.e. $PPID is not updated when the PPID changes, i.e. the parent
> process is killed.

$PPID means the ID of the process that spawned this one, not the
current value of getppid().

If you want the latter, it's $sysparams[ppid] (with the zsh/system
module loaded).  In fact the correct way to determine whether the
original parent has exited is to check (( PPID != sysparams[ppid] )).

We could enable a non-portable way to be alerted on parent exit using
prctl() now that HAVE_PRCTL is being checked by configure, but it
would be expensive to emulate in other cases.


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

* Re: $PPID not updated when the PPID changes (parent killed)
  2021-05-16 18:37 ` Bart Schaefer
@ 2021-05-17 20:26   ` Vincent Lefevre
  2021-05-17 21:00     ` Bart Schaefer
  0 siblings, 1 reply; 23+ messages in thread
From: Vincent Lefevre @ 2021-05-17 20:26 UTC (permalink / raw)
  To: zsh-workers

On 2021-05-16 11:37:15 -0700, Bart Schaefer wrote:
> On Sun, May 16, 2021 at 8:24 AM Vincent Lefevre <vincent@vinc17.net> wrote:
> >
> > i.e. $PPID is not updated when the PPID changes, i.e. the parent
> > process is killed.
> 
> $PPID means the ID of the process that spawned this one, not the
> current value of getppid().

OK, then I think that this should be clarified in the man pages.

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


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

* Re: $PPID not updated when the PPID changes (parent killed)
  2021-05-17 20:26   ` Vincent Lefevre
@ 2021-05-17 21:00     ` Bart Schaefer
  2021-05-17 22:27       ` Phil Pennock
  0 siblings, 1 reply; 23+ messages in thread
From: Bart Schaefer @ 2021-05-17 21:00 UTC (permalink / raw)
  To: Zsh hackers list

On Mon, May 17, 2021 at 1:28 PM Vincent Lefevre <vincent@vinc17.net> wrote:
>
> > $PPID means the ID of the process that spawned this one, not the
> > current value of getppid().
>
> OK, then I think that this should be clarified in the man pages.

I wonder if the phrase "As for $$," is part of the confusion here.  It
could be read to mean either "In the same manner as $$, ..." or as
"Conversely, $$ ..." (the former being the correct interpretation).

Still mulling over possible rewording (at the very least change "for"
to "with" in that phrase).  Suggestions that don't try to explain unix
process hierarchy management in detail, are welcome.


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

* Re: $PPID not updated when the PPID changes (parent killed)
  2021-05-17 21:00     ` Bart Schaefer
@ 2021-05-17 22:27       ` Phil Pennock
  2021-05-17 23:15         ` Bart Schaefer
  2021-05-18 15:56         ` $PPID not updated when the PPID changes (parent killed) Stephane Chazelas
  0 siblings, 2 replies; 23+ messages in thread
From: Phil Pennock @ 2021-05-17 22:27 UTC (permalink / raw)
  To: zsh-workers

On 2021-05-17 at 14:00 -0700, Bart Schaefer wrote:
> Still mulling over possible rewording (at the very least change "for"
> to "with" in that phrase).  Suggestions that don't try to explain unix
> process hierarchy management in detail, are welcome.

PPID <S>
  The initial parent process ID; that is, the process ID of the process
  which created this shell process, at the time that it did so.
  Just as for $$, the value is only set for the original shell and does
  not dynamically change for implicit subshells (as created for (list)
  and for pipelines).

I am unsure of the subshells to implicit subshells and the parenthetical
note.  It's an attempt to be clearer that "if you run zsh within zsh,
that's not a subshell, sure _that_ gets $$ and $PPID reset".

-Phil


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

* Re: $PPID not updated when the PPID changes (parent killed)
  2021-05-17 22:27       ` Phil Pennock
@ 2021-05-17 23:15         ` Bart Schaefer
  2021-05-18  0:30           ` Phil Pennock
                             ` (4 more replies)
  2021-05-18 15:56         ` $PPID not updated when the PPID changes (parent killed) Stephane Chazelas
  1 sibling, 5 replies; 23+ messages in thread
From: Bart Schaefer @ 2021-05-17 23:15 UTC (permalink / raw)
  To: Zsh hackers list

On Mon, May 17, 2021 at 3:27 PM Phil Pennock
<zsh-workers+phil.pennock@spodhuis.org> wrote:
>
>   The initial parent process ID; that is, the process ID of the process
>   which created this shell process, at the time that it did so.

This is pretty close to what I was thinking.  Does "at the time"
really add anything?

>   Just as for $$, the value is only set for the original shell and does
>   not dynamically change for implicit subshells (as created for (list)
>   and for pipelines).

Except (list) is an explicit subshell, isn't it?

> It's an attempt to be clearer that "if you run zsh within zsh,
> that's not a subshell, sure _that_ gets $$ and $PPID reset".

Indeed.  I'm tempted to say

  Just as with $$, the value is updated only within a new shell
  that increases $SHLVL, and not in other subshells.

And then clarify "a new shell" under SHLVL if necessary, but I hope it isn't.

Aside, it's a bit odd that SHLVL isn't read-only.  Even assorted Bash
tutorials found by online search treat it as immutable (statements
like "$dog is only available at SHLVL 2") despite that it can be
arbitrarily messed with.


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

* Re: $PPID not updated when the PPID changes (parent killed)
  2021-05-17 23:15         ` Bart Schaefer
@ 2021-05-18  0:30           ` Phil Pennock
  2021-05-18  1:14             ` Bart Schaefer
  2021-05-18  8:28             ` Vincent Lefevre
  2021-05-18  8:15           ` Vincent Lefevre
                             ` (3 subsequent siblings)
  4 siblings, 2 replies; 23+ messages in thread
From: Phil Pennock @ 2021-05-18  0:30 UTC (permalink / raw)
  To: zsh-workers

On 2021-05-17 at 16:15 -0700, Bart Schaefer wrote:
> Aside, it's a bit odd that SHLVL isn't read-only.

Needs to be something which can be unset.

At various points in things such as X11 run files, if SHLVL is still set
then newly launched terminal emulators would start with SHLVL=2, which
is misleading.

I don't recall which generations of changes in the flow (startx, xdm,
etc etc) have introduced what breakage but I recall that any time in
recent years that I've needed to customize X11 start-up, I've ended up
having to explicitly unset SHLVL somewhere.

Here we go, my `.xsessionrc` file has some cruft which for once is only
about 15 years old:

# Sometimes /bin/sh is a shell which sets $SHLVL
if [ ".$SHLVL" != "." ]; then
        echo >&2 "Warning: unsetting \$SHLVL: $SHLVL"
        unset SHLVL
# In addition, if the window manager is invoked via a script which is #!/bin/sh
# then it may be necessary to repair damage therein.  Eg, startxfce4,
# /etc/xdg/xfce4/xinitrc
fi



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

* Re: $PPID not updated when the PPID changes (parent killed)
  2021-05-18  0:30           ` Phil Pennock
@ 2021-05-18  1:14             ` Bart Schaefer
  2021-05-18  8:28             ` Vincent Lefevre
  1 sibling, 0 replies; 23+ messages in thread
From: Bart Schaefer @ 2021-05-18  1:14 UTC (permalink / raw)
  To: Zsh hackers list

On Mon, May 17, 2021 at 5:37 PM Phil Pennock
<zsh-workers+phil.pennock@spodhuis.org> wrote:
>
> On 2021-05-17 at 16:15 -0700, Bart Schaefer wrote:
> > Aside, it's a bit odd that SHLVL isn't read-only.
>
> Needs to be something which can be unset.

It just needs to be something that can be "un-export-ed", but I
suppose there's no way to specify that.


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

* Re: $PPID not updated when the PPID changes (parent killed)
  2021-05-17 23:15         ` Bart Schaefer
  2021-05-18  0:30           ` Phil Pennock
@ 2021-05-18  8:15           ` Vincent Lefevre
  2021-05-18 14:53             ` Bart Schaefer
  2021-05-18 15:18           ` Mikael Magnusson
                             ` (2 subsequent siblings)
  4 siblings, 1 reply; 23+ messages in thread
From: Vincent Lefevre @ 2021-05-18  8:15 UTC (permalink / raw)
  To: zsh-workers

On 2021-05-17 16:15:24 -0700, Bart Schaefer wrote:
> On Mon, May 17, 2021 at 3:27 PM Phil Pennock
> <zsh-workers+phil.pennock@spodhuis.org> wrote:
> >
> >   The initial parent process ID; that is, the process ID of the process
> >   which created this shell process, at the time that it did so.
> 
> This is pretty close to what I was thinking.  Does "at the time"
> really add anything?

It would be better without it.

> >   Just as for $$, the value is only set for the original shell and does
> >   not dynamically change for implicit subshells (as created for (list)
> >   and for pipelines).
> 
> Except (list) is an explicit subshell, isn't it?
> 
> > It's an attempt to be clearer that "if you run zsh within zsh,
> > that's not a subshell, sure _that_ gets $$ and $PPID reset".
> 
> Indeed.  I'm tempted to say
> 
>   Just as with $$, the value is updated only within a new shell
>   that increases $SHLVL, and not in other subshells.
> 
> And then clarify "a new shell" under SHLVL if necessary, but I hope it isn't.

The zsh man pages use "subshell" at various places. They should be
consistent on what a subshell is, perhaps have a clear definition
at one place, and say what happens when a subshell is created.

Note also that the following in the zshmodules(1) man page should be
updated too:

  sysparams
      A readonly associative array.  The keys are:
[...]
      ppid   Returns  the  process  ID  of  the  parent of the current
             process, even in subshells.  Compare $PPID, which returns
             the process ID of the parent of the main shell process.

This is even more confusing, because there are actually 2 differences
between $sysparams[ppid] and $PPID:

1. The fact that $sysparams[ppid] takes a new value in subshells, but
   not $PPID.

2. The fact that $sysparams[ppid] is updated when the parent process
   terminates, but not $PPID.

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


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

* Re: $PPID not updated when the PPID changes (parent killed)
  2021-05-18  0:30           ` Phil Pennock
  2021-05-18  1:14             ` Bart Schaefer
@ 2021-05-18  8:28             ` Vincent Lefevre
  1 sibling, 0 replies; 23+ messages in thread
From: Vincent Lefevre @ 2021-05-18  8:28 UTC (permalink / raw)
  To: zsh-workers

On 2021-05-17 20:30:06 -0400, Phil Pennock wrote:
> Here we go, my `.xsessionrc` file has some cruft which for once is only
> about 15 years old:
> 
> # Sometimes /bin/sh is a shell which sets $SHLVL
> if [ ".$SHLVL" != "." ]; then
>         echo >&2 "Warning: unsetting \$SHLVL: $SHLVL"
>         unset SHLVL
> # In addition, if the window manager is invoked via a script which is #!/bin/sh
> # then it may be necessary to repair damage therein.  Eg, startxfce4,
> # /etc/xdg/xfce4/xinitrc
> fi

My window manager is started by a zsh script (mainly for ssh-agent
handling), so that I had to do "unset SHLVL". I suppose that I could
have used "env -u SHLVL ...", but "unset SHLVL" is simpler.

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


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

* Re: $PPID not updated when the PPID changes (parent killed)
  2021-05-18  8:15           ` Vincent Lefevre
@ 2021-05-18 14:53             ` Bart Schaefer
  2021-05-19  4:25               ` Bart Schaefer
  0 siblings, 1 reply; 23+ messages in thread
From: Bart Schaefer @ 2021-05-18 14:53 UTC (permalink / raw)
  To: Zsh hackers list

On Tue, May 18, 2021 at 1:17 AM Vincent Lefevre <vincent@vinc17.net> wrote:
>
> The zsh man pages use "subshell" at various places. They should be
> consistent on what a subshell is, perhaps have a clear definition

I believe that we are consistent in the usage of subshell in the docs,
but the wider world sometimes uses subshell to refer to any shell
started by another, e.g. in
  zsh -c 'bash -c "echo some people call me a subshell"'

> at one place, and say what happens when a subshell is created.

Frankly this is probably unhelpful except to people who want to make
RTFM references.  If "subshell" isn't a sufficiently understandable
term in isolation, nobody who isn't deliberately reading through the
entire manual is going to go looking for a definition when seeing
"subshell" in some other context.

> This is even more confusing, because there are actually 2 differences

It's only confusing because you're making yourself confused.

PPID is not a special variable.  (Something for which we DO have an
explicit definition).  That means the only way it changes is when
assigned to or unset.  It's been this way for 30 years and nobody ever
mentioned expecting it to change or being confused by that before you
in this thread.

sysparams[ppid] is explicitly a readonly API on a system call.  It
should be obvious from that that it changes if the value of the system
call would change.  It's been this way for 20-something years and
again no complaints.

It can't become necessary to spell out all the side effects of this
type of thing in every possible situation, it's just untenable.


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

* Re: $PPID not updated when the PPID changes (parent killed)
  2021-05-17 23:15         ` Bart Schaefer
  2021-05-18  0:30           ` Phil Pennock
  2021-05-18  8:15           ` Vincent Lefevre
@ 2021-05-18 15:18           ` Mikael Magnusson
  2021-05-18 16:14             ` Peter Stephenson
  2021-05-18 17:37           ` Martijn Dekker
  2021-05-18 18:06           ` Stephane Chazelas
  4 siblings, 1 reply; 23+ messages in thread
From: Mikael Magnusson @ 2021-05-18 15:18 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh hackers list

On 5/18/21, Bart Schaefer <schaefer@brasslantern.com> wrote:

> Aside, it's a bit odd that SHLVL isn't read-only.  Even assorted Bash
> tutorials found by online search treat it as immutable (statements
> like "$dog is only available at SHLVL 2") despite that it can be
> arbitrarily messed with.

I sometimes set it (in practice only to the empty string), such that
the first shell in a new terminal always ends up with SHLVL=1 (my
prompt hilights when it is non-1 so that i know i can safely exit
without having the terminal be closed, eg while testing new zsh builds
or other things where i want a temporary shell state). I also do some
window manager development so things like "SHLVL= openbox --replace"
tend to happen as well.

-- 
Mikael Magnusson


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

* Re: $PPID not updated when the PPID changes (parent killed)
  2021-05-17 22:27       ` Phil Pennock
  2021-05-17 23:15         ` Bart Schaefer
@ 2021-05-18 15:56         ` Stephane Chazelas
  1 sibling, 0 replies; 23+ messages in thread
From: Stephane Chazelas @ 2021-05-18 15:56 UTC (permalink / raw)
  To: zsh-workers

2021-05-17 18:27:00 -0400, Phil Pennock:
> On 2021-05-17 at 14:00 -0700, Bart Schaefer wrote:
> > Still mulling over possible rewording (at the very least change "for"
> > to "with" in that phrase).  Suggestions that don't try to explain unix
> > process hierarchy management in detail, are welcome.
> 
> PPID <S>
>   The initial parent process ID; that is, the process ID of the process
>   which created this shell process, at the time that it did so.
>   Just as for $$, the value is only set for the original shell and does
>   not dynamically change for implicit subshells (as created for (list)
>   and for pipelines).
[...]


processes don't really create shell processes. A process may or
may  not fork itself, and then, maybe at a later point in
either the parent or the child (generally the child) execute a
shell (and generally, the parent will be waiting for that child).

For PPID, POSIX specification of sh says:

> Set by the shell to the decimal value of its parent process ID
> during initialization of the shell

Which is more accurate and clearer than any wording I can think
of. Note that by the time the shell "initialises" (where it
calls getppid() to fill in $PPID), the process that did fork the
process that executed the shell ($$) may alread be gone.

As in:

$ ( (sleep 1; exec zsh -c 'print $PPID') &)
1

I kind of agree with Vincent that it would be more useful if
$PPID reported the realtime value of the parent pid, but no
other shell does it and that would break POSIX compliance, and we
already have sysparam[ppid] as already noted.

-- 
Stephane


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

* Re: $PPID not updated when the PPID changes (parent killed)
  2021-05-18 15:18           ` Mikael Magnusson
@ 2021-05-18 16:14             ` Peter Stephenson
  2021-05-18 18:20               ` Bart Schaefer
  0 siblings, 1 reply; 23+ messages in thread
From: Peter Stephenson @ 2021-05-18 16:14 UTC (permalink / raw)
  To: Zsh hackers list

On 18 May 2021 at 16:18 Mikael Magnusson <mikachu@gmail.com> wrote:
> On 5/18/21, Bart Schaefer <schaefer@brasslantern.com> wrote:
> > Aside, it's a bit odd that SHLVL isn't read-only.  Even assorted Bash
> > tutorials found by online search treat it as immutable (statements
> > like "$dog is only available at SHLVL 2") despite that it can be
> > arbitrarily messed with.
> 
> I sometimes set it (in practice only to the empty string), such that
> the first shell in a new terminal always ends up with SHLVL=1 (my
> prompt hilights when it is non-1 so that i know i can safely exit
> without having the terminal be closed, eg while testing new zsh builds
> or other things where i want a temporary shell state). I also do some
> window manager development so things like "SHLVL= openbox --replace"
> tend to happen as well.

I do similar stuff to the extent of resetting SHLVL to zero, but I think
there is actually a logical argument for being able to adjust it, i.e.
set it rather than reset it, if you happen to know that's going to have a
useful effect.

For example, if SHLVL=1 before a terminal emulator starts because there's
a shell that spawned the window initialisation (this used to be a lot
more common way back when), then it's 2 in your terminal.  It would
be quite natural to start a new terminal (actually forked from the
current one, but logically at the same level as far as the user is
concerned) prefixed with:

SHLVL=$(( SHLVL - 1 ))

Then your init files in the new terminal behave as they do in the
one you're in.

I'm certainly not saying this is the absolute one true way of doing
things, only that I don't think it should be excluded.  It's fairly
low level; I think it's a case of giving enough rope etc.  I don't
think we have any expectation of or guidance on what people do with this
anyway.

pws


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

* Re: $PPID not updated when the PPID changes (parent killed)
  2021-05-17 23:15         ` Bart Schaefer
                             ` (2 preceding siblings ...)
  2021-05-18 15:18           ` Mikael Magnusson
@ 2021-05-18 17:37           ` Martijn Dekker
  2021-05-18 18:08             ` Bart Schaefer
  2021-05-18 18:06           ` Stephane Chazelas
  4 siblings, 1 reply; 23+ messages in thread
From: Martijn Dekker @ 2021-05-18 17:37 UTC (permalink / raw)
  To: zsh-workers

Op 18-05-21 om 01:15 schreef Bart Schaefer:
> Aside, it's a bit odd that SHLVL isn't read-only.

SHLVL is increased even when you do something like 'exec zsh' to replace 
your current shell with a new zsh invocation which is not actually a 
child shell. If you make SHLVL read-only, then it becomes impossible to 
correct that.

-- 
||	modernish -- harness the shell
||	https://github.com/modernish/modernish
||
||	KornShell lives!
||	https://github.com/ksh93/ksh


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

* Re: $PPID not updated when the PPID changes (parent killed)
  2021-05-17 23:15         ` Bart Schaefer
                             ` (3 preceding siblings ...)
  2021-05-18 17:37           ` Martijn Dekker
@ 2021-05-18 18:06           ` Stephane Chazelas
  2021-05-18 18:12             ` Bart Schaefer
  4 siblings, 1 reply; 23+ messages in thread
From: Stephane Chazelas @ 2021-05-18 18:06 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh hackers list

2021-05-17 16:15:24 -0700, Bart Schaefer:
[...]
> Indeed.  I'm tempted to say
> 
>   Just as with $$, the value is updated only within a new shell
>   that increases $SHLVL, and not in other subshells.
[...]

$ (SHLVL=0; zsh -c 'echo $SHLVL $$ $PPID; sleep 2; exec zsh -c "echo \$SHLVL \$$ \$PPID"' & sleep 1)
1 485756 485755
1 485756 302910

(where in that case 302910 is the pid of the systemd user's
session child subreaper, traditionally, one would get 1
instead).

$PPID, like $$ are set when the shell is initialised, just after
it had been executed.

In the example above, the same 485756 process executed zsh twice
and $$ and $PPID were set twice.

Because of the exec (and it would have been the same without
exec in a few contexts where zsh skips forks implicitly), $SHLVL
is not incremented (well it is decremented by the first zsh
before exec and incremented again by the second).

$SHLVL (a (t)csh thing IIRC, not handled by all shells) is the
number of shell *invocations* you need to exit before that logs
you out.

I would find it much clearer if we described $$/$PPID in
relation to shell invocation initialisation like POSIX does.

-- 
Stephane



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

* Re: $PPID not updated when the PPID changes (parent killed)
  2021-05-18 17:37           ` Martijn Dekker
@ 2021-05-18 18:08             ` Bart Schaefer
  2021-05-18 19:40               ` Martijn Dekker
  0 siblings, 1 reply; 23+ messages in thread
From: Bart Schaefer @ 2021-05-18 18:08 UTC (permalink / raw)
  To: Martijn Dekker; +Cc: Zsh hackers list

On Tue, May 18, 2021 at 10:37 AM Martijn Dekker <martijn@inlv.org> wrote:
>
> Op 18-05-21 om 01:15 schreef Bart Schaefer:
> > Aside, it's a bit odd that SHLVL isn't read-only.
>
> SHLVL is increased even when you do something like 'exec zsh'

Actually no, it isn't.

% echo $SHLVL && exec Src/zsh -fc 'echo $SHLVL'
2
2


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

* Re: $PPID not updated when the PPID changes (parent killed)
  2021-05-18 18:06           ` Stephane Chazelas
@ 2021-05-18 18:12             ` Bart Schaefer
  2021-05-18 18:50               ` $SHLVL origin (was: $PPID not updated when the PPID changes (parent killed)) Stephane Chazelas
  0 siblings, 1 reply; 23+ messages in thread
From: Bart Schaefer @ 2021-05-18 18:12 UTC (permalink / raw)
  To: Bart Schaefer, Zsh hackers list

On Tue, May 18, 2021 at 11:06 AM Stephane Chazelas
<stephane@chazelas.org> wrote:
>
> $SHLVL (a (t)csh thing IIRC, not handled by all shells)

Bash has it, for sure.

> I would find it much clearer if we described $$/$PPID in
> relation to shell invocation initialisation like POSIX does.

I'm tending to agree.


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

* Re: $PPID not updated when the PPID changes (parent killed)
  2021-05-18 16:14             ` Peter Stephenson
@ 2021-05-18 18:20               ` Bart Schaefer
  0 siblings, 0 replies; 23+ messages in thread
From: Bart Schaefer @ 2021-05-18 18:20 UTC (permalink / raw)
  To: Zsh hackers list

On Tue, May 18, 2021 at 9:15 AM Peter Stephenson
<p.w.stephenson@ntlworld.com> wrote:
>
> I don't
> think we have any expectation of or guidance on what people do with
> [SHLVL] anyway.

I didn't have any expectation that my "aside" would take over this thread!


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

* $SHLVL origin (was: $PPID not updated when the PPID changes (parent killed))
  2021-05-18 18:12             ` Bart Schaefer
@ 2021-05-18 18:50               ` Stephane Chazelas
  0 siblings, 0 replies; 23+ messages in thread
From: Stephane Chazelas @ 2021-05-18 18:50 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh hackers list

2021-05-18 11:12:54 -0700, Bart Schaefer:
> On Tue, May 18, 2021 at 11:06 AM Stephane Chazelas
> <stephane@chazelas.org> wrote:
> >
> > $SHLVL (a (t)csh thing IIRC, not handled by all shells)
> 
> Bash has it, for sure.
[...]

I remember looking up the origin of it when we were discussing
https://www.zsh.org/mla/workers/2016/msg01574.html

It does come from tcsh in the early 80s (see
https://groups.google.com/forum/message/raw?msg=net.sources/3eER9GEdo2c/B9G1KGziIx4J
for instance).

Also supported by bash, ksh93, fish, zsh and yash (implement in
startup file as shell code only for interactive instances
there), but not much else and not in POSIX.

-- 
Stephane


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

* Re: $PPID not updated when the PPID changes (parent killed)
  2021-05-18 18:08             ` Bart Schaefer
@ 2021-05-18 19:40               ` Martijn Dekker
  0 siblings, 0 replies; 23+ messages in thread
From: Martijn Dekker @ 2021-05-18 19:40 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh hackers list

Op 18-05-21 om 20:08 schreef Bart Schaefer:
> On Tue, May 18, 2021 at 10:37 AM Martijn Dekker <martijn@inlv.org> wrote:
>>
>> Op 18-05-21 om 01:15 schreef Bart Schaefer:
>>> Aside, it's a bit odd that SHLVL isn't read-only.
>>
>> SHLVL is increased even when you do something like 'exec zsh'
> 
> Actually no, it isn't.
> 
> % echo $SHLVL && exec Src/zsh -fc 'echo $SHLVL'
> 2
> 2

I see.

Or, actually, zsh does increase it, but also decreases it when doing an 
'exec'. Not all shells do the latter. What I got from ksh93 was:

$ ksh
$ echo $SHLVL
2
$ exec zsh -fc 'echo $SHLVL'
3

Since I now maintain a ksh93 fork, maybe I should fix its 'exec' to act 
like zsh. It seems like the sensible thing to do.

-- 
||	modernish -- harness the shell
||	https://github.com/modernish/modernish
||
||	KornShell lives!
||	https://github.com/ksh93/ksh


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

* Re: $PPID not updated when the PPID changes (parent killed)
  2021-05-18 14:53             ` Bart Schaefer
@ 2021-05-19  4:25               ` Bart Schaefer
  2021-07-18 23:10                 ` Lawrence Velázquez
  0 siblings, 1 reply; 23+ messages in thread
From: Bart Schaefer @ 2021-05-19  4:25 UTC (permalink / raw)
  To: Zsh hackers list

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

On Tue, May 18, 2021 at 7:53 AM Bart Schaefer <schaefer@brasslantern.com> wrote
>
> PPID is not a special variable.

Well, I have to take that back; it IS documented as special, but only
because it's readonly (which it also is in e.g. bash).

Anyway, as per Stephane Chazelas,
> POSIX specification of sh says:
>
> > Set by the shell to the decimal value of its parent process ID
> > during initialization of the shell
>
> [...] it would be more useful if
> $PPID reported the realtime value of the parent pid, but no
> other shell does it and that would break POSIX compliance, and we
> already have sysparam[ppid] as already noted.

POSIX doesn't say whether it should be readonly, as far as I can tell.
It is not readonly when bash is invoked as "sh" but remains readonly
in zsh's sh emulation, which might be considered a bug.

So ... hopefully the attached patch addresses the assorted issues raised.

[-- Attachment #2: PPID.txt --]
[-- Type: text/plain, Size: 2403 bytes --]

diff --git a/Doc/Zsh/params.yo b/Doc/Zsh/params.yo
index dc28a45ae..b514eb072 100644
--- a/Doc/Zsh/params.yo
+++ b/Doc/Zsh/params.yo
@@ -654,10 +654,11 @@ Same as tt(#).
 )
 vindex($)
 item(tt($) <S>)(
-The process ID of this shell.  Note that this indicates the original
-shell started by invoking tt(zsh); all processes forked from the shells
-without executing a new program, such as subshells started by
-tt(LPAR())var(...)tt(RPAR()), substitute the same value.
+The process ID of this shell, set when the shell initializes.  Processes
+forked from the shell without executing a new program, such as command
+substitutions and commands grouped with tt(LPAR())var(...)tt(RPAR()),
+are subshells that duplicate the current shell, and thus substitute the
+same value for tt($$) as their parent shell.
 )
 vindex(-)
 item(tt(-) <S>)(
@@ -817,9 +818,9 @@ The operating system, as determined at compile time.
 )
 vindex(PPID)
 item(tt(PPID) <S>)(
-The process ID of the parent of the shell.  As for tt($$), the
-value indicates the parent of the original shell and does not
-change in subshells.
+The process ID of the parent of the shell, set when the shell initializes.
+As with tt($$), the value does not change in subshells created as a
+duplicate of the current shell.
 )
 vindex(PWD)
 item(tt(PWD))(
diff --git a/Test/E03posix.ztst b/Test/E03posix.ztst
index 7db4c0c84..564afac29 100644
--- a/Test/E03posix.ztst
+++ b/Test/E03posix.ztst
@@ -161,3 +161,6 @@ F:POSIX has neither math functions nor floating point
 0f:Width of %s is computed in bytes not characters
 F:This is considered a bugfix in zsh
 ><  Stéphane>
+
+  PPID=foo
+-f:PPID is not a readonly variable
diff --git a/Doc/Zsh/mod_system.yo b/Doc/Zsh/mod_system.yo
index 8f525c576..399b6fe03 100644
--- a/Doc/Zsh/mod_system.yo
+++ b/Doc/Zsh/mod_system.yo
@@ -263,9 +263,9 @@ tt($$), which returns the process ID of the main shell process.
 )
 item(tt(ppid))(
 vindex(ppid, sysparams)
-Returns the process ID of the parent of the current process, even in
-subshells.  Compare tt($PPID), which returns the process ID of the parent
-of the main shell process.
+Returns the current process ID of the parent of the current process, even
+in subshells.  Compare tt($PPID), which returns the process ID of the
+initial parent of the main shell process.
 )
 item(tt(procsubstpid))(
 Returns the process ID of the last process started for process

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

* Re: $PPID not updated when the PPID changes (parent killed)
  2021-05-19  4:25               ` Bart Schaefer
@ 2021-07-18 23:10                 ` Lawrence Velázquez
  0 siblings, 0 replies; 23+ messages in thread
From: Lawrence Velázquez @ 2021-07-18 23:10 UTC (permalink / raw)
  To: zsh-workers

On Wed, May 19, 2021, at 12:25 AM, Bart Schaefer wrote:
> So ... hopefully the attached patch addresses the assorted issues raised.
> 
> Attachments:
> * PPID.txt

Anyone else have feedback on this (48888), or is it ready to go?

-- 
vq


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

end of thread, other threads:[~2021-07-18 23:10 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-16 15:24 $PPID not updated when the PPID changes (parent killed) Vincent Lefevre
2021-05-16 18:37 ` Bart Schaefer
2021-05-17 20:26   ` Vincent Lefevre
2021-05-17 21:00     ` Bart Schaefer
2021-05-17 22:27       ` Phil Pennock
2021-05-17 23:15         ` Bart Schaefer
2021-05-18  0:30           ` Phil Pennock
2021-05-18  1:14             ` Bart Schaefer
2021-05-18  8:28             ` Vincent Lefevre
2021-05-18  8:15           ` Vincent Lefevre
2021-05-18 14:53             ` Bart Schaefer
2021-05-19  4:25               ` Bart Schaefer
2021-07-18 23:10                 ` Lawrence Velázquez
2021-05-18 15:18           ` Mikael Magnusson
2021-05-18 16:14             ` Peter Stephenson
2021-05-18 18:20               ` Bart Schaefer
2021-05-18 17:37           ` Martijn Dekker
2021-05-18 18:08             ` Bart Schaefer
2021-05-18 19:40               ` Martijn Dekker
2021-05-18 18:06           ` Stephane Chazelas
2021-05-18 18:12             ` Bart Schaefer
2021-05-18 18:50               ` $SHLVL origin (was: $PPID not updated when the PPID changes (parent killed)) Stephane Chazelas
2021-05-18 15:56         ` $PPID not updated when the PPID changes (parent killed) Stephane Chazelas

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).