Fix: return expected error return values for sessiond
[lttng-ust.git] / liblttng-ust-comm / lttng-ust-comm.c
index 23de8c0949d46a1fdb140a9dc72bcc93ec5e66a0..3644a1628a7fc12af488f79764c11422cceb3d39 100644 (file)
@@ -79,7 +79,6 @@ const char *lttng_ust_strerror(int code)
        if (code >= LTTNG_UST_ERR_NR)
                code = LTTNG_UST_ERR;
        return ustcomm_readable_code[USTCOMM_CODE_OFFSET(code)];
-
 }
 
 /*
@@ -121,7 +120,11 @@ int ustcomm_connect_unix_sock(const char *pathname)
                 * is used in normal execution to detect if sessiond is
                 * alive.
                 */
+               if (errno != ECONNREFUSED && errno != ECONNRESET)
+                       PERROR("connect");
                ret = -errno;
+               if (ret == -ECONNREFUSED || ret == -ECONNRESET)
+                       ret = -EPIPE;
                goto error_connect;
        }
 
@@ -155,8 +158,11 @@ int ustcomm_accept_unix_sock(int sock)
        /* Blocking call */
        new_fd = accept(sock, (struct sockaddr *) &sun, &len);
        if (new_fd < 0) {
-               PERROR("accept");
-               return -errno;
+               if (errno != ECONNABORTED)
+                       PERROR("accept");
+               new_fd = -errno;
+               if (new_fd == -ECONNABORTED)
+                       new_fd = -EPIPE;
        }
        return new_fd;
 }
@@ -272,9 +278,11 @@ ssize_t ustcomm_recv_unix_sock(int sock, void *buf, size_t len)
        if (ret < 0) {
                int shutret;
 
-               if (errno != EPIPE && errno != ECONNRESET)
+               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)
@@ -320,6 +328,8 @@ ssize_t ustcomm_send_unix_sock(int sock, const void *buf, size_t len)
                if (errno != EPIPE && errno != ECONNRESET)
                        PERROR("sendmsg");
                ret = -errno;
+               if (ret == -ECONNRESET)
+                       ret = -EPIPE;
 
                shutret = shutdown(sock, SHUT_RDWR);
                if (shutret)
@@ -376,6 +386,9 @@ ssize_t ustcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd)
                if (errno != EPIPE && errno != ECONNRESET) {
                        PERROR("sendmsg");
                }
+               ret = -errno;
+               if (ret == -ECONNRESET)
+                       ret = -EPIPE;
        }
        return ret;
 }
@@ -416,8 +429,9 @@ ssize_t ustcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd)
                if (errno != EPIPE && errno != ECONNRESET) {
                        PERROR("recvmsg fds");
                }
-               if (errno == EPIPE || errno == ECONNRESET)
-                       ret = -errno;
+               ret = -errno;
+               if (ret == -ECONNRESET)
+                       ret = -EPIPE;
                goto end;
        }
        if (ret == 0) {
@@ -502,15 +516,10 @@ int ustcomm_recv_app_reply(int sock, struct ustcomm_ust_reply *lur,
                }
                return lur->ret_code;
        default:
-               if (len < 0) {
-                       /* Transport level error */
-                       if (errno == EPIPE || errno == ECONNRESET)
-                               len = -errno;
-                       return len;
-               } else {
+               if (len >= 0) {
                        ERR("incorrect message size: %zd\n", len);
-                       return len;
                }
+               return len;
        }
 }
 
@@ -1015,3 +1024,43 @@ int ustcomm_register_channel(int sock,
                }
        }
 }
+
+/*
+ * Set socket reciving timeout.
+ */
+int ustcomm_setsockopt_rcv_timeout(int sock, unsigned int msec)
+{
+       int ret;
+       struct timeval tv;
+
+       tv.tv_sec = msec / 1000;
+       tv.tv_usec = (msec * 1000 % 1000000);
+
+       ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
+       if (ret < 0) {
+               PERROR("setsockopt SO_RCVTIMEO");
+               ret = -errno;
+       }
+
+       return ret;
+}
+
+/*
+ * Set socket sending timeout.
+ */
+int ustcomm_setsockopt_snd_timeout(int sock, unsigned int msec)
+{
+       int ret;
+       struct timeval tv;
+
+       tv.tv_sec = msec / 1000;
+       tv.tv_usec = (msec * 1000) % 1000000;
+
+       ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
+       if (ret < 0) {
+               PERROR("setsockopt SO_SNDTIMEO");
+               ret = -errno;
+       }
+
+       return ret;
+}
This page took 0.024434 seconds and 4 git commands to generate.