+ 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;
+ }