Skip to contentSkip to conversation

binfmt_script: interpreted interpreter doesn't work

BUG? Pozsar Balazs 2 participants 3 messages Sep 15, 2002 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw
Quoted 11 lines · Hans-Peter Jansen
Quoted 9 lines

Consider these two scripts:

/home/pozsy/a:
#!/usr/bin/perl
print <>;

/home/pozsy/b:
#!/home/pozsy/a
"This is some text to printed out by the 'a' script"

What's the problem?

I have a script written in perl, which interprets its input (or the files in its arguments) [this is "a" in the above example]. I want to use this script, as an interpreter to another "script" [the "b" in the above example].

I well know bash, but this has nothing to do with shell coding.

Try my above example and you will understand what the problem is. If you execute /home/pozsy/b, you _should_ get this as the output (2 lines) (the "a" program simply cats its input or argument files to its output):
#!/home/pozsy/b
"This is some text to printed out by the 'a' script"

BUT this is what you get (1 line):
/home/pozsy/b: This is some text to printed out by the 'a' script: command not found

This is because the kernel cannot execute the "/home/pozsy/b" script, and then bash tries to interpret it itself. (but this in *not* what I want: I want the "b" 'script' interpreted by the "a" script).
If you try this:
strace -f /home/pozsy/b
You will get this:
execve("/home/pozsy/b", ["/home/pozsy/b"], [/* 24 vars */]) = 0
strace: exec: Exec format error

The root of the problem is still that /home/pozsy/b cannot be execve'd. That is a kernel problem.

Signature · 1 line
pozsy
#2 lore raw
Quoted 11 lines · Pozsar Balazs

This is because the kernel cannot execute the "/home/pozsy/b" script, and then bash tries to interpret it itself. (but this in *not* what I want: I want the "b" 'script' interpreted by the "a" script).
If you try this:
strace -f /home/pozsy/b
You will get this:
execve("/home/pozsy/b", ["/home/pozsy/b"], [/* 24 vars */]) = 0
strace: exec: Exec format error

The root of the problem is still that /home/pozsy/b cannot be execve'd. That is a kernel problem.

the problem is far simpler :
when you execute /home/pozsy/b, the kernel should have to launch /home/pozsy/a with /home/pozsy/b in argv[0]. If it accepted to run it, it would run sh (or perl or any other interpreter) with /home/pozsy/a in argv[0], thus loosing track of /home/pozsy/b.

The simplest solution for you is to write a little C wrapper to start your interpreted interpreter with the script in argument. Written with dietlibc or anything like it, it would not be more than a few hundred bytes long.

Cheers,
Willy

#3 lore raw
Quoted 20 lines · Willy Tarreau
Quoted 11 lines · Pozsar Balazs

This is because the kernel cannot execute the "/home/pozsy/b" script, and then bash tries to interpret it itself. (but this in *not* what I want: I want the "b" 'script' interpreted by the "a" script).
If you try this:
strace -f /home/pozsy/b
You will get this:
execve("/home/pozsy/b", ["/home/pozsy/b"], [/* 24 vars */]) = 0
strace: exec: Exec format error

The root of the problem is still that /home/pozsy/b cannot be execve'd. That is a kernel problem.

the problem is far simpler :
when you execute /home/pozsy/b, the kernel should have to launch /home/pozsy/a with /home/pozsy/b in argv[0]. If it accepted to run it, it would run sh (or perl or any other interpreter) with /home/pozsy/a in argv[0], thus loosing track of /home/pozsy/b.

Quoted 3 lines

The simplest solution for you is to write a little C wrapper to start your interpreted interpreter with the script in argument. Written with dietlibc or anything like it, it would not be more than a few hundred bytes long.

I wrote a little wrapper, but that showed me that the kernel does _not_ replace argv[0], instead it pushes the original argv[0] into argv[1] or argv[2].
So we would not loose track of the original name, as interpreters could be called like this:
/interpreter1 script
/interpreter2 /interpreter1 script
/interpreter3 /interpreter2 /interpreter1 script
...and so on.

Signature · 1 line
pozsy