daemon: Introduce parent_waits flag for daemonize().
[paraslash.git] / audiod.c
index 03218e249ec3b69b8863c245628514a5ae32a225..8008e2cb6459f84d3b258bd9efeb0fffc77e4016 100644 (file)
--- a/audiod.c
+++ b/audiod.c
@@ -289,7 +289,7 @@ out:
                length / 60,
                length % 60
        );
-       PARA_DEBUG_LOG("slot %d: %s\n", slot_num, msg);
+       //PARA_DEBUG_LOG("slot %d: %s\n", slot_num, msg);
        return msg;
 empty:
        return para_strdup(NULL);
@@ -522,16 +522,12 @@ static void open_writers(struct slot_info *s)
        assert(s->wns == NULL);
        s->wns = para_calloc(PARA_MAX(1U, a->num_writers)
                * sizeof(struct writer_node));
-       if (a->num_writers == 0)
-               setup_writer_node(NULL, parent, s->wns);
-       else {
-               PARA_INFO_LOG("opening %s writers\n", audio_formats[s->format]);
-               for (i = 0; i < a->num_writers; i++) {
-                       wn = s->wns + i;
-                       wn->conf = a->writer_conf[i];
-                       wn->writer_num = a->writer_nums[i];
-                       register_writer_node(wn, parent);
-               }
+       PARA_INFO_LOG("opening %s writers\n", audio_formats[s->format]);
+       for (i = 0; i < a->num_writers; i++) {
+               wn = s->wns + i;
+               wn->conf = a->writer_conf[i];
+               wn->writer_num = a->writer_nums[i];
+               register_writer_node(wn, parent);
        }
 }
 
@@ -780,29 +776,38 @@ static int parse_writer_args(void)
 
                ret = parse_stream_command(conf.writer_arg[i], &cmd);
                if (ret < 0)
-                       goto out;
+                       return ret;
                af_mask = ret;
                FOR_EACH_AUDIO_FORMAT(j) {
                        a = afi + j;
                        if ((af_mask & (1 << j)) == 0) /* no match */
                                continue;
-                       ret = -E_WRITE_COMMON_SYNTAX;
-                       wconf = check_writer_arg(cmd, &writer_num);
-                       if (!wconf)
-                               goto out;
+                       wconf = check_writer_arg_or_die(cmd, &writer_num);
                        nw = a->num_writers;
                        a->writer_nums = para_realloc(a->writer_nums, (nw + 1) * sizeof(int));
                        a->writer_conf = para_realloc(a->writer_conf, (nw + 1) * sizeof(void *));
                        a->writer_nums[nw] = writer_num;
                        a->writer_conf[nw] = wconf;
-                       PARA_INFO_LOG("%s writer #%d: %s\n", audio_formats[writer_num],
+                       PARA_INFO_LOG("%s writer #%d: %s\n", audio_formats[j],
                                nw, writer_names[writer_num]);
                        a->num_writers++;
                }
        }
-       ret = 1;
-out:
-       return ret;
+       /* Use default writer for audio formats which are not yet set up. */
+       FOR_EACH_AUDIO_FORMAT(i) {
+               struct writer *w = writers + DEFAULT_WRITER;
+               a = afi + i;
+               if (a->num_writers > 0)
+                       continue; /* already set up */
+               PARA_INFO_LOG("%s writer: %s (default)\n", audio_formats[i],
+                       writer_names[DEFAULT_WRITER]);
+               a->writer_nums = para_malloc(sizeof(int));
+               a->writer_nums[0] = DEFAULT_WRITER;
+               a->writer_conf = para_malloc(sizeof(void *));
+               a->writer_conf[0] = w->parse_config_or_die("");
+               a->num_writers = 1;
+       }
+       return 1;
 }
 
 static int parse_receiver_args(void)
@@ -1048,7 +1053,6 @@ static void close_stat_pipe(void)
 {
        if (!stat_task->ct)
                return;
-       btr_free_node(stat_task->ct->btrn);
        client_close(stat_task->ct);
        stat_task->ct = NULL;
        clear_and_dump_items();
@@ -1366,7 +1370,7 @@ int main(int argc, char *argv[])
                exit(EXIT_FAILURE);
        }
        log_welcome("para_audiod");
-       server_uptime(UPTIME_SET);
+       set_server_start_time(NULL);
        set_initial_status();
        FOR_EACH_SLOT(i)
                clear_slot(i);
@@ -1377,7 +1381,7 @@ int main(int argc, char *argv[])
        init_command_task(cmd_task);
 
        if (conf.daemon_given)
-               daemonize();
+               daemonize(false /* parent exits immediately */);
 
        register_task(&sig_task->task);
        register_task(&cmd_task->task);