Strange echo behaviour

Rich Felker dalias at aerifal.cx
Thu Jan 27 05:48:11 UTC 2011


On Thu, Jan 27, 2011 at 06:18:25AM +0100, Denys Vlasenko wrote:
> On Wednesday 26 January 2011 13:21, Baruch Siach wrote:
> > Hi busybox list,
> > 
> > I'm running the following command under strace (thanks Rob):
> > 
> > echo 56 > /sys/class/gpio/export
> > 
> > and I see the following output:
> > 
> > write(1, "56\n", 3)                     = -1 EBUSY (Device or resource busy)
> > write(1, "5", 1)                        = 1
> > 
> > The first EBUSY is OK, since GPIO 56 is already requested. But the second 
> > write() attempt seems strange, and leads to an unwanted outcome. GPIO 5 gets 
> > exported.
> > 
> > Looking into the code it seems like the fflush() call at the end for 
> > echo_main() is somehow responsible for this, but this is only a guess.
> 
> I would like to make it so that echo doesn't retry failed writes
> (except on EINTR).
> 
> I wonder how to fix this, though. I can easily detect write error,
> but how to discard buffered data in a FILE object?
> Even without fflush(), libc will try to write it out at fclose()!

Are you sure? My impression was that after error, the FILE enters an
error state, the buffer is invalidated, and no further IO will be
attempted unless clearerr is called. I suspect libcs could differ in
this regard...

One way to prevent further writes reliably is:

tmp=open("/dev/null", O_RDONLY);
dup2(tmp, fileno(f));
close(tmp);

Rich


More information about the busybox mailing list