#include <unistd.h>
#include <assert.h>
#include <errno.h>
+#include <fcntl.h>
#include <ust-comm.h>
* libust threads require the close-on-exec flag for all
* resources so it does not leak file descriptors upon exec.
*/
- fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
+ fd = socket(PF_UNIX, SOCK_STREAM, 0);
if (fd < 0) {
perror("socket");
ret = fd;
goto error;
}
+ ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
+ if (ret < 0) {
+ perror("fcntl");
+ goto error_fcntl;
+ }
memset(&sun, 0, sizeof(sun));
sun.sun_family = AF_UNIX;
return fd;
error_connect:
+error_fcntl:
close(fd);
error:
return ret;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
- ret = recvmsg(sock, &msg, 0);
- if (ret < 0) {
+ do {
+ ret = recvmsg(sock, &msg, 0);
+ } while (ret < 0 && errno == EINTR);
+ if (ret < 0 && errno != EPIPE) {
perror("recvmsg");
}
* by ignoring SIGPIPE, but we don't have this luxury on the
* libust side.
*/
- ret = sendmsg(sock, &msg, MSG_NOSIGNAL);
- if (ret < 0) {
+ do {
+ ret = sendmsg(sock, &msg, MSG_NOSIGNAL);
+ } while (ret < 0 && errno == EINTR);
+ if (ret < 0 && errno != EPIPE) {
perror("sendmsg");
}
{
int ret;
- /* Shutdown receptions and transmissions */
- ret = shutdown(sock, SHUT_RDWR);
+ ret = close(sock);
if (ret < 0) {
- perror("shutdown");
+ perror("close");
}
return ret;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
- ret = sendmsg(sock, &msg, 0);
- if (ret < 0) {
+ do {
+ ret = sendmsg(sock, &msg, MSG_NOSIGNAL);
+ } while (ret < 0 && errno == EINTR);
+ if (ret < 0 && errno != EPIPE) {
perror("sendmsg");
}
msg.msg_control = recv_fd;
msg.msg_controllen = sizeof(recv_fd);
- if ((ret = recvmsg(sock, &msg, 0)) < 0) {
- perror("recvmsg");
+ do {
+ ret = recvmsg(sock, &msg, 0);
+ } while (ret < 0 && errno == EINTR);
+ if (ret < 0) {
+ if (errno != EPIPE) {
+ perror("recvmsg");
+ }
goto end;
}
if (ret != sizeof(data_fd)) {
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;
+}