1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker *
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker * it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker * the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker * (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13*49cdfc7eSAndroid Build Coastguard Worker * the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker *
15*49cdfc7eSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker * along with this program; if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker */
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker /*
21*49cdfc7eSAndroid Build Coastguard Worker * Test Name: connect01
22*49cdfc7eSAndroid Build Coastguard Worker *
23*49cdfc7eSAndroid Build Coastguard Worker * Test Description:
24*49cdfc7eSAndroid Build Coastguard Worker * Verify that connect() returns the proper errno for various failure cases
25*49cdfc7eSAndroid Build Coastguard Worker *
26*49cdfc7eSAndroid Build Coastguard Worker * Usage: <for command-line>
27*49cdfc7eSAndroid Build Coastguard Worker * connect01 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
28*49cdfc7eSAndroid Build Coastguard Worker * where, -c n : Run n copies concurrently.
29*49cdfc7eSAndroid Build Coastguard Worker * -e : Turn on errno logging.
30*49cdfc7eSAndroid Build Coastguard Worker * -i n : Execute test n times.
31*49cdfc7eSAndroid Build Coastguard Worker * -I x : Execute test for x seconds.
32*49cdfc7eSAndroid Build Coastguard Worker * -P x : Pause for x seconds between iterations.
33*49cdfc7eSAndroid Build Coastguard Worker * -t : Turn on syscall timing.
34*49cdfc7eSAndroid Build Coastguard Worker *
35*49cdfc7eSAndroid Build Coastguard Worker * HISTORY
36*49cdfc7eSAndroid Build Coastguard Worker * 07/2001 Ported by Wayne Boyer
37*49cdfc7eSAndroid Build Coastguard Worker *
38*49cdfc7eSAndroid Build Coastguard Worker * RESTRICTIONS:
39*49cdfc7eSAndroid Build Coastguard Worker * None.
40*49cdfc7eSAndroid Build Coastguard Worker *
41*49cdfc7eSAndroid Build Coastguard Worker */
42*49cdfc7eSAndroid Build Coastguard Worker
43*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
45*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
46*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
49*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
50*49cdfc7eSAndroid Build Coastguard Worker #include <sys/signal.h>
51*49cdfc7eSAndroid Build Coastguard Worker #include <sys/un.h>
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/in.h>
54*49cdfc7eSAndroid Build Coastguard Worker
55*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
56*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
57*49cdfc7eSAndroid Build Coastguard Worker
58*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "connect01";
59*49cdfc7eSAndroid Build Coastguard Worker int testno;
60*49cdfc7eSAndroid Build Coastguard Worker
61*49cdfc7eSAndroid Build Coastguard Worker int s, s2; /* socket descriptor */
62*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr_in sin1, sin2, sin3, sin4;
63*49cdfc7eSAndroid Build Coastguard Worker static int sfd; /* shared between start_server and do_child */
64*49cdfc7eSAndroid Build Coastguard Worker
65*49cdfc7eSAndroid Build Coastguard Worker void setup(void), setup0(void), setup1(void), setup2(void),
66*49cdfc7eSAndroid Build Coastguard Worker cleanup(void), cleanup0(void), cleanup1(void), do_child(void);
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker static pid_t start_server(struct sockaddr_in *);
69*49cdfc7eSAndroid Build Coastguard Worker
70*49cdfc7eSAndroid Build Coastguard Worker struct test_case_t { /* test case structure */
71*49cdfc7eSAndroid Build Coastguard Worker int domain; /* PF_INET, PF_UNIX, ... */
72*49cdfc7eSAndroid Build Coastguard Worker int type; /* SOCK_STREAM, SOCK_DGRAM ... */
73*49cdfc7eSAndroid Build Coastguard Worker int proto; /* protocol number (usually 0 = default) */
74*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr *sockaddr; /* socket address buffer */
75*49cdfc7eSAndroid Build Coastguard Worker int salen; /* connect's 3rd argument */
76*49cdfc7eSAndroid Build Coastguard Worker int retval; /* syscall return value */
77*49cdfc7eSAndroid Build Coastguard Worker int experrno; /* expected errno */
78*49cdfc7eSAndroid Build Coastguard Worker void (*setup) (void);
79*49cdfc7eSAndroid Build Coastguard Worker void (*cleanup) (void);
80*49cdfc7eSAndroid Build Coastguard Worker char *desc;
81*49cdfc7eSAndroid Build Coastguard Worker } tdat[] = {
82*49cdfc7eSAndroid Build Coastguard Worker {
83*49cdfc7eSAndroid Build Coastguard Worker PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&sin1,
84*49cdfc7eSAndroid Build Coastguard Worker sizeof(struct sockaddr_in), -1, EBADF, setup0,
85*49cdfc7eSAndroid Build Coastguard Worker cleanup0, "bad file descriptor"},
86*49cdfc7eSAndroid Build Coastguard Worker {
87*49cdfc7eSAndroid Build Coastguard Worker PF_INET, SOCK_STREAM, 0, (struct sockaddr *)-1,
88*49cdfc7eSAndroid Build Coastguard Worker sizeof(struct sockaddr_in), -1, EFAULT, setup1,
89*49cdfc7eSAndroid Build Coastguard Worker cleanup1, "invalid socket buffer"},
90*49cdfc7eSAndroid Build Coastguard Worker {
91*49cdfc7eSAndroid Build Coastguard Worker PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&sin1,
92*49cdfc7eSAndroid Build Coastguard Worker 3, -1, EINVAL, setup1, cleanup1, "invalid salen"}, {
93*49cdfc7eSAndroid Build Coastguard Worker 0, 0, 0, (struct sockaddr *)&sin1,
94*49cdfc7eSAndroid Build Coastguard Worker sizeof(sin1), -1, ENOTSOCK, setup0, cleanup0,
95*49cdfc7eSAndroid Build Coastguard Worker "invalid socket"}
96*49cdfc7eSAndroid Build Coastguard Worker , {
97*49cdfc7eSAndroid Build Coastguard Worker PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&sin1,
98*49cdfc7eSAndroid Build Coastguard Worker sizeof(sin1), -1, EISCONN, setup2, cleanup1,
99*49cdfc7eSAndroid Build Coastguard Worker "already connected"}
100*49cdfc7eSAndroid Build Coastguard Worker , {
101*49cdfc7eSAndroid Build Coastguard Worker PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&sin2,
102*49cdfc7eSAndroid Build Coastguard Worker sizeof(sin2), -1, ECONNREFUSED, setup1, cleanup1,
103*49cdfc7eSAndroid Build Coastguard Worker "connection refused"}
104*49cdfc7eSAndroid Build Coastguard Worker , {
105*49cdfc7eSAndroid Build Coastguard Worker PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&sin4,
106*49cdfc7eSAndroid Build Coastguard Worker sizeof(sin4), -1, EAFNOSUPPORT, setup1, cleanup1,
107*49cdfc7eSAndroid Build Coastguard Worker "invalid address family"}
108*49cdfc7eSAndroid Build Coastguard Worker ,};
109*49cdfc7eSAndroid Build Coastguard Worker
110*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
111*49cdfc7eSAndroid Build Coastguard Worker
112*49cdfc7eSAndroid Build Coastguard Worker /**
113*49cdfc7eSAndroid Build Coastguard Worker * bionic's connect() implementation calls netdClientInitConnect() before
114*49cdfc7eSAndroid Build Coastguard Worker * sending the request to the kernel. We need to bypass this, or the test will
115*49cdfc7eSAndroid Build Coastguard Worker * segfault during the addr = (struct sockaddr *)-1 testcase. We had cases where
116*49cdfc7eSAndroid Build Coastguard Worker * tests started to segfault on glibc upgrade or in special conditions where
117*49cdfc7eSAndroid Build Coastguard Worker * libc had to convert structure layouts between 32bit/64bit userspace/kernel =>
118*49cdfc7eSAndroid Build Coastguard Worker * safer to call the raw syscall regardless of the libc implementation.
119*49cdfc7eSAndroid Build Coastguard Worker */
120*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
121*49cdfc7eSAndroid Build Coastguard Worker
sys_connect(int sockfd,const struct sockaddr * addr,socklen_t addrlen)122*49cdfc7eSAndroid Build Coastguard Worker static int sys_connect(int sockfd, const struct sockaddr *addr,
123*49cdfc7eSAndroid Build Coastguard Worker socklen_t addrlen)
124*49cdfc7eSAndroid Build Coastguard Worker {
125*49cdfc7eSAndroid Build Coastguard Worker return tst_syscall(__NR_connect, sockfd, addr, addrlen);
126*49cdfc7eSAndroid Build Coastguard Worker }
127*49cdfc7eSAndroid Build Coastguard Worker
128*49cdfc7eSAndroid Build Coastguard Worker #define connect(sockfd, addr, addrlen) sys_connect(sockfd, addr, addrlen)
129*49cdfc7eSAndroid Build Coastguard Worker
main(int argc,char * argv[])130*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
131*49cdfc7eSAndroid Build Coastguard Worker {
132*49cdfc7eSAndroid Build Coastguard Worker int lc;
133*49cdfc7eSAndroid Build Coastguard Worker
134*49cdfc7eSAndroid Build Coastguard Worker tst_parse_opts(argc, argv, NULL, NULL);
135*49cdfc7eSAndroid Build Coastguard Worker
136*49cdfc7eSAndroid Build Coastguard Worker setup();
137*49cdfc7eSAndroid Build Coastguard Worker
138*49cdfc7eSAndroid Build Coastguard Worker for (lc = 0; TEST_LOOPING(lc); ++lc) {
139*49cdfc7eSAndroid Build Coastguard Worker tst_count = 0;
140*49cdfc7eSAndroid Build Coastguard Worker for (testno = 0; testno < TST_TOTAL; ++testno) {
141*49cdfc7eSAndroid Build Coastguard Worker tdat[testno].setup();
142*49cdfc7eSAndroid Build Coastguard Worker
143*49cdfc7eSAndroid Build Coastguard Worker TEST(connect
144*49cdfc7eSAndroid Build Coastguard Worker (s, tdat[testno].sockaddr, tdat[testno].salen));
145*49cdfc7eSAndroid Build Coastguard Worker
146*49cdfc7eSAndroid Build Coastguard Worker if (TEST_RETURN != tdat[testno].retval ||
147*49cdfc7eSAndroid Build Coastguard Worker (TEST_RETURN < 0 &&
148*49cdfc7eSAndroid Build Coastguard Worker TEST_ERRNO != tdat[testno].experrno)) {
149*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "%s ; returned"
150*49cdfc7eSAndroid Build Coastguard Worker " %ld (expected %d), errno %d (expected"
151*49cdfc7eSAndroid Build Coastguard Worker " %d)", tdat[testno].desc,
152*49cdfc7eSAndroid Build Coastguard Worker TEST_RETURN, tdat[testno].retval,
153*49cdfc7eSAndroid Build Coastguard Worker TEST_ERRNO, tdat[testno].experrno);
154*49cdfc7eSAndroid Build Coastguard Worker } else {
155*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "%s successful",
156*49cdfc7eSAndroid Build Coastguard Worker tdat[testno].desc);
157*49cdfc7eSAndroid Build Coastguard Worker }
158*49cdfc7eSAndroid Build Coastguard Worker tdat[testno].cleanup();
159*49cdfc7eSAndroid Build Coastguard Worker }
160*49cdfc7eSAndroid Build Coastguard Worker }
161*49cdfc7eSAndroid Build Coastguard Worker cleanup();
162*49cdfc7eSAndroid Build Coastguard Worker
163*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
164*49cdfc7eSAndroid Build Coastguard Worker }
165*49cdfc7eSAndroid Build Coastguard Worker
166*49cdfc7eSAndroid Build Coastguard Worker pid_t pid;
167*49cdfc7eSAndroid Build Coastguard Worker
setup(void)168*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
169*49cdfc7eSAndroid Build Coastguard Worker {
170*49cdfc7eSAndroid Build Coastguard Worker TEST_PAUSE; /* if -p option specified */
171*49cdfc7eSAndroid Build Coastguard Worker
172*49cdfc7eSAndroid Build Coastguard Worker pid = start_server(&sin1);
173*49cdfc7eSAndroid Build Coastguard Worker
174*49cdfc7eSAndroid Build Coastguard Worker sin2.sin_family = AF_INET;
175*49cdfc7eSAndroid Build Coastguard Worker /* this port must be unused! */
176*49cdfc7eSAndroid Build Coastguard Worker sin2.sin_port = TST_GET_UNUSED_PORT(NULL, AF_INET, SOCK_STREAM);
177*49cdfc7eSAndroid Build Coastguard Worker sin2.sin_addr.s_addr = INADDR_ANY;
178*49cdfc7eSAndroid Build Coastguard Worker
179*49cdfc7eSAndroid Build Coastguard Worker sin3.sin_family = AF_INET;
180*49cdfc7eSAndroid Build Coastguard Worker sin3.sin_port = 0;
181*49cdfc7eSAndroid Build Coastguard Worker /* assumes no route to this network! */
182*49cdfc7eSAndroid Build Coastguard Worker sin3.sin_addr.s_addr = htonl(0x0AFFFEFD);
183*49cdfc7eSAndroid Build Coastguard Worker
184*49cdfc7eSAndroid Build Coastguard Worker sin4.sin_family = 47; /* bogus address family */
185*49cdfc7eSAndroid Build Coastguard Worker sin4.sin_port = 0;
186*49cdfc7eSAndroid Build Coastguard Worker sin4.sin_addr.s_addr = htonl(0x0AFFFEFD);
187*49cdfc7eSAndroid Build Coastguard Worker
188*49cdfc7eSAndroid Build Coastguard Worker }
189*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)190*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
191*49cdfc7eSAndroid Build Coastguard Worker {
192*49cdfc7eSAndroid Build Coastguard Worker (void)kill(pid, SIGKILL);
193*49cdfc7eSAndroid Build Coastguard Worker
194*49cdfc7eSAndroid Build Coastguard Worker }
195*49cdfc7eSAndroid Build Coastguard Worker
setup0(void)196*49cdfc7eSAndroid Build Coastguard Worker void setup0(void)
197*49cdfc7eSAndroid Build Coastguard Worker {
198*49cdfc7eSAndroid Build Coastguard Worker if (tdat[testno].experrno == EBADF)
199*49cdfc7eSAndroid Build Coastguard Worker s = 400; /* anything not an open file */
200*49cdfc7eSAndroid Build Coastguard Worker else if ((s = open("/dev/null", O_WRONLY)) == -1)
201*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup, "open(/dev/null) failed");
202*49cdfc7eSAndroid Build Coastguard Worker
203*49cdfc7eSAndroid Build Coastguard Worker }
204*49cdfc7eSAndroid Build Coastguard Worker
cleanup0(void)205*49cdfc7eSAndroid Build Coastguard Worker void cleanup0(void)
206*49cdfc7eSAndroid Build Coastguard Worker {
207*49cdfc7eSAndroid Build Coastguard Worker close(s);
208*49cdfc7eSAndroid Build Coastguard Worker s = -1;
209*49cdfc7eSAndroid Build Coastguard Worker }
210*49cdfc7eSAndroid Build Coastguard Worker
setup1(void)211*49cdfc7eSAndroid Build Coastguard Worker void setup1(void)
212*49cdfc7eSAndroid Build Coastguard Worker {
213*49cdfc7eSAndroid Build Coastguard Worker s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
214*49cdfc7eSAndroid Build Coastguard Worker tdat[testno].proto);
215*49cdfc7eSAndroid Build Coastguard Worker }
216*49cdfc7eSAndroid Build Coastguard Worker
cleanup1(void)217*49cdfc7eSAndroid Build Coastguard Worker void cleanup1(void)
218*49cdfc7eSAndroid Build Coastguard Worker {
219*49cdfc7eSAndroid Build Coastguard Worker (void)close(s);
220*49cdfc7eSAndroid Build Coastguard Worker s = -1;
221*49cdfc7eSAndroid Build Coastguard Worker }
222*49cdfc7eSAndroid Build Coastguard Worker
setup2(void)223*49cdfc7eSAndroid Build Coastguard Worker void setup2(void)
224*49cdfc7eSAndroid Build Coastguard Worker {
225*49cdfc7eSAndroid Build Coastguard Worker setup1(); /* get a socket in s */
226*49cdfc7eSAndroid Build Coastguard Worker SAFE_CONNECT(cleanup, s, (const struct sockaddr *)&sin1, sizeof(sin1));
227*49cdfc7eSAndroid Build Coastguard Worker }
228*49cdfc7eSAndroid Build Coastguard Worker
start_server(struct sockaddr_in * sin0)229*49cdfc7eSAndroid Build Coastguard Worker pid_t start_server(struct sockaddr_in *sin0)
230*49cdfc7eSAndroid Build Coastguard Worker {
231*49cdfc7eSAndroid Build Coastguard Worker pid_t pid;
232*49cdfc7eSAndroid Build Coastguard Worker socklen_t slen = sizeof(*sin0);
233*49cdfc7eSAndroid Build Coastguard Worker
234*49cdfc7eSAndroid Build Coastguard Worker sin0->sin_family = AF_INET;
235*49cdfc7eSAndroid Build Coastguard Worker sin0->sin_port = 0; /* pick random free port */
236*49cdfc7eSAndroid Build Coastguard Worker sin0->sin_addr.s_addr = INADDR_ANY;
237*49cdfc7eSAndroid Build Coastguard Worker
238*49cdfc7eSAndroid Build Coastguard Worker sfd = socket(PF_INET, SOCK_STREAM, 0);
239*49cdfc7eSAndroid Build Coastguard Worker if (sfd < 0) {
240*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup, "server socket failed");
241*49cdfc7eSAndroid Build Coastguard Worker return -1;
242*49cdfc7eSAndroid Build Coastguard Worker }
243*49cdfc7eSAndroid Build Coastguard Worker if (bind(sfd, (struct sockaddr *)sin0, sizeof(*sin0)) < 0) {
244*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup, "server bind failed");
245*49cdfc7eSAndroid Build Coastguard Worker return -1;
246*49cdfc7eSAndroid Build Coastguard Worker }
247*49cdfc7eSAndroid Build Coastguard Worker if (listen(sfd, 10) < 0) {
248*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup, "server listen failed");
249*49cdfc7eSAndroid Build Coastguard Worker return -1;
250*49cdfc7eSAndroid Build Coastguard Worker }
251*49cdfc7eSAndroid Build Coastguard Worker SAFE_GETSOCKNAME(cleanup, sfd, (struct sockaddr *)sin0, &slen);
252*49cdfc7eSAndroid Build Coastguard Worker
253*49cdfc7eSAndroid Build Coastguard Worker switch ((pid = tst_fork())) {
254*49cdfc7eSAndroid Build Coastguard Worker case 0: /* child */
255*49cdfc7eSAndroid Build Coastguard Worker do_child();
256*49cdfc7eSAndroid Build Coastguard Worker break;
257*49cdfc7eSAndroid Build Coastguard Worker case -1:
258*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup, "server fork failed");
259*49cdfc7eSAndroid Build Coastguard Worker /* fall through */
260*49cdfc7eSAndroid Build Coastguard Worker default: /* parent */
261*49cdfc7eSAndroid Build Coastguard Worker (void)close(sfd);
262*49cdfc7eSAndroid Build Coastguard Worker return pid;
263*49cdfc7eSAndroid Build Coastguard Worker }
264*49cdfc7eSAndroid Build Coastguard Worker
265*49cdfc7eSAndroid Build Coastguard Worker return -1;
266*49cdfc7eSAndroid Build Coastguard Worker }
267*49cdfc7eSAndroid Build Coastguard Worker
do_child(void)268*49cdfc7eSAndroid Build Coastguard Worker void do_child(void)
269*49cdfc7eSAndroid Build Coastguard Worker {
270*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr_in fsin;
271*49cdfc7eSAndroid Build Coastguard Worker fd_set afds, rfds;
272*49cdfc7eSAndroid Build Coastguard Worker int nfds, cc, fd;
273*49cdfc7eSAndroid Build Coastguard Worker char c;
274*49cdfc7eSAndroid Build Coastguard Worker
275*49cdfc7eSAndroid Build Coastguard Worker FD_ZERO(&afds);
276*49cdfc7eSAndroid Build Coastguard Worker FD_SET(sfd, &afds);
277*49cdfc7eSAndroid Build Coastguard Worker
278*49cdfc7eSAndroid Build Coastguard Worker nfds = sfd + 1;
279*49cdfc7eSAndroid Build Coastguard Worker
280*49cdfc7eSAndroid Build Coastguard Worker /* accept connections until killed */
281*49cdfc7eSAndroid Build Coastguard Worker while (1) {
282*49cdfc7eSAndroid Build Coastguard Worker socklen_t fromlen;
283*49cdfc7eSAndroid Build Coastguard Worker
284*49cdfc7eSAndroid Build Coastguard Worker memcpy(&rfds, &afds, sizeof(rfds));
285*49cdfc7eSAndroid Build Coastguard Worker
286*49cdfc7eSAndroid Build Coastguard Worker if (select(nfds, &rfds, NULL, NULL,
287*49cdfc7eSAndroid Build Coastguard Worker NULL) < 0)
288*49cdfc7eSAndroid Build Coastguard Worker if (errno != EINTR)
289*49cdfc7eSAndroid Build Coastguard Worker exit(1);
290*49cdfc7eSAndroid Build Coastguard Worker if (FD_ISSET(sfd, &rfds)) {
291*49cdfc7eSAndroid Build Coastguard Worker int newfd;
292*49cdfc7eSAndroid Build Coastguard Worker
293*49cdfc7eSAndroid Build Coastguard Worker fromlen = sizeof(fsin);
294*49cdfc7eSAndroid Build Coastguard Worker newfd = accept(sfd, (struct sockaddr *)&fsin, &fromlen);
295*49cdfc7eSAndroid Build Coastguard Worker if (newfd >= 0) {
296*49cdfc7eSAndroid Build Coastguard Worker FD_SET(newfd, &afds);
297*49cdfc7eSAndroid Build Coastguard Worker nfds = MAX(nfds, newfd + 1);
298*49cdfc7eSAndroid Build Coastguard Worker }
299*49cdfc7eSAndroid Build Coastguard Worker }
300*49cdfc7eSAndroid Build Coastguard Worker for (fd = 0; fd < nfds; ++fd)
301*49cdfc7eSAndroid Build Coastguard Worker if (fd != sfd && FD_ISSET(fd, &rfds)) {
302*49cdfc7eSAndroid Build Coastguard Worker if ((cc = read(fd, &c, 1)) == 0) {
303*49cdfc7eSAndroid Build Coastguard Worker (void)close(fd);
304*49cdfc7eSAndroid Build Coastguard Worker FD_CLR(fd, &afds);
305*49cdfc7eSAndroid Build Coastguard Worker }
306*49cdfc7eSAndroid Build Coastguard Worker }
307*49cdfc7eSAndroid Build Coastguard Worker }
308*49cdfc7eSAndroid Build Coastguard Worker }
309