develooper Front page | perl.perl5.porters | Postings from December 2016

Re: [perl #121105] During a system(), unquoted Perl vars areevaluated _after_ the fork() call

Thread Previous | Thread Next
From:
Leon Timmermans
Date:
December 19, 2016 15:10
Subject:
Re: [perl #121105] During a system(), unquoted Perl vars areevaluated _after_ the fork() call
Message ID:
CAHhgV8gRQXyTsaF79SqP2XW4nyC0GSwcKw2A8ndcbf60DSHuzQ@mail.gmail.com
On Thu, Jan 30, 2014 at 4:00 PM, Dave Mitchell <davem@iabyn.com> wrote:

> On Thu, Jan 30, 2014 at 01:53:35PM -0000, Father Chrysostomos wrote:
> > Gian Piero Carrubba wrote:
> > > $ perl -l
> > > print $$;
> > > system 'echo', "$$";
> > > system 'echo', $$;
> > > 32480
> > > 32480
> > > 32488
> > >
> > > The last pid printed (from the system() with unquoted $$) is the pid of
> > > the child process, not the one of the parent.
> >
> > I think this is related to #104084.
>
> It bisects to
>
>     commit 0e21945565eb4664d843bb819fb032cedee4d5a6
>     Author: Max Maischein <corion@corion.net>
>     Date:   Sun May 22 21:36:57 2011 -0700
>
>     Turn $$ into a magical readonly variable that always fetches getpid()
> instead of caching it
>
>     The intent is that by not caching $$, we eliminate one opportunity for
> bugs:
>     If one embeds Perl or uses XS and calls fork(3) from C, Perls notion
> of $$
>     may go out of sync with what getpid() returns. By always fetching the
>     value of $$ via getpid(), this bug opportunity is eliminated. The
> overhead
>     of always fetching $$ should be small and is likely only used for
> tempfile
>     creation, which should be dwarfed by file system accesses.
>
> Maybe system() should call get magic on its args *before* forking?
>

The simplest solution would be to just create mortal copies of the stack in
a temporary array.

Though to be honest, that function has been a mess for a long time and the
recent Amigo work made it even worse. What we really want is to split out
much of that logic into a sensible interface, and that interface should
probably be do_aspawn and do_spawn.

Leon

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About