From 1cb3cdd7f163147ccc0ecfd7e421008eec799d64 Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Wed, 31 May 2017 15:01:40 -0400 Subject: [PATCH] Port: Add winsock support to live for mingw MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Michael Jeanson Signed-off-by: Jérémie Galarneau --- cli/Makefile.am | 2 +- include/Makefile.am | 2 +- include/babeltrace/compat/send-internal.h | 117 ------ include/babeltrace/compat/socket-internal.h | 417 ++++++++++++++++++++ plugins/ctf/lttng-live/Makefile.am | 4 + plugins/ctf/lttng-live/viewer-connection.c | 181 ++++----- plugins/ctf/lttng-live/viewer-connection.h | 3 +- 7 files changed, 517 insertions(+), 209 deletions(-) delete mode 100644 include/babeltrace/compat/send-internal.h create mode 100644 include/babeltrace/compat/socket-internal.h diff --git a/cli/Makefile.am b/cli/Makefile.am index d305bf69..d5918366 100644 --- a/cli/Makefile.am +++ b/cli/Makefile.am @@ -74,7 +74,7 @@ endif endif if BABELTRACE_BUILD_WITH_MINGW -babeltrace_bin_LDADD += -lrpcrt4 -lintl -liconv -lole32 -lpopt -lpthread +babeltrace_bin_LDADD += -lws2_32 -lrpcrt4 -lintl -liconv -lole32 -lpopt -lpthread endif # Only used for in-tree execution and tests diff --git a/include/Makefile.am b/include/Makefile.am index 0859427f..aeb7cdd8 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -81,7 +81,7 @@ noinst_HEADERS = \ babeltrace/compat/limits-internal.h \ babeltrace/compat/memstream-internal.h \ babeltrace/compat/mman-internal.h \ - babeltrace/compat/send-internal.h \ + babeltrace/compat/socket-internal.h \ babeltrace/compat/stdio-internal.h \ babeltrace/compat/stdlib-internal.h \ babeltrace/compat/string-internal.h \ diff --git a/include/babeltrace/compat/send-internal.h b/include/babeltrace/compat/send-internal.h deleted file mode 100644 index 39986945..00000000 --- a/include/babeltrace/compat/send-internal.h +++ /dev/null @@ -1,117 +0,0 @@ -#ifndef _BABELTRACE_COMPAT_SEND_H -#define _BABELTRACE_COMPAT_SEND_H - -/* - * babeltrace/compat/send.h - * - * Copyright (C) 2015 Michael Jeanson - * 2015 Mathieu Desnoyers - * - * 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. - */ - -/* - * This wrapper is used on platforms that have no way of ignoring SIGPIPE - * during a send(). - */ - -#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 - -#include - -static inline -ssize_t bt_send_nosigpipe(int fd, const void *buffer, size_t size) -{ - ssize_t sent; - int saved_err; - sigset_t sigpipe_set, pending_set, old_set; - int sigpipe_was_pending; - - /* - * Discard the SIGPIPE from send(), not disturbing any SIGPIPE - * that might be already pending. If a bogus SIGPIPE is sent to - * the entire process concurrently by a malicious user, it may - * be simply discarded. - */ - if (sigemptyset(&pending_set)) { - return -1; - } - /* - * sigpending returns the mask of signals that are _both_ - * blocked for the thread _and_ pending for either the thread or - * the entire process. - */ - if (sigpending(&pending_set)) { - return -1; - } - sigpipe_was_pending = sigismember(&pending_set, SIGPIPE); - /* - * If sigpipe was pending, it means it was already blocked, so - * no need to block it. - */ - if (!sigpipe_was_pending) { - if (sigemptyset(&sigpipe_set)) { - return -1; - } - if (sigaddset(&sigpipe_set, SIGPIPE)) { - return -1; - } - if (pthread_sigmask(SIG_BLOCK, &sigpipe_set, &old_set)) { - return -1; - } - } - - /* Send and save errno. */ - sent = send(fd, buffer, size, 0); - saved_err = errno; - - if (sent == -1 && errno == EPIPE && !sigpipe_was_pending) { - struct timespec timeout = { 0, 0 }; - int ret; - - do { - ret = sigtimedwait(&sigpipe_set, NULL, - &timeout); - } while (ret == -1 && errno == EINTR); - } - if (!sigpipe_was_pending) { - if (pthread_sigmask(SIG_SETMASK, &old_set, NULL)) { - return -1; - } - } - /* Restore send() errno */ - errno = saved_err; - - return sent; -} -#endif - -#endif /* _BABELTRACE_COMPAT_SEND_H */ diff --git a/include/babeltrace/compat/socket-internal.h b/include/babeltrace/compat/socket-internal.h new file mode 100644 index 00000000..0bbe83e4 --- /dev/null +++ b/include/babeltrace/compat/socket-internal.h @@ -0,0 +1,417 @@ +#ifndef _BABELTRACE_COMPAT_SOCKET_H +#define _BABELTRACE_COMPAT_SOCKET_H + +/* + * babeltrace/compat/socket.h + * + * Copyright (C) 2015-2017 Michael Jeanson + * 2015 Mathieu Desnoyers + * + * 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. + */ + +#ifdef __MINGW32__ + +#include + +#define BT_INVALID_SOCKET INVALID_SOCKET +#define BT_SOCKET_ERROR SOCKET_ERROR +#define BT_SOCKET SOCKET + +static inline +int bt_socket_init(void) +{ + WORD verreq; + WSADATA wsa; + int ret; + + /* Request winsock 2.2 support */ + verreq = MAKEWORD(2, 2); + + ret = WSAStartup(verreq, &wsa); + if (ret != 0) { + BT_LOGE("Winsock init failed with error: %d", ret); + goto end; + } + + if (LOBYTE(wsa.wVersion) != 2 || HIBYTE(wsa.wVersion) != 2) { + BT_LOGE_STR("Could not init winsock 2.2 support"); + WSACleanup(); + ret = -1; + } + +end: + return ret; +} + +static inline +int bt_socket_fini(void) +{ + return WSACleanup(); +} + +static inline +int bt_socket_send(int sockfd, const void *buf, size_t len, int flags) +{ + return send(sockfd, buf, len, flags); +} + +static inline +int bt_socket_recv(int sockfd, void *buf, size_t len, int flags) +{ + return recv(sockfd, buf, len, flags); +} + +static inline +int bt_socket_close(int fd) +{ + return closesocket(fd); +} + +static inline +bool bt_socket_interrupted(void) +{ + /* There is no equivalent to EINTR in winsock 2.2 */ + return false; +} + +static inline +const char *bt_socket_errormsg(void) +{ + const char *errstr; + int error = WSAGetLastError(); + + switch (error) { + case WSAEINTR: + errstr = "Call interrupted"; + break; + case WSAEBADF: + errstr = "Bad file"; + break; + case WSAEACCES: + errstr = "Bad access"; + break; + case WSAEFAULT: + errstr = "Bad argument"; + break; + case WSAEINVAL: + errstr = "Invalid arguments"; + break; + case WSAEMFILE: + errstr = "Out of file descriptors"; + break; + case WSAEWOULDBLOCK: + errstr = "Call would block"; + break; + case WSAEINPROGRESS: + case WSAEALREADY: + errstr = "Blocking call in progress"; + break; + case WSAENOTSOCK: + errstr = "Descriptor is not a socket"; + break; + case WSAEDESTADDRREQ: + errstr = "Need destination address"; + break; + case WSAEMSGSIZE: + errstr = "Bad message size"; + break; + case WSAEPROTOTYPE: + errstr = "Bad protocol"; + break; + case WSAENOPROTOOPT: + errstr = "Protocol option is unsupported"; + break; + case WSAEPROTONOSUPPORT: + errstr = "Protocol is unsupported"; + break; + case WSAESOCKTNOSUPPORT: + errstr = "Socket is unsupported"; + break; + case WSAEOPNOTSUPP: + errstr = "Operation not supported"; + break; + case WSAEAFNOSUPPORT: + errstr = "Address family not supported"; + break; + case WSAEPFNOSUPPORT: + errstr = "Protocol family not supported"; + break; + case WSAEADDRINUSE: + errstr = "Address already in use"; + break; + case WSAEADDRNOTAVAIL: + errstr = "Address not available"; + break; + case WSAENETDOWN: + errstr = "Network down"; + break; + case WSAENETUNREACH: + errstr = "Network unreachable"; + break; + case WSAENETRESET: + errstr = "Network has been reset"; + break; + case WSAECONNABORTED: + errstr = "Connection was aborted"; + break; + case WSAECONNRESET: + errstr = "Connection was reset"; + break; + case WSAENOBUFS: + errstr = "No buffer space"; + break; + case WSAEISCONN: + errstr = "Socket is already connected"; + break; + case WSAENOTCONN: + errstr = "Socket is not connected"; + break; + case WSAESHUTDOWN: + errstr = "Socket has been shut down"; + break; + case WSAETOOMANYREFS: + errstr = "Too many references"; + break; + case WSAETIMEDOUT: + errstr = "Timed out"; + break; + case WSAECONNREFUSED: + errstr = "Connection refused"; + break; + case WSAELOOP: + errstr = "Loop??"; + break; + case WSAENAMETOOLONG: + errstr = "Name too long"; + break; + case WSAEHOSTDOWN: + errstr = "Host down"; + break; + case WSAEHOSTUNREACH: + errstr = "Host unreachable"; + break; + case WSAENOTEMPTY: + errstr = "Not empty"; + break; + case WSAEPROCLIM: + errstr = "Process limit reached"; + break; + case WSAEUSERS: + errstr = "Too many users"; + break; + case WSAEDQUOT: + errstr = "Bad quota"; + break; + case WSAESTALE: + errstr = "Something is stale"; + break; + case WSAEREMOTE: + errstr = "Remote error"; + break; + case WSAEDISCON: + errstr = "Disconnected"; + break; + + /* Extended Winsock errors */ + case WSASYSNOTREADY: + errstr = "Winsock library is not ready"; + break; + case WSANOTINITIALISED: + errstr = "Winsock library not initialised"; + break; + case WSAVERNOTSUPPORTED: + errstr = "Winsock version not supported"; + break; + + /* getXbyY() errors (already handled in herrmsg): + * Authoritative Answer: Host not found */ + case WSAHOST_NOT_FOUND: + errstr = "Host not found"; + break; + + /* Non-Authoritative: Host not found, or SERVERFAIL */ + case WSATRY_AGAIN: + errstr = "Host not found, try again"; + break; + + /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */ + case WSANO_RECOVERY: + errstr = "Unrecoverable error in call to nameserver"; + break; + + /* Valid name, no data record of requested type */ + case WSANO_DATA: + errstr = "No data record of requested type"; + break; + + default: + errstr = "Unknown error"; + } + + return errstr; +} + +#else /* __MINGW32__ */ + +#include +#include +#include +#include + +#define BT_INVALID_SOCKET -1 +#define BT_SOCKET_ERROR -1 +#define BT_SOCKET int + +static inline +int bt_socket_init(void) +{ + return 0; +} + +static inline +int bt_socket_fini(void) +{ + return 0; +} + +static inline +int bt_socket_send(int sockfd, const void *buf, size_t len, int flags) +{ + return send(sockfd, buf, len, flags); +} + +static inline +int bt_socket_recv(int sockfd, void *buf, size_t len, int flags) +{ + return recv(sockfd, buf, len, flags); +} + +static inline +int bt_socket_close(int fd) +{ + return close(fd); +} + +static inline +bool bt_socket_interrupted(void) +{ + return (errno == EINTR); +} + +static inline +const char *bt_socket_errormsg(void) +{ + return g_strerror(errno); +} +#endif + + +/* + * This wrapper is used on platforms that have no way of ignoring SIGPIPE + * during a send(). + */ + +#ifndef MSG_NOSIGNAL +# ifdef SO_NOSIGPIPE +# define MSG_NOSIGNAL SO_NOSIGPIPE +# elif defined(__MINGW32__) +# define MSG_NOSIGNAL 0 +# endif +#endif + +#if defined(MSG_NOSIGNAL) +static inline +ssize_t bt_socket_send_nosigpipe(int fd, const void *buffer, size_t size) +{ + return bt_socket_send(fd, buffer, size, MSG_NOSIGNAL); +} +#else + +#include + +static inline +ssize_t bt_socket_send_nosigpipe(int fd, const void *buffer, size_t size) +{ + ssize_t sent; + int saved_err; + sigset_t sigpipe_set, pending_set, old_set; + int sigpipe_was_pending; + + /* + * Discard the SIGPIPE from send(), not disturbing any SIGPIPE + * that might be already pending. If a bogus SIGPIPE is sent to + * the entire process concurrently by a malicious user, it may + * be simply discarded. + */ + if (sigemptyset(&pending_set)) { + return -1; + } + /* + * sigpending returns the mask of signals that are _both_ + * blocked for the thread _and_ pending for either the thread or + * the entire process. + */ + if (sigpending(&pending_set)) { + return -1; + } + sigpipe_was_pending = sigismember(&pending_set, SIGPIPE); + /* + * If sigpipe was pending, it means it was already blocked, so + * no need to block it. + */ + if (!sigpipe_was_pending) { + if (sigemptyset(&sigpipe_set)) { + return -1; + } + if (sigaddset(&sigpipe_set, SIGPIPE)) { + return -1; + } + if (pthread_sigmask(SIG_BLOCK, &sigpipe_set, &old_set)) { + return -1; + } + } + + /* Send and save errno. */ + sent = bt_socket_send(fd, buffer, size, 0); + saved_err = errno; + + if (sent == -1 && errno == EPIPE && !sigpipe_was_pending) { + struct timespec timeout = { 0, 0 }; + int ret; + + do { + ret = sigtimedwait(&sigpipe_set, NULL, + &timeout); + } while (ret == -1 && errno == EINTR); + } + if (!sigpipe_was_pending) { + if (pthread_sigmask(SIG_SETMASK, &old_set, NULL)) { + return -1; + } + } + /* Restore send() errno */ + errno = saved_err; + + return sent; +} +#endif + + +#endif /* _BABELTRACE_COMPAT_SOCKET_H */ diff --git a/plugins/ctf/lttng-live/Makefile.am b/plugins/ctf/lttng-live/Makefile.am index d65662e2..4fa0220d 100644 --- a/plugins/ctf/lttng-live/Makefile.am +++ b/plugins/ctf/lttng-live/Makefile.am @@ -7,4 +7,8 @@ libbabeltrace_plugin_ctf_lttng_live_la_SOURCES = lttng-live.c \ lttng-viewer-abi.h \ logging.c logging.h +if BABELTRACE_BUILD_WITH_MINGW +libbabeltrace_plugin_ctf_lttng_live_la_LIBADD = -lws2_32 +endif + noinst_LTLIBRARIES = libbabeltrace-plugin-ctf-lttng-live.la diff --git a/plugins/ctf/lttng-live/viewer-connection.c b/plugins/ctf/lttng-live/viewer-connection.c index aca5a3e3..13b88277 100644 --- a/plugins/ctf/lttng-live/viewer-connection.c +++ b/plugins/ctf/lttng-live/viewer-connection.c @@ -30,14 +30,11 @@ #include #include #include -#include #include -#include -#include #include -#include -#include +#include +#include #include #include #include @@ -55,16 +52,16 @@ static ssize_t lttng_live_recv(struct bt_live_viewer_connection *viewer_connecti size_t copied = 0, to_copy = len; struct lttng_live_component *lttng_live = viewer_connection->lttng_live; - int fd = viewer_connection->control_sock; + BT_SOCKET sock = viewer_connection->control_sock; do { - ret = recv(fd, buf + copied, to_copy, 0); + ret = bt_socket_recv(sock, buf + copied, to_copy, 0); if (ret > 0) { assert(ret <= to_copy); copied += ret; to_copy -= ret; } - if (ret < 0 && errno == EINTR) { + if (ret == BT_SOCKET_ERROR && bt_socket_interrupted()) { if (lttng_live_is_canceled(lttng_live)) { break; } else { @@ -74,7 +71,7 @@ static ssize_t lttng_live_recv(struct bt_live_viewer_connection *viewer_connecti } while (ret > 0 && to_copy > 0); if (ret > 0) ret = copied; - /* ret = 0 means orderly shutdown, ret < 0 is error. */ + /* ret = 0 means orderly shutdown, ret == BT_SOCKET_ERROR is error. */ return ret; } @@ -83,12 +80,12 @@ static ssize_t lttng_live_send(struct bt_live_viewer_connection *viewer_connecti { struct lttng_live_component *lttng_live = viewer_connection->lttng_live; - int fd = viewer_connection->control_sock; + BT_SOCKET sock = viewer_connection->control_sock; ssize_t ret; for (;;) { - ret = bt_send_nosigpipe(fd, buf, len); - if (ret < 0 && errno == EINTR) { + ret = bt_socket_send_nosigpipe(sock, buf, len); + if (ret == BT_SOCKET_ERROR && bt_socket_interrupted()) { if (lttng_live_is_canceled(lttng_live)) { break; } else { @@ -173,15 +170,15 @@ static int lttng_live_handshake(struct bt_live_viewer_connection *viewer_connect connect.type = htobe32(LTTNG_VIEWER_CLIENT_COMMAND); ret_len = lttng_live_send(viewer_connection, &cmd, sizeof(cmd)); - if (ret_len < 0) { - BT_LOGE("Error sending cmd: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error sending cmd: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(cmd)); ret_len = lttng_live_send(viewer_connection, &connect, sizeof(connect)); - if (ret_len < 0) { - BT_LOGE("Error sending version: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error sending version: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(connect)); @@ -191,8 +188,8 @@ static int lttng_live_handshake(struct bt_live_viewer_connection *viewer_connect BT_LOGI("Remote side has closed connection"); goto error; } - if (ret_len < 0) { - BT_LOGE("Error receiving version: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error receiving version: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(connect)); @@ -238,8 +235,8 @@ static int lttng_live_connect_viewer(struct bt_live_viewer_connection *viewer_co goto error; } - if ((viewer_connection->control_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { - BT_LOGE("Socket creation failed: %s", strerror(errno)); + if ((viewer_connection->control_sock = socket(AF_INET, SOCK_STREAM, 0)) == BT_INVALID_SOCKET) { + BT_LOGE("Socket creation failed: %s", bt_socket_errormsg()); goto error; } @@ -249,8 +246,8 @@ static int lttng_live_connect_viewer(struct bt_live_viewer_connection *viewer_co memset(&(server_addr.sin_zero), 0, 8); if (connect(viewer_connection->control_sock, (struct sockaddr *) &server_addr, - sizeof(struct sockaddr)) == -1) { - BT_LOGE("Connection failed: %s", strerror(errno)); + sizeof(struct sockaddr)) == BT_SOCKET_ERROR) { + BT_LOGE("Connection failed: %s", bt_socket_errormsg()); goto error; } if (lttng_live_handshake(viewer_connection)) { @@ -262,23 +259,23 @@ static int lttng_live_connect_viewer(struct bt_live_viewer_connection *viewer_co return ret; error: - if (viewer_connection->control_sock >= 0) { - if (close(viewer_connection->control_sock)) { - BT_LOGE("Close: %s", strerror(errno)); + if (viewer_connection->control_sock != BT_INVALID_SOCKET) { + if (bt_socket_close(viewer_connection->control_sock) == BT_SOCKET_ERROR) { + BT_LOGE("Close: %s", bt_socket_errormsg()); } } - viewer_connection->control_sock = -1; + viewer_connection->control_sock = BT_INVALID_SOCKET; return -1; } static void lttng_live_disconnect_viewer(struct bt_live_viewer_connection *viewer_connection) { - if (viewer_connection->control_sock < 0) { + if (viewer_connection->control_sock == BT_INVALID_SOCKET) { return; } - if (close(viewer_connection->control_sock)) { - BT_LOGE("Close: %s", strerror(errno)); - viewer_connection->control_sock = -1; + if (bt_socket_close(viewer_connection->control_sock) == BT_SOCKET_ERROR) { + BT_LOGE("Close: %s", bt_socket_errormsg()); + viewer_connection->control_sock = BT_INVALID_SOCKET; } } @@ -573,8 +570,8 @@ struct bt_value *bt_live_viewer_connection_list_sessions(struct bt_live_viewer_c cmd.cmd_version = htobe32(0); ret_len = lttng_live_send(viewer_connection, &cmd, sizeof(cmd)); - if (ret_len < 0) { - BT_LOGE("Error sending cmd: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error sending cmd: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(cmd)); @@ -584,8 +581,8 @@ struct bt_value *bt_live_viewer_connection_list_sessions(struct bt_live_viewer_c BT_LOGI("Remote side has closed connection"); goto error; } - if (ret_len < 0) { - BT_LOGE("Error receiving session list: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error receiving session list: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(list)); @@ -600,8 +597,8 @@ struct bt_value *bt_live_viewer_connection_list_sessions(struct bt_live_viewer_c BT_LOGI("Remote side has closed connection"); goto error; } - if (ret_len < 0) { - BT_LOGE("Error receiving session: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error receiving session: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(lsession)); @@ -637,8 +634,8 @@ int lttng_live_query_session_ids(struct lttng_live_component *lttng_live) cmd.cmd_version = htobe32(0); ret_len = lttng_live_send(viewer_connection, &cmd, sizeof(cmd)); - if (ret_len < 0) { - BT_LOGE("Error sending cmd: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error sending cmd: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(cmd)); @@ -648,8 +645,8 @@ int lttng_live_query_session_ids(struct lttng_live_component *lttng_live) BT_LOGI("Remote side has closed connection"); goto error; } - if (ret_len < 0) { - BT_LOGE("Error receiving session list: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error receiving session list: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(list)); @@ -662,8 +659,8 @@ int lttng_live_query_session_ids(struct lttng_live_component *lttng_live) BT_LOGI("Remote side has closed connection"); goto error; } - if (ret_len < 0) { - BT_LOGE("Error receiving session: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error receiving session: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(lsession)); @@ -676,9 +673,9 @@ int lttng_live_query_session_ids(struct lttng_live_component *lttng_live) if ((strncmp(lsession.session_name, viewer_connection->session_name->str, - MAXNAMLEN) == 0) && (strncmp(lsession.hostname, + LTTNG_VIEWER_NAME_MAX) == 0) && (strncmp(lsession.hostname, viewer_connection->target_hostname->str, - MAXNAMLEN) == 0)) { + LTTNG_VIEWER_HOST_NAME_MAX) == 0)) { if (lttng_live_add_session(lttng_live, session_id, lsession.hostname, lsession.session_name)) { @@ -708,8 +705,8 @@ int lttng_live_create_viewer_session(struct lttng_live_component *lttng_live) cmd.cmd_version = htobe32(0); ret_len = lttng_live_send(viewer_connection, &cmd, sizeof(cmd)); - if (ret_len < 0) { - BT_LOGE("Error sending cmd: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error sending cmd: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(cmd)); @@ -719,8 +716,8 @@ int lttng_live_create_viewer_session(struct lttng_live_component *lttng_live) BT_LOGI("Remote side has closed connection"); goto error; } - if (ret_len < 0) { - BT_LOGE("Error receiving create session reply: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error receiving create session reply: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(resp)); @@ -761,7 +758,7 @@ int receive_streams(struct lttng_live_session *session, BT_LOGI("Remote side has closed connection"); goto error; } - if (ret_len < 0) { + if (ret_len == BT_SOCKET_ERROR) { BT_LOGE("Error receiving stream"); goto error; } @@ -829,15 +826,15 @@ int lttng_live_attach_session(struct lttng_live_session *session) rq.seek = htobe32(LTTNG_VIEWER_SEEK_LAST); ret_len = lttng_live_send(viewer_connection, &cmd, sizeof(cmd)); - if (ret_len < 0) { - BT_LOGE("Error sending cmd: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error sending cmd: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(cmd)); ret_len = lttng_live_send(viewer_connection, &rq, sizeof(rq)); - if (ret_len < 0) { - BT_LOGE("Error sending attach request: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error sending attach request: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(rq)); @@ -847,8 +844,8 @@ int lttng_live_attach_session(struct lttng_live_session *session) BT_LOGI("Remote side has closed connection"); goto error; } - if (ret_len < 0) { - BT_LOGE("Error receiving attach response: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error receiving attach response: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(rp)); @@ -912,15 +909,15 @@ int lttng_live_detach_session(struct lttng_live_session *session) rq.session_id = htobe64(session_id); ret_len = lttng_live_send(viewer_connection, &cmd, sizeof(cmd)); - if (ret_len < 0) { - BT_LOGE("Error sending cmd: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error sending cmd: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(cmd)); ret_len = lttng_live_send(viewer_connection, &rq, sizeof(rq)); - if (ret_len < 0) { - BT_LOGE("Error sending detach request: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error sending detach request: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(rq)); @@ -930,8 +927,8 @@ int lttng_live_detach_session(struct lttng_live_session *session) BT_LOGI("Remote side has closed connection"); goto error; } - if (ret_len < 0) { - BT_LOGE("Error receiving detach response: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error receiving detach response: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(rp)); @@ -981,15 +978,15 @@ ssize_t lttng_live_get_one_metadata_packet(struct lttng_live_trace *trace, cmd.cmd_version = htobe32(0); ret_len = lttng_live_send(viewer_connection, &cmd, sizeof(cmd)); - if (ret_len < 0) { - BT_LOGE("Error sending cmd: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error sending cmd: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(cmd)); ret_len = lttng_live_send(viewer_connection, &rq, sizeof(rq)); - if (ret_len < 0) { - BT_LOGE("Error sending get_metadata request: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error sending get_metadata request: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(rq)); @@ -999,8 +996,8 @@ ssize_t lttng_live_get_one_metadata_packet(struct lttng_live_trace *trace, BT_LOGI("Remote side has closed connection"); goto error; } - if (ret_len < 0) { - BT_LOGE("Error receiving get_metadata response: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error receiving get_metadata response: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(rp)); @@ -1037,8 +1034,8 @@ ssize_t lttng_live_get_one_metadata_packet(struct lttng_live_trace *trace, BT_LOGI("Remote side has closed connection"); goto error_free_data; } - if (ret_len < 0) { - BT_LOGE("Error receiving trace packet: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error receiving trace packet: %s", bt_socket_errormsg()); goto error_free_data; } assert(ret_len == len); @@ -1104,15 +1101,15 @@ enum bt_ctf_lttng_live_iterator_status lttng_live_get_next_index(struct lttng_li rq.stream_id = htobe64(stream->viewer_stream_id); ret_len = lttng_live_send(viewer_connection, &cmd, sizeof(cmd)); - if (ret_len < 0) { - BT_LOGE("Error sending cmd: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error sending cmd: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(cmd)); ret_len = lttng_live_send(viewer_connection, &rq, sizeof(rq)); - if (ret_len < 0) { - BT_LOGE("Error sending get_next_index request: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error sending get_next_index request: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(rq)); @@ -1122,8 +1119,8 @@ enum bt_ctf_lttng_live_iterator_status lttng_live_get_next_index(struct lttng_li BT_LOGI("Remote side has closed connection"); goto error; } - if (ret_len < 0) { - BT_LOGE("Error receiving get_next_index response: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error receiving get_next_index response: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(rp)); @@ -1241,15 +1238,15 @@ enum bt_ctf_notif_iter_medium_status lttng_live_get_stream_bytes(struct lttng_li rq.len = htobe32(req_len); ret_len = lttng_live_send(viewer_connection, &cmd, sizeof(cmd)); - if (ret_len < 0) { - BT_LOGE("Error sending cmd: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error sending cmd: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(cmd)); ret_len = lttng_live_send(viewer_connection, &rq, sizeof(rq)); - if (ret_len < 0) { - BT_LOGE("Error sending get_data request: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error sending get_data request: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(rq)); @@ -1259,8 +1256,8 @@ enum bt_ctf_notif_iter_medium_status lttng_live_get_stream_bytes(struct lttng_li BT_LOGI("Remote side has closed connection"); goto error; } - if (ret_len < 0) { - BT_LOGE("Error receiving get_data response: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error receiving get_data response: %s", bt_socket_errormsg()); goto error; } if (ret_len != sizeof(rp)) { @@ -1316,8 +1313,8 @@ enum bt_ctf_notif_iter_medium_status lttng_live_get_stream_bytes(struct lttng_li BT_LOGI("Remote side has closed connection"); goto error; } - if (ret_len < 0) { - BT_LOGE("Error receiving trace packet: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error receiving trace packet: %s", bt_socket_errormsg()); goto error; } assert(ret_len == req_len); @@ -1364,15 +1361,15 @@ enum bt_ctf_lttng_live_iterator_status lttng_live_get_new_streams( rq.session_id = htobe64(session->id); ret_len = lttng_live_send(viewer_connection, &cmd, sizeof(cmd)); - if (ret_len < 0) { - BT_LOGE("Error sending cmd: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error sending cmd: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(cmd)); ret_len = lttng_live_send(viewer_connection, &rq, sizeof(rq)); - if (ret_len < 0) { - BT_LOGE("Error sending get_new_streams request: %s", strerror(errno)); + if (ret_len == BT_SOCKET_ERROR) { + BT_LOGE("Error sending get_new_streams request: %s", bt_socket_errormsg()); goto error; } assert(ret_len == sizeof(rq)); @@ -1382,7 +1379,7 @@ enum bt_ctf_lttng_live_iterator_status lttng_live_get_new_streams( BT_LOGI("Remote side has closed connection"); goto error; } - if (ret_len < 0) { + if (ret_len == BT_SOCKET_ERROR) { BT_LOGE("Error receiving get_new_streams response"); goto error; } @@ -1434,8 +1431,12 @@ struct bt_live_viewer_connection * viewer_connection = g_new0(struct bt_live_viewer_connection, 1); + if (bt_socket_init() != 0) { + goto error; + } + bt_object_init(&viewer_connection->obj, connection_release); - viewer_connection->control_sock = -1; + viewer_connection->control_sock = BT_INVALID_SOCKET; viewer_connection->port = -1; viewer_connection->lttng_live = lttng_live; viewer_connection->url = g_string_new(url); @@ -1473,4 +1474,6 @@ void bt_live_viewer_connection_destroy(struct bt_live_viewer_connection *viewer_ g_string_free(viewer_connection->session_name, TRUE); } g_free(viewer_connection); + + bt_socket_fini(); } diff --git a/plugins/ctf/lttng-live/viewer-connection.h b/plugins/ctf/lttng-live/viewer-connection.h index db9554d7..bb9c2852 100644 --- a/plugins/ctf/lttng-live/viewer-connection.h +++ b/plugins/ctf/lttng-live/viewer-connection.h @@ -27,6 +27,7 @@ #include #include +#include //TODO: this should not be used by plugins. Should copy code into plugin //instead. @@ -48,7 +49,7 @@ struct bt_live_viewer_connection { GString *target_hostname; GString *session_name; - int control_sock; + BT_SOCKET control_sock; int port; int32_t major; -- 2.34.1