+ 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;
+ struct para_buffer pb = {.buf = NULL};
+
+ 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;
+ para_printf(&pb, "successfully created %s table\n", t->name);
+ }
+ ret = open_afs_tables();
+out:
+ if (ret < 0)
+ para_printf(&pb, "%s\n", para_strerror(-ret));
+ if (pb.buf)
+ pass_buffer_as_shm(pb.buf, pb.offset, &fd);
+ free(pb.buf);
+}
+
+int com_init(int fd, int argc, char * const * const argv)
+{
+ int i, j, ret;
+ uint32_t table_mask = (1 << (NUM_AFS_TABLES + 1)) - 1;
+ struct osl_object query = {.data = &table_mask,
+ .size = sizeof(table_mask)};
+
+ ret = make_database_dir();
+ if (ret < 0)
+ return ret;
+ if (argc != 1) {
+ table_mask = 0;
+ for (i = 1; i < argc; i++) {
+ for (j = 0; j < NUM_AFS_TABLES; j++) {
+ struct afs_table *t = &afs_tables[j];
+
+ if (strcmp(argv[i], t->name))
+ continue;
+ table_mask |= (1 << j);
+ break;
+ }
+ if (j == NUM_AFS_TABLES)
+ return -E_BAD_TABLE_NAME;
+ }
+ }
+ ret = send_callback_request(create_tables_callback, &query, &send_result, &fd);
+ if (ret < 0)
+ return send_va_buffer(fd, "%s\n", para_strerror(-ret));
+ return ret;
+}
+
+/**
+ * Flags for the check command.
+ *
+ * \sa com_check().
+ */
+enum com_check_flags {
+ /** Check the audio file table. */
+ CHECK_AFT = 1,
+ /** Check the mood table. */
+ CHECK_MOODS = 2,
+ /** Check the playlist table. */
+ CHECK_PLAYLISTS = 4
+};
+
+int com_check(int fd, int argc, char * const * const argv)
+{
+ unsigned flags = 0;
+ int i, ret;
+
+ for (i = 1; i < argc; i++) {
+ const char *arg = argv[i];
+ if (arg[0] != '-')
+ break;
+ if (!strcmp(arg, "--")) {
+ i++;
+ break;
+ }
+ if (!strcmp(arg, "-a")) {
+ flags |= CHECK_AFT;
+ continue;
+ }
+ if (!strcmp(arg, "-p")) {
+ flags |= CHECK_PLAYLISTS;
+ continue;
+ }
+ if (!strcmp(arg, "-m")) {
+ flags |= CHECK_MOODS;
+ continue;
+ }
+ return -E_AFS_SYNTAX;
+ }
+ if (i < argc)
+ return -E_AFS_SYNTAX;
+ if (!flags)
+ flags = ~0U;
+ if (flags & CHECK_AFT) {
+ ret = send_callback_request(aft_check_callback, NULL, send_result, &fd);
+ if (ret < 0)
+ return ret;
+ }
+ if (flags & CHECK_PLAYLISTS) {
+ ret = send_callback_request(playlist_check_callback, NULL, send_result, &fd);
+ if (ret < 0)
+ return ret;
+ }
+ if (flags & CHECK_MOODS) {
+ ret = send_callback_request(mood_check_callback, NULL, send_result, &fd);
+ if (ret < 0)
+ return ret;
+ }
+ return 1;
+}
+
+/**
+ * The afs event dispatcher.
+ *
+ * \param event Type of the event.
+ * \param pb May be \p NULL.
+ * \param data Type depends on \a event.
+ *
+ * This function calls the table handlers of all tables and passes \a pb and \a
+ * data verbatim. It's up to the handlers to interpret the \a data pointer.
+ */
+void afs_event(enum afs_events event, struct para_buffer *pb,
+ void *data)
+{
+ int i, ret;
+
+ for (i = 0; i < NUM_AFS_TABLES; i++) {
+ struct afs_table *t = &afs_tables[i];
+ if (!t->event_handler)
+ continue;
+ ret = t->event_handler(event, pb, data);
+ if (ret < 0)
+ PARA_CRIT_LOG("table %s, event %d: %s\n", t->name,
+ event, para_strerror(-ret));
+ }
+}
+
+int images_event_handler(__a_unused enum afs_events event,
+ __a_unused struct para_buffer *pb, __a_unused void *data)
+{
+ return 1;
+}
+
+int lyrics_event_handler(__a_unused enum afs_events event,
+ __a_unused struct para_buffer *pb, __a_unused void *data)
+{
+ return 1;