Fix: ustctl: return -EPIPE to sessiond if connection is closed
[lttng-ust.git] / liblttng-ust-comm / lttng-ust-comm.c
index 1395eee2f22ea6bb19f56e496d30853e1aee6168..5d7a049b11568f620bd661c0a2dc73c8567f74d5 100644 (file)
@@ -155,8 +155,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;
 }
@@ -275,6 +278,8 @@ ssize_t ustcomm_recv_unix_sock(int sock, void *buf, size_t len)
                if (errno != EPIPE && errno != ECONNRESET)
                        PERROR("recvmsg");
                ret = -errno;
+               if (ret == -ECONNRESET)
+                       ret = -EPIPE;
 
                shutret = shutdown(sock, SHUT_RDWR);
                if (shutret)
@@ -320,6 +325,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 +383,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;
 }
@@ -418,6 +428,8 @@ ssize_t ustcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd)
                }
                if (errno == EPIPE || errno == ECONNRESET)
                        ret = -errno;
+               if (ret == -ECONNRESET)
+                       ret = -EPIPE;
                goto end;
        }
        if (ret == 0) {
@@ -631,7 +643,8 @@ int ustcomm_send_reg_msg(int sock,
 }
 
 static
-int serialize_basic_type(enum lttng_abstract_types atype,
+int serialize_basic_type(enum ustctl_abstract_types *uatype,
+               enum lttng_abstract_types atype,
                union _ustctl_basic_type *ubt,
                const union _lttng_basic_type *lbt)
 {
@@ -649,11 +662,13 @@ int serialize_basic_type(enum lttng_abstract_types atype,
                uit->base = lit->base;
                uit->encoding = lit->encoding;
                uit->alignment = lit->alignment;
+               *uatype = ustctl_atype_integer;
                break;
        }
        case atype_string:
        {
                ubt->string.encoding = lbt->string.encoding;
+               *uatype = ustctl_atype_string;
                break;
        }
        case atype_float:
@@ -667,6 +682,7 @@ int serialize_basic_type(enum lttng_abstract_types atype,
                uft->mant_dig = lft->mant_dig;
                uft->alignment = lft->alignment;
                uft->reverse_byte_order = lft->reverse_byte_order;
+               *uatype = ustctl_atype_float;
                break;
        }
        case atype_enum:
@@ -676,7 +692,6 @@ int serialize_basic_type(enum lttng_abstract_types atype,
                return -EINVAL;
        }
        return 0;
-
 }
 
 static
@@ -688,7 +703,7 @@ int serialize_one_type(struct ustctl_type *ut, const struct lttng_type *lt)
        case atype_integer:
        case atype_float:
        case atype_string:
-               ret = serialize_basic_type(lt->atype,
+               ret = serialize_basic_type(&ut->atype, lt->atype,
                        &ut->u.basic, &lt->u.basic);
                if (ret)
                        return ret;
@@ -702,10 +717,11 @@ int serialize_one_type(struct ustctl_type *ut, const struct lttng_type *lt)
                ubt = &ut->u.array.elem_type;
                lbt = &lt->u.array.elem_type;
                ut->u.array.length = lt->u.array.length;
-               ret = serialize_basic_type(lbt->atype,
+               ret = serialize_basic_type(&ubt->atype, lbt->atype,
                        &ubt->u.basic, &lbt->u.basic);
                if (ret)
                        return -EINVAL;
+               ut->atype = ustctl_atype_array;
                break;
        }
        case atype_sequence:
@@ -716,16 +732,17 @@ int serialize_one_type(struct ustctl_type *ut, const struct lttng_type *lt)
 
                ubt = &ut->u.sequence.length_type;
                lbt = &lt->u.sequence.length_type;
-               ret = serialize_basic_type(lbt->atype,
+               ret = serialize_basic_type(&ubt->atype, lbt->atype,
                        &ubt->u.basic, &lbt->u.basic);
                if (ret)
                        return -EINVAL;
                ubt = &ut->u.sequence.elem_type;
                lbt = &lt->u.sequence.elem_type;
-               ret = serialize_basic_type(lbt->atype,
+               ret = serialize_basic_type(&ubt->atype, lbt->atype,
                        &ubt->u.basic, &lbt->u.basic);
                if (ret)
                        return -EINVAL;
+               ut->atype = ustctl_atype_sequence;
                break;
        }
        case atype_enum:
@@ -1010,3 +1027,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.027074 seconds and 4 git commands to generate.