1 /* $OpenBSD: close.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 /* 36 * Test the semantics of close() while a select() is happening. 37 * Not a great test. You need the 'discard' service running in inetd for 38 * this to work. 39 */ 40 41 #include <pthread.h> 42 #include <stdio.h> 43 #include <unistd.h> 44 #include <fcntl.h> 45 #include <sys/types.h> 46 #include <sys/socket.h> 47 #include <netinet/in.h> 48 #include "test.h" 49 50 int fd; 51 52 void* new_thread(void* arg) 53 { 54 fd_set r; 55 int ret; 56 char garbage[] = "blah blah blah"; 57 58 FD_ZERO(&r); 59 FD_SET(fd, &r); 60 61 printf("child: writing some garbage to fd %d\n", fd); 62 CHECKe(write(fd, garbage, sizeof garbage)); 63 printf("child: calling select() with fd %d\n", fd); 64 CHECKe(ret = select(fd + 1, &r, NULL, NULL, NULL)); 65 printf("child: select() returned %d\n", ret); 66 return NULL; 67 } 68 69 int 70 main() 71 { 72 pthread_t thread; 73 pthread_attr_t attr; 74 struct sockaddr_in addr; 75 int ret; 76 77 /* Open up a TCP connection to the local discard port */ 78 addr.sin_family = AF_INET; 79 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 80 addr.sin_port = htons(9); /* port 9/tcp is discard service */ 81 82 CHECKe(fd = socket(AF_INET, SOCK_STREAM, 0)); 83 printf("main: connecting to discard port with fd %d\n", fd); 84 ret = connect(fd, (struct sockaddr *)&addr, sizeof addr); 85 if (ret == -1) 86 fprintf(stderr, "connect() failed: ensure that the discard port is enabled for inetd(8)\n"); 87 CHECKe(ret); 88 printf("main: connected on fd %d\n", fd); 89 90 CHECKr(pthread_attr_init(&attr)); 91 CHECKe(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)); 92 printf("starting child thread\n"); 93 CHECKr(pthread_create(&thread, &attr, new_thread, NULL)); 94 sleep(1); 95 printf("main: closing fd %d\n", fd); 96 CHECKe(close(fd)); 97 printf("main: closed\n"); 98 sleep(1); 99 SUCCEED; 100 } 101