[pull] check stream status

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,

Another bigger change for this evening.  The 14 patches I wrote
divide to two, copy of a solution from gnulib and apply of it to each
source directory separately.  The later 13 patches could be squashed
to be one big patch, which I leave for Karel to decide whether that
should or shouldn't be done.

The most important patch is featured below which adds functions to
close streams atexit() and on request in place of fclose().  As
mentioned in internet talk many, if not all, commands failed when
executed by directing output to /dev/full or to a closed file
descriptor.  For example

$ flock -V > /dev/full ; echo $?
0
$ flock -V >&- ; echo $?
0

is wrong because one should expect error message, and non-zero exit
code.

$ flock -V >/dev/full ; echo $?
flock: write error: No space left on device
1
$ flock -V >&- ; echo $?
flock: write error: Bad file descriptor
1

Notice that I left 'login' and 'agetty' commands untouched, because I
where not quite sure how they should deal stdout or stderr write
errors.  Perhaps both should inform to syslog writing failed.
Assuming someone has opinion how they should behave he/she surely
writes a patch on top of these.

Comments, suggestions?


>From dbcad72f800b4ed956815da2ddb751a396e03fb3 Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@xxxxxx>
Date: Sat, 24 Mar 2012 20:46:59 +0100
Subject: [PATCH 02/16] lib: add fileutils file with stream error checking
 facility

The close_stream() is copied from GNU lib.  Inspiration to do this
is talk by Jim Meyering - Goodbye World!  The perils of relying on
output streams in C.

Reference: http://www.irill.org/events/ghm-gnu-hackers-meeting/videos/jim-meyering-goodbye-world-the-perils-of-relying-on-output-streams-in-c
Signed-off-by: Sami Kerola <kerolasa@xxxxxx>
---
 include/fileutils.h |    2 ++
 lib/fileutils.c     |   37 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/include/fileutils.h b/include/fileutils.h
index 691429b..a49ff47 100644
--- a/include/fileutils.h
+++ b/include/fileutils.h
@@ -1,6 +1,8 @@
 #ifndef UTIL_LINUX_FILEUTILS
 #define UTIL_LINUX_FILEUTILS

+int close_stream(FILE * stream);
+void close_stdout(void);
 extern int xmkstemp(char **tmpname);

 static inline FILE *xfmkstemp(char **tmpname)
diff --git a/lib/fileutils.c b/lib/fileutils.c
index 4849061..24aae9f 100644
--- a/lib/fileutils.c
+++ b/lib/fileutils.c
@@ -2,16 +2,46 @@
  * Copyright (C) 2012 Sami Kerola <kerolasa@xxxxxx>
  */

+#include <errno.h>
+#include <error.h>
 #include <stdio.h>
+#include <stdio_ext.h>
 #include <stdlib.h>
 #include <sys/stat.h>
 #include <unistd.h>

 #include "c.h"
 #include "fileutils.h"
+#include "nls.h"
 #include "pathnames.h"
 #include "xalloc.h"

+int close_stream(FILE * stream)
+{
+	const int some_pending = (__fpending(stream) != 0);
+	const int prev_fail = (ferror(stream) != 0);
+	const int fclose_fail = (fclose(stream) != 0);
+	if (prev_fail || (fclose_fail && (some_pending || errno != EBADF))) {
+		if (!fclose_fail)
+			errno = 0;
+		return EOF;
+	}
+	return 0;
+}
+
+/* Use atexit(); */
+void close_stdout(void)
+{
+	if (close_stream(stdout) != 0 && !(errno == EPIPE)) {
+		char const *write_error = _("write error");
+		error(0, errno, "%s", write_error);
+		_exit(EXIT_FAILURE);
+	}
+
+	if (close_stream(stderr) != 0)
+		_exit(EXIT_FAILURE);
+}
+
 /* Create open temporary file in safe way.  Please notice that the
  * file permissions are -rw------- by default. */
 int xmkstemp(char **tmpname)
@@ -40,11 +70,16 @@ int xmkstemp(char **tmpname)
 }

 #ifdef TEST_PROGRAM
-int main(void)
+int main(int argc, char **argv)
 {
 	FILE *f;
 	char *tmpname;
+	atexit(close_stdout);
 	f = xfmkstemp(&tmpname);
+	/* Write something to stdout so that tests such as
+	 * test_fileutils >&- || test_fileutils >/dev/full
+	 * has change to work properly. */
+	printf("%s\n", tmpname);
 	unlink(tmpname);
 	free(tmpname);
 	fclose(f);
-- 
1.7.9.5



The following changes since commit a36e9a94087acb4f375fca000b5968623b6b58a7:

  build-sys: move obsolete lib/fsprobe to mount/ (2012-03-27 12:23:07 +0200)

are available in the git repository at:

  git://github.com/kerolasa/lelux-utiliteetit.git close_stream

for you to fetch changes up to ce696e760edf003164d4a95287c2ef575a680927:

  disk-utils: verify writing to streams was successful (2012-03-29
21:54:41 +0200)

----------------------------------------------------------------
Sami Kerola (14):
      lib: add fileutils file with stream error checking facility
      text-utils: verify writing to streams was successful
      term-utils: verify writing to streams was successful
      include/fileutils: fix implicit declaration warning
      sys-utils: verify writing to streams was successful
      schedutils: verify writing to streams was successful
      partx: verify writing to streams was successful
      mount: verify writing to streams was successful
      misc-utils: verify writing to streams was successful
      login-utils: verify writing to streams was successful
      hwclock: verify writing to streams was successful
      getopt: verify writing to streams was successful
      fdisk: verify writing to streams was successful
      disk-utils: verify writing to streams was successful

 disk-utils/Makefile.am    |   19 +++++++--
 disk-utils/blockdev.c     |    2 +
 disk-utils/elvtune.c      |    2 +
 disk-utils/fdformat.c     |    2 +
 disk-utils/fsck.c         |    2 +
 disk-utils/fsck.cramfs.c  |    2 +
 disk-utils/fsck.minix.c   |    2 +
 disk-utils/isosize.c      |    2 +
 disk-utils/mkfs.bfs.c     |    2 +
 disk-utils/mkfs.c         |    2 +
 disk-utils/mkfs.cramfs.c  |    2 +
 disk-utils/mkfs.minix.c   |    2 +
 disk-utils/mkswap.c       |    2 +
 disk-utils/raw.c          |    3 +-
 disk-utils/swaplabel.c    |    2 +
 fdisk/Makefile.am         |    1 +
 fdisk/cfdisk.c            |    2 +
 fdisk/fdisk.c             |    2 +
 fdisk/gpt.c               |    2 +
 fdisk/partitiontype.c     |    3 ++
 fdisk/sfdisk.c            |    2 +
 getopt/Makefile.am        |    1 +
 getopt/getopt.c           |    2 +
 hwclock/Makefile.am       |    1 +
 hwclock/hwclock.c         |    7 +++-
 include/fileutils.h       |    4 ++
 lib/Makefile.am           |    1 +
 lib/fileutils.c           |   39 +++++++++++++++++-
 login-utils/Makefile.am   |   13 ++++--
 login-utils/chfn.c        |    2 +
 login-utils/chsh.c        |    2 +
 login-utils/islocal.c     |    2 +
 login-utils/last.c        |    2 +
 login-utils/logindefs.c   |    2 +
 login-utils/newgrp.c      |    2 +
 login-utils/setpwnam.c    |    4 +-
 login-utils/sulogin.c     |    2 +
 login-utils/vipw.c        |    4 +-
 misc-utils/Makefile.am    |   20 +++++++--
 misc-utils/blkid.c        |    2 +
 misc-utils/cal.c          |    2 +
 misc-utils/ddate.c        |    2 +
 misc-utils/findfs.c       |    2 +
 misc-utils/findmnt.c      |    2 +
 misc-utils/kill.c         |    2 +
 misc-utils/logger.c       |    2 +
 misc-utils/look.c         |    2 +
 misc-utils/lsblk.c        |    2 +
 misc-utils/lslocks.c      |    2 +
 misc-utils/mcookie.c      |   11 ++---
 misc-utils/namei.c        |    2 +
 misc-utils/rename.c       |    2 +
 misc-utils/uuidd.c        |    2 +
 misc-utils/uuidgen.c      |    2 +
 misc-utils/whereis.c      |    2 +
 misc-utils/wipefs.c       |    2 +
 mount/Makefile.am         |    1 +
 mount/mount.c             |    2 +
 mount/mount_mntent.c      |    6 ++-
 mount/umount.c            |    2 +
 partx/Makefile.am         |    1 +
 partx/partx.c             |    2 +
 schedutils/Makefile.am    |    5 ++-
 schedutils/chrt.c         |    3 +-
 schedutils/ionice.c       |    2 +
 schedutils/taskset.c      |    2 +
 sys-utils/Makefile.am     |   98 ++++++++++++++++++++++++++++++++++++---------
 sys-utils/arch.c          |    2 +
 sys-utils/chcpu.c         |    2 +
 sys-utils/ctrlaltdel.c    |    2 +
 sys-utils/cytune.c        |    2 +
 sys-utils/dmesg.c         |    2 +
 sys-utils/fallocate.c     |    3 +-
 sys-utils/flock.c         |    2 +
 sys-utils/fsfreeze.c      |    2 +
 sys-utils/fstrim.c        |    2 +
 sys-utils/ipcmk.c         |    2 +
 sys-utils/ipcrm.c         |    2 +
 sys-utils/ipcs.c          |    2 +
 sys-utils/ldattach.c      |    2 +
 sys-utils/losetup.c       |    2 +
 sys-utils/lscpu.c         |    2 +
 sys-utils/mount.c         |    2 +
 sys-utils/mountpoint.c    |    2 +
 sys-utils/pivot_root.c    |    2 +
 sys-utils/prlimit.c       |    2 +
 sys-utils/readprofile.c   |    2 +
 sys-utils/renice.c        |    2 +
 sys-utils/rtcwake.c       |    5 ++-
 sys-utils/setarch.c       |    2 +
 sys-utils/setsid.c        |    2 +
 sys-utils/swapon.c        |    2 +
 sys-utils/switch_root.c   |    2 +
 sys-utils/tunelp.c        |    2 +
 sys-utils/umount.c        |    2 +
 sys-utils/unshare.c       |    2 +
 term-utils/Makefile.am    |   12 +++++-
 term-utils/mesg.c         |    3 ++
 term-utils/script.c       |   16 ++++++--
 term-utils/scriptreplay.c |    2 +
 term-utils/setterm.c      |    5 ++-
 term-utils/ttymsg.c       |    3 +-
 term-utils/wall.c         |    4 +-
 term-utils/write.c        |    3 ++
 text-utils/Makefile.am    |   37 +++++++++++++----
 text-utils/col.c          |    4 +-
 text-utils/colcrt.c       |    4 +-
 text-utils/colrm.c        |    5 ++-
 text-utils/column.c       |    5 +--
 text-utils/hexdump.c      |    2 +
 text-utils/more.c         |   18 +++++----
 text-utils/pg.c           |    2 +
 text-utils/rev.c          |    2 +
 text-utils/tailf.c        |    2 +
 text-utils/ul.c           |    5 +--
 115 files changed, 446 insertions(+), 87 deletions(-)

-- 
   Sami Kerola
   http://www.iki.fi/kerolasa/
--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux