1 /* $OpenBSD: socket1.c,v 1.3 2006/01/05 03:47:19 tedu Exp $ */
3 * Copyright (c) 1993, 1994, 1995, 1996 by Chris Provenzano and contributors,
4 * proven@mit.edu All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
35 /* ==== test_sock_1.c =========================================================
36 * Copyright (c) 1993 by Chris Provenzano, proven@athena.mit.edu
38 * Description : Test pthread_create() and pthread_exit() calls.
40 * 1.00 93/08/03 proven
41 * -Started coding this file.
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <netinet/in.h>
56 struct sockaddr_in a_sout;
58 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
61 static int counter = 0;
64 sock_connect(void *arg)
69 /* Ensure sock_read runs first */
70 CHECKr(pthread_mutex_lock(&mutex));
72 a_sout.sin_addr.s_addr = htonl(0x7f000001); /* loopback */
73 CHECKe(fd = socket(AF_INET, SOCK_STREAM, 0));
75 ASSERT(++counter == 2);
77 /* connect to the socket */
78 CHECKe(connect(fd, (struct sockaddr *) &a_sout, sizeof(a_sout)));
81 CHECKr(pthread_mutex_unlock(&mutex));
83 CHECKe(fd = socket(AF_INET, SOCK_STREAM, 0));
84 ASSERT(++counter == 3);
85 CHECKe(connect(fd, (struct sockaddr *) &a_sout, sizeof(a_sout)));
87 /* Ensure sock_read runs again */
91 CHECKr(pthread_mutex_lock(&mutex));
92 memset(buf, 0, sizeof(buf));
93 CHECKe(read(fd, buf, 1024));
95 ASSERT(++counter == atoi(buf));
100 CHECKr(pthread_mutex_unlock(&mutex));
106 sock_write(void *arg)
108 int fd = *(int *)arg;
110 CHECKe(write(fd, "5", 1));
115 sock_accept(void *arg)
118 struct sockaddr a_sin;
119 int a_sin_size, a_fd, fd;
124 a_sout.sin_family = AF_INET;
125 a_sout.sin_port = htons(port);
126 a_sout.sin_addr.s_addr = INADDR_ANY;
128 CHECKe(a_fd = socket(AF_INET, SOCK_STREAM, 0));
131 if(0 == bind(a_fd, (struct sockaddr *) &a_sout, sizeof(a_sout)))
133 if (errno == EADDRINUSE) {
134 a_sout.sin_port = htons((++port));
139 CHECKe(listen(a_fd, 2));
141 ASSERT(++counter == 1);
143 CHECKr(pthread_create(&thread, &attr, sock_connect,
144 (void *)0xdeadbeaf));
146 a_sin_size = sizeof(a_sin);
147 CHECKe(fd = accept(a_fd, &a_sin, &a_sin_size));
148 CHECKr(pthread_mutex_lock(&mutex));
151 ASSERT(++counter == 4);
153 a_sin_size = sizeof(a_sin);
154 CHECKe(fd = accept(a_fd, &a_sin, &a_sin_size));
155 CHECKr(pthread_mutex_unlock(&mutex));
157 /* Setup a write thread */
158 CHECKr(pthread_create(&thread, &attr, sock_write, &fd));
159 memset(buf, 0, sizeof(buf));
160 CHECKe(read(fd, buf, 1024));
162 ASSERT(++counter == atoi(buf));
166 CHECKr(pthread_mutex_lock(&mutex));
168 CHECKr(pthread_mutex_unlock(&mutex));
170 CHECKr(pthread_join(thread, NULL));
175 main(int argc, char *argv[])
179 setbuf(stdout, NULL);
180 setbuf(stderr, NULL);
182 CHECKr(pthread_attr_init(&attr));
184 CHECKr(pthread_attr_setschedpolicy(&attr, SCHED_FIFO));
186 CHECKr(pthread_create(&thread, &attr, sock_accept,
187 (void *)0xdeadbeaf));
189 CHECKr(pthread_join(thread, NULL));
191 ASSERT(success == 2);