1 /* $OpenBSD: close.c,v 1.7 2012/02/26 21:43:25 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.
38 */
39
40 #include <pthread.h>
41 #include <stdio.h>
42 #include <unistd.h>
43 #include <stdlib.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 #define BUFSIZE 4096
51
52 int fd;
53
54 /*
55 * meat of inetd discard service -- ignore data
56 */
57 static void
discard(int s)58 discard(int s)
59 {
60 char buffer[BUFSIZE];
61
62 while ((errno = 0, read(s, buffer, sizeof(buffer)) > 0) ||
63 errno == EINTR)
64 ;
65 }
66
67 /*
68 * Listen on localhost:TEST_PORT for a connection
69 */
70 #define TEST_PORT 9876
71
72 static void
server(void)73 server(void)
74 {
75 int sock;
76 int client;
77 int client_addr_len;
78 struct sockaddr_in serv_addr;
79 struct sockaddr client_addr;
80
81 CHECKe(sock = socket(AF_INET, SOCK_STREAM, 0));
82 bzero((char *) &serv_addr, sizeof serv_addr);
83 serv_addr.sin_family = AF_INET;
84 serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
85 serv_addr.sin_port = htons(TEST_PORT);
86 CHECKe(bind(sock, (struct sockaddr *) &serv_addr, sizeof serv_addr));
87 CHECKe(listen(sock,3));
88
89 client_addr_len = sizeof client_addr;
90 CHECKe(client = accept(sock, &client_addr, &client_addr_len ));
91 CHECKe(close(sock));
92 discard(client);
93 CHECKe(close(client));
94 exit(0);
95 }
96
97 static void *
new_thread(void * arg)98 new_thread(void* arg)
99 {
100 fd_set r;
101 int ret;
102 char garbage[] = "blah blah blah";
103
104 FD_ZERO(&r);
105 FD_SET(fd, &r);
106
107 printf("child: writing some garbage to fd %d\n", fd);
108 CHECKe(write(fd, garbage, sizeof garbage));
109 printf("child: calling select() with fd %d\n", fd);
110 ASSERT((ret = select(fd + 1, &r, NULL, NULL, NULL)) == -1);
111 ASSERT(errno == EBADF);
112 printf("child: select() returned %d, errno %d = %s [correct]\n", ret,
113 errno, strerror(errno));
114 return NULL;
115 }
116
117 int
main(int argc,char * argv[])118 main(int argc, char *argv[])
119 {
120 pthread_t thread;
121 pthread_attr_t attr;
122 struct sockaddr_in addr;
123 int ret;
124
125 /* fork and have the child open a listener */
126 signal(SIGCHLD, SIG_IGN);
127 switch (fork()) {
128 case 0:
129 server();
130 exit(0);
131 case -1:
132 exit(errno);
133 default:
134 sleep(2);
135 }
136
137 /* Open up a TCP connection to the local discard port */
138 addr.sin_family = AF_INET;
139 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
140 addr.sin_port = htons(TEST_PORT);
141
142 CHECKe(fd = socket(AF_INET, SOCK_STREAM, 0));
143 printf("main: connecting to test port with fd %d\n", fd);
144 ret = connect(fd, (struct sockaddr *)&addr, sizeof addr);
145 if (ret == -1)
146 fprintf(stderr, "connect() failed\n");
147 CHECKe(ret);
148 printf("main: connected on fd %d\n", fd);
149
150 CHECKr(pthread_attr_init(&attr));
151 CHECKr(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED));
152 printf("starting child thread\n");
153 CHECKr(pthread_create(&thread, &attr, new_thread, NULL));
154 sleep(1);
155 printf("main: closing fd %d\n", fd);
156 CHECKe(close(fd));
157 printf("main: closed\n");
158 sleep(1);
159 SUCCEED;
160 }
161