File 0001-toolcontext-Only-reopen-stdin-if-readable.patch of Package lvm2

Index: LVM2.2.02.98/lib/commands/toolcontext.c
===================================================================
--- LVM2.2.02.98.orig/lib/commands/toolcontext.c	2014-04-14 14:06:23.245391597 +0800
+++ LVM2.2.02.98/lib/commands/toolcontext.c	2014-04-14 14:06:47.761421565 +0800
@@ -1315,6 +1315,7 @@ struct cmd_context *create_toolcontext(u
 {
 	struct cmd_context *cmd;
 	FILE *new_stream;
+	int flags;
 
 #ifdef M_MMAP_MAX
 	mallopt(M_MMAP_MAX, 0);
@@ -1358,7 +1359,10 @@ struct cmd_context *create_toolcontext(u
 			goto out;
 		}
 
-		if (is_valid_fd(STDIN_FILENO)) {
+		/* nohup might set stdin O_WRONLY ! */
+		if (is_valid_fd(STDIN_FILENO) &&
+		    ((flags = fcntl(STDIN_FILENO, F_GETFL)) > 0) &&
+		    (flags & O_ACCMODE) != O_WRONLY) {
 			if (!_reopen_stream(stdin, STDIN_FILENO, "r", "stdin", &new_stream))
 				goto_out;
 			stdin = new_stream;
@@ -1368,7 +1372,9 @@ struct cmd_context *create_toolcontext(u
 			}
 		}
 
-		if (is_valid_fd(STDOUT_FILENO)) {
+		if (is_valid_fd(STDOUT_FILENO) &&
+		    ((flags = fcntl(STDOUT_FILENO, F_GETFL)) > 0) &&
+		    (flags & O_ACCMODE) != O_RDONLY) {
 			if (!_reopen_stream(stdout, STDOUT_FILENO, "w", "stdout", &new_stream))
 				goto_out;
 			stdout = new_stream;
@@ -1629,6 +1635,7 @@ void destroy_toolcontext(struct cmd_cont
 {
 	struct dm_config_tree *cft_cmdline;
 	FILE *new_stream;
+	int flags;
 
 	if (cmd->dump_filter)
 		persistent_filter_dump(cmd->filter, 1);
@@ -1654,7 +1661,9 @@ void destroy_toolcontext(struct cmd_cont
 #ifndef VALGRIND_POOL
 	if (cmd->linebuffer) {
 		/* Reset stream buffering to defaults */
-		if (is_valid_fd(STDIN_FILENO)) {
+		if (is_valid_fd(STDIN_FILENO) &&
+		    ((flags = fcntl(STDIN_FILENO, F_GETFL)) > 0) &&
+		    (flags & O_ACCMODE) != O_WRONLY) {
 			if (_reopen_stream(stdin, STDIN_FILENO, "r", "stdin", &new_stream)) {
 				stdin = new_stream;
 				setlinebuf(stdin);
@@ -1662,7 +1671,9 @@ void destroy_toolcontext(struct cmd_cont
 				cmd->linebuffer = NULL;	/* Leave buffer in place (deliberate leak) */
 		}
 
-		if (is_valid_fd(STDOUT_FILENO)) {
+		if (is_valid_fd(STDOUT_FILENO) &&
+		    ((flags = fcntl(STDOUT_FILENO, F_GETFL)) > 0) &&
+		    (flags & O_ACCMODE) != O_RDONLY) {
 			if (_reopen_stream(stdout, STDOUT_FILENO, "w", "stdout", &new_stream)) {
 				stdout = new_stream;
 				setlinebuf(stdout);
openSUSE Build Service is sponsored by