1 /* $OpenBSD: socket2a.c,v 1.3 2002/01/02 16:15:32 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 <string.h> 52 #include <stdlib.h> 53 #include "test.h" 54 55 struct sockaddr_in a_sout; 56 57 #define MESSAGE5 "This should be message #5" 58 #define MESSAGE6 "This should be message #6" 59 60 void * 61 sock_connect(arg) 62 void *arg; 63 { 64 char buf[1024]; 65 int fd; 66 short port; 67 68 port = atoi(arg); 69 a_sout.sin_family = AF_INET; 70 a_sout.sin_port = htons(port); 71 a_sout.sin_addr.s_addr = htonl(INADDR_LOOPBACK); /* loopback */ 72 73 CHECKe(fd = socket(AF_INET, SOCK_STREAM, 0)); 74 75 printf("%d: This should be message #2\n", getpid()); 76 77 CHECKe(connect(fd, (struct sockaddr *) &a_sout, sizeof(a_sout))); 78 CHECKe(close(fd)); 79 80 CHECKe(fd = socket(AF_INET, SOCK_STREAM, 0)); 81 82 printf("%d: This should be message #3\n", getpid()); 83 84 CHECKe(connect(fd, (struct sockaddr *) &a_sout, sizeof(a_sout))); 85 86 /* Ensure sock_read runs again */ 87 88 CHECKe(read(fd, buf, 1024)); 89 CHECKe(write(fd, MESSAGE6, sizeof(MESSAGE6))); 90 91 printf("%d: %s\n", getpid(), buf); 92 93 CHECKe(close(fd)); 94 return (NULL); 95 } 96 97 int 98 main(argc, argv) 99 int argc; 100 char **argv; 101 { 102 pthread_t thread; 103 104 if (argc == 3 && (!strcmp(argv[1], "fork okay"))) { 105 sleep(1); 106 setbuf(stdout, NULL); 107 setbuf(stderr, NULL); 108 109 CHECKr(pthread_create(&thread, NULL, sock_connect, 110 (void *)argv[2])); 111 CHECKr(pthread_join(thread, NULL)); 112 SUCCEED; 113 } else { 114 fprintf(stderr, "test_sock_2a needs to be exec'ed from " 115 "test_sock_2.\n"); 116 fprintf(stderr, "It is not a stand alone test.\n"); 117 PANIC("usage"); 118 } 119 } 120