xref: /openbsd-src/regress/lib/libpthread/socket/2/socket2.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: socket2.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 <pthread_np.h>
46 #include <errno.h>
47 #include <stdio.h>
48 #include <sys/types.h>
49 #include <sys/wait.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <unistd.h>
53 #include <string.h>
54 #include "test.h"
55 
56 struct sockaddr_in a_sout;
57 
58 #define MESSAGE5 "This should be message #5"
59 #define MESSAGE6 "This should be message #6"
60 
61 void *
62 sock_write(arg)
63 	void *arg;
64 {
65 	int fd = *(int *)arg;
66 
67 	SET_NAME("writer");
68 	CHECKe(write(fd, MESSAGE5, sizeof(MESSAGE5)));
69 	return(NULL);
70 }
71 
72 static pthread_mutex_t waiter_mutex = PTHREAD_MUTEX_INITIALIZER;
73 
74 void*
75 waiter(sig)
76 {
77 	int status;
78 	pid_t pid;
79 
80 	SET_NAME("waiter");
81 	CHECKr(pthread_mutex_lock(&waiter_mutex));
82 	printf("waiting for child\n");
83 	CHECKe(pid = wait(&status));
84 	ASSERT(WIFEXITED(status));
85 	ASSERT(WEXITSTATUS(status) == 0);
86 	printf("child exited\n");
87 	CHECKr(pthread_mutex_unlock(&waiter_mutex));
88 	return (NULL);
89 }
90 
91 void *
92 sock_accept(arg)
93 	void *arg;
94 {
95 	pthread_t thread, wthread;
96 	struct sockaddr a_sin;
97 	int a_sin_size, a_fd, fd;
98 	u_int16_t port;
99 	char buf[1024];
100 	pid_t pid;
101 
102 	port = 3276;
103 	a_sout.sin_family = AF_INET;
104 	a_sout.sin_port = htons(port);
105 	a_sout.sin_addr.s_addr = INADDR_ANY;
106 
107 	CHECKe(a_fd = socket(AF_INET, SOCK_STREAM, 0));
108 
109 	while(1) {
110 		if (bind(a_fd, (struct sockaddr *)&a_sout, sizeof(a_sout))==0)
111 			break;
112 		if (errno == EADDRINUSE) {
113 			a_sout.sin_port = htons((++port));
114 			continue;
115 		}
116 		DIE(errno, "bind");
117 	}
118 
119 	printf("listening on port %d\n", port);
120 
121 	CHECKe(listen(a_fd, 2));
122 
123 	printf("%d: This should be message #1\n", getpid());
124 
125 	CHECKr(pthread_mutex_init(&waiter_mutex, NULL));
126 	CHECKr(pthread_mutex_lock(&waiter_mutex));
127 	CHECKr(pthread_create(&wthread, NULL, waiter, NULL));
128 
129 	CHECKe(pid = fork());
130 	switch(pid) {
131 	case 0:
132 		execl("socket2a", "socket2a", "fork okay", (char *)NULL);
133 		DIE(errno, "execl");
134 	default:
135 		break;
136 	}
137 	CHECKr(pthread_mutex_unlock(&waiter_mutex));
138 	pthread_yield();
139 
140 	a_sin_size = sizeof(a_sin);
141 	CHECKe(fd = accept(a_fd, &a_sin, &a_sin_size));
142 	CHECKe(close(fd));
143 
144 	sleep(1);
145 
146 	printf("%d: This should be message #4\n", getpid());
147 
148 	a_sin_size = sizeof(a_sin);
149 	memset(&a_sin, 0, sizeof(a_sin));
150 	CHECKe(fd = accept(a_fd, &a_sin, &a_sin_size));
151 
152 	/* Setup a write thread */
153 
154 	CHECKr(pthread_create(&thread, NULL, sock_write, &fd));
155 	CHECKe(read(fd, buf, 1024));
156 
157 	printf("%d: %s\n", getpid(), buf); /* message 6 */
158 
159 	CHECKe(close(fd));
160 
161 	if (pthread_mutex_trylock(&waiter_mutex) == EBUSY) {
162 		sleep(2);
163 		if (pthread_mutex_trylock(&waiter_mutex) == EBUSY) {
164 			/* forcibly kill child */
165 			CHECKe(kill(pid, SIGKILL));
166 			PANIC("child %d took too long to exit", pid);
167 		}
168 	}
169 	CHECKr(pthread_join(wthread, NULL));
170 
171 	return(NULL);
172 }
173 
174 int
175 main()
176 {
177 	pthread_t thread;
178 
179 	setbuf(stdout, NULL);
180 	setbuf(stderr, NULL);
181 
182 	CHECKr(pthread_create(&thread, NULL, sock_accept,
183 	    (void *)0xdeadbeaf));
184 
185 	CHECKr(pthread_join(thread, NULL));
186 
187 	SUCCEED;
188 }
189