Skip to contentSkip to conversation

#! incompatible -- binfmt_script.c broken?

DISCUSSION Matthias Andree 6 participants 10 messages Dec 4, 2002 – Dec 5, 2002

lore
Share & tools
lore
#0 lore raw

Hi,

I tried some of the Perl magic tricks shown in the perlrun man page with Linux 2.4.19; consider this Perl one-liner. It works on FreeBSD and Solaris, but fails on Linux. Looking at binfmt_script.c, I believe the "pass the rest of the line as the first argument to the interpreter" is the problem with Linux. Haven't yet figured if the other boxes just use the interpreter, ignoring the arguments or if they are doing argument splitting.

------------------------------------------------------------------------
#!/bin/sh -- # -*- perl -*- -T
eval 'exec perl -wTS $0 ${1+"$@"}'
if 0;
print "Hello there.\n";
------------------------------------------------------------------------

FreeBSD 4.7:

$ /tmp/try.pl
Hello there.

Solaris 8:

$ /tmp/try.pl
Hello there.

SuSE Linux 7.0, 7.3, 8.1 (2.4.19 kernel, binfmt_script.c identical to 2.4.20 BK):

$ /tmp/try.pl
/bin/sh: -- # -*- perl -*- -T: invalid option
Usage:  /bin/sh [GNU long option] [option] ...
        /bin/sh [GNU long option] [option] script-file ...
[...]
Signature · 1 line
Matthias Andree
#1 lore raw
Quoted 21 lines · Matthias Andree

Hi,

I tried some of the Perl magic tricks shown in the perlrun man page with Linux 2.4.19; consider this Perl one-liner. It works on FreeBSD and Solaris, but fails on Linux. Looking at binfmt_script.c, I believe the "pass the rest of the line as the first argument to the interpreter" is the problem with Linux. Haven't yet figured if the other boxes just use the interpreter, ignoring the arguments or if they are doing argument splitting.

------------------------------------------------------------------------
#!/bin/sh -- # -*- perl -*- -T
eval 'exec perl -wTS $0 ${1+"$@"}'
if 0;
print "Hello there.\n";
------------------------------------------------------------------------

FreeBSD 4.7:

$ /tmp/try.pl
Hello there.
Quoted 8 lines

Solaris 8:

$ /tmp/try.pl
Hello there.

SuSE Linux 7.0, 7.3, 8.1 (2.4.19 kernel, binfmt_script.c identical to 2.4.20 BK):

$ /tmp/try.pl
/bin/sh: -- # -*- perl -*- -T: invalid option

looks correct. The interpreter (/bin/sh) has got everything after its name. IOW: "-- # -*- perl -*- -T"
It's just solaris' shell (/bin/sh) just ignores options starting with "--". And freebsd's as well.
Anyway - it's bash, not the bin_fmt.

Quoted 3 lines

Usage: /bin/sh [GNU long option] [option] ...
/bin/sh [GNU long option] [option] script-file ...
[...]

#2 lore raw
Quoted 10 lines · Alexander Riesen
Quoted 4 lines · Matthias Andree

SuSE Linux 7.0, 7.3, 8.1 (2.4.19 kernel, binfmt_script.c identical to 2.4.20 BK):

$ /tmp/try.pl
/bin/sh: -- # -*- perl -*- -T: invalid option

looks correct. The interpreter (/bin/sh) has got everything after its name. IOW: "-- # -*- perl -*- -T"
It's just solaris' shell (/bin/sh) just ignores options starting with "--". And freebsd's as well.

FreeBSD splits #! magic strings on whitespace and passes multiple arguments. Linux passes everything after the first whitespace as a single argument but strips trailing whitespace. NetBSD does the same as Linux but passes trailing whitespace as part of the argument.

Quoted 1 line

Anyway - it's bash, not the bin_fmt.

Bash is (correctly) complaining that it's been passed an invalid argument, but the reason for the different behaviour between it and FreeBSD is because of binfmt_script. There's no clearly defined standard for how this should behave.

Signature · 1 line
Matthew Garrett | [email protected]
#3 lore raw
In reply to Alex Riesen
Quoted 6 lines · Alex Riesen
Quoted 4 lines

SuSE Linux 7.0, 7.3, 8.1 (2.4.19 kernel, binfmt_script.c identical to 2.4.20 BK):

$ /tmp/try.pl
/bin/sh: -- # -*- perl -*- -T: invalid option

looks correct.

Nope. It cannot be correct if it breaks compatibility without giving us any advantage.

Quoted 2 lines

The interpreter (/bin/sh) has got everything after its name. IOW: "-- # -*- perl -*- -T"

Yes, as SINGLE argument. Therefore, Perl programs break if they use this procedure recommended by "man perlrun".

I don't care WHY it works everywhere else, I want this incompatibility fixed and I'm not going through a flame war as with the 4.4BSD SIOCGIFNETMASK issue again. This is not negotiable.

BTW, 2.2 is also affected.

Think of someone using /usr/bin/env -i /path/to/program -- won't work on Linux, but works on FreeBSD.

I cannot see technical reasons why this should remain unfixed.

We have enough braindead frivulous incompatibilities in Linux.

Quoted 3 lines

It's just solaris' shell (/bin/sh) just ignores options starting with "--". And freebsd's as well.
Anyway - it's bash, not the bin_fmt.

Nope, zsh as /bin/sh complains as well:

/bin/sh: no such option: # _*_ perl _*_ _T

so does pdksh:

/bin/sh: /bin/sh: --: unknown option

#4 lore raw
Quoted 10 lines · Matthias Andre
Quoted 2 lines · Alex Riesen

The interpreter (/bin/sh) has got everything after its name. IOW: "-- # -*- perl -*- -T"

Yes, as SINGLE argument. Therefore, Perl programs break if they use this procedure recommended by "man perlrun".

I don't care WHY it works everywhere else, I want this incompatibility fixed and I'm not going through a flame war as with the 4.4BSD SIOCGIFNETMASK issue again. This is not negotiable.

See http://www.uni-ulm.de/~s_smasch/various/shebang/ . FreeBSD is the *only* OS to pass multiple arguments. SUS says nothing about it, and pretty much every single implementation varies. It does not work everywhere else.

Quoted 1 line

We have enough braindead frivulous incompatibilities in Linux.

The *only* thing you can reliably use in #! lines is an interpreter followed by a single argument with no trailing space. On NetBSD with bash as /bin/sh:

mjg59@cysteine:/tmp$ cat foo.pl
#!/bin/sh -- # -*- perl -*- -p
mjg59@cysteine:/tmp$ ./foo.pl
/bin/sh: -- # -*- perl -*- -p: unrecognized option

File a bug against perlrun(1).

Signature · 1 line
Matthew Garrett | [email protected]
#5 lore raw
Followup to:  <[email protected]>
By author:    Matthew Garrett <[email protected]>
In newsgroup: linux.dev.kernel
Quoted 5 lines

The *only* thing you can reliably use in #! lines is an interpreter followed by a single argument with no trailing space. On NetBSD with bash as /bin/sh:

Quoted 7 lines
mjg59@cysteine:/tmp$ cat foo.pl
#!/bin/sh -- # -*- perl -*- -p
mjg59@cysteine:/tmp$ ./foo.pl
/bin/sh: -- # -*- perl -*- -p: unrecognized option

File a bug against perlrun(1).

I personally think that it would be nice to split it by spaces (but yes, ' " and \ need to be handled for this to work.) It allows things like using env to spawn a binary where the location of the interpreter is to be extracted from PATH, for example.

-hpa

Signature · 3 lines
<[email protected]> at work, <[email protected]> in private!
"Unix gives you enough rope to shoot yourself in the foot."
http://www.zytor.com/~hpa/puzzle.txt	<[email protected]>
#6 lore raw
In reply to Matthew Garrett
Quoted 4 lines · Matthew Garrett

See http://www.uni-ulm.de/~s_smasch/various/shebang/ . FreeBSD is the *only* OS to pass multiple arguments. SUS says nothing about it, and pretty much every single implementation varies. It does not work everywhere else.

Now that's a useful resource.

Quoted 1 line

File a bug against perlrun(1).

Hum, no. In fact, the offending line will work:

- on systems that only pass on the first argument or none at all
- on systems that split on whitespace
- on /bin/sh that silently ignore options starting with "-- "

So it's somewhat compatible already.

#7 lore raw
In reply to Alex Riesen

Alex Riesen <[email protected]> said:

[...]

Quoted 4 lines

looks correct. The interpreter (/bin/sh) has got everything after its name. IOW: "-- # -*- perl -*- -T"
It's just solaris' shell (/bin/sh) just ignores options starting with "--". And freebsd's as well.

And Linux's too. Try it.
--
Dr. Horst H. von Brand User #22616 counter.li.org
Departamento de Informatica Fono: +56 32 654431
Universidad Tecnica Federico Santa Maria +56 32 654239
Casilla 110-V, Valparaiso, Chile Fax: +56 32 797513

#8 lore raw
Quoted 10 lines · Horst von Brand

Alex Riesen <[email protected]> said:

[...]

Quoted 4 lines

looks correct. The interpreter (/bin/sh) has got everything after its name. IOW: "-- # -*- perl -*- -T"
It's just solaris' shell (/bin/sh) just ignores options starting with "--". And freebsd's as well.

And Linux's too. Try it.

Is there the "Linux's /bin/sh"? I believe most distributions use GNU bash for /bin/sh, and that certainly does NOT ignore these, but parse.

The problem is that binfmt_script.c does not split the remainder of the line at whitespace.

Assuming your current working directory does not have files that match -*-:

$ /bin/sh '-- # -*- perl -*- -T'
/bin/sh: -- # -*- perl -*- -T: invalid option
Usage:  /bin/sh [GNU long option] [option] ...
        /bin/sh [GNU long option] [option] script-file ...
GNU long options:
        --debug
...
$ /bin/bash -- # -*- perl -*- -T
$ 
$ /usr/bin/ksh '-- # -*- perl -*- -T'
/usr/bin/ksh: /usr/bin/ksh: --: unknown option
$ /usr/bin/ksh -- # -*- perl -*- -T
$
$ /usr/bin/zsh '-- # -*- perl -*- -T'
/usr/bin/zsh: no such option:  # _*_ perl _*_ _T
$ /usr/bin/zsh -- # -*- perl -*- -T
$
Signature · 1 line
Matthias Andree
#9 lore raw
In reply to Matthias Andree
Quoted 11 lines · Matthias Andree

I tried some of the Perl magic tricks shown in the perlrun man page with Linux 2.4.19; consider this Perl one-liner. It works on FreeBSD and Solaris, but fails on Linux.

------------------------------------------------------------------------
#!/bin/sh -- # -*- perl -*- -T
eval 'exec perl -wTS $0 ${1+"$@"}'
if 0;
print "Hello there.\n";
------------------------------------------------------------------------

Quoted 7 lines

FreeBSD 4.7 and Solaris 8:

$ /tmp/try.pl
Hello there.

SuSE Linux 7.0, 7.3, 8.1

$ /tmp/try.pl
/bin/sh: -- # -*- perl -*- -T: invalid option

See http://www.cwi.nl/~aeb/std/hashexclam-1.html

There I wrote long ago:

...
The interpreter is called with a parameter list consisting of four groups of arguments: arg0, argi, argn, args.

The first group, arg0, consists of one argument. For SysVR4, SunOS, Solaris, IRIX, BSDI, DU, AIX, Unixware, Linux this argument is /path/interpreter. For FreeBSD and HPUX this argument is /scriptpath/script.

The second group, argi, consists of the 0 or 1 or perhaps more arguments to the interpreter found in the #! line. Thus, this group is empty if there is no nonblank text following the interpreter name in the #! line. If there is such nonblank text then for SysVR4, SunOS, Solaris, IRIX, HPUX, AIX, Unixware, Linux, FreeBSD this group consists of precisely one argument, as in the example above where argi consists of the single argument "-a -b". BSDI splits the text following the interpreter name into zero or more arguments, and hence has an argi consisting of the two arguments "-a", "-b" in the above example. (Also Plan9 allows several arguments here.)
...

Instead of trying this complicated perl example, you might try small scripts that test one thing at a time. I am interested in the results of such tests on various operating systems.

But regarding your post: there is no reason to assume that Linux does something "wrong" here if it disagrees with Solaris and FreeBSD. There is no standard and no uniformity here.

Andries