Cygwin: Pass file paths instead of file descriptors over UNIX sockets
[lttng-ust.git] / liblttng-ust-comm / lttng-ust-comm.c
index 078b56a01bb68c49c2322fc2a8f454d597305314..3138016569e8d0c11d2036e2c2fb97225efc399a 100644 (file)
@@ -509,3 +509,85 @@ int ustcomm_recv_fd(int sock)
 end:
        return ret;
 }
+
+ssize_t ustcomm_send_string(int sock, char *str, size_t len)
+{
+       ssize_t slen, ret = -1;
+
+       if (!str) {
+               goto end;
+       }
+
+       /* Send string len first */
+       slen = ustcomm_send_unix_sock(sock, &len, sizeof(len));
+
+       if (slen != sizeof(len)) {
+               fprintf(stderr,
+                       "Unexpected sent size. Expected %zu got %zu\n",
+                       sizeof(len), slen);
+               ret = -1;
+               goto end;
+       }
+
+       /* Send the actual string */
+       slen = ustcomm_send_unix_sock(sock, str, len);
+       if (slen != len) {
+               fprintf(stderr,
+                       "Unexpected sent size. Expected %zu got %zu\n",
+                       len, slen);
+               ret = -1;
+               goto end;
+       }
+
+       ret = slen;
+
+end:
+       return ret;
+}
+
+/*
+ * Allocate and return the received string.
+ * Return NULL on error.
+ * Caller is responsible of freeing the allocated string.
+ */
+char *ustcomm_recv_string(int sock)
+{
+       ssize_t rlen;
+       size_t len;
+       char *ret;
+
+       /* Get the string len first */
+       rlen = ustcomm_recv_unix_sock(sock, &len, sizeof(len));
+
+       if (rlen != sizeof(len)) {
+               fprintf(stderr,
+                       "Unexpected received size. Expected %zu got %zu\n",
+                       sizeof(len), rlen);
+               ret = NULL;
+               goto end;
+       }
+
+       /* Account for the NULL byte */
+       ret = malloc(len + 1);
+       if (!ret) {
+               ret = NULL;
+               goto end;
+       }
+
+       /* Get the actual string */
+       rlen = ustcomm_recv_unix_sock(sock, ret, len);
+       if (rlen != len) {
+               fprintf(stderr,
+                       "Unexpected received size. Expected %zu got %zu\n",
+                       len, rlen);
+               free(ret);
+               ret = NULL;
+               goto end;
+       }
+
+       /* Set terminating NULL byte */
+       ret[len] = '\0';
+
+end:
+       return ret;
+}
This page took 0.023552 seconds and 4 git commands to generate.