From 64efa44ea4babee85a5d855f62ca168d6aed69d0 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Wed, 22 Sep 2021 08:16:19 -0400 Subject: [PATCH] common: compile libsessiond-common as C++ MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Convert lttcomm_readable_code to a switch in a function, and rework lttcomm_get_readable_code a bit. That changes the behavior a bit, but probably not in a meaningful way. Change-Id: I92da95f5d27de9df176835e820dd81ab93fb7b89 Signed-off-by: Simon Marchi Signed-off-by: Jérémie Galarneau --- src/common/sessiond-comm/Makefile.am | 12 +- src/common/sessiond-comm/{inet.c => inet.cpp} | 12 +- .../sessiond-comm/{inet6.c => inet6.cpp} | 8 +- .../{sessiond-comm.c => sessiond-comm.cpp} | 109 +++++++++++++----- src/common/sessiond-comm/sessiond-comm.h | 7 +- 5 files changed, 100 insertions(+), 48 deletions(-) rename src/common/sessiond-comm/{inet.c => inet.cpp} (96%) rename src/common/sessiond-comm/{inet6.c => inet6.cpp} (96%) rename src/common/sessiond-comm/{sessiond-comm.c => sessiond-comm.cpp} (73%) diff --git a/src/common/sessiond-comm/Makefile.am b/src/common/sessiond-comm/Makefile.am index d026651a6..8086cb010 100644 --- a/src/common/sessiond-comm/Makefile.am +++ b/src/common/sessiond-comm/Makefile.am @@ -2,6 +2,12 @@ noinst_LTLIBRARIES = libsessiond-comm.la -libsessiond_comm_la_SOURCES = sessiond-comm.c sessiond-comm.h \ - inet.c inet.h inet6.c inet6.h \ - relayd.h agent.h +libsessiond_comm_la_SOURCES = \ + agent.h \ + inet.cpp \ + inet.h \ + inet6.cpp \ + inet6.h \ + relayd.h \ + sessiond-comm.cpp \ + sessiond-comm.h diff --git a/src/common/sessiond-comm/inet.c b/src/common/sessiond-comm/inet.cpp similarity index 96% rename from src/common/sessiond-comm/inet.c rename to src/common/sessiond-comm/inet.cpp index 93fb19bb5..20eb613a8 100644 --- a/src/common/sessiond-comm/inet.c +++ b/src/common/sessiond-comm/inet.cpp @@ -6,6 +6,7 @@ */ #define _LGPL_SOURCE +#include #include #include #include @@ -89,7 +90,7 @@ int lttcomm_bind_inet_sock(struct lttcomm_sock *sock) { struct sockaddr_in sockaddr = sock->sockaddr.addr.sin; - return bind(sock->fd, &sockaddr, sizeof(sockaddr)); + return bind(sock->fd, (struct sockaddr *) &sockaddr, sizeof(sockaddr)); } static @@ -97,7 +98,7 @@ int connect_no_timeout(struct lttcomm_sock *sock) { struct sockaddr_in sockaddr = sock->sockaddr.addr.sin; - return connect(sock->fd, &sockaddr, sizeof(sockaddr)); + return connect(sock->fd, (struct sockaddr *) &sockaddr, sizeof(sockaddr)); } static @@ -130,7 +131,7 @@ int connect_with_timeout(struct lttcomm_sock *sock) } sockaddr = sock->sockaddr.addr.sin; - connect_ret = connect(sock->fd, &sockaddr, sizeof(sockaddr)); + connect_ret = connect(sock->fd, (struct sockaddr *) &sockaddr, sizeof(sockaddr)); if (connect_ret == -1 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINPROGRESS) { goto error; @@ -359,7 +360,7 @@ ssize_t lttcomm_recvmsg_inet_sock(struct lttcomm_sock *sock, void *buf, if (flags & MSG_DONTWAIT) { goto end; } - iov[0].iov_base += ret; + iov[0].iov_base = ((char *) iov[0].iov_base) + ret; iov[0].iov_len -= ret; LTTNG_ASSERT(ret <= len_last); } @@ -524,8 +525,7 @@ void lttcomm_inet_init(void) * Get the maximum between the two possible timeout value and use that to * get the maximum with the default timeout. */ - lttcomm_inet_tcp_timeout = max_t(unsigned long, - max_t(unsigned long, syn_timeout, fin_timeout), + lttcomm_inet_tcp_timeout = std::max(std::max(syn_timeout, fin_timeout), lttcomm_inet_tcp_timeout); end: diff --git a/src/common/sessiond-comm/inet6.c b/src/common/sessiond-comm/inet6.cpp similarity index 96% rename from src/common/sessiond-comm/inet6.c rename to src/common/sessiond-comm/inet6.cpp index 8c0d0b795..adabebfae 100644 --- a/src/common/sessiond-comm/inet6.c +++ b/src/common/sessiond-comm/inet6.cpp @@ -86,14 +86,14 @@ error: int lttcomm_bind_inet6_sock(struct lttcomm_sock *sock) { struct sockaddr_in6 sockaddr = sock->sockaddr.addr.sin6; - return bind(sock->fd, &sockaddr, sizeof(sockaddr)); + return bind(sock->fd, (struct sockaddr *) &sockaddr, sizeof(sockaddr)); } static int connect_no_timeout(struct lttcomm_sock *sock) { struct sockaddr_in6 sockaddr = sock->sockaddr.addr.sin6; - return connect(sock->fd, &sockaddr, sizeof(sockaddr)); + return connect(sock->fd, (struct sockaddr *) &sockaddr, sizeof(sockaddr)); } static @@ -126,7 +126,7 @@ int connect_with_timeout(struct lttcomm_sock *sock) } sockaddr = sock->sockaddr.addr.sin6; - connect_ret = connect(sock->fd, &sockaddr, sizeof(sockaddr)); + connect_ret = connect(sock->fd, (struct sockaddr *) &sockaddr, sizeof(sockaddr)); if (connect_ret == -1 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINPROGRESS) { goto error; @@ -336,7 +336,7 @@ ssize_t lttcomm_recvmsg_inet6_sock(struct lttcomm_sock *sock, void *buf, if (flags & MSG_DONTWAIT) { goto end; } - iov[0].iov_base += ret; + iov[0].iov_base = ((char *) iov[0].iov_base) + ret; iov[0].iov_len -= ret; LTTNG_ASSERT(ret <= len_last); } diff --git a/src/common/sessiond-comm/sessiond-comm.c b/src/common/sessiond-comm/sessiond-comm.cpp similarity index 73% rename from src/common/sessiond-comm/sessiond-comm.c rename to src/common/sessiond-comm/sessiond-comm.cpp index f3680c139..6a011b8b4 100644 --- a/src/common/sessiond-comm/sessiond-comm.c +++ b/src/common/sessiond-comm/sessiond-comm.cpp @@ -38,28 +38,78 @@ static struct lttcomm_net_family net_families[] = { /* * Human readable error message. */ -static const char *lttcomm_readable_code[] = { - [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) ] = "consumerd command socket ready", - [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SUCCESS_RECV_FD) ] = "consumerd success on receiving fds", - [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ERROR_RECV_FD) ] = "consumerd error on receiving fds", - [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ERROR_RECV_CMD) ] = "consumerd error on receiving command", - [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_POLL_ERROR) ] = "consumerd error in polling thread", - [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_POLL_NVAL) ] = "consumerd polling on closed fd", - [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_POLL_HUP) ] = "consumerd all fd hung up", - [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_EXIT_SUCCESS) ] = "consumerd exiting normally", - [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_EXIT_FAILURE) ] = "consumerd exiting on error", - [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_OUTFD_ERROR) ] = "consumerd error opening the tracefile", - [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_EBADF) ] = "consumerd splice EBADF", - [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_EINVAL) ] = "consumerd splice EINVAL", - [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_ENOMEM) ] = "consumerd splice ENOMEM", - [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_SPLICE_ESPIPE) ] = "consumerd splice ESPIPE", - [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ENOMEM) ] = "Consumer is out of memory", - [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_ERROR_METADATA) ] = "Error with metadata", - [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_FATAL) ] = "Fatal error", - [ LTTCOMM_ERR_INDEX(LTTCOMM_CONSUMERD_RELAYD_FAIL) ] = "Error on remote relayd", - - /* Last element */ - [ LTTCOMM_ERR_INDEX(LTTCOMM_NR) ] = "Unknown error code" +static +const char *lttcomm_return_code_str(lttcomm_return_code code) { + switch (code) { + case LTTCOMM_CONSUMERD_SUCCESS: + return "consumerd success"; + case LTTCOMM_CONSUMERD_COMMAND_SOCK_READY: + return "consumerd command socket ready"; + case LTTCOMM_CONSUMERD_SUCCESS_RECV_FD: + return "consumerd success on receiving fds"; + case LTTCOMM_CONSUMERD_ERROR_RECV_FD: + return "consumerd error on receiving fds"; + case LTTCOMM_CONSUMERD_ERROR_RECV_CMD: + return "consumerd error on receiving command"; + case LTTCOMM_CONSUMERD_POLL_ERROR: + return "consumerd error in polling thread"; + case LTTCOMM_CONSUMERD_POLL_NVAL: + return "consumerd polling on closed fd"; + case LTTCOMM_CONSUMERD_POLL_HUP: + return "consumerd all fd hung up"; + case LTTCOMM_CONSUMERD_EXIT_SUCCESS: + return "consumerd exiting normally"; + case LTTCOMM_CONSUMERD_EXIT_FAILURE: + return "consumerd exiting on error"; + case LTTCOMM_CONSUMERD_OUTFD_ERROR: + return "consumerd error opening the tracefile"; + case LTTCOMM_CONSUMERD_SPLICE_EBADF: + return "consumerd splice EBADF"; + case LTTCOMM_CONSUMERD_SPLICE_EINVAL: + return "consumerd splice EINVAL"; + case LTTCOMM_CONSUMERD_SPLICE_ENOMEM: + return "consumerd splice ENOMEM"; + case LTTCOMM_CONSUMERD_SPLICE_ESPIPE: + return "consumerd splice ESPIPE"; + case LTTCOMM_CONSUMERD_ENOMEM: + return "consumerd is out of memory"; + case LTTCOMM_CONSUMERD_ERROR_METADATA: + return "consumerd error with metadata"; + case LTTCOMM_CONSUMERD_FATAL: + return "consumerd fatal error"; + case LTTCOMM_CONSUMERD_RELAYD_FAIL: + return "consumerd error on remote relayd"; + case LTTCOMM_CONSUMERD_CHANNEL_FAIL: + return "consumerd channel creation fail"; + case LTTCOMM_CONSUMERD_CHAN_NOT_FOUND: + return "consumerd channel not found"; + case LTTCOMM_CONSUMERD_ALREADY_SET: + return "consumerd resource already set"; + case LTTCOMM_CONSUMERD_ROTATION_FAIL: + return "consumerd rotation failed"; + case LTTCOMM_CONSUMERD_SNAPSHOT_FAILED: + return "consumerd snapshot has failed"; + case LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED: + return "consumerd trace chunk creation failed"; + case LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED: + return "consumerd trace chunk closing failed"; + case LTTCOMM_CONSUMERD_INVALID_PARAMETERS: + return "consumerd invalid parameters"; + case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL: + return "consumerd trace chunk exists on consumer daemon"; + case LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE: + return "consumedd trace chunk exists on relay daemon"; + case LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK: + return "consumerd unknown trace chunk"; + case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED: + return "consumed relayd does not accept clear command"; + case LTTCOMM_CONSUMERD_UNKNOWN_ERROR: + return "consumerd unknown error"; + case LTTCOMM_NR: + abort(); + } + + abort(); }; static unsigned long network_timeout; @@ -72,13 +122,14 @@ static unsigned long network_timeout; */ const char *lttcomm_get_readable_code(enum lttcomm_return_code code) { - code = -code; + code = (lttcomm_return_code) -code; - if (code < LTTCOMM_CONSUMERD_COMMAND_SOCK_READY || code > LTTCOMM_NR) { - code = LTTCOMM_NR; + if (code != LTTCOMM_CONSUMERD_SUCCESS && + !(code >= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY && code < LTTCOMM_NR)) { + code = LTTCOMM_CONSUMERD_UNKNOWN_ERROR; } - return lttcomm_readable_code[LTTCOMM_ERR_INDEX(code)]; + return lttcomm_return_code_str(code); } /* @@ -128,7 +179,7 @@ struct lttcomm_sock *lttcomm_alloc_sock(enum lttcomm_sock_proto proto) { struct lttcomm_sock *sock; - sock = zmalloc(sizeof(struct lttcomm_sock)); + sock = (lttcomm_sock *) zmalloc(sizeof(lttcomm_sock)); if (sock == NULL) { PERROR("zmalloc create sock"); goto end; @@ -263,7 +314,7 @@ struct lttcomm_sock *lttcomm_alloc_sock_from_uri(struct lttng_uri *uri) goto alloc_error; } - sock = lttcomm_alloc_sock(_sock_proto); + sock = lttcomm_alloc_sock((lttcomm_sock_proto) _sock_proto); if (sock == NULL) { goto alloc_error; } @@ -318,7 +369,7 @@ struct lttcomm_relayd_sock *lttcomm_alloc_relayd_sock(struct lttng_uri *uri, LTTNG_ASSERT(uri); - rsock = zmalloc(sizeof(*rsock)); + rsock = (lttcomm_relayd_sock *) zmalloc(sizeof(*rsock)); if (!rsock) { PERROR("zmalloc relayd sock"); goto error; diff --git a/src/common/sessiond-comm/sessiond-comm.h b/src/common/sessiond-comm/sessiond-comm.h index 65020bbfc..b155fe4ad 100644 --- a/src/common/sessiond-comm/sessiond-comm.h +++ b/src/common/sessiond-comm/sessiond-comm.h @@ -52,11 +52,6 @@ extern "C" { #define LTTCOMM_MAX_SEND_FDS 16 #endif -/* - * Get the error code index from 0 since LTTCOMM_OK start at 1000 - */ -#define LTTCOMM_ERR_INDEX(code) (code - LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) - enum lttcomm_sessiond_command { /* Tracer command */ LTTNG_ADD_CONTEXT = 0, @@ -279,7 +274,7 @@ enum lttcomm_return_code { LTTCOMM_CONSUMERD_ROTATION_FAIL, /* Rotation has failed. */ LTTCOMM_CONSUMERD_SNAPSHOT_FAILED, /* snapshot has failed. */ LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED,/* Trace chunk creation failed. */ - LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED, /* Trace chunk creation failed. */ + LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED, /* Trace chunk close failed. */ LTTCOMM_CONSUMERD_INVALID_PARAMETERS, /* Invalid parameters. */ LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL, /* Trace chunk exists on consumer daemon. */ LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE,/* Trace chunk exists on relay daemon. */ -- 2.34.1