]> git.ozlabs.org Git - petitboot/commitdiff
lib/process: Add add_stderr flag to process module
authorJeremy Kerr <jk@ozlabs.org>
Tue, 14 Jan 2014 07:44:10 +0000 (15:44 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Tue, 14 Jan 2014 07:55:21 +0000 (15:55 +0800)
For some process execution functions, we'd like to capture stderr as
well as stdout. Currently, we unconditionally redirect subprocess stderr
to the petitboot log file.

This change adds an add_stderr flag to struct process, which indicates
to the process library that we want stderr as well as stdout. If this is
specified, the subprocess' stderr is captured to stdout_buf.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
lib/process/process.c
lib/process/process.h
test/lib/Makefile.am
test/lib/test-process-stderr-stdout.c [new file with mode: 0644]
test/lib/test-process-stderr.c [new file with mode: 0644]

index 1a16fb6312b4fb74dc8cc753806009e939bd7691..3c81b29340f8ae69ccd99f4ebe10133eb408e47d 100644 (file)
@@ -125,7 +125,10 @@ static void process_setup_stdout_child(struct process_info *procinfo)
        else
                dup2(log, STDOUT_FILENO);
 
-       dup2(log, STDERR_FILENO);
+       if (procinfo->process.keep_stdout && procinfo->process.add_stderr)
+               dup2(procinfo->stdout_pipe[1], STDERR_FILENO);
+       else
+               dup2(log, STDERR_FILENO);
 }
 
 static void process_finish_stdout(struct process_info *procinfo)
index caeda92131327d862492ade1b65acbaa56348e9c..ae7fe32d06350679660ac21473b8cd45fa6fc986 100644 (file)
@@ -31,6 +31,7 @@ struct process {
        const char              *path;
        const char              **argv;
        bool                    keep_stdout;
+       bool                    add_stderr;
        process_exit_cb         exit_cb;
        void                    *data;
 
index 36067100553a09de2e7deec2e018f9fcd2b52dad..ae6027f80eb2ff933bcd332451f87eb7b2a7d773 100644 (file)
@@ -26,6 +26,8 @@ check_PROGRAMS = list-test \
        test-process-noargs \
        test-process-sync \
        test-process-sync-stdout \
+       test-process-stderr \
+       test-process-stderr-stdout \
        test-process-async \
        test-process-async-stdout \
        test-process-parent-stdout \
diff --git a/test/lib/test-process-stderr-stdout.c b/test/lib/test-process-stderr-stdout.c
new file mode 100644 (file)
index 0000000..3192306
--- /dev/null
@@ -0,0 +1,57 @@
+
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+#include <process/process.h>
+#include <waiter/waiter.h>
+#include <talloc/talloc.h>
+
+static int do_child(void)
+{
+       fprintf(stderr, "forty ");
+       fflush(stderr);
+       fprintf(stdout, "two\n");
+       fflush(stdout);
+       return 42;
+}
+
+int main(int argc, char **argv)
+{
+       struct waitset *waitset;
+       struct process *process;
+       const char *child_argv[3];
+       void *ctx;
+
+       if (argc == 2 && !strcmp(argv[1], "child"))
+               return do_child();
+
+       ctx = talloc_new(NULL);
+
+       waitset = waitset_create(ctx);
+
+       process_init(ctx, waitset, false);
+
+       child_argv[0] = argv[0];
+       child_argv[1] = "child";
+       child_argv[2] = NULL;
+
+       process = process_create(ctx);
+       process->path = child_argv[0];
+       process->argv = child_argv;
+       process->keep_stdout = true;
+       process->add_stderr = true;
+
+       process_run_sync(process);
+
+       assert(WIFEXITED(process->exit_status));
+       assert(WEXITSTATUS(process->exit_status) == 42);
+
+       assert(process->stdout_len == strlen("forty two\n"));
+       assert(!memcmp(process->stdout_buf, "forty two\n",
+                               process->stdout_len));
+
+       talloc_free(ctx);
+
+       return EXIT_SUCCESS;
+}
diff --git a/test/lib/test-process-stderr.c b/test/lib/test-process-stderr.c
new file mode 100644 (file)
index 0000000..a165caa
--- /dev/null
@@ -0,0 +1,54 @@
+
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+#include <process/process.h>
+#include <waiter/waiter.h>
+#include <talloc/talloc.h>
+
+static int do_child(void)
+{
+       fprintf(stderr, "forty two\n");
+       return 42;
+}
+
+int main(int argc, char **argv)
+{
+       struct waitset *waitset;
+       struct process *process;
+       const char *child_argv[3];
+       void *ctx;
+
+       if (argc == 2 && !strcmp(argv[1], "child"))
+               return do_child();
+
+       ctx = talloc_new(NULL);
+
+       waitset = waitset_create(ctx);
+
+       process_init(ctx, waitset, false);
+
+       child_argv[0] = argv[0];
+       child_argv[1] = "child";
+       child_argv[2] = NULL;
+
+       process = process_create(ctx);
+       process->path = child_argv[0];
+       process->argv = child_argv;
+       process->keep_stdout = true;
+       process->add_stderr = true;
+
+       process_run_sync(process);
+
+       assert(WIFEXITED(process->exit_status));
+       assert(WEXITSTATUS(process->exit_status) == 42);
+
+       assert(process->stdout_len == strlen("forty two\n"));
+       assert(!memcmp(process->stdout_buf, "forty two\n",
+                               process->stdout_len));
+
+       talloc_free(ctx);
+
+       return EXIT_SUCCESS;
+}