Port: Add compat for platforms with no MSG_NOSIGNAL or SO_NOSIGPIPE
authorMichael Jeanson <mjeanson@efficios.com>
Mon, 28 Sep 2015 15:53:19 +0000 (11:53 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 13 Oct 2015 22:49:32 +0000 (18:49 -0400)
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
formats/lttng-live/lttng-live-comm.c
include/Makefile.am
include/babeltrace/compat/send.h [new file with mode: 0644]

index c01ac8eb2ddabb2a8f5a9ac624871742c3b38ba8..6cad4ab56bcade3c4ddc6be9ce11e0822c802522 100644 (file)
@@ -52,6 +52,7 @@
 
 #include <babeltrace/endian.h>
 #include <babeltrace/compat/memstream.h>
+#include <babeltrace/compat/send.h>
 
 #include "lttng-live.h"
 #include "lttng-viewer-abi.h"
@@ -103,7 +104,7 @@ ssize_t lttng_live_send(int fd, const void *buf, size_t len)
        ssize_t ret;
 
        do {
-               ret = send(fd, buf, len, MSG_NOSIGNAL);
+               ret = bt_send_nosigpipe(fd, buf, len);
        } while (ret < 0 && errno == EINTR);
        return ret;
 }
index 88c28c28e3501b162e0bb2e19d2f5d346623e66c..636f1cecf01dbfec7d9fcc42101740a01fcdbbdd 100644 (file)
@@ -72,5 +72,6 @@ noinst_HEADERS = \
        babeltrace/compat/utc.h \
        babeltrace/compat/limits.h \
        babeltrace/compat/glib.h \
+       babeltrace/compat/send.h \
        babeltrace/endian.h \
        babeltrace/mmap-align.h
diff --git a/include/babeltrace/compat/send.h b/include/babeltrace/compat/send.h
new file mode 100644 (file)
index 0000000..5654d95
--- /dev/null
@@ -0,0 +1,83 @@
+#ifndef _BABELTRACE_COMPAT_SEND_H
+#define _BABELTRACE_COMPAT_SEND_H
+
+/*
+ * babeltrace/compat/send.h
+ *
+ * Copyright (C) 2015  Michael Jeanson <mjeanson@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <config.h>
+
+/*
+ * This wrapper is used on platforms that have no way of ignoring SIGPIPE
+ * during a send(). Instead, we set the signal action to ignore. This is OK
+ * in a single-threaded app, but would be problematic in a multi-threaded app
+ * since sigaction applies to all threads.
+ */
+
+#ifndef MSG_NOSIGNAL
+# ifdef SO_NOSIGPIPE
+#   define MSG_NOSIGNAL SO_NOSIGPIPE
+# endif
+#endif
+
+#if defined(MSG_NOSIGNAL)
+static inline
+ssize_t bt_send_nosigpipe(int fd, const void *buffer, size_t size)
+{
+       return send(fd, buffer, size, MSG_NOSIGNAL);
+}
+#else
+static inline
+ssize_t bt_send_nosigpipe(int fd, const void *buffer, size_t size)
+{
+       ssize_t sent;
+       int saved_err;
+       struct sigaction act, oldact;
+
+       /* Set SIGPIPE action to ignore and save current signal action */
+       act.sa_handler = SIG_IGN;
+       if (sigaction(SIGPIPE, &act, &oldact)) {
+               perror("sigaction");
+               sent = -1;
+               goto end;
+       }
+
+       /* Send and save errno */
+       sent = send(fd, buffer, size, 0);
+       saved_err = errno;
+
+       /* Restore original signal action */
+       if (sigaction(SIGPIPE, &oldact, NULL)) {
+               perror("sigaction");
+               sent = -1;
+               goto end;
+       }
+
+       /* Restore send() errno */
+       errno = saved_err;
+end:
+       return sent;
+}
+#endif
+
+#endif /* _BABELTRACE_COMPAT_SEND_H */
This page took 0.026809 seconds and 4 git commands to generate.