ustcomm: implement shutdown API
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 13 May 2021 13:55:49 +0000 (09:55 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Fri, 14 May 2021 18:14:52 +0000 (14:14 -0400)
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 <mathieu.desnoyers@efficios.com>
Change-Id: If896396d9104b21aee09981cac773996ed79aa4d

src/common/ustcomm.c
src/common/ustcomm.h

index ada896f68f60ad1b0d077a4786c63b44f45c123d..6e6b8f3e564e8a5823a98d16b382f44fa0a1bf53 100644 (file)
@@ -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;
index 837332b71b3f75090d09f466279d2448b1b7713d..99a3abc19813130bed4090d6c3a222b2935efe4e 100644 (file)
@@ -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")));
 
This page took 0.025764 seconds and 4 git commands to generate.