[PATCH V7 1/4] libmpathcmd: Block SIGPIPE when write()

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

 



 * Just block SIGPIPE when write() and restore it when done.
 * Use 'errno_save()' to preserve the errno of write().

Signed-off-by: Gris Ge <fge@xxxxxxxxxx>
---
 libmpathcmd/mpath_cmd.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/libmpathcmd/mpath_cmd.c b/libmpathcmd/mpath_cmd.c
index 2290ecb..0daaf53 100644
--- a/libmpathcmd/mpath_cmd.c
+++ b/libmpathcmd/mpath_cmd.c
@@ -7,6 +7,7 @@
 #include <poll.h>
 #include <string.h>
 #include <errno.h>
+#include <signal.h>
 
 #include "mpath_cmd.h"
 
@@ -54,20 +55,33 @@ static ssize_t read_all(int fd, void *buf, size_t len, unsigned int timeout)
 static size_t write_all(int fd, const void *buf, size_t len)
 {
 	size_t total = 0;
+	sigset_t set, old;
+	int errno_save = 0;
+
+	/* Block SIGPIPE */
+	sigemptyset(&set);
+	sigaddset(&set, SIGPIPE);
+	pthread_sigmask(SIG_BLOCK, &set, &old);
 
 	while (len) {
 		ssize_t n = write(fd, buf, len);
+		errno_save = errno;
 		if (n < 0) {
 			if ((errno == EINTR) || (errno == EAGAIN))
 				continue;
-			return total;
+			goto out;
 		}
 		if (!n)
-			return total;
+			goto out;
 		buf = n + (char *)buf;
 		len -= n;
 		total += n;
 	}
+out:
+	/* And unblock it again */
+	pthread_sigmask(SIG_SETMASK, &old, NULL);
+	errno = errno_save;
+
 	return total;
 }
 
-- 
2.9.2

--
dm-devel mailing list
dm-devel@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/dm-devel



[Index of Archives]     [DM Crypt]     [Fedora Desktop]     [ATA RAID]     [Fedora Marketing]     [Fedora Packaging]     [Fedora SELinux]     [Yosemite Discussion]     [KDE Users]     [Fedora Docs]

  Powered by Linux