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