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)
committerMichael Jeanson <mjeanson@efficios.com>
Fri, 16 Oct 2015 19:40:28 +0000 (15:40 -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 d60126f4dd981c18097996611c497ffe9b8d712d..485dabba376cf67351b659edef18726770f6fdb2 100644 (file)
@@ -52,6 +52,7 @@
 #include <formats/ctf/events-private.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 8599efd7d3b8d792d9f21996df8ed15c84a7a781..9109642dcda8cb037c87640eccca7796c36436ce 100644 (file)
@@ -53,5 +53,6 @@ noinst_HEADERS = \
        babeltrace/compat/string.h \
        babeltrace/compat/utc.h \
        babeltrace/compat/limits.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.030785 seconds and 4 git commands to generate.