Problem with ash redirection changing behaviour

Richard Purdie rpurdie at rpsys.net
Wed Jun 2 22:56:02 UTC 2010


On Wed, 2010-06-02 at 17:51 +0200, Denys Vlasenko wrote:
> On Wed, Jun 2, 2010 at 11:56 AM, Richard Purdie <rpurdie at rpsys.net> wrote:
> > Hi,
> >
> > I have an issue having recently upgraded busybox.
> 
> To which version do you upgrade?

1.15.3. Not the latest I know but I did look at the ash source from git
and did not see any significant change in the code paths that would
alter this behaviour.

> > The change that is
> > causing me problems is:
> >
> > http://git.busybox.net/busybox/commit/?id=6a0ad2506116f4ddc3f9f617a90ba04a57eeef88
> >
> > which is sanity checking file descriptors before allowing redirection.
> >
> > A script that triggers the problem is:
> >
> > """
> > exec 9>&0 </etc/fstab
> > while read fs mnt type opts dump pass junk
> > do
> >        echo $fs
> > done
> > exec 0>&9 9>&-
> > """
> >
> > The idea being to save stdin, read from a file and then restore stdin
> > without forking. This used to work but now when run as ". script" from
> > ash this results in:
> >
> > line 1: 0: Bad file descriptor
> >
> > which is in turn due to is_hidden_fd() returning 1 since stdin was being
> > read as the source for commands.
> >
> > I can see what its getting at as stdin was where the main shell was
> > accepting data from. bash doesn't error with this.
> >
> > Is this error the expected result or a bug in busybox?
> 
> This would be the bug, but I can't reproduce it. Test script:
> 
> 
> 
> echo Hi there >b.tmp
> 
> exec 9>&0 <b.tmp
> while read a b
> do
>        echo "$a $b"
> done
> exec 0>&9 9>&-
> 
> if test "$sourced"; then
>     echo Returning from sourced file
>     return
> fi
> sourced=1
> echo Sourcing
> . "$0"
> echo Sourcing done
> 
> ls -l /proc/$$/fd
> 
> 
> 
> Running it:
> 
> # ./busybox ash b
> Hi there
> Sourcing
> Hi there
> Returning from sourced file
> Sourcing done
> total 0
> lrwx------ 1 root root 64 2010-06-02 17:49 0 -> /dev/pts/0
> lrwx------ 1 root root 64 2010-06-02 17:49 1 -> /dev/pts/0
> lr-x------ 1 root root 64 2010-06-02 17:49 10 ->
> /root/srcdevel/bbox/fix/busybox.t7/b
> lrwx------ 1 root root 64 2010-06-02 17:49 2 -> /dev/pts/0

Running your script as above does not show the problem for me either,
presumably as it didn't start with stdin as fd 0 when a script is
specified.

I can make your script show the problem by doing:

$ ./ash
$ . b
/ash: b: line 3: 0: Bad file descriptor

(where ./ash is a symlink to busybox)

Cheers,

Richard





More information about the busybox mailing list