1 /* $OpenBSD: socket1.c,v 1.1.1.1 2001/08/15 14:37:10 fgsch 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 void * 64 sock_connect(arg) 65 void *arg; 66 { 67 char buf[1024]; 68 int fd; 69 70 /* Ensure sock_read runs first */ 71 CHECKr(pthread_mutex_lock(&mutex)); 72 73 a_sout.sin_addr.s_addr = htonl(0x7f000001); /* loopback */ 74 CHECKe(fd = socket(AF_INET, SOCK_STREAM, 0)); 75 76 ASSERT(++counter == 2); 77 78 /* connect to the socket */ 79 CHECKe(connect(fd, (struct sockaddr *) &a_sout, sizeof(a_sout))); 80 CHECKe(close(fd)); 81 82 CHECKr(pthread_mutex_unlock(&mutex)); 83 84 CHECKe(fd = socket(AF_INET, SOCK_STREAM, 0)); 85 ASSERT(++counter == 3); 86 CHECKe(connect(fd, (struct sockaddr *) &a_sout, sizeof(a_sout))); 87 88 /* Ensure sock_read runs again */ 89 pthread_yield(); 90 sleep(1); 91 92 CHECKr(pthread_mutex_lock(&mutex)); 93 CHECKe(read(fd, buf, 1024)); 94 95 write(fd, "6", 1); 96 97 ASSERT(++counter == atoi(buf)); 98 CHECKe(close(fd)); 99 success++; 100 CHECKr(pthread_mutex_unlock(&mutex)); 101 102 return(NULL); 103 } 104 105 void * 106 sock_write(arg) 107 void *arg; 108 { 109 int fd = *(int *)arg; 110 111 CHECKe(write(fd, "5", 1)); 112 return(NULL); 113 } 114 115 void * 116 sock_accept(arg) 117 void *arg; 118 { 119 pthread_t thread; 120 struct sockaddr a_sin; 121 int a_sin_size, a_fd, fd; 122 short port; 123 char buf[1024]; 124 125 port = 3276; 126 a_sout.sin_family = AF_INET; 127 a_sout.sin_port = htons(port); 128 a_sout.sin_addr.s_addr = INADDR_ANY; 129 130 CHECKe(a_fd = socket(AF_INET, SOCK_STREAM, 0)); 131 132 while (1) { 133 if(0 == bind(a_fd, (struct sockaddr *) &a_sout, sizeof(a_sout))) 134 break; 135 if (errno == EADDRINUSE) { 136 a_sout.sin_port = htons((++port)); 137 continue; 138 } 139 DIE(errno, "bind"); 140 } 141 CHECKe(listen(a_fd, 2)); 142 143 ASSERT(++counter == 1); 144 145 CHECKr(pthread_create(&thread, &attr, sock_connect, 146 (void *)0xdeadbeaf)); 147 148 a_sin_size = sizeof(a_sin); 149 CHECKe(fd = accept(a_fd, &a_sin, &a_sin_size)); 150 CHECKr(pthread_mutex_lock(&mutex)); 151 CHECKe(close(fd)); 152 153 ASSERT(++counter == 4); 154 155 a_sin_size = sizeof(a_sin); 156 CHECKe(fd = accept(a_fd, &a_sin, &a_sin_size)); 157 CHECKr(pthread_mutex_unlock(&mutex)); 158 159 /* Setup a write thread */ 160 CHECKr(pthread_create(&thread, &attr, sock_write, &fd)); 161 CHECKe(read(fd, buf, 1024)); 162 163 ASSERT(++counter == atoi(buf)); 164 165 CHECKe(close(fd)); 166 167 CHECKr(pthread_mutex_lock(&mutex)); 168 success++; 169 CHECKr(pthread_mutex_unlock(&mutex)); 170 171 CHECKr(pthread_join(thread, NULL)); 172 return(NULL); 173 } 174 175 int 176 main() 177 { 178 pthread_t thread; 179 180 setbuf(stdout, NULL); 181 setbuf(stderr, NULL); 182 183 CHECKr(pthread_attr_init(&attr)); 184 #if 0 185 CHECKr(pthread_attr_setschedpolicy(&attr, SCHED_FIFO)); 186 #endif 187 CHECKr(pthread_create(&thread, &attr, sock_accept, 188 (void *)0xdeadbeaf)); 189 190 CHECKr(pthread_join(thread, NULL)); 191 192 ASSERT(success == 2); 193 SUCCEED; 194 } 195