Strange echo behaviour

Denys Vlasenko vda.linux at googlemail.com
Thu Jan 27 09:07:31 UTC 2011


On Thu, Jan 27, 2011 at 7:59 AM, Baruch Siach <baruch at tkos.co.il> wrote:
> Here is some more information that may help in understanding this issue.
>
> I've only observed this problem when running a statically linked Busybox
> linked against uClibc 0.9.30.3, and only when running echo directly from the
> shell (ash). When I run
>
> ./strace-armv5l echo 56 > /sys/class/gpio/export
>
> I see
>
> write(1, "56\n", 3)                     = -1 EBUSY (Device or resource busy)
> write(1, "56\n", 3)                     = -1 EBUSY (Device or resource busy)
>
> as expected,

I wouldn't call this "expected". Why there are two writes?
User definitely didn't instruct echo to do multiple writes!

> but when I run
>
> ./strace-armv5l sh -c 'echo 56 > /sys/class/gpio/export'
>
> I see
>
> write(1, "56\n", 3)                     = -1 EBUSY (Device or resource busy)
> write(1, "5", 1)                        = 1
>
> So apparently this problem has something to do with either uClibc or ash, not
> echo.

Looks like uClibc. Need to experiment with glibc...

> However, we can implement echo using write(), thus avoiding the
> buffering of stdio.
>
> Another related problem that I've observed with echo goes as follows:
>
> ./strace-armv5l sh -c 'echo test > /dev/input/event0'
>
> shows:
>
> write(1, "test\n", 5)                   = 16
> write(1, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 2147483647) = -1 EFAULT (Bad address)
> write(1, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 4095) = 4096
> write(1, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 2147483647) = -1 EFAULT (Bad address)
> write(1, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 4096) = 4096
>
> That is, the return value of write() is larger that the written buffer (is
> this legal?) so echo (or ash) tries again with a bogus count == -1.

Horror.
-- 
vda


More information about the busybox mailing list