diff --git a/common/utils/time_manager/time_client.c b/common/utils/time_manager/time_client.c index 4cecf0e6c6..f587992fa8 100644 --- a/common/utils/time_manager/time_client.c +++ b/common/utils/time_manager/time_client.c @@ -55,16 +55,16 @@ static int connect_to_server(time_client_private_t *time_client) /* try forever (or exit requested) until we succeed */ while (1) { sock = socket(AF_INET, SOCK_STREAM, 0); - DevAssert(sock != -1); + AssertFatal(sock != -1, "socket() failed: %s\n", strerror(errno)); int v = 1; r = setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v)); - DevAssert(r == 0); + AssertFatal(r == 0, "setsockopt() failed: %s\n", strerror(errno)); /* unblock socket */ opts = fcntl(sock, F_GETFL); - DevAssert(opts >= 0); + AssertFatal(opts >= 0, "fcntl() failed: %s\n", strerror(errno)); opts |= O_NONBLOCK; r = fcntl(sock, F_SETFL, opts); - DevAssert(r == 0); + AssertFatal(r == 0, "fctnl() failed: %s\n", strerror(errno)); if (connect(sock, (struct sockaddr *)&time_client->server_ip, sizeof(time_client->server_ip)) == 0) break; if (errno == EINPROGRESS) { @@ -74,7 +74,7 @@ static int connect_to_server(time_client_private_t *time_client) fds[1].fd = time_client->exit_fd; fds[1].events = POLLIN; r = poll(fds, 2, -1); - DevAssert(r >= 0); + AssertFatal(r >= 0, "poll() failed: %s\n", strerror(errno)); /* do we need to exit? */ if (fds[1].revents) { shutdown(sock, SHUT_RDWR); @@ -87,7 +87,9 @@ static int connect_to_server(time_client_private_t *time_client) int so_error; socklen_t so_error_len = sizeof(so_error); r = getsockopt(sock, SOL_SOCKET, SO_ERROR, &so_error, &so_error_len); - DevAssert(r == 0 && so_error_len == sizeof(so_error)); + AssertFatal(r == 0 && so_error_len == sizeof(so_error), + "getsockopt() failed (r = %d) or so_error_len (= %d) is not %ld\n", + r, so_error_len, sizeof(so_error)); if (so_error == 0) /* success */ break; @@ -98,13 +100,13 @@ static int connect_to_server(time_client_private_t *time_client) sock = -1; char server_address[256]; const char *ret = inet_ntop(AF_INET, &time_client->server_ip.sin_addr, server_address, sizeof(server_address)); - DevAssert(ret != NULL); + AssertFatal(ret != NULL, "inet_ntop() failed: %s\n", strerror(errno)); LOG_W(UTIL, "time client: connection to %s:%d failed, try again in 1s\n", server_address, time_client->server_port); /* sleep 1s (or exit requested) */ fds[0].fd = time_client->exit_fd; fds[0].events = POLLIN; r = poll(fds, 1, 1000); - DevAssert(r >= 0); + AssertFatal(r >= 0, "poll() failed: %s\n", strerror(errno)); /* exit requested? */ if (fds[0].revents) { shutdown(sock, SHUT_RDWR); @@ -117,7 +119,7 @@ static int connect_to_server(time_client_private_t *time_client) /* reblock socket */ opts &= ~O_NONBLOCK; r = fcntl(sock, F_SETFL, opts); - DevAssert(r == 0); + AssertFatal(r == 0, "fcntl() failed: %s\n", strerror(errno)); } return sock; } @@ -142,7 +144,7 @@ static void *time_client_thread(void *tc) polls[1].events = POLLIN; ret = poll(polls, 2, -1); - DevAssert(ret >= 0); + AssertFatal(ret >= 0, "poll() failed: %s\n", strerror(errno)); /* whatever is in revents for exit_fd means exit */ if (polls[1].revents) @@ -160,7 +162,7 @@ reconnect: } /* here we only accept POLLIN */ - DevAssert(polls[0].revents == POLLIN); + AssertFatal(polls[0].revents == POLLIN, "poll() did not return expected POLLIN\n"); /* get the ticks from server */ uint8_t tick_count; @@ -196,14 +198,13 @@ time_client_t *new_time_client(const char *server_ip, time_client_private_t *tc; int ret; - tc = calloc(1, sizeof(*tc)); - DevAssert(tc != NULL); + tc = calloc_or_fail(1, sizeof(*tc)); tc->exit_fd = eventfd(0, 0); - DevAssert(tc->exit_fd != -1); + AssertFatal(tc->exit_fd != -1, "eventfd() failed: %s\n", strerror(errno)); ret = inet_aton(server_ip, &tc->server_ip.sin_addr); - DevAssert(ret != 0); + AssertFatal(ret != 0, "inet_aton() failed: %s\n", strerror(errno)); tc->server_ip.sin_family = AF_INET; tc->server_ip.sin_port = htons(server_port); tc->server_port = server_port; diff --git a/common/utils/time_manager/time_server.c b/common/utils/time_manager/time_server.c index 83b9e30fd2..a1a95d8ebe 100644 --- a/common/utils/time_manager/time_server.c +++ b/common/utils/time_manager/time_server.c @@ -51,8 +51,7 @@ static void *time_server_thread(void *ts) int *socket_fds = NULL; int client_count = 0; - polls = calloc(2, sizeof(struct pollfd)); - DevAssert(polls != NULL); + polls = calloc_or_fail(2, sizeof(struct pollfd)); while (!time_server->exit) { /* polls[0] is to receive ticks */ @@ -69,7 +68,7 @@ static void *time_server_thread(void *ts) } int ret = poll(polls, client_count + 2, -1); - DevAssert(ret >= 1 || (ret == -1 && errno == EINTR)); + AssertFatal(ret >= 1 || (ret == -1 && errno == EINTR), "poll() failed\n"); if (time_server->exit) break; @@ -77,7 +76,7 @@ static void *time_server_thread(void *ts) /* check tick */ if (polls[0].revents) { /* do not accept errors */ - DevAssert(polls[0].revents == POLLIN); + AssertFatal(polls[0].revents == POLLIN, "poll() failed\n"); uint64_t ticks; int ret = read(time_server->tick_fd, &ticks, 8); DevAssert(ret == 8); @@ -107,7 +106,7 @@ static void *time_server_thread(void *ts) socket_fds = realloc(socket_fds, sizeof(int) * client_count); } polls = realloc(polls, sizeof(struct pollfd) * (client_count + 2)); - DevAssert(polls != NULL); + AssertFatal(polls != NULL, "realloc() failed\n"); } ticks -= count; } @@ -116,18 +115,18 @@ static void *time_server_thread(void *ts) /* check new client */ if (polls[1].revents) { /* do not accept errors */ - DevAssert(polls[1].revents == POLLIN); + AssertFatal(polls[1].revents == POLLIN, "poll() failed\n"); int new_socket = accept(time_server->server_socket, NULL, NULL); - DevAssert(new_socket != -1); + AssertFatal(new_socket != -1, "accept() failed: %s\n", strerror(errno)); /* add client */ client_count++; socket_fds = realloc(socket_fds, sizeof(int) * client_count); - DevAssert(socket_fds != NULL); + AssertFatal(socket_fds != NULL, "realloc() failed\n"); socket_fds[client_count - 1] = new_socket; polls = realloc(polls, sizeof(struct pollfd) * (client_count + 2)); - DevAssert(polls != NULL); + AssertFatal(polls != NULL, "realloc() failed\n"); continue; } @@ -151,12 +150,12 @@ static void *time_server_thread(void *ts) } else { memmove(&socket_fds[i + 1], &socket_fds[i + 2], sizeof(int) * (client_count - i - 1)); socket_fds = realloc(socket_fds, sizeof(int) * client_count); - DevAssert(socket_fds != NULL); + AssertFatal(socket_fds != NULL, "realloco() failed\n"); } if (client_count) memmove(&polls[i + 2 + 1], &polls[i + 2 + 2], sizeof(struct pollfd) * (client_count - i - 1)); polls = realloc(polls, sizeof(struct pollfd) * (client_count + 2)); - DevAssert(polls != NULL); + AssertFatal(polls != NULL, "realloc() failed\n"); } } @@ -180,31 +179,30 @@ time_server_t *new_time_server(const char *ip, time_server_private_t *ts; int ret; - ts = calloc(1, sizeof(*ts)); - DevAssert(ts != NULL); + ts = calloc_or_fail(1, sizeof(*ts)); ts->server_socket = socket(AF_INET, SOCK_STREAM, 0); - DevAssert(ts->server_socket != -1); + AssertFatal(ts->server_socket != -1, "socket() failed: %s\n", strerror(errno)); int v = 1; ret = setsockopt(ts->server_socket, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)); - DevAssert(ret == 0); + AssertFatal(ret == 0, "setsockopt() failed: %s\n", strerror(errno)); v = 1; ret = setsockopt(ts->server_socket, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v)); - DevAssert(ret == 0); + AssertFatal(ret == 0, "setsockopt() failed: %s\n", strerror(errno)); ts->tick_fd = eventfd(0, 0); - DevAssert(ts->tick_fd != -1); + AssertFatal(ts->tick_fd != -1, "eventfd() failed: %s\n", strerror(errno)); struct sockaddr_in ip_addr; ip_addr.sin_family = AF_INET; ip_addr.sin_port = htons(port); ret = inet_aton(ip, &ip_addr.sin_addr); - DevAssert(ret != 0); + AssertFatal(ret != 0, "inet_aton() failed: %s\n", strerror(errno)); ret = bind(ts->server_socket, (struct sockaddr *)&ip_addr, sizeof(ip_addr)); - DevAssert(ret == 0); + AssertFatal(ret == 0, "bind() failed: %s\n", strerror(errno)); ret = listen(ts->server_socket, 10); /* 10: arbitrary value */ - DevAssert(ret == 0); + AssertFatal(ret == 0, "listen() failed: %s\n", strerror(errno)); ts->callback = callback; ts->callback_data = callback_data; diff --git a/common/utils/time_manager/time_source.c b/common/utils/time_manager/time_source.c index 76a76bc655..02a7ce0fa6 100644 --- a/common/utils/time_manager/time_source.c +++ b/common/utils/time_manager/time_source.c @@ -28,6 +28,7 @@ #include "common/utils/assertions.h" #include "common/utils/system.h" +#include "common/utils/utils.h" typedef void (*callback_function_t)(void *callback_data); typedef void * callback_data_t; @@ -190,16 +191,14 @@ time_source_t *new_time_source(time_source_type_t type) switch (type) { case TIME_SOURCE_REALTIME: { - time_source_realtime_t *ts = calloc(1, sizeof(*ts)); - DevAssert(ts != NULL); + time_source_realtime_t *ts = calloc_or_fail(1, sizeof(*ts)); thread_function = time_source_realtime_thread; thread_function_name = "time source realtime"; ret = ts; break; } case TIME_SOURCE_IQ_SAMPLES: { - time_source_iq_t *ts = calloc(1, sizeof(*ts)); - DevAssert(ts != NULL); + time_source_iq_t *ts = calloc_or_fail(1, sizeof(*ts)); thread_function = time_source_iq_samples_thread; terminate_function = time_source_iq_samples_terminate; thread_function_name = "time source iq samples";