util/hooks: Don't update DT when ttyS* console active
[petitboot] / test / lib / test-process-async-stdout.c
1
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
5
6 #include <process/process.h>
7 #include <waiter/waiter.h>
8 #include <talloc/talloc.h>
9
10 static int do_child(void)
11 {
12         printf("forty two\n");
13         return 42;
14 }
15
16 struct test {
17         bool    exited;
18         int     exit_status;
19         char    *stdout_buf;
20         int     stdout_len;
21 };
22
23 static void exit_cb(struct process *process)
24 {
25         struct test *test = process->data;
26
27         test->exited = true;
28         test->exit_status = process->exit_status;
29
30         test->stdout_len = process->stdout_len;
31         test->stdout_buf = talloc_steal(test, process->stdout_buf);
32 }
33
34 int main(int argc, char **argv)
35 {
36         struct waitset *waitset;
37         struct process *process;
38         const char *child_argv[3];
39         struct test *test;
40
41         if (argc == 2 && !strcmp(argv[1], "child"))
42                 return do_child();
43
44         test = talloc_zero(NULL, struct test);
45
46         waitset = waitset_create(test);
47
48         process_init(test, waitset, false);
49
50         child_argv[0] = argv[0];
51         child_argv[1] = "child";
52         child_argv[2] = NULL;
53
54         process = process_create(test);
55         process->path = child_argv[0];
56         process->argv = child_argv;
57         process->keep_stdout = true;
58         process->exit_cb = exit_cb;
59         process->data = test;
60
61         process_run_async(process);
62
63         for (;;) {
64                 waiter_poll(waitset);
65
66                 if (test->exited)
67                         break;
68         }
69
70         assert(WIFEXITED(test->exit_status));
71         assert(WEXITSTATUS(test->exit_status) == 42);
72         assert(test->stdout_len == strlen("forty two\n"));
73         assert(test->stdout_buf);
74         assert(!memcmp(test->stdout_buf, "forty two\n", strlen("forty two\n")));
75
76         talloc_free(test);
77
78         return EXIT_SUCCESS;
79 }