18b8e27d1d759f3bcfc8d9bf966de207bc3d4897
[libc-test] / src / process / spawn.c
1 #define _XOPEN_SOURCE 700
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <stdio.h>
5 #include <errno.h>
6 #include <string.h>
7 #include <spawn.h>
8 #include <sys/wait.h>
9 #include "test.h"
10
11 #define TEST(r, f, x, m) ( \
12         ((r) = (f)) == (x) || \
13         (error("%s failed (" m ")\n", #f, r, x), 0) )
14
15 #define TEST_E(f) ( (errno = 0), (f) || \
16         (error("%s failed (errno = %d \"%s\")\n", #f, errno, strerror(errno)), 0) )
17
18 void test_spawn(void) {
19         int r;
20         char foo[10];
21         int p[2];
22         pid_t pid;
23         int status;
24         posix_spawnattr_t attr;
25         posix_spawn_file_actions_t fa;
26
27         TEST_E(!pipe(p));
28         TEST(r, posix_spawn_file_actions_init(&fa), 0, "%d != %d");
29         TEST(r, posix_spawn_file_actions_addclose(&fa, p[0]), 0, "%d != %d");
30         TEST(r, posix_spawn_file_actions_adddup2(&fa, p[1], 1), 0, "%d != %d");
31         TEST(r, posix_spawn_file_actions_addclose(&fa, p[1]), 0, "%d != %d");
32         TEST(r, posix_spawnp(&pid, "echo", &fa, 0, (char *[]){"echo","hello",0}, 0), 0, "%d != %d");
33         close(p[1]);
34         TEST(r, waitpid(pid, &status, 0), pid, "%d != %d");
35         TEST(r, read(p[0], foo, sizeof foo), 6, "%d != %d");
36         close(p[0]);
37         TEST(r, posix_spawn_file_actions_destroy(&fa), 0, "%d != %d");
38 }