From: Michael Jeanson Date: Mon, 28 Sep 2015 15:53:19 +0000 (-0400) Subject: Port: Add compat for platforms with no MSG_NOSIGNAL or SO_NOSIGPIPE X-Git-Tag: v1.3.0~25 X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=b319430e3071f61a34b1df73327f3f46e7661c0d Port: Add compat for platforms with no MSG_NOSIGNAL or SO_NOSIGPIPE Signed-off-by: Michael Jeanson Signed-off-by: Jérémie Galarneau --- diff --git a/formats/lttng-live/lttng-live-comm.c b/formats/lttng-live/lttng-live-comm.c index d60126f4..485dabba 100644 --- a/formats/lttng-live/lttng-live-comm.c +++ b/formats/lttng-live/lttng-live-comm.c @@ -52,6 +52,7 @@ #include #include +#include #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; } diff --git a/include/Makefile.am b/include/Makefile.am index 8599efd7..9109642d 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -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 index 00000000..5654d95f --- /dev/null +++ b/include/babeltrace/compat/send.h @@ -0,0 +1,83 @@ +#ifndef _BABELTRACE_COMPAT_SEND_H +#define _BABELTRACE_COMPAT_SEND_H + +/* + * babeltrace/compat/send.h + * + * Copyright (C) 2015 Michael Jeanson + * + * 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 + +/* + * 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 */