+ PERROR("listen");
+ goto free_addr;
+ }
+
+ sock = ustcomm_init_sock(fd, epoll_fd,
+ NULL);
+ if (!sock) {
+ ERR("failed to create ustcomm_sock");
+ goto free_addr;
+ }
+
+ free(addr);
+
+ return sock;
+
+free_addr:
+ free(addr);
+close_sock:
+ close(fd);
+
+ return NULL;
+}
+
+void ustcomm_del_named_sock(struct ustcomm_sock *sock,
+ int keep_socket_file)
+{
+ int result, fd;
+ struct stat st;
+ struct sockaddr dummy;
+ struct sockaddr_un *sockaddr = NULL;
+ int alloc_size;
+
+ fd = sock->fd;
+
+ if(!keep_socket_file) {
+
+ /* Get the socket name */
+ alloc_size = sizeof(dummy);
+ if (getsockname(fd, &dummy, (socklen_t *)&alloc_size) < 0) {
+ PERROR("getsockname failed");
+ goto del_sock;
+ }
+
+ sockaddr = zmalloc(alloc_size);
+ if (!sockaddr) {
+ ERR("failed to allocate sockaddr");
+ goto del_sock;
+ }
+
+ if (getsockname(fd, sockaddr, (socklen_t *)&alloc_size) < 0) {
+ PERROR("getsockname failed");
+ goto free_sockaddr;
+ }
+
+ /* Destroy socket */
+ result = stat(sockaddr->sun_path, &st);
+ if(result < 0) {
+ PERROR("stat (%s)", sockaddr->sun_path);
+ goto free_sockaddr;
+ }
+
+ /* Paranoid check before deleting. */
+ result = S_ISSOCK(st.st_mode);
+ if(!result) {
+ ERR("The socket we are about to delete is not a socket.");
+ goto free_sockaddr;
+ }
+
+ result = unlink(sockaddr->sun_path);
+ if(result < 0) {
+ PERROR("unlink");
+ }
+ }
+
+free_sockaddr:
+ free(sockaddr);
+
+del_sock:
+ ustcomm_del_sock(sock, keep_socket_file);
+}
+
+int ustcomm_recv_alloc(int sock,
+ struct ustcomm_header *header,
+ char **data) {
+ int result;
+ struct ustcomm_header peek_header;
+ struct iovec iov[2];
+ struct msghdr msg;
+
+ /* Just to make the caller fail hard */
+ *data = NULL;
+
+ result = recv(sock, &peek_header, sizeof(peek_header),
+ MSG_PEEK | MSG_WAITALL);
+ if (result <= 0) {
+ if(errno == ECONNRESET) {
+ return 0;
+ } else if (errno == EINTR) {
+ return -1;
+ } else if (result < 0) {
+ PERROR("recv");
+ return -1;
+ }
+ return 0;
+ }
+
+ memset(&msg, 0, sizeof(msg));
+
+ iov[0].iov_base = (char *)header;
+ iov[0].iov_len = sizeof(struct ustcomm_header);
+
+ msg.msg_iov = iov;
+ msg.msg_iovlen = 1;
+
+ if (peek_header.size) {
+ *data = zmalloc(peek_header.size);
+ if (!*data) {
+ return -ENOMEM;
+ }
+
+ iov[1].iov_base = *data;
+ iov[1].iov_len = peek_header.size;
+
+ msg.msg_iovlen++;
+ }
+
+ result = recvmsg(sock, &msg, MSG_WAITALL);
+ if (result < 0) {
+ free(*data);
+ PERROR("recvmsg failed");
+ }
+
+ return result;
+}
+
+/* returns 1 to indicate a message was received
+ * returns 0 to indicate no message was received (end of stream)
+ * returns -1 to indicate an error
+ */
+int ustcomm_recv_fd(int sock,
+ struct ustcomm_header *header,
+ char *data, int *fd)
+{
+ int result;
+ struct ustcomm_header peek_header;
+ struct iovec iov[2];
+ struct msghdr msg;
+ struct cmsghdr *cmsg;
+ char buf[CMSG_SPACE(sizeof(int))];
+
+ result = recv(sock, &peek_header, sizeof(peek_header),
+ MSG_PEEK | MSG_WAITALL);
+ if (result <= 0) {
+ if(errno == ECONNRESET) {
+ return 0;
+ } else if (errno == EINTR) {
+ return -1;
+ } else if (result < 0) {
+ PERROR("recv");
+ return -1;
+ }
+ return 0;
+ }
+
+ memset(&msg, 0, sizeof(msg));
+
+ iov[0].iov_base = (char *)header;
+ iov[0].iov_len = sizeof(struct ustcomm_header);
+
+ msg.msg_iov = iov;
+ msg.msg_iovlen = 1;
+
+ if (peek_header.size && data) {
+ if (peek_header.size < 0 ||
+ peek_header.size > USTCOMM_DATA_SIZE) {
+ ERR("big peek header! %d", peek_header.size);
+ return 0;
+ }
+
+ iov[1].iov_base = data;
+ iov[1].iov_len = peek_header.size;
+
+ msg.msg_iovlen++;