summaryrefslogtreecommitdiff
path: root/regress/sys/kern/rfork/madness/madness.c
blob: 7b1006bffa5973e2330b49a69dc26acf455ee2af (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
/*	$OpenBSD: madness.c,v 1.3 2003/08/02 01:24:36 david Exp $	*/
/*
 *	Written by Artur Grabowski <art@openbsd.org>, 2002 Public Domain.
 */
#include <sys/param.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <err.h>

volatile int step;

int
main(int argc, char *argv[])
{
	int fds[2], fd;
	pid_t pid1, pid2, pid3;

	if (pipe(fds) < 0)
		err(1, "pipe");

	fd = fds[0];

	if ((pid1 = rfork(RFPROC|RFMEM|RFFDG|RFNOWAIT)) == 0) {
		char foo[1024];
		step = 1;
		read(fd, foo, sizeof(foo));
		_exit(0);
	}
        
	if ((pid2 = rfork(RFPROC|RFMEM|RFFDG|RFNOWAIT)) == 0) {
		while (step < 1)
			sleep(1);
		sleep(1);
		step = 2;
		close(fd);
		_exit(0);
	}
	if ((pid3 = rfork(RFPROC|RFMEM|RFFDG|RFNOWAIT)) == 0) {
		while (step < 2)
			sleep(1);
		sleep(1);
		step = 3;
		dup2(0, fd);
		_exit(0);
	}
	while (step < 3)
		sleep(1);
	sleep(1);
	kill(pid1, SIGKILL);
	kill(pid2, SIGKILL);
	kill(pid3, SIGKILL);
	return 0;
}