summaryrefslogtreecommitdiffstats
path: root/test/lib/test-process-stdout-eintr.c
blob: 68df5edcdcdea72336339bb7606fa93050bac658 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <signal.h>

#include <process/process.h>
#include <waiter/waiter.h>
#include <talloc/talloc.h>

static int do_child(int ppid)
{
	sleep(1);
	kill(ppid, SIGCHLD);
	printf("forty two\n");
	return 42;
}

int main(int argc, char **argv)
{
	struct waitset *waitset;
	struct process *process;
	const char *child_argv[4];
	void *ctx;

	if (argc == 3 && !strcmp(argv[1], "child"))
		return do_child(atoi(argv[2]));

	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] = talloc_asprintf(ctx, "%d", getpid());
	child_argv[3] = NULL;

	process = process_create(ctx);
	process->path = child_argv[0];
	process->argv = child_argv;
	process->keep_stdout = 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