+ int ret, shmid, fd = *(int *)fd_ptr;
+ void *shm;
+ struct callback_result *cr;
+
+ if (!buf || !size)
+ return 0;
+ ret = shm_new(size + sizeof(struct callback_result));
+ if (ret < 0)
+ return ret;
+ shmid = ret;
+ ret = shm_attach(shmid, ATTACH_RW, &shm);
+ if (ret < 0)
+ goto err;
+ cr = shm;
+ cr->result_size = size;
+ memcpy(shm + sizeof(*cr), buf, size);
+ ret = shm_detach(shm);
+ if (ret < 0)
+ goto err;
+ ret = send_bin_buffer(fd, (char *)&shmid, sizeof(int));
+ if (ret >= 0)
+ return ret;
+err:
+ if (shm_destroy(shmid) < 0)
+ PARA_ERROR_LOG("destroy result failed\n");
+ return ret;
+}
+
+/*
+ * On errors, negative value is written to fd.
+ * On success: If query produced a result, the result_shmid is written to fd.
+ * Otherwise, zero is written.
+ */
+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 1;
+}
+
+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)
+{
+ struct command_task *ct = container_of(t, struct command_task, task);
+ struct sockaddr_un unix_addr;
+ struct afs_client *client, *tmp;
+ int fd, ret;
+
+ if (FD_ISSET(server_socket, &s->rfds)) {
+ ret = execute_server_command();
+ if (ret < 0) {
+ PARA_EMERG_LOG("%s\n", para_strerror(-ret));
+ sched_shutdown();
+ return;
+ }
+ }
+
+ /* Check the list of connected clients. */
+ list_for_each_entry_safe(client, tmp, &afs_client_list, node) {
+ if (FD_ISSET(client->fd, &s->rfds))
+ execute_afs_command(client->fd, ct->cookie);
+ else { /* prevent bogus connection flooding */
+ struct timeval diff;
+ tv_diff(now, &client->connect_time, &diff);
+ if (diff.tv_sec < AFS_CLIENT_TIMEOUT)
+ continue;
+ PARA_WARNING_LOG("connection timeout\n");
+ }
+ close(client->fd);
+ list_del(&client->node);
+ free(client);
+ }
+ /* Accept connections on the local socket. */
+ if (!FD_ISSET(ct->fd, &s->rfds))
+ return;
+ ret = para_accept(ct->fd, &unix_addr, sizeof(unix_addr));
+ if (ret < 0) {
+ PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
+ return;
+ }
+ fd = ret;
+ ret = mark_fd_nonblocking(fd);
+ if (ret < 0) {
+ PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
+ close(fd);
+ return;
+ }
+ client = para_malloc(sizeof(*client));
+ client->fd = fd;
+ client->connect_time = *now;
+ para_list_add(&client->node, &afs_client_list);
+}
+
+static void register_command_task(uint32_t cookie)
+{
+ struct command_task *ct = &command_task_struct;
+ ct->fd = setup_command_socket_or_die();
+ ct->cookie = cookie;
+
+ ct->task.pre_select = command_pre_select;
+ ct->task.post_select = command_post_select;
+ sprintf(ct->task.status, "command task");
+ register_task(&ct->task);
+}
+
+/**
+ * Initialize the audio file selector process.
+ *
+ * \param cookie The value used for "authentication".
+ * \param socket_fd File descriptor used for communication with the server.
+ */
+__noreturn void afs_init(uint32_t cookie, int socket_fd)
+{
+ static struct sched s;
+ int i, ret;
+
+ register_signal_task();
+ INIT_LIST_HEAD(&afs_client_list);
+ for (i = 0; i < NUM_AFS_TABLES; i++)
+ afs_tables[i].init(&afs_tables[i]);
+ ret = open_afs_tables();
+ if (ret < 0)
+ goto out;
+ server_socket = socket_fd;
+ ret = mark_fd_nonblocking(server_socket);
+ if (ret < 0)
+ goto out_close;
+ PARA_INFO_LOG("server_socket: %d, afs_socket_cookie: %u\n",
+ server_socket, (unsigned) cookie);
+ init_admissible_files(conf.afs_initial_mode_arg);
+ register_command_task(cookie);
+ s.default_timeout.tv_sec = 0;
+ s.default_timeout.tv_usec = 999 * 1000;
+ ret = schedule(&s);
+out_close:
+ close_afs_tables();
+out:
+ if (ret < 0)
+ PARA_EMERG_LOG("%s\n", para_strerror(-ret));
+ exit(EXIT_FAILURE);
+}
+
+static void create_tables_callback(int fd, const struct osl_object *query)
+{
+ uint32_t table_mask = *(uint32_t *)query->data;
+ int i, ret;
+ char *buf;
+
+ close_afs_tables();
+ for (i = 0; i < NUM_AFS_TABLES; i++) {
+ struct afs_table *t = &afs_tables[i];
+
+ if (!(table_mask & (1 << i)))
+ continue;
+ if (!t->create)
+ continue;
+ ret = t->create(database_dir);
+ if (ret < 0)
+ goto out;
+ }
+ ret = open_afs_tables();
+out:
+ if (ret >= 0)
+ buf = make_message("successfully created afs table(s)\n");