[PATCH] prevent retries on fclose/fflush after write errors

Kevin Cernekee cernekee at gmail.com
Sat Mar 10 22:55:33 UTC 2012

On Sat, Feb 12, 2011 at 6:31 PM, Denys Vlasenko
<vda.linux at googlemail.com> wrote:
>> >Currently, uclibc retains buffered data on stdio write errors,
>> >and subsequent fclose and fflush will try to write it out again
>> >(in most cases, in vain).

This seems to be causing some strange problems on bash as well.  Is
there any interest in trying to mimic the glibc behavior?

uClibc version is

bash problem:

# bash --version
GNU bash, version 3.2.0(1)-release (mipsel-unknown-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.
# echo "none of the above" > /sys/power/state
sh: echo: write error: Invalid argument
# aaaaa
sh: aaaaa: command not found
none of the abov# bbbbb
sh: bbbbb: command not found
none of the abov#
# echo mem > /sys/power/state
sh: echo: write error: Invalid argument
# echo mem > /sys/power/state
sh: echo: write error: Invalid argument
# busybox echo mem > /sys/power/state
PM: Syncing filesystems ... done.
Freezing user space processes ... (elapsed 0.01 seconds) done.
Freezing remaining freezable tasks ... (elapsed 0.01 seconds) done.
Suspending console(s) (use no_console_suspend to debug)

Simple test case to show that the buffered data persists (albeit in
truncated form) even after calling fflush() and clearerr():

-- 8< --

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char **argv)
	int tmpfd, fd;

	tmpfd = fcntl(fileno(stdout), F_DUPFD, 0);
	fd = open("/dev/null", O_RDONLY);

	dup2(fd, fileno(stdout));

	printf("hello world\n");
	if (ferror(stdout))

	dup2(tmpfd, fileno(stdout));

	printf("goodbye world\n");

	return 0;

-- 8< --


# ./test
hello worlgoodbye world

More information about the uClibc mailing list