+ PARA_ERROR_LOG("%s init: %s\n", afs_tables[i].name,
+ para_strerror(-ret));
+ break;
+ }
+ if (ret >= 0)
+ return ret;
+ while (i)
+ afs_tables[--i].close();
+ return ret;
+}
+
+static void signal_pre_select(struct sched *s, struct task *t)
+{
+ struct signal_task *st = container_of(t, struct signal_task, task);
+ para_fd_set(st->fd, &s->rfds, &s->max_fileno);
+}
+
+static void afs_signal_post_select(struct sched *s, struct task *t)
+{
+ struct signal_task *st = container_of(t, struct signal_task, task);
+ if (getppid() == 1) {
+ PARA_EMERG_LOG("para_server died\n");
+ goto shutdown;
+ }
+ if (!FD_ISSET(st->fd, &s->rfds))
+ return;
+ st->signum = para_next_signal();
+ if (st->signum == SIGHUP) {
+ close_afs_tables();
+ parse_config_or_die(1);
+ t->error = open_afs_tables();
+ if (t->error < 0)
+ return;
+ init_admissible_files(current_mop);
+ return;
+ }
+ PARA_EMERG_LOG("terminating on signal %d\n", st->signum);
+shutdown:
+ sched_shutdown();
+ t->error = -E_AFS_SIGNAL;
+}
+
+static void register_signal_task(void)
+{
+ struct signal_task *st = &signal_task_struct;
+
+ para_sigaction(SIGPIPE, SIG_IGN);
+ 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(SIGHUP);
+
+ st->task.pre_select = signal_pre_select;
+ st->task.post_select = afs_signal_post_select;
+ sprintf(st->task.status, "signal task");
+ register_task(&st->task);
+}
+
+static struct list_head afs_client_list;
+
+/** Describes on connected afs client. */
+struct afs_client {
+ /** Position in the afs client list. */
+ struct list_head node;
+ /** The socket file descriptor for this client. */
+ int fd;
+ /** The time the client connected. */
+ struct timeval connect_time;
+};
+
+static void command_pre_select(struct sched *s, struct task *t)
+{
+ struct command_task *ct = container_of(t, struct command_task, task);
+ struct afs_client *client;
+
+ para_fd_set(server_socket, &s->rfds, &s->max_fileno);
+ 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);
+}
+
+/**
+ * Send data as shared memory to a file descriptor.
+ *
+ * \param buf The buffer holding the data to be sent.
+ * \param size The size of \a buf.
+ * \param fd_ptr A pointer to the file descriptor.
+ *
+ * This function is used as the \a max_size handler in a \ref para_buffer
+ * structure. If used this way, it is called by \ref para_printf() whenever
+ * the buffer passed to para_printf() is about to exceed its maximal size.
+ *
+ * This function creates a shared memory area large enough to hold
+ * the content given by \a buf and \a size and sends the identifier
+ * of this area to the file descriptor given by \a fd_ptr.
+ *
+ * \return Zero if \a buf is \p NULL or \a size is zero. Negative on errors,
+ * and positive on success.
+ */
+int pass_buffer_as_shm(char *buf, size_t size, void *fd_ptr)
+{
+ 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 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)
+{
+ 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)