From b2be0f7bf1550b80316ec3703dbd9effdb1759dd Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Thu, 13 May 2021 09:55:49 -0400 Subject: [PATCH] ustcomm: implement shutdown API Implement socket shutdown API to allow the ust-ctl library to shutdown the communication socket when it cannot recover from errors which make the sender and receiver out-of-sync. Signed-off-by: Mathieu Desnoyers Change-Id: If896396d9104b21aee09981cac773996ed79aa4d --- src/common/ustcomm.c | 32 +++++++++++++++++++++----------- src/common/ustcomm.h | 3 +++ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/common/ustcomm.c b/src/common/ustcomm.c index ada896f6..6e6b8f3e 100644 --- a/src/common/ustcomm.c +++ b/src/common/ustcomm.c @@ -214,7 +214,7 @@ int ustcomm_listen_unix_sock(int sock) /* * ustcomm_close_unix_sock * - * Shutdown cleanly a unix socket. + * Close unix socket. * * Handles fd tracker internally. */ @@ -235,6 +235,24 @@ int ustcomm_close_unix_sock(int sock) return ret; } +/* + * ustcomm_shutdown_unix_sock + * + * Shutdown unix socket. Keeps the file descriptor open, but shutdown + * communication. + */ +int ustcomm_shutdown_unix_sock(int sock) +{ + int ret; + + ret = shutdown(sock, SHUT_RDWR); + if (ret) { + PERROR("Socket shutdown error"); + ret = -errno; + } + return ret; +} + /* * ustcomm_recv_unix_sock * @@ -268,17 +286,13 @@ ssize_t ustcomm_recv_unix_sock(int sock, void *buf, size_t len) } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR)); if (ret < 0) { - int shutret; - if (errno != EPIPE && errno != ECONNRESET && errno != ECONNREFUSED) PERROR("recvmsg"); ret = -errno; if (ret == -ECONNRESET || ret == -ECONNREFUSED) ret = -EPIPE; - shutret = shutdown(sock, SHUT_RDWR); - if (shutret) - ERR("Socket shutdown error"); + (void) ustcomm_shutdown_unix_sock(sock); } else if (ret > 0) { ret = len; } @@ -318,17 +332,13 @@ ssize_t ustcomm_send_unix_sock(int sock, const void *buf, size_t len) } while (ret < 0 && errno == EINTR); if (ret < 0) { - int shutret; - if (errno != EPIPE && errno != ECONNRESET) PERROR("sendmsg"); ret = -errno; if (ret == -ECONNRESET) ret = -EPIPE; - shutret = shutdown(sock, SHUT_RDWR); - if (shutret) - ERR("Socket shutdown error"); + (void) ustcomm_shutdown_unix_sock(sock); } return ret; diff --git a/src/common/ustcomm.h b/src/common/ustcomm.h index 837332b7..99a3abc1 100644 --- a/src/common/ustcomm.h +++ b/src/common/ustcomm.h @@ -207,6 +207,9 @@ int ustcomm_accept_unix_sock(int sock) int ustcomm_listen_unix_sock(int sock) __attribute__((visibility("hidden"))); +int ustcomm_shutdown_unix_sock(int sock) + __attribute__((visibility("hidden"))); + int ustcomm_close_unix_sock(int sock) __attribute__((visibility("hidden"))); -- 2.34.1