summaryrefslogtreecommitdiffstats
path: root/test/lib/test-process-stderr-stdout.c
diff options
context:
space:
mode:
authorJeremy Kerr <jk@ozlabs.org>2014-01-14 15:44:10 +0800
committerJeremy Kerr <jk@ozlabs.org>2014-01-14 15:55:21 +0800
commit33527e065d9506e05f61c020a473544123c0601b (patch)
treee76ef3df4907151438406013a011e69808a8c2c0 /test/lib/test-process-stderr-stdout.c
parent4153ab54b5933fc315ad896e264f74571a494c90 (diff)
downloadtalos-petitboot-33527e065d9506e05f61c020a473544123c0601b.tar.gz
talos-petitboot-33527e065d9506e05f61c020a473544123c0601b.zip
lib/process: Add add_stderr flag to process module
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>
Diffstat (limited to 'test/lib/test-process-stderr-stdout.c')
-rw-r--r--test/lib/test-process-stderr-stdout.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/test/lib/test-process-stderr-stdout.c b/test/lib/test-process-stderr-stdout.c
new file mode 100644
index 0000000..3192306
--- /dev/null
+++ b/test/lib/test-process-stderr-stdout.c
@@ -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;
+}
OpenPOWER on IntegriCloud