summaryrefslogtreecommitdiff
path: root/ccan/antithread/test/run-spawn.c
blob: f1dcd2f82c1cc2322028ac7569d57d42a916f057 (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
#include <ccan/antithread/antithread.c>
#include <assert.h>
#include <ccan/tap/tap.h>

int main(int argc, char *argv[])
{
	struct at_pool *atp;
	struct athread *at;
	int err, *pid;
	void *arg;
	char *bad_args[] = { (char *)"/", NULL };

	atp = at_get_pool(&argc, argv, &arg);
	if (atp) {
		*(int *)arg = getpid();
		at_tell_parent(atp, arg);
		exit(0);
	}
	assert(!argv[1]);

	err = errno;
	plan_tests(7);
	ok1(err == EINVAL);

	atp = at_pool(1*1024*1024);
	assert(atp);
	pid = talloc(at_pool_ctx(atp), int);
	assert(pid);
	ok1((char *)pid >= (char *)atp->p->pool
	    && (char *)pid < (char *)atp->p->pool + atp->p->poolsize);

	/* This is a failed spawn. */
	at = at_spawn(atp, pid, bad_args);
	ok1(at == NULL);

	/* This should work */
	at = at_spawn(atp, pid, argv);
	ok1(at);

	/* Should read back the pid pointer. */
	ok1(at_read(at) == pid);
	talloc_free(at);

	ok1(*pid != 0);
	ok1(*pid != getpid());

	return exit_status();
}