1 /* $OpenBSD: socket1.c,v 1.3 2006/01/05 03:47:19 tedu 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 memset(buf, 0, sizeof(buf)); 93 CHECKe(read(fd, buf, 1024)); 94 95 ASSERT(++counter == atoi(buf)); 96 write(fd, "6", 1); 97 98 CHECKe(close(fd)); 99 success++; 100 CHECKr(pthread_mutex_unlock(&mutex)); 101 102 return(NULL); 103 } 104 105 static void * 106 sock_write(void *arg) 107 { 108 int fd = *(int *)arg; 109 110 CHECKe(write(fd, "5", 1)); 111 return(NULL); 112 } 113 114 static void * 115 sock_accept(void *arg) 116 { 117 pthread_t thread; 118 struct sockaddr a_sin; 119 int a_sin_size, a_fd, fd; 120 short port; 121 char buf[1024]; 122 123 port = 3276; 124 a_sout.sin_family = AF_INET; 125 a_sout.sin_port = htons(port); 126 a_sout.sin_addr.s_addr = INADDR_ANY; 127 128 CHECKe(a_fd = socket(AF_INET, SOCK_STREAM, 0)); 129 130 while (1) { 131 if(0 == bind(a_fd, (struct sockaddr *) &a_sout, sizeof(a_sout))) 132 break; 133 if (errno == EADDRINUSE) { 134 a_sout.sin_port = htons((++port)); 135 continue; 136 } 137 DIE(errno, "bind"); 138 } 139 CHECKe(listen(a_fd, 2)); 140 141 ASSERT(++counter == 1); 142 143 CHECKr(pthread_create(&thread, &attr, sock_connect, 144 (void *)0xdeadbeaf)); 145 146 a_sin_size = sizeof(a_sin); 147 CHECKe(fd = accept(a_fd, &a_sin, &a_sin_size)); 148 CHECKr(pthread_mutex_lock(&mutex)); 149 CHECKe(close(fd)); 150 151 ASSERT(++counter == 4); 152 153 a_sin_size = sizeof(a_sin); 154 CHECKe(fd = accept(a_fd, &a_sin, &a_sin_size)); 155 CHECKr(pthread_mutex_unlock(&mutex)); 156 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)); 161 162 ASSERT(++counter == atoi(buf)); 163 164 CHECKe(close(fd)); 165 166 CHECKr(pthread_mutex_lock(&mutex)); 167 success++; 168 CHECKr(pthread_mutex_unlock(&mutex)); 169 170 CHECKr(pthread_join(thread, NULL)); 171 return(NULL); 172 } 173 174 int 175 main(int argc, char *argv[]) 176 { 177 pthread_t thread; 178 179 setbuf(stdout, NULL); 180 setbuf(stderr, NULL); 181 182 CHECKr(pthread_attr_init(&attr)); 183 #if 0 184 CHECKr(pthread_attr_setschedpolicy(&attr, SCHED_FIFO)); 185 #endif 186 CHECKr(pthread_create(&thread, &attr, sock_accept, 187 (void *)0xdeadbeaf)); 188 189 CHECKr(pthread_join(thread, NULL)); 190 191 ASSERT(success == 2); 192 SUCCEED; 193 } 194