From cc133e1865af68b87b61e558fa5e40a302f60c76 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Thu, 12 Apr 2007 16:11:40 +0000 Subject: [PATCH] Work around glibc's failure to reset seekable stdin on exit. * modules/closein: New module. * lib/closein.c: New file. * lib/closein.h: Likewise. * m4/closein.m4: Likewise. * MODULES.html.sh (File stream based Input/Output): Document it. --- ChangeLog | 9 +++++ MODULES.html.sh | 1 + lib/closein.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/closein.h | 33 +++++++++++++++++++ m4/closein.m4 | 13 ++++++++ modules/closein | 26 +++++++++++++++ 6 files changed, 182 insertions(+) create mode 100644 lib/closein.c create mode 100644 lib/closein.h create mode 100644 m4/closein.m4 create mode 100644 modules/closein diff --git a/ChangeLog b/ChangeLog index e14f7d91f..9cb7ecb3d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2007-04-12 Eric Blake + + Work around glibc's failure to reset seekable stdin on exit. + * modules/closein: New module. + * lib/closein.c: New file. + * lib/closein.h: Likewise. + * m4/closein.m4: Likewise. + * MODULES.html.sh (File stream based Input/Output): Document it. + 2007-04-12 Simon Josefsson * gnulib-tool: Rename generated 'autobuild' script to diff --git a/MODULES.html.sh b/MODULES.html.sh index 3f13fa35e..277b09d62 100755 --- a/MODULES.html.sh +++ b/MODULES.html.sh @@ -2192,6 +2192,7 @@ func_all_modules () func_begin_table func_module close-stream + func_module closein func_module closeout func_module fopen-safer func_module fpending diff --git a/lib/closein.c b/lib/closein.c new file mode 100644 index 000000000..1aade1d18 --- /dev/null +++ b/lib/closein.c @@ -0,0 +1,100 @@ +/* Close standard input, rewinding seekable stdin if necessary. + + Copyright (C) 2007 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#include + +#include "closein.h" +#include "closeout.h" + +#include +#include +#include +#include + +#include "gettext.h" +#define _(msgid) gettext (msgid) + +#include "close-stream.h" +#include "error.h" +#include "exitfail.h" +#include "quotearg.h" + +static const char *file_name; + +/* Set the file name to be reported in the event an error is detected + on stdin by close_stdin. See also close_stdout_set_file_name, if + an error is detected when closing stdout. */ +void +close_stdin_set_file_name (const char *file) +{ + file_name = file; +} + +/* Close standard input, rewinding any unused input if stdin is + seekable. On error, issue a diagnostic and _exit with status + 'exit_failure'. Then call close_stdout. + + Most programs can get by with close_stdout. close_stdin is only + needed when a program wants to guarantee that partially read input + from seekable stdin is not consumed, for any subsequent clients. + For example, POSIX requires that these two commands behave alike: + + (sed -ne 1q; cat) < file + tail -n 1 file + + Since close_stdin is commonly registered via 'atexit', POSIX + and the C standard both say that it should not call 'exit', + because the behavior is undefined if 'exit' is called more than + once. So it calls '_exit' instead of 'exit'. If close_stdin + is registered via atexit before other functions are registered, + the other functions can act before this _exit is invoked. + + Applications that use close_stdout should flush any streams other + than stdin, stdout, and stderr before exiting, since the call to + _exit will bypass other buffer flushing. Applications should be + flushing and closing other streams anyway, to check for I/O errors. + Also, applications should not use tmpfile, since _exit can bypass + the removal of these files. + + It's important to detect such failures and exit nonzero because many + tools (most notably `make' and other build-management systems) depend + on being able to detect failure in other tools via their exit status. */ + +void +close_stdin (void) +{ + bool fail = false; + if (close_stream (stdin) != 0) + { + char const *close_error = _("error closing file"); + if (file_name) + error (0, errno, "%s: %s", quotearg_colon (file_name), + close_error); + else + error (0, errno, "%s", close_error); + + /* Defer failure until after closing stdout, since the output + can still usefully be flushed. */ + fail = true; + } + + close_stdout (); + + if (fail) + _exit (exit_failure); +} diff --git a/lib/closein.h b/lib/closein.h new file mode 100644 index 000000000..43a713d45 --- /dev/null +++ b/lib/closein.h @@ -0,0 +1,33 @@ +/* Close standard input, rewinding seekable stdin if necessary. + + Copyright (C) 2007 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#ifndef _GL_CLOSEIN_H +# define _GL_CLOSEIN_H 1 + +# ifdef __cplusplus +extern "C" { +# endif + +void close_stdin_set_file_name (const char *file); +void close_stdin (void); + +# ifdef __cplusplus +} +# endif + +#endif diff --git a/m4/closein.m4 b/m4/closein.m4 new file mode 100644 index 000000000..bf8ff3845 --- /dev/null +++ b/m4/closein.m4 @@ -0,0 +1,13 @@ +# closein.m4 serial 1 +dnl Copyright (C) 2007 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_CLOSEIN], +[ + AC_LIBOBJ([closein]) + + dnl Prerequisites of lib/closein.c. + : +]) diff --git a/modules/closein b/modules/closein new file mode 100644 index 000000000..fefeae364 --- /dev/null +++ b/modules/closein @@ -0,0 +1,26 @@ +Description: +Close all standard streams, resetting seekable stdin if needed, and exiting with a diagnostic on error. + +Files: +lib/closein.h +lib/closein.c +m4/closein.m4 + +Depends-on: +closeout +fflush +stdbool + +configure.ac: +gl_CLOSEIN + +Makefile.am: + +Include: +"closein.h" + +License: +GPL + +Maintainer: +Eric Blake -- 2.11.0