Add an exception handling about recvmsg() 34/261534/1
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 21 Jul 2021 01:53:47 +0000 (10:53 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Wed, 21 Jul 2021 01:59:56 +0000 (10:59 +0900)
When calling the recvmsg(), the function returns a negative error value
immediately if the receive is interrupted by delivering of a signal.
This patch adds an exception handling for the error case.
If the errno is EINTR, calling the recvmsg() is tried for receiving the message
from the socket fd again.

Change-Id: I78ffc49bb9b128d14db489db95881fb48f009186
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/aul_sock.c

index ae0a1cd..c4e7359 100644 (file)
@@ -677,6 +677,7 @@ static int __recv_message(int sock, struct iovec *vec, int vec_max_size, int *ve
        char buff[CMSG_SPACE(sizeof(int) * MAX_NR_OF_DESCRIPTORS) + CMSG_SPACE(50)] = {0};
        struct msghdr msg = {0};
        struct cmsghdr *cmsg = NULL;
+       bool is_blocking;
        int ret;
 
        if (vec == NULL || vec_max_size < 1 || vec_size == NULL)
@@ -687,9 +688,31 @@ static int __recv_message(int sock, struct iovec *vec, int vec_max_size, int *ve
        msg.msg_control = buff;
        msg.msg_controllen = sizeof(buff);
 
+       if (fcntl(sock, F_GETFL, 0) & O_NONBLOCK)
+               is_blocking = false;
+       else
+               is_blocking = true;
+
+retry:
        ret = recvmsg(sock, &msg, 0);
-       if (ret < 0)
-               return -errno;
+       if (ret == 0) {
+               _W("Socket was disconnected. fd(%d)", sock);
+               return -ECOMM;
+       } else if (ret < 0) {
+               if (errno == EINTR || errno == EAGAIN) {
+                       if (is_blocking && errno == EAGAIN) {
+                               _E("recvmsg timeout. fd(%d)", sock);
+                               return -EAGAIN;
+                       }
+
+                       goto retry;
+               }
+
+               ret = -errno;
+               _E("recvmsg error. fd(%d), errno(%d)", sock, errno);
+               return ret;
+       }
+
        *vec_size = msg.msg_iovlen;
 
        /* get the ANCILLARY data */