1/*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License as
4 * published by the Free Software Foundation; either version 2 of
5 * the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
15 * MA 02111-1307 USA
16 */
17#include <stdio.h>
18#include <sys/wait.h>
19#include <sys/types.h>
20#include <sys/socket.h>
21#define HAVE_SOCKETPAIR
22main()
23{
24
25	int fds[2], i;
26	char line[4];
27	int pid;
28	char *m1 = "to";
29	char *m2 = "fro";
30#ifdef HAVE_SOCKETPAIR
31    i = socketpair( AF_UNIX, SOCK_STREAM, 0, fds);
32#else
33    i = pipe( fds );
34#endif
35
36	if( i < 0 ) exit(1);
37	printf("pipe\n");
38	if( (pid = fork()) < 0) exit(1);
39	if( pid == 0 ){
40		/* Child */
41		printf("child\n");
42		if( write( fds[0], m1, strlen(m1) ) < 0 ) exit(1);
43		printf("child write\n");
44		i = read( fds[0], line, sizeof(line) );
45		if( i < 0 ) exit(1);
46		line[i] = 0;
47		if( strcmp( line, m2 ) ) exit(1);
48		printf("child read OK\n");
49		fflush(stdout);
50		exit(0);
51	} else {
52		printf("mother\n");
53		i = read( fds[1], line, sizeof(line) );
54		if( i < 0 ) exit(1);
55		line[i] = 0;
56		if( strcmp( line, m1 ) ) exit(1);
57		printf("mother read\n");
58		if( write( fds[1], m2, strlen(m2) ) < 0 ) exit(1);
59		printf("mother write OK\n");
60		wait(&i);
61		printf("child exit %d\n", i);
62	}
63	exit(0);
64}
65