- int i, ret;
-
- mmd->selector_change = -1; /* no change nec., set to new num by com_chs */
- if (!conf.selector_given)
- goto random;
- for (i = 0; selectors[i].name; i++) {
- if (strcmp(selectors[i].name, conf.selector_arg))
- continue;
- PARA_NOTICE_LOG("initializing %s audio file selector\n",
- selectors[i].name);
- ret = selectors[i].init(&selectors[i]);
- if (ret < 0) {
- PARA_WARNING_LOG("%s", PARA_STRERROR(-ret));
- break;
- }
- mmd->selector_num = i;
- return;
+ signal_task = signal_init_or_die();
+ para_install_sighandler(SIGINT);
+ para_install_sighandler(SIGTERM);
+ para_install_sighandler(SIGHUP);
+ para_install_sighandler(SIGCHLD);
+ para_sigaction(SIGPIPE, SIG_IGN);
+ add_close_on_fork_list(signal_task->fd);
+ signal_task->task = task_register(&(struct task_info) {
+ .name = "signal",
+ .pre_select = signal_pre_select,
+ .post_select = signal_post_select,
+ .context = signal_task,
+
+ }, &sched);
+}
+
+static void command_pre_select(struct sched *s, void *context)
+{
+ struct server_command_task *sct = context;
+ para_fd_set(sct->listen_fd, &s->rfds, &s->max_fileno);
+}
+
+static int command_post_select(struct sched *s, void *context)
+{
+ struct server_command_task *sct = context;
+
+ int new_fd, ret, i;
+ char *peer_name;
+ pid_t child_pid;
+ uint32_t *chunk_table;
+
+ ret = para_accept(sct->listen_fd, &s->rfds, NULL, 0, &new_fd);
+ if (ret <= 0)
+ goto out;
+ peer_name = remote_name(new_fd);
+ PARA_INFO_LOG("got connection from %s, forking\n", peer_name);
+ mmd->num_connects++;
+ mmd->active_connections++;
+ /*
+ * The chunk table is a pointer located in the mmd struct that points
+ * to dynamically allocated memory, i.e. it must be freed by the parent
+ * and the child. However, as the mmd struct is in a shared memory
+ * area, there's no guarantee that after the fork this pointer is still
+ * valid in child context. As it is not used in the child anyway, we
+ * save it to a local variable before the fork and free the memory via
+ * that copy in the child directly after the fork.
+ */
+ chunk_table = mmd->afd.afhi.chunk_table;
+ child_pid = fork();
+ if (child_pid < 0) {
+ ret = -ERRNO_TO_PARA_ERROR(errno);
+ goto out;