summaryrefslogtreecommitdiffstats
path: root/usr/klibc/tests/vfork.c
blob: 7adc0369350df01eb0770ed729a271403ccf361c (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
/*
 * usr/klibc/tests/vfork.c
 *
 * vfork is messy on most architectures.  Do our best to test it out.
 */

#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char *argv[])
{
	pid_t f, rv;
	int status;

	f = vfork();

	if (f == 0) {
		printf("Child (%d)...\n", (int)getpid());
		_exit(123);
	} else if (f > 0) {
		int err = 0;

		printf("Parent (child = %d)\n", (int)f);

		rv = waitpid(f, &status, 0);
		if (rv != f) {
			printf("waitpid returned %d, errno = %d\n",
			       (int)rv, errno);
			err++;
		}
		if (!WIFEXITED(status) || WEXITSTATUS(status) != 123) {
			printf("Child process existed with wrong status %d\n",
			       status);
			err++;
		}
		return err;
	} else {
		printf("vfork returned %d, errno = %d\n",
		       (int)f, errno);
		return 127;
	}
}