lib/process: Allow process output to be retrieved on each event
authorSamuel Mendoza-Jonas <sam@mendozajonas.com>
Fri, 21 Oct 2016 04:54:52 +0000 (15:54 +1100)
committerSamuel Mendoza-Jonas <sam@mendozajonas.com>
Tue, 20 Dec 2016 05:40:21 +0000 (16:40 +1100)
Allow a custom callback function to be set when registering the IO
waiter for asynchronous processes.

To allow output from processes to be parsed as it is received, add
process_stdout_custom() which passes a new "line" parameter to
process_read_stdout_once() in order to consume output as it appears.

Users of a custom IO callback will only have access to the process_info
struct which is internal to lib/process; the function
procinfo_get_process() is added to allow these callers to access process
information.

Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
lib/process/process.c
lib/process/process.h

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);
        }
 
index f75f1976c718df3970930c88be9bf1de2bde299f..65fdba8d537beeb9e43efe9d3f3b08429d143530 100644 (file)
@@ -23,6 +23,7 @@
 
 struct process;
 struct procset;
+struct process_info;
 
 typedef void   (*process_exit_cb)(struct process *);
 
@@ -34,6 +35,8 @@ struct process {
        bool                    add_stderr;
        process_exit_cb         exit_cb;
        void                    *data;
+       waiter_cb               stdout_cb;
+       void                    *stdout_data;
 
        /* runtime data */
        pid_t                   pid;
@@ -79,4 +82,8 @@ void process_stop_async(struct process *process);
  * exit status */
 bool process_exit_ok(struct process *process);
 
+/* Functions to assist callers using a custom stdout callback */
+struct process *procinfo_get_process(struct process_info *procinfo);
+int process_stdout_custom(struct process_info *procinfo, char **line);
+
 #endif /* PROCESS_H */