Re: bin/12578: `` subshell taints PWD

1999-07-14 Thread Oliver Fromme
Niall Smart wrote in list.freebsd-hackers:
 > 
 > > I'm not sure if XPG4v2 requires command substitution to behave
 > > like that.  At least, both Solaris' and DEC UNIX... oops...
 > > True64 UNIX do execute all command substitutions in a subshell
 > > (`pwd` does not affect the surrounding shell), and both claim
 > > XPG4 compliance.
 > 
 > They only execute a subshell when they need to:

No, it _always_ spawns a subshell.

 > $ echo $$ `echo $$`
 > 14405 14405
 > $ uname -a
 > SunOS molotov.boi.ie 5.6 Generic_105181-05 sun4u sparc SUNW,Ultra-5_10

That doesn't prove anything, because $$ always contains the
PID of the "top-level" shell, as I explained in an earlier
mail.  Try this one:

$ echo $$ `( /bin/echo $$ )`
14762 14762

I think you agree that a subshell is spawned in that case,
don't you?  ;-)

Regards
   Oliver

-- 
Oliver Fromme, Leibnizstr. 18/61, 38678 Clausthal, Germany
(Info: finger userinfo:o...@dorifer.heim3.tu-clausthal.de)

"In jedem Stück Kohle wartet ein Diamant auf seine Geburt"
 (Terry Pratchett)


To Unsubscribe: send mail to majord...@freebsd.org
with "unsubscribe freebsd-hackers" in the body of the message



Re: bin/12578: `` subshell taints PWD

1999-07-14 Thread Oliver Fromme

Niall Smart wrote in list.freebsd-hackers:
 > 
 > > I'm not sure if XPG4v2 requires command substitution to behave
 > > like that.  At least, both Solaris' and DEC UNIX... oops...
 > > True64 UNIX do execute all command substitutions in a subshell
 > > (`pwd` does not affect the surrounding shell), and both claim
 > > XPG4 compliance.
 > 
 > They only execute a subshell when they need to:

No, it _always_ spawns a subshell.

 > $ echo $$ `echo $$`
 > 14405 14405
 > $ uname -a
 > SunOS molotov.boi.ie 5.6 Generic_105181-05 sun4u sparc SUNW,Ultra-5_10

That doesn't prove anything, because $$ always contains the
PID of the "top-level" shell, as I explained in an earlier
mail.  Try this one:

$ echo $$ `( /bin/echo $$ )`
14762 14762

I think you agree that a subshell is spawned in that case,
don't you?  ;-)

Regards
   Oliver

-- 
Oliver Fromme, Leibnizstr. 18/61, 38678 Clausthal, Germany
(Info: finger userinfo:[EMAIL PROTECTED])

"In jedem Stück Kohle wartet ein Diamant auf seine Geburt"
 (Terry Pratchett)


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: bin/12578: `` subshell taints PWD

1999-07-14 Thread Niall Smart

> I'm not sure if XPG4v2 requires command substitution to behave
> like that.  At least, both Solaris' and DEC UNIX... oops...
> True64 UNIX do execute all command substitutions in a subshell
> (`pwd` does not affect the surrounding shell), and both claim
> XPG4 compliance.

They only execute a subshell when they need to:

$ echo $$ `echo $$`
14405 14405
$ uname -a
SunOS molotov.boi.ie 5.6 Generic_105181-05 sun4u sparc SUNW,Ultra-5_10

Regards,

Niall


To Unsubscribe: send mail to majord...@freebsd.org
with "unsubscribe freebsd-hackers" in the body of the message



Re: bin/12578: `` subshell taints PWD

1999-07-14 Thread Niall Smart


> I'm not sure if XPG4v2 requires command substitution to behave
> like that.  At least, both Solaris' and DEC UNIX... oops...
> True64 UNIX do execute all command substitutions in a subshell
> (`pwd` does not affect the surrounding shell), and both claim
> XPG4 compliance.

They only execute a subshell when they need to:

$ echo $$ `echo $$`
14405 14405
$ uname -a
SunOS molotov.boi.ie 5.6 Generic_105181-05 sun4u sparc SUNW,Ultra-5_10

Regards,

Niall


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: bin/12578: `` subshell taints PWD

1999-07-13 Thread Doug
On Tue, 13 Jul 1999, Oliver Fromme wrote:

>  > But isn't that exactly what's happening here, where PWD is being tainted
>  > by the commands evaluated within the substitution?
> 
> Yes, I'd call that a bug which should be fixed.
> The manpage clearly says:
> 
> "The shell expands the command substitution by executing
> command in a subshell environment and replacing the command
> substitution with the standard output of the command [...]"
> 
> Alternatively, the manpage could be "fixed".  ;-)

The correct way to fix the problem is to bring our sh in line with
posix in this respect. Someone more familiar with the spec than I could
tell you for sure, however I can say with relative security that subshell
processes should not taint parent shell variables.

Exercising my firm grasp of the obvious,

Doug
-- 
On account of being a democracy and run by the people, we are the only
nation in the world that has to keep a government four years, no matter
what it does.
-- Will Rogers



To Unsubscribe: send mail to majord...@freebsd.org
with "unsubscribe freebsd-hackers" in the body of the message



Re: bin/12578: `` subshell taints PWD

1999-07-13 Thread Oliver Fromme
Sheldon Hearn wrote in list.freebsd-hackers:
 > On Tue, 13 Jul 1999 18:13:42 +0200, Oliver Fromme wrote:
 > 
 > > Command substitution certainly has to spawn a subshell, even
 > > for built-in commands, because otherwise you could modify
 > > parent shell variables within command substitutions.
 > 
 > But isn't that exactly what's happening here, where PWD is being tainted
 > by the commands evaluated within the substitution?

Yes, I'd call that a bug which should be fixed.
The manpage clearly says:

"The shell expands the command substitution by executing
command in a subshell environment and replacing the command
substitution with the standard output of the command [...]"

Alternatively, the manpage could be "fixed".  ;-)

I'm not sure if XPG4v2 requires command substitution to behave
like that.  At least, both Solaris' and DEC UNIX... oops...
True64 UNIX do execute all command substitutions in a subshell
(`pwd` does not affect the surrounding shell), and both claim
XPG4 compliance.

Therefore I think the right thing to do is to fix FreeBSD's
sh to always execute command substitutions in a subshell.

Regards
   Oliver

-- 
Oliver Fromme, Leibnizstr. 18/61, 38678 Clausthal, Germany
(Info: finger userinfo:o...@dorifer.heim3.tu-clausthal.de)

"In jedem Stück Kohle wartet ein Diamant auf seine Geburt"
 (Terry Pratchett)


To Unsubscribe: send mail to majord...@freebsd.org
with "unsubscribe freebsd-hackers" in the body of the message



Re: bin/12578: `` subshell taints PWD

1999-07-13 Thread Doug

On Tue, 13 Jul 1999, Oliver Fromme wrote:

>  > But isn't that exactly what's happening here, where PWD is being tainted
>  > by the commands evaluated within the substitution?
> 
> Yes, I'd call that a bug which should be fixed.
> The manpage clearly says:
> 
> "The shell expands the command substitution by executing
> command in a subshell environment and replacing the command
> substitution with the standard output of the command [...]"
> 
> Alternatively, the manpage could be "fixed".  ;-)

The correct way to fix the problem is to bring our sh in line with
posix in this respect. Someone more familiar with the spec than I could
tell you for sure, however I can say with relative security that subshell
processes should not taint parent shell variables.

Exercising my firm grasp of the obvious,

Doug
-- 
On account of being a democracy and run by the people, we are the only
nation in the world that has to keep a government four years, no matter
what it does.
-- Will Rogers



To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: bin/12578: `` subshell taints PWD

1999-07-13 Thread Oliver Fromme

Sheldon Hearn wrote in list.freebsd-hackers:
 > On Tue, 13 Jul 1999 18:13:42 +0200, Oliver Fromme wrote:
 > 
 > > Command substitution certainly has to spawn a subshell, even
 > > for built-in commands, because otherwise you could modify
 > > parent shell variables within command substitutions.
 > 
 > But isn't that exactly what's happening here, where PWD is being tainted
 > by the commands evaluated within the substitution?

Yes, I'd call that a bug which should be fixed.
The manpage clearly says:

"The shell expands the command substitution by executing
command in a subshell environment and replacing the command
substitution with the standard output of the command [...]"

Alternatively, the manpage could be "fixed".  ;-)

I'm not sure if XPG4v2 requires command substitution to behave
like that.  At least, both Solaris' and DEC UNIX... oops...
True64 UNIX do execute all command substitutions in a subshell
(`pwd` does not affect the surrounding shell), and both claim
XPG4 compliance.

Therefore I think the right thing to do is to fix FreeBSD's
sh to always execute command substitutions in a subshell.

Regards
   Oliver

-- 
Oliver Fromme, Leibnizstr. 18/61, 38678 Clausthal, Germany
(Info: finger userinfo:[EMAIL PROTECTED])

"In jedem Stück Kohle wartet ein Diamant auf seine Geburt"
 (Terry Pratchett)


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: bin/12578: `` subshell taints PWD

1999-07-13 Thread Sheldon Hearn


On Tue, 13 Jul 1999 18:13:42 +0200, Oliver Fromme wrote:

> Command substitution certainly has to spawn a subshell, even
> for built-in commands, because otherwise you could modify
> parent shell variables within command substitutions.

But isn't that exactly what's happening here, where PWD is being tainted
by the commands evaluated within the substitution?

Ciao,
Sheldon.


To Unsubscribe: send mail to majord...@freebsd.org
with "unsubscribe freebsd-hackers" in the body of the message



Re: bin/12578: `` subshell taints PWD

1999-07-13 Thread Oliver Fromme
Niall Smart wrote in list.freebsd-hackers:
 > As I understand it most builtins will not spawn a new shell
 > when they are used in command substitution:
 > 
 > niall% echo `echo $$` $$
 > 20354 20354
 > niall% 

Actually, that example doesn't prove anything.  :-)

$ echo `echo $$` $$
8376 8376
$ echo `/bin/echo $$` $$
8376 8376
$ echo `(/bin/echo $$)` $$
8376 8376
$ echo `eval /bin/echo '$$'` $$
8376 8376
$ echo `(eval /bin/echo '$$')` $$
8376 8376
$ 

At first I thought that behaviour would be a bug, but it
isn't.  sh(1) says:

$Expands to the process ID of the invoked shell.  A
 subshell retains the same value of $ as its parent.

I.e. $$ does not change when a subshell is spawned, but it
rather inherits the value of $$ from its parent.  This makes
sense, because it enables you to use constructs like this in
shell scripts:

touch tmp.$$
chmod 600 tmp.$$
ls | grep foo | while read file; do
something ... >> tmp.$$# <-- subshell!
done
do_more_things < tmp.$$

Command substitution certainly has to spawn a subshell, even
for built-in commands, because otherwise you could modify
parent shell variables within command substitutions.
You can easily verify this with ktrace.  ;-)

Regards
   Oliver

-- 
Oliver Fromme, Leibnizstr. 18/61, 38678 Clausthal, Germany
(Info: finger userinfo:o...@dorifer.heim3.tu-clausthal.de)

"In jedem Stück Kohle wartet ein Diamant auf seine Geburt"
 (Terry Pratchett)


To Unsubscribe: send mail to majord...@freebsd.org
with "unsubscribe freebsd-hackers" in the body of the message



Re: bin/12578: `` subshell taints PWD

1999-07-13 Thread Sheldon Hearn



On Tue, 13 Jul 1999 18:13:42 +0200, Oliver Fromme wrote:

> Command substitution certainly has to spawn a subshell, even
> for built-in commands, because otherwise you could modify
> parent shell variables within command substitutions.

But isn't that exactly what's happening here, where PWD is being tainted
by the commands evaluated within the substitution?

Ciao,
Sheldon.


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: bin/12578: `` subshell taints PWD

1999-07-13 Thread Oliver Fromme

Niall Smart wrote in list.freebsd-hackers:
 > As I understand it most builtins will not spawn a new shell
 > when they are used in command substitution:
 > 
 > niall% echo `echo $$` $$
 > 20354 20354
 > niall% 

Actually, that example doesn't prove anything.  :-)

$ echo `echo $$` $$
8376 8376
$ echo `/bin/echo $$` $$
8376 8376
$ echo `(/bin/echo $$)` $$
8376 8376
$ echo `eval /bin/echo '$$'` $$
8376 8376
$ echo `(eval /bin/echo '$$')` $$
8376 8376
$ 

At first I thought that behaviour would be a bug, but it
isn't.  sh(1) says:

$Expands to the process ID of the invoked shell.  A
 subshell retains the same value of $ as its parent.

I.e. $$ does not change when a subshell is spawned, but it
rather inherits the value of $$ from its parent.  This makes
sense, because it enables you to use constructs like this in
shell scripts:

touch tmp.$$
chmod 600 tmp.$$
ls | grep foo | while read file; do
something ... >> tmp.$$# <-- subshell!
done
do_more_things < tmp.$$

Command substitution certainly has to spawn a subshell, even
for built-in commands, because otherwise you could modify
parent shell variables within command substitutions.
You can easily verify this with ktrace.  ;-)

Regards
   Oliver

-- 
Oliver Fromme, Leibnizstr. 18/61, 38678 Clausthal, Germany
(Info: finger userinfo:[EMAIL PROTECTED])

"In jedem Stück Kohle wartet ein Diamant auf seine Geburt"
 (Terry Pratchett)


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: bin/12578: `` subshell taints PWD

1999-07-13 Thread Niall Smart
Sheldon Hearn wrote:
> 
> On Mon, 12 Jul 1999 18:37:13 GMT, Niall Smart wrote:
> 
> > The patch appended seems to fix this, I'd like someone familiar
> > with sh to review it though, since this may be symptomatic of
> > a general problem with command substitution.
> 
> As I understand your patch, you're saying "we should fork off a child
> process when the command in question is cd"? This is what I missed when
> I tried ``echo .`sleep 600`.'' and assumed that the result was proof
> that we always spawn a subprocess for backtick evaluation. :-(

As I understand it most builtins will not spawn a new shell
when they are used in command substitution:

niall% echo `echo $$` $$
20354 20354
niall% 


Niall


To Unsubscribe: send mail to majord...@freebsd.org
with "unsubscribe freebsd-hackers" in the body of the message



Re: bin/12578: `` subshell taints PWD

1999-07-13 Thread Niall Smart

Sheldon Hearn wrote:
> 
> On Mon, 12 Jul 1999 18:37:13 GMT, Niall Smart wrote:
> 
> > The patch appended seems to fix this, I'd like someone familiar
> > with sh to review it though, since this may be symptomatic of
> > a general problem with command substitution.
> 
> As I understand your patch, you're saying "we should fork off a child
> process when the command in question is cd"? This is what I missed when
> I tried ``echo .`sleep 600`.'' and assumed that the result was proof
> that we always spawn a subprocess for backtick evaluation. :-(

As I understand it most builtins will not spawn a new shell
when they are used in command substitution:

niall% echo `echo $$` $$
20354 20354
niall% 


Niall


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: bin/12578: `` subshell taints PWD

1999-07-12 Thread Sheldon Hearn


On Mon, 12 Jul 1999 18:37:13 GMT, Niall Smart wrote:

> The patch appended seems to fix this, I'd like someone familiar
> with sh to review it though, since this may be symptomatic of
> a general problem with command substitution.

As I understand your patch, you're saying "we should fork off a child
process when the command in question is cd"? This is what I missed when
I tried ``echo .`sleep 600`.'' and assumed that the result was proof
that we always spawn a subprocess for backtick evaluation. :-(

Ciao,
Sheldon.


To Unsubscribe: send mail to majord...@freebsd.org
with "unsubscribe freebsd-hackers" in the body of the message



Re: bin/12578: `` subshell taints PWD

1999-07-12 Thread Niall Smart
Sheldon Hearn wrote:

> cd /tmp
> echo .`cd /`.
> pwd
> 
> Any takers?

The patch appended seems to fix this, I'd like someone familiar
with sh to review it though, since this may be symptomatic of
a general problem with command substitution.

> PS: And no, this is not an invitation to chat about the default shell
> for the base system. :-)

You're hinting it should be /bin/sh for root, right?

Regards,

Niall


*** eval.c~ Mon May 10 16:10:16 1999
--- eval.c  Mon Jul 12 18:27:44 1999
***
*** 710,715 
--- 710,716 
&& ((flags & EV_EXIT) == 0 || Tflag))
 || ((flags & EV_BACKCMD) != 0
&& (cmdentry.cmdtype != CMDBUILTIN
+|| cmdentry.u.index == CDCMD
 || cmdentry.u.index == DOTCMD
 || cmdentry.u.index == EVALCMD))) {
jp = makejob(cmd, 1);


To Unsubscribe: send mail to majord...@freebsd.org
with "unsubscribe freebsd-hackers" in the body of the message



Re: bin/12578: `` subshell taints PWD

1999-07-12 Thread Sheldon Hearn



On Mon, 12 Jul 1999 18:37:13 GMT, Niall Smart wrote:

> The patch appended seems to fix this, I'd like someone familiar
> with sh to review it though, since this may be symptomatic of
> a general problem with command substitution.

As I understand your patch, you're saying "we should fork off a child
process when the command in question is cd"? This is what I missed when
I tried ``echo .`sleep 600`.'' and assumed that the result was proof
that we always spawn a subprocess for backtick evaluation. :-(

Ciao,
Sheldon.


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: bin/12578: `` subshell taints PWD

1999-07-12 Thread Niall Smart

Sheldon Hearn wrote:

> cd /tmp
> echo .`cd /`.
> pwd
> 
> Any takers?

The patch appended seems to fix this, I'd like someone familiar
with sh to review it though, since this may be symptomatic of
a general problem with command substitution.

> PS: And no, this is not an invitation to chat about the default shell
> for the base system. :-)

You're hinting it should be /bin/sh for root, right?

Regards,

Niall


*** eval.c~ Mon May 10 16:10:16 1999
--- eval.c  Mon Jul 12 18:27:44 1999
***
*** 710,715 
--- 710,716 
&& ((flags & EV_EXIT) == 0 || Tflag))
 || ((flags & EV_BACKCMD) != 0
&& (cmdentry.cmdtype != CMDBUILTIN
+|| cmdentry.u.index == CDCMD
 || cmdentry.u.index == DOTCMD
 || cmdentry.u.index == EVALCMD))) {
jp = makejob(cmd, 1);


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message



Re: bin/12578: `` subshell taints PWD

1999-07-12 Thread Sheldon Hearn

Hi folks,

I'm hoping someone here is interested in tracking down a bug in our
/bin/sh . Changing directory within a backtick (``) subshell in sh
taints the parent's working directory. The following sample code gives
the expected result for /bin/csh, but breaks for /bin/sh

cd /tmp
echo .`cd /`.
pwd

Any takers?

Ciao,
Sheldon.

PS: And no, this is not an invitation to chat about the default shell
for the base system. :-)

PPS: S!

PPPS: www.s.com


To Unsubscribe: send mail to majord...@freebsd.org
with "unsubscribe freebsd-hackers" in the body of the message



Re: bin/12578: `` subshell taints PWD

1999-07-12 Thread Sheldon Hearn


Hi folks,

I'm hoping someone here is interested in tracking down a bug in our
/bin/sh . Changing directory within a backtick (``) subshell in sh
taints the parent's working directory. The following sample code gives
the expected result for /bin/csh, but breaks for /bin/sh

cd /tmp
echo .`cd /`.
pwd

Any takers?

Ciao,
Sheldon.

PS: And no, this is not an invitation to chat about the default shell
for the base system. :-)

PPS: S!

PPPS: www.s.com


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message