+static int call_callback(int fd, int query_shmid)
+{
+ void *query_shm;
+ struct callback_query *cq;
+ struct osl_object query;
+ int ret;
+
+ ret = shm_attach(query_shmid, ATTACH_RW, &query_shm);
+ if (ret < 0)
+ return ret;
+ cq = query_shm;
+ query.data = (char *)query_shm + sizeof(*cq);
+ query.size = cq->query_size;
+ cq->handler(fd, &query);
+ return shm_detach(query_shm);
+}
+
+static int execute_server_command(void)
+{
+ char buf[8];
+ int ret = recv_bin_buffer(server_socket, buf, sizeof(buf) - 1);
+
+ if (ret <= 0) {
+ if (!ret)
+ ret = -ERRNO_TO_PARA_ERROR(ECONNRESET);
+ goto err;
+ }
+ buf[ret] = '\0';
+ PARA_DEBUG_LOG("received: %s\n", buf);
+ ret = -E_BAD_CMD;
+ if (strcmp(buf, "new"))
+ goto err;
+ ret = open_next_audio_file();
+err:
+ return ret;
+}
+
+static void execute_afs_command(int fd, uint32_t expected_cookie)
+{
+ uint32_t cookie;
+ int query_shmid;
+ char buf[sizeof(cookie) + sizeof(query_shmid)];
+ int ret = recv_bin_buffer(fd, buf, sizeof(buf));
+
+ if (ret < 0)
+ goto err;
+ if (ret != sizeof(buf)) {
+ PARA_NOTICE_LOG("short read (%d bytes, expected %lu)\n",
+ ret, (long unsigned) sizeof(buf));
+ return;
+ }
+ cookie = *(uint32_t *)buf;
+ if (cookie != expected_cookie) {
+ PARA_NOTICE_LOG("received invalid cookie(got %u, expected %u)\n",
+ (unsigned)cookie, (unsigned)expected_cookie);
+ return;
+ }
+ query_shmid = *(int *)(buf + sizeof(cookie));
+ if (query_shmid < 0) {
+ PARA_WARNING_LOG("received invalid query shmid %d)\n",
+ query_shmid);
+ return;
+ }
+ ret = call_callback(fd, query_shmid);
+ if (ret >= 0)
+ return;
+err:
+ PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
+}
+
+/** Shutdown connection if query has not arrived until this many seconds. */
+#define AFS_CLIENT_TIMEOUT 3
+
+static void command_post_select(struct sched *s, struct task *t)