[lvm-devel] [PATCH 11/20] Add test for 'read' result

Zdenek Kabelac zkabelac at redhat.com
Thu Dec 16 10:36:19 UTC 2010


Read result should not be ignored.
Add some small check even when there is very low probability to
fail in this place.

FIXME Generic read routine should be used.

Signed-off-by: Zdenek Kabelac <zkabelac at redhat.com>
---
 tools/lvmcmdline.c |   16 ++++++++++------
 1 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/tools/lvmcmdline.c b/tools/lvmcmdline.c
index 9f20d4b..dcb1f28 100644
--- a/tools/lvmcmdline.c
+++ b/tools/lvmcmdline.c
@@ -1191,15 +1191,19 @@ static const char *_get_cmdline(pid_t pid)
 {
 	static char _proc_cmdline[32];
 	char buf[256];
-	int fd;
+	int fd, n = 0;
 
 	snprintf(buf, sizeof(buf), DEFAULT_PROC_DIR "/%u/cmdline", pid);
+	/* FIXME Use generic read code. */
 	if ((fd = open(buf, O_RDONLY)) > 0) {
-		read(fd, _proc_cmdline, sizeof(_proc_cmdline) - 1);
-		_proc_cmdline[sizeof(_proc_cmdline) - 1] = '\0';
-		close(fd);
-	} else
-		_proc_cmdline[0] = '\0';
+		if ((n = read(fd, _proc_cmdline, sizeof(_proc_cmdline) - 1)) < 0) {
+			log_sys_error("read", buf);
+			n = 0;
+		}
+		if (close(fd))
+			log_sys_error("close", buf);
+	}
+	_proc_cmdline[n] = '\0';
 
 	return _proc_cmdline;
 }
-- 
1.7.3.3




More information about the lvm-devel mailing list