lib/process: Allow process output to be retrieved on each event
[petitboot] / lib / process / process.c
index 6e143e0e4784e086b9f171bdb61d670e31efe523..93fd9c90b9bff4c8c67f0dcf02039d04c8adfd56 100644 (file)
@@ -60,15 +60,22 @@ static struct process_info *get_info(struct process *process)
        return container_of(process, struct process_info, process);
 }
 
+struct process *procinfo_get_process(struct process_info *procinfo)
+{
+       return &procinfo->process;
+}
+
 /* Read as much as possible into the currently-allocated stdout buffer, and
  * possibly realloc it for the next read
+ * If the line pointer is not NULL, it is set to the start of the latest
+ * output.
  *
  * Returns:
  *  > 0 on success (even though no bytes may have been read)
  *    0 on EOF (no error, but no more reads can be performed)
  *  < 0 on error
  **/
-static int process_read_stdout_once(struct process_info *procinfo)
+static int process_read_stdout_once(struct process_info *procinfo, char **line)
 {
        struct process *process = &procinfo->process;
        int rc, fd, max_len;
@@ -89,6 +96,9 @@ static int process_read_stdout_once(struct process_info *procinfo)
                return rc;
        }
 
+       if (line)
+               *line = process->stdout_buf + process->stdout_len;
+
        process->stdout_len += rc;
        if (process->stdout_len == procinfo->stdout_buf_len - 1) {
                procinfo->stdout_buf_len *= 2;
@@ -157,7 +167,7 @@ static int process_read_stdout(struct process_info *procinfo)
                return 0;
 
        do {
-               rc = process_read_stdout_once(procinfo);
+               rc = process_read_stdout_once(procinfo, NULL);
        } while (rc > 0);
 
        process_finish_stdout(procinfo);
@@ -170,7 +180,27 @@ static int process_stdout_cb(void *arg)
        struct process_info *procinfo = arg;
        int rc;
 
-       rc = process_read_stdout_once(procinfo);
+       rc = process_read_stdout_once(procinfo, NULL);
+
+       /* if we're going to signal to the waitset that we're done (ie, non-zero
+        * return value), then the waiters will remove us, so we drop the
+        * reference */
+       if (rc < 0) {
+               talloc_unlink(procset, procinfo);
+               procinfo->stdout_waiter = NULL;
+               rc = -1;
+       } else {
+               rc = 0;
+       }
+
+       return rc;
+}
+
+int process_stdout_custom(struct process_info *procinfo, char **line)
+{
+       int rc;
+
+       rc = process_read_stdout_once(procinfo, line);
 
        /* if we're going to signal to the waitset that we're done (ie, non-zero
         * return value), then the waiters will remove us, so we drop the
@@ -387,6 +417,7 @@ int process_run_sync(struct process *process)
 int process_run_async(struct process *process)
 {
        struct process_info *procinfo = get_info(process);
+       waiter_cb stdout_cb;
        int rc;
 
        rc = process_run_common(procinfo);
@@ -394,10 +425,10 @@ int process_run_async(struct process *process)
                return rc;
 
        if (process->keep_stdout) {
+               stdout_cb = process->stdout_cb ?: process_stdout_cb;
                procinfo->stdout_waiter = waiter_register_io(procset->waitset,
                                                procinfo->stdout_pipe[0],
-                                               WAIT_IN, process_stdout_cb,
-                                               procinfo);
+                                               WAIT_IN, stdout_cb, procinfo);
                talloc_reference(procset, procinfo);
        }