+static void close_afs_tables(enum osl_close_flags flags)
+{
+ PARA_NOTICE_LOG("closing afs_tables\n");
+ score_shutdown(flags);
+ attribute_shutdown(flags);
+ close_current_mood();
+ playlist_close();
+ moods_shutdown(flags);
+ playlists_shutdown(flags);
+ lyrics_shutdown(flags);
+ images_shutdown(flags);
+ aft_shutdown(flags);
+}
+
+static void signal_pre_select(struct sched *s, struct task *t)
+{
+ struct signal_task *st = t->private_data;
+ t->ret = 1;
+ para_fd_set(st->fd, &s->rfds, &s->max_fileno);
+}
+
+static void signal_post_select(struct sched *s, struct task *t)
+{
+ struct signal_task *st = t->private_data;
+ t->ret = 1;
+ if (!FD_ISSET(st->fd, &s->rfds))
+ return;
+ st->signum = para_next_signal();
+ t->ret = 1;
+ if (st->signum == SIGUSR1)
+ return; /* ignore SIGUSR1 */
+ PARA_NOTICE_LOG("caught signal %d\n", st->signum);
+ t->ret = -E_SIGNAL_CAUGHT;
+ unregister_tasks();
+}
+
+static void register_signal_task(void)
+{
+ struct signal_task *st = &signal_task_struct;
+ st->fd = para_signal_init();
+ PARA_INFO_LOG("signal pipe: fd %d\n", st->fd);
+ para_install_sighandler(SIGINT);
+ para_install_sighandler(SIGTERM);
+ para_install_sighandler(SIGPIPE);
+
+ st->task.pre_select = signal_pre_select;
+ st->task.post_select = signal_post_select;
+ st->task.private_data = st;
+ sprintf(st->task.status, "signal task");
+ register_task(&st->task);
+}
+
+static struct list_head afs_client_list;
+
+struct afs_client {
+ struct list_head node;
+ int fd;
+ struct timeval connect_time;
+};
+
+static void command_pre_select(struct sched *s, struct task *t)
+{
+ struct command_task *ct = t->private_data;
+ struct afs_client *client;
+
+ para_fd_set(ct->fd, &s->rfds, &s->max_fileno);
+ list_for_each_entry(client, &afs_client_list, node)
+ para_fd_set(client->fd, &s->rfds, &s->max_fileno);
+ t->ret = 1;
+}
+
+/*
+ * 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, *result_shm;
+ struct callback_query *cq;
+ struct callback_result *cr;
+ struct osl_object query, result = {.data = NULL};
+ int result_shmid = -1, ret, ret2;
+
+ ret = shm_attach(query_shmid, ATTACH_RW, &query_shm);
+ if (ret < 0)
+ goto out;
+ cq = query_shm;
+ query.data = (char *)query_shm + sizeof(*cq);
+ query.size = cq->query_size;
+ ret = cq->handler(&query, &result);
+ ret2 = shm_detach(query_shm);
+ if (ret2 < 0 && ret >= 0)
+ ret = ret2;
+ if (ret < 0)
+ goto out;
+ ret = 0;
+ if (!result.data || !result.size)
+ goto out;
+ ret = shm_new(result.size + sizeof(struct callback_result));
+ if (ret < 0)
+ goto out;
+ result_shmid = ret;
+ ret = shm_attach(result_shmid, ATTACH_RW, &result_shm);
+ if (ret < 0)
+ goto out;
+ cr = result_shm;
+ cr->result_size = result.size;
+ memcpy(result_shm + sizeof(*cr), result.data, result.size);
+ ret = shm_detach(result_shm);
+ if (ret < 0)
+ goto out;
+ ret = result_shmid;
+out:
+ free(result.data);
+ ret2 = send_bin_buffer(fd, (char *)&ret, sizeof(int));
+ if (ret < 0 || ret2 < 0) {
+ if (result_shmid >= 0)
+ if (shm_destroy(result_shmid) < 0)
+ PARA_ERROR_LOG("destroy result failed\n");
+ if (ret >= 0)
+ ret = ret2;
+ }
+ 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) {
+ PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-ret));
+ return;
+ }
+ 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;
+ }
+ /* Ignore return value: Errors might be ok here. */
+ call_callback(fd, query_shmid);
+}
+
+#define AFS_CLIENT_TIMEOUT 3
+
+static void command_post_select(struct sched *s, struct task *t)
+{
+ struct command_task *ct = t->private_data;
+ struct sockaddr_un unix_addr;
+ struct afs_client *client, *tmp;
+
+ /* First, 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 flodding */
+ 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);
+ }
+ /* Next, accept connections on the local socket. */
+ if (!FD_ISSET(ct->fd, &s->rfds))
+ goto out;
+ t->ret = para_accept(ct->fd, &unix_addr, sizeof(unix_addr));
+ if (t->ret < 0) {
+ PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
+ goto out;
+ }
+ client = para_malloc(sizeof(*client));
+ client->fd = t->ret;
+ client->connect_time = *now;
+ para_list_add(&client->node, &afs_client_list);
+out:
+ t->ret = 1;
+}
+
+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;
+ ct->task.private_data = ct;
+ sprintf(ct->task.status, "command task");
+ register_task(&ct->task);
+}
+
+void register_tasks(uint32_t cookie)
+{
+ register_signal_task();
+ register_command_task(cookie);
+}
+
+static char *database_dir;
+
+static int make_database_dir(void)