From: Valery Reznic on
Hi,

I Have following to scripts:

a.sh
#!/bin/sh
echo "It's a.sh

and b.sh:
#! ./b.sh
echo "It's b.sh"

As per execve man page, script interpreter should not be script itself.
When I run it on my Fedora 8 x86_64 box (with stock kernel, never updated)
under strace I got following:

strace -f -e execve setarch x86_64 ./b.sh
execve("/usr/bin/setarch", ["setarch", "x86_64", "./b.sh"], [/* 23 vars */]) = 0
execve("./b.sh", ["./b.sh"], [/* 23 vars */]) = -1 ENOEXEC (Exec format error)
execve("/bin/sh", ["/bin/sh", "./b.sh"], [/* 23 vars */]) = 0
It's b.sh

I.e execve failed as it should

When I run same scripts on Fedora 12 x86_64 box with stock kernel 2.6.31.5-127.fc12.x86_64 I got following:

strace -f -e execve setarch i386 ./b.sh
execve("/usr/bin/setarch", ["setarch", "i386", "./b.sh"], [/* 41 vars */]) = 0
execve("./b.sh", ["./b.sh"], [/* 41 vars */]) = 0
It's a.sh

I.e execve succeeded, instead of failing with ENOEXEC

Regards,
Valery.

P.S. I am not subscribed to this list, so please CC me







--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo(a)vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
From: Andrew Morton on
On Thu, 11 Mar 2010 02:56:16 -0800 (PST)
Valery Reznic <valery_reznic(a)yahoo.com> wrote:

> Hi,
>
> I Have following to scripts:
>
> a.sh
> #!/bin/sh
> echo "It's a.sh
>
> and b.sh:
> #! ./b.sh
> echo "It's b.sh"
>
> As per execve man page, script interpreter should not be script itself.
> When I run it on my Fedora 8 x86_64 box (with stock kernel, never updated)
> under strace I got following:
>
> strace -f -e execve setarch x86_64 ./b.sh
> execve("/usr/bin/setarch", ["setarch", "x86_64", "./b.sh"], [/* 23 vars */]) = 0
> execve("./b.sh", ["./b.sh"], [/* 23 vars */]) = -1 ENOEXEC (Exec format error)
> execve("/bin/sh", ["/bin/sh", "./b.sh"], [/* 23 vars */]) = 0
> It's b.sh
>
> I.e execve failed as it should
>
> When I run same scripts on Fedora 12 x86_64 box with stock kernel 2.6.31.5-127.fc12.x86_64 I got following:
>
> strace -f -e execve setarch i386 ./b.sh
> execve("/usr/bin/setarch", ["setarch", "i386", "./b.sh"], [/* 41 vars */]) = 0
> execve("./b.sh", ["./b.sh"], [/* 41 vars */]) = 0
> It's a.sh
>
> I.e execve succeeded, instead of failing with ENOEXEC
>

It works for me, I think:

z:/home/akpm> uname -a
Linux z 2.6.31.5-127.fc12.x86_64 #1 SMP Sat Nov 7 21:11:14 EST 2009 x86_64 x86_64 x86_64 GNU/Linux
z:/home/akpm> cat b.sh
#! ./b.sh
echo "It's b.sh"
z:/home/akpm> strace -f -e execve setarch i386 ./b.sh
execve("/usr/bin/setarch", ["setarch", "i386", "./b.sh"], [/* 60 vars */]) = 0
execve("./b.sh", ["./b.sh"], [/* 60 vars */]) = -1 ENOEXEC (Exec format error)
execve("/bin/sh", ["/bin/sh", "./b.sh"], [/* 60 vars */]) = 0
It's b.sh


Did I do something wrong? If not, I wonder what's different.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo(a)vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
From: David Newall on
On Thu, 11 Mar 2010 02:56:16 -0800 (PST) Valery Reznic
<valery_reznic(a)yahoo.com> wrote:
> Hi,
>
> I Have following to scripts:
>
> a.sh
> #!/bin/sh
> echo "It's a.sh
>
> and b.sh:
> #! ./b.sh
> echo "It's b.sh"
>
[...]
> When I run same scripts on Fedora 12 x86_64 box with stock kernel 2.6.31.5-127.fc12.x86_64 I got following:
>
> strace -f -e execve setarch i386 ./b.sh
> execve("/usr/bin/setarch", ["setarch", "i386", "./b.sh"], [/* 41 vars */]) = 0
> execve("./b.sh", ["./b.sh"], [/* 41 vars */]) = 0
> It's a.sh

I see no circumstance which would make b.sh invoke a.sh (and thus emit
"It's a.sh"). Are you sure these are the actual scripts and output?

On the other hand, if the output was "It's b.sh", this is compatible
with many historical versions of UNIX, which assumed /bin/sh to be the
script interpreter. On
2.6.28-16-generic #55-Ubuntu SMP i686 GNU/Linux I get successful output
for the following variants of "x.sh":

:
echo worked

and

#!
echo worked

and

#! ./x.sh
echo worked
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo(a)vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
From: Valery Reznic on


--- On Sat, 3/20/10, David Newall <davidn(a)davidnewall.com> wrote:

> From: David Newall <davidn(a)davidnewall.com>
> Subject: Re: execve for script don't return ENOEXEC, bug ?
> To: "Valery Reznic" <valery_reznic(a)yahoo.com>
> Cc: linux-kernel(a)vger.kernel.org
> Date: Saturday, March 20, 2010, 2:37 AM
> On Thu, 11 Mar 2010 02:56:16 -0800
> (PST) Valery Reznic <valery_reznic(a)yahoo.com>
> wrote:
> > Hi,
> >
> > I Have following to scripts:
> >
> > a.sh
> > #!/bin/sh
> > echo "It's a.sh
> >
> > and b.sh:
> > #! ./b.sh
> > echo "It's b.sh"
> >���
> [...]
> > When I run same scripts on Fedora 12 x86_64 box with
> stock kernel 2.6.31.5-127.fc12.x86_64 I got following:
> >
> > strace -f -e execve setarch i386� ./b.sh
> execve("/usr/bin/setarch", ["setarch", "i386", "./b.sh"],
> [/* 41 vars */]) = 0
> > execve("./b.sh", ["./b.sh"], [/* 41 vars */]) = 0
> > It's a.sh
>
> I see no circumstance which would make b.sh invoke a.sh
> (and thus emit "It's a.sh"). Are you sure these are the
> actual scripts and output?

Of course you are right. Somehow I messed up b.sh
(OK, this file is so long and complicated, so no wonder :)
Anyway my bad, sorry.

In the b.sh interpreter should be ./a.sh

Another attempt to provide correct data:

[valery(a)localhost ~]$ cat a.sh
#!/bin/sh
echo "It's a.sh"
[valery(a)localhost ~]$ cat b.sh
#! ./a.sh
echo "It's b.sh"
[valery(a)localhost ~]$
[valery(a)localhost ~]$ strace -f -e execve setarch i386 ./b.sh
execve("/usr/bin/setarch", ["setarch", "i386", "./b.sh"], [/* 40 vars */]) = 0
execve("./b.sh", ["./b.sh"], [/* 40 vars */]) = 0
It's a.sh
[valery(a)localhost ~]$




>
> On the other hand, if the output was "It's b.sh", this is
> compatible with many historical versions of UNIX, which
> assumed /bin/sh to be the script interpreter. On
> 2.6.28-16-generic #55-Ubuntu SMP i686 GNU/Linux I get
> successful output for the following variants of "x.sh":
>
> :
> echo worked
>
> and
>
> #!
> echo worked
>
> and
>
> #! ./x.sh
> echo worked
My problem is not incorrect output, but successful execve, when script's interpreter is interpreter itself.

Somewhere between Fedora 8 and Fedora 12 execve's behaviour changed.

@Andrew:
When b.sh has interpreter ./b.sh I got expected ENOEXEC, same as you.

Regards,
Valery.

>



--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo(a)vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
From: David Newall on
Valery Reznic wrote:
> [valery(a)localhost ~]$ cat a.sh
> #!/bin/sh
> echo "It's a.sh"
> [valery(a)localhost ~]$ cat b.sh
> #! ./a.sh
> echo "It's b.sh"
> [valery(a)localhost ~]$
> [valery(a)localhost ~]$ strace -f -e execve setarch i386 ./b.sh
> execve("/usr/bin/setarch", ["setarch", "i386", "./b.sh"], [/* 40 vars */]) = 0
> execve("./b.sh", ["./b.sh"], [/* 40 vars */]) = 0
> It's a.sh
>

That is the correct output for a script which is being interpreted by
a.sh, when a.sh prints "It's a.sh" using /bin/sh as its interpreter.
Remember, you didn't ask /bin/sh to interpret b.sh, you asked a.sh to do
it, and a.sh is a simple echo statement, not an interpreter. There is
no error here.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo(a)vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/