xref: /openbsd-src/regress/lib/libpthread/socket/1/socket1.c (revision 0eea0d082377cb9c3ec583313dc4d52b7b6a4d6d)
1 /*	$OpenBSD: socket1.c,v 1.2 2003/07/31 21:48:06 deraadt Exp $	*/
2 /*
3  * Copyright (c) 1993, 1994, 1995, 1996 by Chris Provenzano and contributors,
4  * proven@mit.edu All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Chris Provenzano,
17  *	the University of California, Berkeley, and contributors.
18  * 4. Neither the name of Chris Provenzano, the University, nor the names of
19  *   contributors may be used to endorse or promote products derived
20  *   from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY CHRIS PROVENZANO AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL CHRIS PROVENZANO, THE REGENTS OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /* ==== test_sock_1.c =========================================================
36  * Copyright (c) 1993 by Chris Provenzano, proven@athena.mit.edu
37  *
38  * Description : Test pthread_create() and pthread_exit() calls.
39  *
40  *  1.00 93/08/03 proven
41  *      -Started coding this file.
42  */
43 
44 #include <pthread.h>
45 #include <errno.h>
46 #include <stdio.h>
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <unistd.h>
51 #include "test.h"
52 #include <sched.h>
53 #include <string.h>
54 #include <stdlib.h>
55 
56 struct sockaddr_in a_sout;
57 int success = 0;
58 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
59 pthread_attr_t attr;
60 
61 static int counter = 0;
62 
63 static void *
64 sock_connect(void *arg)
65 {
66 	char buf[1024];
67 	int fd;
68 
69 	/* Ensure sock_read runs first */
70 	CHECKr(pthread_mutex_lock(&mutex));
71 
72 	a_sout.sin_addr.s_addr = htonl(0x7f000001); /* loopback */
73 	CHECKe(fd = socket(AF_INET, SOCK_STREAM, 0));
74 
75 	ASSERT(++counter == 2);
76 
77 	/* connect to the socket */
78 	CHECKe(connect(fd, (struct sockaddr *) &a_sout, sizeof(a_sout)));
79 	CHECKe(close(fd));
80 
81 	CHECKr(pthread_mutex_unlock(&mutex));
82 
83 	CHECKe(fd = socket(AF_INET, SOCK_STREAM, 0));
84 	ASSERT(++counter == 3);
85 	CHECKe(connect(fd, (struct sockaddr *) &a_sout, sizeof(a_sout)));
86 
87 	/* Ensure sock_read runs again */
88 	pthread_yield();
89 	sleep(1);
90 
91 	CHECKr(pthread_mutex_lock(&mutex));
92 	CHECKe(read(fd, buf, 1024));
93 
94 	write(fd, "6", 1);
95 
96 	ASSERT(++counter == atoi(buf));
97 	CHECKe(close(fd));
98 	success++;
99 	CHECKr(pthread_mutex_unlock(&mutex));
100 
101 	return(NULL);
102 }
103 
104 static void *
105 sock_write(void *arg)
106 {
107 	int fd = *(int *)arg;
108 
109 	CHECKe(write(fd, "5", 1));
110 	return(NULL);
111 }
112 
113 static void *
114 sock_accept(void *arg)
115 {
116 	pthread_t thread;
117 	struct sockaddr a_sin;
118 	int a_sin_size, a_fd, fd;
119 	short port;
120 	char buf[1024];
121 
122 	port = 3276;
123 	a_sout.sin_family = AF_INET;
124 	a_sout.sin_port = htons(port);
125 	a_sout.sin_addr.s_addr = INADDR_ANY;
126 
127 	CHECKe(a_fd = socket(AF_INET, SOCK_STREAM, 0));
128 
129 	while (1) {
130 		if(0 == bind(a_fd, (struct sockaddr *) &a_sout, sizeof(a_sout)))
131 			break;
132 		if (errno == EADDRINUSE) {
133 			a_sout.sin_port = htons((++port));
134 			continue;
135 		}
136 		DIE(errno, "bind");
137 	}
138 	CHECKe(listen(a_fd, 2));
139 
140 	ASSERT(++counter == 1);
141 
142 	CHECKr(pthread_create(&thread, &attr, sock_connect,
143 	    (void *)0xdeadbeaf));
144 
145 	a_sin_size = sizeof(a_sin);
146 	CHECKe(fd = accept(a_fd, &a_sin, &a_sin_size));
147 	CHECKr(pthread_mutex_lock(&mutex));
148 	CHECKe(close(fd));
149 
150 	ASSERT(++counter == 4);
151 
152 	a_sin_size = sizeof(a_sin);
153 	CHECKe(fd = accept(a_fd, &a_sin, &a_sin_size));
154 	CHECKr(pthread_mutex_unlock(&mutex));
155 
156 	/* Setup a write thread */
157 	CHECKr(pthread_create(&thread, &attr, sock_write, &fd));
158 	CHECKe(read(fd, buf, 1024));
159 
160 	ASSERT(++counter == atoi(buf));
161 
162 	CHECKe(close(fd));
163 
164 	CHECKr(pthread_mutex_lock(&mutex));
165 	success++;
166 	CHECKr(pthread_mutex_unlock(&mutex));
167 
168 	CHECKr(pthread_join(thread, NULL));
169 	return(NULL);
170 }
171 
172 int
173 main(int argc, char *argv[])
174 {
175 	pthread_t thread;
176 
177 	setbuf(stdout, NULL);
178 	setbuf(stderr, NULL);
179 
180 	CHECKr(pthread_attr_init(&attr));
181 #if 0
182 	CHECKr(pthread_attr_setschedpolicy(&attr, SCHED_FIFO));
183 #endif
184 	CHECKr(pthread_create(&thread, &attr, sock_accept,
185 	    (void *)0xdeadbeaf));
186 
187 	CHECKr(pthread_join(thread, NULL));
188 
189 	ASSERT(success == 2);
190 	SUCCEED;
191 }
192