]> git.ozlabs.org Git - petitboot/blob - test/lib/test-process-stdout-eintr.c
test/lib: Avoid array overflow of child_argv[]
[petitboot] / test / lib / test-process-stdout-eintr.c
1
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
5 #include <signal.h>
6
7 #include <process/process.h>
8 #include <waiter/waiter.h>
9 #include <talloc/talloc.h>
10
11 static int do_child(int ppid)
12 {
13         sleep(1);
14         kill(ppid, SIGCHLD);
15         printf("forty two\n");
16         return 42;
17 }
18
19 int main(int argc, char **argv)
20 {
21         struct waitset *waitset;
22         struct process *process;
23         const char *child_argv[4];
24         void *ctx;
25
26         if (argc == 3 && !strcmp(argv[1], "child"))
27                 return do_child(atoi(argv[2]));
28
29         ctx = talloc_new(NULL);
30
31         waitset = waitset_create(ctx);
32
33         process_init(ctx, waitset, false);
34
35         child_argv[0] = argv[0];
36         child_argv[1] = "child";
37         child_argv[2] = talloc_asprintf(ctx, "%d", getpid());
38         child_argv[3] = NULL;
39
40         process = process_create(ctx);
41         process->path = child_argv[0];
42         process->argv = child_argv;
43         process->keep_stdout = true;
44
45         process_run_sync(process);
46
47         assert(WIFEXITED(process->exit_status));
48         assert(WEXITSTATUS(process->exit_status) == 42);
49
50         assert(process->stdout_len == strlen("forty two\n"));
51         assert(!memcmp(process->stdout_buf, "forty two\n",
52                                 process->stdout_len));
53
54         talloc_free(ctx);
55
56         return EXIT_SUCCESS;
57 }