Blame src/process/spawn.c

nsz 73214b
#define _XOPEN_SOURCE 700
nsz 73214b
#include <stdlib.h>
nsz 73214b
#include <unistd.h>
nsz 73214b
#include <stdio.h>
nsz 73214b
#include <errno.h>
nsz 73214b
#include <string.h>
nsz 73214b
#include <spawn.h>
nsz 73214b
#include <sys/wait.h>
nsz 73214b
#include "test.h"
nsz 73214b
nsz 73214b
#define TEST(r, f, x, m) ( \
nsz 73214b
	((r) = (f)) == (x) || \
nsz 73214b
	(error("%s failed (" m ")\n", #f, r, x), 0) )
nsz 73214b
nsz 73214b
#define TEST_E(f) ( (errno = 0), (f) || \
nsz 73214b
	(error("%s failed (errno = %d \"%s\")\n", #f, errno, strerror(errno)), 0) )
nsz 73214b
nsz 73214b
void test_spawn(void) {
nsz 73214b
	int r;
nsz 73214b
	char foo[10];
nsz 73214b
	int p[2];
nsz 73214b
	pid_t pid;
nsz 73214b
	int status;
nsz 73214b
	posix_spawn_file_actions_t fa;
nsz 73214b
nsz 73214b
	TEST_E(!pipe(p));
nsz 73214b
	TEST(r, posix_spawn_file_actions_init(&fa), 0, "%d != %d");
nsz 73214b
	TEST(r, posix_spawn_file_actions_addclose(&fa, p[0]), 0, "%d != %d");
nsz 73214b
	TEST(r, posix_spawn_file_actions_adddup2(&fa, p[1], 1), 0, "%d != %d");
nsz 73214b
	TEST(r, posix_spawn_file_actions_addclose(&fa, p[1]), 0, "%d != %d");
nsz 73214b
	TEST(r, posix_spawnp(&pid, "echo", &fa, 0, (char *[]){"echo","hello",0}, 0), 0, "%d != %d");
nsz 73214b
	close(p[1]);
nsz 73214b
	TEST(r, waitpid(pid, &status, 0), pid, "%d != %d");
nsz 73214b
	TEST(r, read(p[0], foo, sizeof foo), 6, "%d != %d");
nsz 73214b
	close(p[0]);
nsz 73214b
	TEST(r, posix_spawn_file_actions_destroy(&fa), 0, "%d != %d");
nsz 73214b
}