+ return tv;
+}
+
+static int recv_afs_msg(int *fd, uint32_t *code, uint32_t *data)
+{
+ char control[255], buf[8];
+ struct msghdr msg = {.msg_iov = NULL};
+ struct cmsghdr *cmsg;
+ struct iovec iov;
+ int ret = 0;
+
+ *fd = -1;
+ iov.iov_base = buf;
+ iov.iov_len = sizeof(buf);
+ msg.msg_iov = &iov;
+ msg.msg_iovlen = 1;
+ msg.msg_control = control;
+ msg.msg_controllen = sizeof(control);
+ memset(buf, 0, sizeof(buf));
+ ret = recvmsg(afs_socket, &msg, 0);
+ if (ret < 0)
+ return -ERRNO_TO_PARA_ERROR(errno);
+ afsss = AFS_SOCKET_READY;
+ if (iov.iov_len != sizeof(buf))
+ return -E_SHORT_AFS_READ;
+ *code = *(uint32_t*)buf;
+ *data = *(uint32_t*)(buf + 4);
+ for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
+ if (cmsg->cmsg_level != SOL_SOCKET
+ || cmsg->cmsg_type != SCM_RIGHTS)
+ continue;
+ if ((cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int) != 1)
+ continue;
+ *fd = *(int *)CMSG_DATA(cmsg);
+ }
+ return 1;
+}
+
+static void recv_afs_result(void)
+{
+ int ret, passed_fd, shmid;
+ uint32_t afs_code = 0, afs_data = 0;
+ struct stat statbuf;
+ struct timeval now;
+
+ ret = recv_afs_msg(&passed_fd, &afs_code, &afs_data);
+ if (ret < 0)
+ goto err;
+ PARA_DEBUG_LOG("fd: %d, code: %u, shmid: %u\n", passed_fd, afs_code,
+ afs_data);
+ ret = -E_NOFD;
+ if (afs_code != NEXT_AUDIO_FILE)
+ goto err;
+ if (passed_fd < 0)
+ goto err;
+ shmid = afs_data;
+ ret = load_afd(shmid, &mmd->afd);
+ if (ret < 0)
+ goto err;
+ shm_destroy(shmid);
+ ret = fstat(passed_fd, &statbuf);
+ if (ret < 0) {
+ PARA_ERROR_LOG("fstat error:\n");
+ ret = -ERRNO_TO_PARA_ERROR(errno);
+ goto err;
+ }
+ mmd->size = statbuf.st_size;
+ mmd->mtime = statbuf.st_mtime;
+ map = para_mmap(mmd->size, PROT_READ, MAP_PRIVATE,
+ passed_fd, 0);
+ close(passed_fd);
+ mmd->chunks_sent = 0;
+ mmd->current_chunk = 0;
+ mmd->offset = 0;
+ mmd->events++;
+ mmd->num_played++;
+ mmd->new_vss_status_flags &= (~VSS_NEXT);
+ gettimeofday(&now, NULL);
+ tv_add(&now, &announce_tv, &data_send_barrier);
+ return;
+err:
+ if (passed_fd >= 0)
+ close(passed_fd);
+ PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
+ mmd->new_vss_status_flags = VSS_NEXT;
+}
+
+void vss_post_select(fd_set *rfds, fd_set *wfds)
+{
+ int ret;
+
+ if (FD_ISSET(afs_socket, rfds))
+ recv_afs_result();
+ if (afsss != AFS_SOCKET_CHECK_FOR_WRITE || !FD_ISSET(afs_socket, wfds))
+ return;
+ PARA_NOTICE_LOG("requesting new fd from afs\n");
+ ret = send_buffer(afs_socket, "new");
+ afsss = AFS_SOCKET_AFD_PENDING;