Bug 4135

Summary: Error running some shell scripts directly
Product: Busybox Reporter: jabba.nalhutta
Component: OtherAssignee: unassigned
Status: RESOLVED INVALID    
Severity: minor CC: busybox-cvs
Priority: P5    
Version: 1.19.x   
Target Milestone: ---   
Hardware: Other   
OS: Linux   
Host: Target:
Build:
Attachments: The .config file for the bug
strace log

Description jabba.nalhutta 2011-09-06 14:23:47 UTC
Changes to ash in v1.19.x result in scripts not being directly executable if they don't start with a line pointing to the desired shell (e.g., "#!/bin/sh"). 

There is a work-around in that these scripts can be executed if they are passed to "sh" at the command line (e.g., "sh foo.sh"). However, ash should be able to run such scripts directly if their executable flags are set - and it has been able to do so until now, when one just gets a "not found" error.
Comment 1 Denys Vlasenko 2011-09-10 15:24:37 UTC
Can't reproduce. In my testing, a shell script w/o "#!interpreter" 1st line _can_ be successfully run from ash command line
Comment 2 jabba.nalhutta 2011-09-12 05:18:28 UTC
I can reproduce it very easily. Perhaps you're not running busybox as the shell and/or default interpreter. Here is a screenshot with me trying to run two scripts, "foo.sh" and "bar" (see below):

router@Ubuntu ~/busybox-1.19.2
$ ./ash


BusyBox v1.19.2 (2011-09-10 06:29:23 BST) built-in shell (ash)
Enter 'help' for a list of built-in commands.

router@Ubuntu ~/busybox-1.19.2
$ help
Built-in commands:
------------------
	. : [ [[ alias bg break cd chdir continue echo eval exec exit
	export false fg hash help jobs kill let local printf pwd read
	readonly return set shift source test times trap true type ulimit
	umask unalias unset wait

router@Ubuntu ~/busybox-1.19.2
$ ./foo.sh
This is foo.sh
calling bar ...
./foo.sh: line 4: ./bar: not found
Back to foo.sh
router@Ubuntu ~/busybox-1.19.2
$ ./bar
./ash: ./bar: not found
router@Ubuntu ~/busybox-1.19.2
$ exit
router@Ubuntu ~/busybox-1.19.2
$ 

----- here are the test scripts (foo.sh) ------
#!./ash
echo "This is foo.sh"
echo "calling bar ..."
./bar
echo "Back to foo.sh"

---- bar ----
echo "This is bar"
echo "Finished running bar"

---- this is is what the output should look like ----
router@Ubuntu ~/busybox-1.19.2
$ ./foo.sh 
This is foo.sh
calling bar ...
This is bar
Finished running bar
Back to foo.sh
router@Ubuntu ~/busybox-1.19.2
$
Comment 3 Denys Vlasenko 2011-09-15 10:58:07 UTC
(In reply to comment #2)
> router@Ubuntu ~/busybox-1.19.2
> $ ./foo.sh
> This is foo.sh
> calling bar ...
> ./foo.sh: line 4: ./bar: not found
> Back to foo.sh
> router@Ubuntu ~/busybox-1.19.2
> $ ./bar
> ./ash: ./bar: not found
> router@Ubuntu ~/busybox-1.19.2
> $ exit
> router@Ubuntu ~/busybox-1.19.2
> $ 


Can't reproduce. I am getting:

/bin # ./foo.sh
This is foo.sh
calling bar ...
This is bar
Finished running bar
Back to foo.sh

If I do "chmod 644 bar", I am getting:

/bin # ./foo.sh
This is foo.sh
calling bar ...
./foo.sh: line 4: ./bar: Permission denied
Back to foo.sh

Thus, I am puzzled how you end up getting "./bar: not found".

Please,

(1) attach your .config file to this bug, and

(2) run "strace -oLOG -s999 -tt -f ./ash", run ./foo.sh under it, then exit the shell and attach resulting LOG file to this bug.
(If you don't have strace tool, download one for your architecture from http://landley.net/aboriginal/downloads/binaries/extras/)
Comment 4 jabba.nalhutta 2011-09-15 19:28:30 UTC
Created attachment 3577 [details]
The .config file for the bug
Comment 5 jabba.nalhutta 2011-09-15 19:29:07 UTC
Created attachment 3583 [details]
strace log
Comment 6 Denys Vlasenko 2011-09-16 08:43:51 UTC
From your .config:

CONFIG_BUSYBOX_EXEC_PATH="./dist"

which looks wrong to me. It's usually "/proc/self/exe", "/bin/busybox" or similar. Why did you set it to "./dist"? I mean, it's a _relative_ path!?


From LOG:

10634 20:13:23.989951 execve("./bar", ["./bar"], [/* 43 vars */]) = -1 ENOEXEC (Exec format error)

Now ash tries to exec itself... using CONFIG_BUSYBOX_EXEC_PATH:

10634 20:13:23.990113 execve("./dist", ["ash", "./bar"], [/* 43 vars */]) = -1 ENOENT (No such file or directory)

and not surprisingly, "./dist" doesn't exist, and execve fails.

10634 20:13:23.990262 write(2, "./foo.sh: "..., 10) = 10
10634 20:13:23.990312 write(2, "line 4: "..., 8) = 8
10634 20:13:23.990361 write(2, "./bar: not found"..., 16) = 16
10634 20:13:23.990414 write(2, "\n"..., 1) = 1
Comment 7 jabba.nalhutta 2011-09-16 12:56:56 UTC
I see. User error. Sorry to have disturbed you :-(