From 4714e12e8feadcf62846323bffae2c5506ffd749 Mon Sep 17 00:00:00 2001 From: Sitsofe Wheeler Date: Sat, 30 Jan 2016 07:41:34 +0000 Subject: [PATCH] socket: Use alternatives if MSG_NOSIGNAL is unavailable Only Linux has MSG_NOSIGNAL as a socket flag (see http://stackoverflow.com/questions/108183/how-to-prevent-sigpipes-or-handle-them-properly ) which makes compilation fail on other OSes. The BSDs and OS X have SO_NOSIGPIPE which implements the same thing but others like Solaris don't even have that (see http://stackoverflow.com/questions/2205455/detecting-broken-pipe-in-solaris-send-call )... Work around this by checking using MSG_NOSIGNAL or SO_NOSIGPIPE if available otherwise don't set anything at all so we at least continue to compile. --- lib/socket.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/socket.c b/lib/socket.c index c308b4c..fb64261 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -643,6 +643,13 @@ iscsi_write_to_socket(struct iscsi_context *iscsi) size_t total; struct iscsi_pdu *pdu; static char padding_buf[3]; + int socket_flags = 0; + +#ifdef MSG_NOSIGNAL + socket_flags |= MSG_NOSIGNAL; +#elif SO_NOSIGPIPE + socket_flags |= SO_NOSIGPIPE; +#endif if (iscsi->fd == -1) { iscsi_set_error(iscsi, "trying to write but not connected"); @@ -697,7 +704,7 @@ iscsi_write_to_socket(struct iscsi_context *iscsi) count = send(iscsi->fd, pdu->outdata.data + pdu->outdata_written, pdu->outdata.size - pdu->outdata_written, - MSG_NOSIGNAL); + socket_flags); if (count == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK) { return 0; @@ -746,7 +753,7 @@ iscsi_write_to_socket(struct iscsi_context *iscsi) /* Write padding */ if (pdu->payload_written < total) { - count = send(iscsi->fd, padding_buf, total - pdu->payload_written, MSG_NOSIGNAL); + count = send(iscsi->fd, padding_buf, total - pdu->payload_written, socket_flags); if (count == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK) { return 0;