]> git.ipfire.org Git - thirdparty/squid.git/blame - src/comm.cc
instrumented persistent connection counter histogram
[thirdparty/squid.git] / src / comm.cc
CommitLineData
30a4f2a8 1/*
17a0a4ee 2 * $Id: comm.cc,v 1.183 1997/07/26 04:48:26 wessels Exp $
30a4f2a8 3 *
4 * DEBUG: section 5 Socket Functions
5 * AUTHOR: Harvest Derived
6 *
42c04c16 7 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
30a4f2a8 8 * --------------------------------------------------------
9 *
10 * Squid is the result of efforts by numerous individuals from the
11 * Internet community. Development is led by Duane Wessels of the
12 * National Laboratory for Applied Network Research and funded by
13 * the National Science Foundation.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 *
29 */
d1f14731 30
30a4f2a8 31/*
32 * Copyright (c) 1994, 1995. All rights reserved.
33 *
34 * The Harvest software was developed by the Internet Research Task
35 * Force Research Group on Resource Discovery (IRTF-RD):
36 *
37 * Mic Bowman of Transarc Corporation.
38 * Peter Danzig of the University of Southern California.
39 * Darren R. Hardy of the University of Colorado at Boulder.
40 * Udi Manber of the University of Arizona.
41 * Michael F. Schwartz of the University of Colorado at Boulder.
42 * Duane Wessels of the University of Colorado at Boulder.
43 *
44 * This copyright notice applies to software in the Harvest
45 * ``src/'' directory only. Users should consult the individual
46 * copyright notices in the ``components/'' subdirectories for
47 * copyright information about other software bundled with the
48 * Harvest source code distribution.
49 *
50 * TERMS OF USE
51 *
52 * The Harvest software may be used and re-distributed without
53 * charge, provided that the software origin and research team are
54 * cited in any use of the system. Most commonly this is
55 * accomplished by including a link to the Harvest Home Page
56 * (http://harvest.cs.colorado.edu/) from the query page of any
57 * Broker you deploy, as well as in the query result pages. These
58 * links are generated automatically by the standard Broker
59 * software distribution.
60 *
61 * The Harvest software is provided ``as is'', without express or
62 * implied warranty, and with no support nor obligation to assist
63 * in its use, correction, modification or enhancement. We assume
64 * no liability with respect to the infringement of copyrights,
65 * trade secrets, or any patents, and are not responsible for
66 * consequential damages. Proper use of the Harvest software is
67 * entirely the responsibility of the user.
68 *
69 * DERIVATIVE WORKS
70 *
71 * Users may make derivative works from the Harvest software, subject
72 * to the following constraints:
73 *
74 * - You must include the above copyright notice and these
75 * accompanying paragraphs in all forms of derivative works,
76 * and any documentation and other materials related to such
77 * distribution and use acknowledge that the software was
78 * developed at the above institutions.
79 *
80 * - You must notify IRTF-RD regarding your distribution of
81 * the derivative work.
82 *
83 * - You must clearly notify users that your are distributing
84 * a modified version and not the original Harvest software.
85 *
86 * - Any derivative product is also subject to these copyright
87 * and use restrictions.
88 *
89 * Note that the Harvest software is NOT in the public domain. We
90 * retain copyright, as specified above.
91 *
92 * HISTORY OF FREE SOFTWARE STATUS
93 *
94 * Originally we required sites to license the software in cases
95 * where they were going to build commercial products/services
96 * around Harvest. In June 1995 we changed this policy. We now
97 * allow people to use the core Harvest software (the code found in
98 * the Harvest ``src/'' directory) for free. We made this change
99 * in the interest of encouraging the widest possible deployment of
100 * the technology. The Harvest software is really a reference
101 * implementation of a set of protocols and formats, some of which
102 * we intend to standardize. We encourage commercial
103 * re-implementations of code complying to this set of standards.
104 */
090089c4 105
44a47c6e 106#include "squid.h"
0a0bf5db 107#include <errno.h>
090089c4 108
30a4f2a8 109#ifdef HAVE_NETINET_TCP_H
110#include <netinet/tcp.h>
111#endif
090089c4 112
113/* Block processing new client requests (accepts on ascii port) when we start
114 * running shy of free file descriptors. For example, under SunOS, we'll keep
115 * 64 file descriptors free for disk-i/o and connections to remote servers */
116
090089c4 117#define min(x,y) ((x)<(y)? (x) : (y))
118#define max(a,b) ((a)>(b)? (a) : (b))
119
f17936ab 120struct _cwstate {
30a4f2a8 121 char *buf;
122 long size;
123 long offset;
f17936ab 124 CWCB *handler;
30a4f2a8 125 void *handler_data;
4a63c85f 126 void (*free) (void *);
f17936ab 127};
090089c4 128
f88211e8 129typedef struct {
130 char *host;
131 u_short port;
132 struct sockaddr_in S;
133 CNCB *callback;
134 void *data;
135 int tries;
136 struct in_addr in_addr;
137 int locks;
03a1ee42 138 int fd;
f88211e8 139} ConnectStateData;
140
090089c4 141/* STATIC */
0b2db32c 142static int polledinc = 0;
24382924 143static int commBind _PARAMS((int s, struct in_addr, u_short port));
f88211e8 144#if !HAVE_POLL
5742d7c9 145static int examine_select _PARAMS((fd_set *, fd_set *));
dcfe6390 146#endif
67508012 147static void checkTimeouts _PARAMS((void));
67508012 148static void commSetReuseAddr _PARAMS((int));
67508012 149static void commSetNoLinger _PARAMS((int));
812ed90c 150#if HAVE_POLL
151static void comm_poll_incoming _PARAMS((void));
152#else
67508012 153static void comm_select_incoming _PARAMS((void));
812ed90c 154#endif
f17936ab 155static void CommWriteStateCallbackAndFree _PARAMS((int fd, int code));
30a4f2a8 156#ifdef TCP_NODELAY
67508012 157static void commSetTcpNoDelay _PARAMS((int));
30a4f2a8 158#endif
67508012 159static void commSetTcpRcvbuf _PARAMS((int, int));
f88211e8 160static PF commConnectFree;
03a1ee42 161static PF commConnectHandle;
162static PF commHandleWrite;
812ed90c 163static int fdIsHttpOrIcp _PARAMS((int fd));
edeb28fd 164static IPH commConnectDnsHandle;
03a1ee42 165static void commConnectCallback _PARAMS((ConnectStateData * cs, int status));
30a4f2a8 166
30a4f2a8 167static struct timeval zero_tv;
090089c4 168
81f754fa 169void
f17936ab 170commCancelWriteHandler(int fd)
81f754fa 171{
f17936ab 172 CommWriteStateData *CommWriteState = fd_table[fd].rwstate;
173 if (CommWriteState) {
174 CommWriteState->handler = NULL;
175 CommWriteState->handler_data = NULL;
81f754fa 176 }
177}
178
b8d8561b 179static void
f17936ab 180CommWriteStateCallbackAndFree(int fd, int code)
9864ee44 181{
f17936ab 182 CommWriteStateData *CommWriteState = fd_table[fd].rwstate;
183 CWCB *callback = NULL;
1a8f5ed6 184 void *data;
a56a3abe 185 fd_table[fd].rwstate = NULL;
f17936ab 186 if (CommWriteState == NULL)
9864ee44 187 return;
f17936ab 188 if (CommWriteState->free) {
189 CommWriteState->free(CommWriteState->buf);
190 CommWriteState->buf = NULL;
9864ee44 191 }
f17936ab 192 callback = CommWriteState->handler;
1a8f5ed6 193 data = CommWriteState->handler_data;
f17936ab 194 CommWriteState->handler = NULL;
1a8f5ed6 195 if (callback && cbdataValid(data))
196 callback(fd, CommWriteState->buf, CommWriteState->offset, code, data);
197 cbdataUnlock(data);
f17936ab 198 safe_free(CommWriteState);
9864ee44 199}
200
090089c4 201/* Return the local port associated with fd. */
b8d8561b 202u_short
203comm_local_port(int fd)
090089c4 204{
205 struct sockaddr_in addr;
206 int addr_len = 0;
76f87348 207 fde *F = &fd_table[fd];
090089c4 208
090089c4 209 /* If the fd is closed already, just return */
76f87348 210 if (!F->open) {
a3d5953d 211 debug(5, 0) ("comm_local_port: FD %d has been closed.\n", fd);
30a4f2a8 212 return 0;
090089c4 213 }
76f87348 214 if (F->local_port)
215 return F->local_port;
090089c4 216 addr_len = sizeof(addr);
217 if (getsockname(fd, (struct sockaddr *) &addr, &addr_len)) {
a3d5953d 218 debug(50, 1) ("comm_local_port: Failed to retrieve TCP/UDP port number for socket: FD %d: %s\n", fd, xstrerror());
30a4f2a8 219 return 0;
090089c4 220 }
a3d5953d 221 debug(5, 6) ("comm_local_port: FD %d: sockaddr %u.\n", fd, addr.sin_addr.s_addr);
76f87348 222 F->local_port = ntohs(addr.sin_port);
223 return F->local_port;
090089c4 224}
225
b8d8561b 226static int
227commBind(int s, struct in_addr in_addr, u_short port)
090089c4 228{
229 struct sockaddr_in S;
090089c4 230
090089c4 231 memset(&S, '\0', sizeof(S));
232 S.sin_family = AF_INET;
233 S.sin_port = htons(port);
30a4f2a8 234 S.sin_addr = in_addr;
090089c4 235 if (bind(s, (struct sockaddr *) &S, sizeof(S)) == 0)
236 return COMM_OK;
a3d5953d 237 debug(50, 0) ("commBind: Cannot bind socket FD %d to %s:%d: %s\n",
090089c4 238 s,
30a4f2a8 239 S.sin_addr.s_addr == INADDR_ANY ? "*" : inet_ntoa(S.sin_addr),
44a62238 240 (int) port,
241 xstrerror());
090089c4 242 return COMM_ERROR;
243}
244
245/* Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE
246 * is OR of flags specified in comm.h. */
b8d8561b 247int
16b204c4 248comm_open(int sock_type,
cc6a9d2e 249 int proto,
250 struct in_addr addr,
251 u_short port,
252 int flags,
0ee4272b 253 const char *note)
090089c4 254{
255 int new_socket;
76f87348 256 fde *F = NULL;
b6f794d6 257 int tcp_rcv_bufsz = Config.tcpRcvBufsz;
090089c4 258
259 /* Create socket for accepting new connections. */
16b204c4 260 if ((new_socket = socket(AF_INET, sock_type, proto)) < 0) {
090089c4 261 /* Increase the number of reserved fd's if calls to socket()
262 * are failing because the open file table is full. This
263 * limits the number of simultaneous clients */
264 switch (errno) {
265 case ENFILE:
266 case EMFILE:
a3d5953d 267 debug(50, 1) ("comm_open: socket failure: %s\n", xstrerror());
090089c4 268 break;
269 default:
a3d5953d 270 debug(50, 0) ("comm_open: socket failure: %s\n", xstrerror());
090089c4 271 }
272 return (COMM_ERROR);
273 }
274 /* update fdstat */
365e5b34 275 debug(5, 5) ("comm_open: FD %d is a new socket\n", new_socket);
5c5783a2 276 fd_open(new_socket, FD_SOCKET, note);
76f87348 277 F = &fd_table[new_socket];
16b204c4 278 if (!BIT_TEST(flags, COMM_NOCLOEXEC))
3ca60c86 279 commSetCloseOnExec(new_socket);
7690e8eb 280 if (port > (u_short) 0) {
30a4f2a8 281 commSetNoLinger(new_socket);
282 if (do_reuse)
090089c4 283 commSetReuseAddr(new_socket);
090089c4 284 }
a3724d50 285 if (addr.s_addr != no_addr.s_addr) {
286 if (commBind(new_socket, addr, port) != COMM_OK) {
287 comm_close(new_socket);
30a4f2a8 288 return COMM_ERROR;
a3724d50 289 }
23ff6968 290 }
76f87348 291 F->local_port = port;
090089c4 292
16b204c4 293 if (BIT_TEST(flags, COMM_NONBLOCKING))
30a4f2a8 294 if (commSetNonBlocking(new_socket) == COMM_ERROR)
295 return COMM_ERROR;
296#ifdef TCP_NODELAY
297 if (sock_type == SOCK_STREAM)
298 commSetTcpNoDelay(new_socket);
299#endif
f868539a 300 if (tcp_rcv_bufsz > 0 && sock_type == SOCK_STREAM)
301 commSetTcpRcvbuf(new_socket, tcp_rcv_bufsz);
090089c4 302 return new_socket;
303}
304
305 /*
e83892e9 306 * NOTE: set the listen queue to Squid_MaxFD/4 and rely on the kernel to
090089c4 307 * impose an upper limit. Solaris' listen(3n) page says it has
308 * no limit on this parameter, but sys/socket.h sets SOMAXCONN
309 * to 5. HP-UX currently has a limit of 20. SunOS is 5 and
310 * OSF 3.0 is 8.
311 */
b8d8561b 312int
313comm_listen(int sock)
090089c4 314{
315 int x;
e83892e9 316 if ((x = listen(sock, Squid_MaxFD >> 2)) < 0) {
a3d5953d 317 debug(50, 0) ("comm_listen: listen(%d, %d): %s\n",
e83892e9 318 Squid_MaxFD >> 2,
090089c4 319 sock, xstrerror());
320 return x;
321 }
322 return sock;
323}
324
e5f6c5c2 325void
4f92c80c 326commConnectStart(int fd, const char *host, u_short port, CNCB * callback, void *data)
e924600d 327{
328 ConnectStateData *cs = xcalloc(1, sizeof(ConnectStateData));
8407afee 329 cbdataAdd(cs);
03a1ee42 330 cs->fd = fd;
e924600d 331 cs->host = xstrdup(host);
332 cs->port = port;
333 cs->callback = callback;
334 cs->data = data;
8407afee 335 cbdataLock(data);
e924600d 336 comm_add_close_handler(fd, commConnectFree, cs);
f88211e8 337 cs->locks++;
8407afee 338 ipcache_nbgethostbyname(host, commConnectDnsHandle, cs);
edeb28fd 339}
340
341static void
03a1ee42 342commConnectDnsHandle(const ipcache_addrs * ia, void *data)
edeb28fd 343{
344 ConnectStateData *cs = data;
f88211e8 345 assert(cs->locks == 1);
346 cs->locks--;
edeb28fd 347 if (ia == NULL) {
a3d5953d 348 debug(5, 3) ("commConnectDnsHandle: Unknown host: %s\n", cs->host);
03a1ee42 349 commConnectCallback(cs, COMM_ERR_DNS);
edeb28fd 350 return;
351 }
352 cs->in_addr = ia->in_addrs[ia->cur];
03a1ee42 353 commConnectHandle(cs->fd, cs);
e924600d 354}
355
f88211e8 356static void
03a1ee42 357commConnectCallback(ConnectStateData * cs, int status)
f88211e8 358{
a3d5953d 359 CNCB *callback = cs->callback;
360 void *data = cs->data;
03a1ee42 361 int fd = cs->fd;
a3d5953d 362 comm_remove_close_handler(fd, commConnectFree, cs);
363 commConnectFree(fd, cs);
8407afee 364 if (cbdataValid(data))
365e5b34 365 callback(fd, status, data);
8407afee 366 cbdataUnlock(data);
f88211e8 367}
368
e924600d 369static void
03a1ee42 370commConnectFree(int fdunused, void *data)
e924600d 371{
372 ConnectStateData *cs = data;
8407afee 373 if (cs->locks)
365e5b34 374 ipcacheUnregister(cs->host, cs);
8407afee 375 safe_free(cs->host);
376 cbdataFree(cs);
e924600d 377}
378
edeb28fd 379static int
f88211e8 380commRetryConnect(int fd, ConnectStateData * cs)
edeb28fd 381{
382 int fd2;
f88211e8 383 if (++cs->tries == 4)
edeb28fd 384 return 0;
7dd44885 385 if (!cbdataValid(cs->data))
386 return 0;
edeb28fd 387 fd2 = socket(AF_INET, SOCK_STREAM, 0);
388 if (fd2 < 0) {
a3d5953d 389 debug(5, 0) ("commRetryConnect: socket: %s\n", xstrerror());
edeb28fd 390 return 0;
391 }
392 if (dup2(fd2, fd) < 0) {
a3d5953d 393 debug(5, 0) ("commRetryConnect: dup2: %s\n", xstrerror());
edeb28fd 394 return 0;
395 }
396 commSetNonBlocking(fd);
397 close(fd2);
398 return 1;
399}
400
e924600d 401/* Connect SOCK to specified DEST_PORT at DEST_HOST. */
402static void
403commConnectHandle(int fd, void *data)
090089c4 404{
f88211e8 405 ConnectStateData *cs = data;
406 if (cs->S.sin_addr.s_addr == 0) {
407 cs->S.sin_family = AF_INET;
408 cs->S.sin_addr = cs->in_addr;
409 cs->S.sin_port = htons(cs->port);
17a0a4ee 410 if (Config.onoff.log_fqdn)
f88211e8 411 fqdncache_gethostbyaddr(cs->S.sin_addr, FQDN_LOOKUP_IF_MISS);
e5f6c5c2 412 }
f88211e8 413 switch (comm_connect_addr(fd, &cs->S)) {
e5f6c5c2 414 case COMM_INPROGRESS:
489b22c1 415 debug(5, 5) ("FD %d: COMM_INPROGRESS\n", fd);
f88211e8 416 commSetSelect(fd, COMM_SELECT_WRITE, commConnectHandle, cs, 0);
e5f6c5c2 417 break;
418 case COMM_OK:
f88211e8 419 ipcacheCycleAddr(cs->host);
03a1ee42 420 commConnectCallback(cs, COMM_OK);
e5f6c5c2 421 break;
422 default:
f88211e8 423 if (commRetryConnect(fd, cs)) {
a3d5953d 424 debug(5, 1) ("Retrying connection to %s: %s\n",
f88211e8 425 cs->host, xstrerror());
426 cs->S.sin_addr.s_addr = 0;
427 ipcacheCycleAddr(cs->host);
428 cs->locks++;
8407afee 429 ipcache_nbgethostbyname(cs->host, commConnectDnsHandle, cs);
edeb28fd 430 } else {
f88211e8 431 ipcacheRemoveBadAddr(cs->host, cs->S.sin_addr);
03a1ee42 432 commConnectCallback(cs, COMM_ERR_CONNECT);
edeb28fd 433 }
e5f6c5c2 434 break;
090089c4 435 }
090089c4 436}
b8d8561b 437int
4f92c80c 438commSetTimeout(int fd, int timeout, PF * handler, void *data)
090089c4 439{
76f87348 440 fde *F;
a3d5953d 441 debug(5, 3) ("commSetTimeout: FD %d timeout %d\n", fd, timeout);
03eb2f01 442 assert(fd >= 0);
443 assert(fd < Squid_MaxFD);
76f87348 444 F = &fd_table[fd];
5c5783a2 445 if (timeout < 0) {
76f87348 446 F->timeout_handler = NULL;
447 F->timeout_data = NULL;
448 return F->timeout = 0;
5c5783a2 449 }
bbdb774b 450 if (shutdown_pending || reconfigure_pending) {
4f92c80c 451 /* don't increase the timeout if something pending */
76f87348 452 if (F->timeout > 0 && (int) (F->timeout - squid_curtime) < timeout)
453 return F->timeout;
5c5783a2 454 }
76f87348 455 assert(handler || F->timeout_handler);
5c5783a2 456 if (handler || data) {
76f87348 457 F->timeout_handler = handler;
458 F->timeout_data = data;
30a4f2a8 459 }
76f87348 460 return F->timeout = squid_curtime + (time_t) timeout;
090089c4 461}
462
b8d8561b 463int
0ee4272b 464comm_connect_addr(int sock, const struct sockaddr_in *address)
090089c4 465{
466 int status = COMM_OK;
76f87348 467 fde *F = &fd_table[sock];
090089c4 468 int len;
469 int x;
489b22c1 470 assert(ntohs(address->sin_port) != 0);
090089c4 471 /* Establish connection. */
086bce16 472 if (connect(sock, (struct sockaddr *) address, sizeof(struct sockaddr_in)) < 0) {
365e5b34 473 debug(5, 9) ("connect FD %d: %s\n", sock, xstrerror());
090089c4 474 switch (errno) {
475 case EALREADY:
30a4f2a8 476#if EAGAIN != EWOULDBLOCK
477 case EAGAIN:
478#endif
0a0bf5db 479 case EINTR:
30a4f2a8 480 case EWOULDBLOCK:
090089c4 481 case EINPROGRESS:
e5f6c5c2 482 status = COMM_INPROGRESS;
090089c4 483 break;
484 case EISCONN:
485 status = COMM_OK;
486 break;
487 case EINVAL:
488 len = sizeof(x);
489 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *) &x, &len) >= 0)
490 errno = x;
491 default:
a3d5953d 492 debug(50, 2) ("connect: %s:%d: %s.\n",
28ab0c0a 493 fqdnFromAddr(address->sin_addr),
090089c4 494 ntohs(address->sin_port),
495 xstrerror());
496 return COMM_ERROR;
497 }
e5f6c5c2 498 }
76f87348 499 xstrncpy(F->ipaddr, inet_ntoa(address->sin_addr), 16);
500 F->remote_port = ntohs(address->sin_port);
090089c4 501 if (status == COMM_OK) {
a3d5953d 502 debug(5, 10) ("comm_connect_addr: FD %d connected to %s:%d\n",
76f87348 503 sock, F->ipaddr, F->remote_port);
f21cd581 504 } else if (status == COMM_INPROGRESS) {
a3d5953d 505 debug(5, 10) ("comm_connect_addr: FD %d connection pending\n", sock);
090089c4 506 }
507 /* Add new socket to list of open sockets. */
090089c4 508 return status;
509}
510
511/* Wait for an incoming connection on FD. FD should be a socket returned
512 * from comm_listen. */
b8d8561b 513int
514comm_accept(int fd, struct sockaddr_in *peer, struct sockaddr_in *me)
090089c4 515{
516 int sock;
1f9afe33 517 struct sockaddr_in P;
518 struct sockaddr_in M;
090089c4 519 int Slen;
76f87348 520 fde *F = NULL;
090089c4 521
1f9afe33 522 Slen = sizeof(P);
523 while ((sock = accept(fd, (struct sockaddr *) &P, &Slen)) < 0) {
090089c4 524 switch (errno) {
525#if EAGAIN != EWOULDBLOCK
526 case EAGAIN:
527#endif
528 case EWOULDBLOCK:
090089c4 529 case EINTR:
0a0bf5db 530 return COMM_NOMESSAGE;
090089c4 531 case ENFILE:
532 case EMFILE:
090089c4 533 return COMM_ERROR;
534 default:
a3d5953d 535 debug(50, 1) ("comm_accept: FD %d: accept failure: %s\n",
090089c4 536 fd, xstrerror());
537 return COMM_ERROR;
538 }
539 }
540
541 if (peer)
1f9afe33 542 *peer = P;
4053a845 543 Slen = sizeof(M);
544 memset(&M, '\0', Slen);
545 getsockname(sock, (struct sockaddr *) &M, &Slen);
546 if (me)
1f9afe33 547 *me = M;
3ca60c86 548 commSetCloseOnExec(sock);
090089c4 549 /* fdstat update */
5c5783a2 550 fd_open(sock, FD_SOCKET, "HTTP Request");
76f87348 551 F = &fd_table[sock];
552 strcpy(F->ipaddr, inet_ntoa(P.sin_addr));
553 F->remote_port = htons(P.sin_port);
554 F->local_port = htons(M.sin_port);
090089c4 555 commSetNonBlocking(sock);
090089c4 556 return sock;
557}
558
cb201b7e 559void
560commCallCloseHandlers(int fd)
561{
76f87348 562 fde *F = &fd_table[fd];
f1dc9b30 563 close_handler *ch;
a3d5953d 564 debug(5, 5) ("commCallCloseHandlers: FD %d\n", fd);
76f87348 565 while ((ch = F->close_handler) != NULL) {
566 F->close_handler = ch->next;
cb201b7e 567 ch->handler(fd, ch->data);
568 safe_free(ch);
569 }
570}
571
b8d8561b 572void
573comm_close(int fd)
090089c4 574{
76f87348 575 fde *F = NULL;
a3d5953d 576 debug(5, 5) ("comm_close: FD %d\n", fd);
03eb2f01 577 assert(fd >= 0);
578 assert(fd < Squid_MaxFD);
76f87348 579 F = &fd_table[fd];
580 if (!F->open) {
a47b9029 581 debug(5, 1) ("comm_close: FD %d is not open!\n", fd);
9864ee44 582 return;
76f87348 583 }
584 assert(F->type != FD_FILE);
f17936ab 585 CommWriteStateCallbackAndFree(fd, COMM_ERROR);
cb201b7e 586 commCallCloseHandlers(fd);
5c5783a2 587 fd_close(fd); /* update fdstat */
0a0bf5db 588#if USE_ASYNC_IO
589 aioClose(fd);
590#else
9864ee44 591 close(fd);
0a0bf5db 592#endif
994bbf93 593 memset(F, '\0', sizeof(fde));
090089c4 594}
595
090089c4 596
597/* Send a udp datagram to specified PORT at HOST. */
b8d8561b 598int
0ee4272b 599comm_udp_send(int fd, const char *host, u_short port, const char *buf, int len)
090089c4 600{
0ee4272b 601 const ipcache_addrs *ia = NULL;
090089c4 602 static struct sockaddr_in to_addr;
603 int bytes_sent;
604
605 /* Set up the destination socket address for message to send to. */
606 to_addr.sin_family = AF_INET;
607
e5f6c5c2 608 if ((ia = ipcache_gethostbyname(host, IP_BLOCKING_LOOKUP)) == 0) {
a3d5953d 609 debug(50, 1) ("comm_udp_send: gethostbyname failure: %s: %s\n",
090089c4 610 host, xstrerror());
611 return (COMM_ERROR);
612 }
e5f6c5c2 613 to_addr.sin_addr = ia->in_addrs[ia->cur];
090089c4 614 to_addr.sin_port = htons(port);
615 if ((bytes_sent = sendto(fd, buf, len, 0, (struct sockaddr *) &to_addr,
616 sizeof(to_addr))) < 0) {
a3d5953d 617 debug(50, 1) ("comm_udp_send: sendto failure: FD %d: %s\n",
090089c4 618 fd, xstrerror());
619 return COMM_ERROR;
620 }
621 return bytes_sent;
622}
623
624/* Send a udp datagram to specified TO_ADDR. */
b8d8561b 625int
5df61230 626comm_udp_sendto(int fd,
627 const struct sockaddr_in *to_addr,
628 int addr_len,
629 const char *buf,
630 int len)
090089c4 631{
5df61230 632 int x;
633 x = sendto(fd, buf, len, 0, (struct sockaddr *) to_addr, addr_len);
634 if (x < 0) {
a3d5953d 635 debug(50, 1) ("comm_udp_sendto: FD %d, %s, port %d: %s\n",
5df61230 636 fd,
637 inet_ntoa(to_addr->sin_addr),
638 (int) htons(to_addr->sin_port),
639 xstrerror());
090089c4 640 return COMM_ERROR;
641 }
5df61230 642 return x;
090089c4 643}
644
b8d8561b 645void
646comm_set_stall(int fd, int delta)
4883993a 647{
648 if (fd < 0)
649 return;
b8de7ebe 650 fd_table[fd].stall_until = squid_curtime + delta;
4883993a 651}
652
dcfe6390 653
f88211e8 654#if HAVE_POLL
dcfe6390 655
656/* poll() version by:
657 * Stewart Forster <slf@connect.com.au>, and
658 * Anthony Baxter <arb@connect.com.au> */
659
660static void
812ed90c 661comm_poll_incoming(void)
dcfe6390 662{
429fdbec 663 int fd;
996a0a51 664 int fds[4];
0b2421ea 665 struct pollfd pfds[3 + MAXHTTPPORTS];
996a0a51 666 unsigned long N = 0;
429fdbec 667 unsigned long i, nfds;
812ed90c 668 int j;
582b6456 669 PF *hdl = NULL;
97c03d3c 670 polledinc = 0;
dcfe6390 671 if (theInIcpConnection >= 0)
672 fds[N++] = theInIcpConnection;
933c6d93 673 if (theInIcpConnection != theOutIcpConnection)
1793867a 674 if (theOutIcpConnection >= 0)
933c6d93 675 fds[N++] = theOutIcpConnection;
0b2421ea 676 for (j = 0; j < NHttpSockets; j++) {
812ed90c 677 if (HttpSockets[j] < 0)
0b2421ea 678 continue;
812ed90c 679 if (fd_table[HttpSockets[j]].stall_until > squid_curtime)
0b2421ea 680 continue;
812ed90c 681 fds[N++] = HttpSockets[j];
682 }
429fdbec 683 for (i = nfds = 0; i < N; i++) {
684 int events;
dcfe6390 685 fd = fds[i];
429fdbec 686 events = 0;
687 if (fd_table[fd].read_handler)
688 events |= POLLRDNORM;
689 if (fd_table[fd].write_handler)
690 events |= POLLWRNORM;
691 if (events) {
692 pfds[nfds].fd = fd;
693 pfds[nfds].events = events;
694 pfds[nfds].revents = 0;
695 nfds++;
dcfe6390 696 }
dcfe6390 697 }
429fdbec 698 if (!nfds)
699 return;
97c03d3c 700 polledinc = poll(pfds, nfds, 0);
701 if (polledinc < 1) {
702 polledinc = 0;
996a0a51 703 return;
97c03d3c 704 }
429fdbec 705 for (i = 0; i < nfds; i++) {
706 int revents;
707 if (((revents = pfds[i].revents) == 0) || ((fd = pfds[i].fd) == -1))
dcfe6390 708 continue;
429fdbec 709 if (revents & (POLLRDNORM | POLLIN | POLLHUP | POLLERR)) {
710 hdl = fd_table[fd].read_handler;
76f87348 711 fd_table[fd].read_handler = NULL;
429fdbec 712 hdl(fd, fd_table[fd].read_data);
dcfe6390 713 }
429fdbec 714 if (revents & (POLLWRNORM | POLLOUT | POLLHUP | POLLERR)) {
715 hdl = fd_table[fd].write_handler;
76f87348 716 fd_table[fd].write_handler = NULL;
429fdbec 717 hdl(fd, fd_table[fd].write_data);
dcfe6390 718 }
719 }
720 /* TO FIX: repoll ICP connection here */
721}
722
ca98227c 723#else
dcfe6390 724
b8d8561b 725static void
0673c0ba 726comm_select_incoming(void)
055f4d4d 727{
728 fd_set read_mask;
729 fd_set write_mask;
730 int maxfd = 0;
731 int fd = 0;
0b2421ea 732 int fds[3 + MAXHTTPPORTS];
055f4d4d 733 int N = 0;
734 int i = 0;
582b6456 735 PF *hdl = NULL;
97c03d3c 736 polledinc = 0;
055f4d4d 737 FD_ZERO(&read_mask);
738 FD_ZERO(&write_mask);
0b2421ea 739 for (i = 0; i < NHttpSockets; i++) {
812ed90c 740 if (HttpSockets[i] < 0)
0b2421ea 741 continue;
812ed90c 742 if (fd_table[HttpSockets[i]].stall_until > squid_curtime)
0b2421ea 743 continue;
812ed90c 744 fds[N++] = HttpSockets[i];
745 }
30a4f2a8 746 if (theInIcpConnection >= 0)
747 fds[N++] = theInIcpConnection;
933c6d93 748 if (theInIcpConnection != theOutIcpConnection)
749 if (theOutIcpConnection >= 0)
750 fds[N++] = theOutIcpConnection;
055f4d4d 751 fds[N++] = 0;
055f4d4d 752 for (i = 0; i < N; i++) {
753 fd = fds[i];
754 if (fd_table[fd].read_handler) {
755 FD_SET(fd, &read_mask);
756 if (fd > maxfd)
757 maxfd = fd;
758 }
759 if (fd_table[fd].write_handler) {
760 FD_SET(fd, &write_mask);
761 if (fd > maxfd)
762 maxfd = fd;
763 }
764 }
055f4d4d 765 if (maxfd++ == 0)
766 return;
97c03d3c 767 polledinc = select(maxfd, &read_mask, &write_mask, NULL, &zero_tv);
768 if (polledinc < 1) {
769 polledinc = 0;
dcfe6390 770 return;
97c03d3c 771 }
dcfe6390 772 for (i = 0; i < N; i++) {
773 fd = fds[i];
774 if (FD_ISSET(fd, &read_mask)) {
775 hdl = fd_table[fd].read_handler;
76f87348 776 fd_table[fd].read_handler = NULL;
dcfe6390 777 hdl(fd, fd_table[fd].read_data);
778 }
779 if (FD_ISSET(fd, &write_mask)) {
780 hdl = fd_table[fd].write_handler;
76f87348 781 fd_table[fd].write_handler = NULL;
dcfe6390 782 hdl(fd, fd_table[fd].write_data);
783 }
784 }
785}
786#endif
787
812ed90c 788static int
789fdIsHttpOrIcp(int fd)
790{
791 int j;
792 if (fd == theInIcpConnection)
793 return 1;
794 if (fd == theOutIcpConnection)
795 return 1;
796 for (j = 0; j < NHttpSockets; j++) {
797 if (fd == HttpSockets[j])
798 return 1;
799 }
800 return 0;
801}
802
f88211e8 803#if HAVE_POLL
dcfe6390 804/* poll all sockets; call handlers for those that are ready. */
805int
812ed90c 806comm_poll(time_t sec)
dcfe6390 807{
0a0bf5db 808 struct pollfd pfds[SQUID_MAXFD];
582b6456 809 PF *hdl = NULL;
dcfe6390 810 int fd;
811 int i;
812 int maxfd;
996a0a51 813 unsigned long nfds;
dcfe6390 814 int num;
dcfe6390 815 static time_t last_timeout = 0;
97c03d3c 816 static int lastinc = 0;
429fdbec 817 int poll_time;
812ed90c 818 static int incoming_counter = 0;
dcfe6390 819 time_t timeout;
dcfe6390 820 /* assume all process are very fast (less than 1 second). Call
821 * time() only once */
dcfe6390 822 /* use only 1 second granularity */
823 timeout = squid_curtime + sec;
824 do {
bbdb774b 825 if (shutdown_pending || reconfigure_pending) {
dcfe6390 826 serverConnectionsClose();
dcfe6390 827 dnsShutdownServers();
828 redirectShutdownServers();
429fdbec 829 /* shutdown_pending will be set to
830 * +1 for SIGTERM
831 * -1 for SIGINT */
bbdb774b 832 /* reconfigure_pending always == 1 when SIGHUP received */
833 if (shutdown_pending > 0 || reconfigure_pending > 0)
5c5783a2 834 setSocketShutdownLifetimes(Config.shutdownLifetime);
dcfe6390 835 else
9e4ad609 836 setSocketShutdownLifetimes(1);
dcfe6390 837 }
429fdbec 838 nfds = 0;
839 maxfd = Biggest_FD + 1;
429fdbec 840 for (i = 0; i < maxfd; i++) {
841 int events;
842 events = 0;
dcfe6390 843 /* Check each open socket for a handler. */
429fdbec 844 if (fd_table[i].read_handler && fd_table[i].stall_until <= squid_curtime)
845 events |= POLLRDNORM;
846 if (fd_table[i].write_handler)
847 events |= POLLWRNORM;
848 if (events) {
429fdbec 849 pfds[nfds].fd = i;
850 pfds[nfds].events = events;
851 pfds[nfds].revents = 0;
852 nfds++;
055f4d4d 853 }
0b2421ea 854 }
bbdb774b 855 if (shutdown_pending || reconfigure_pending)
a3d5953d 856 debug(5, 2) ("comm_poll: Still waiting on %d FDs\n", nfds);
dcfe6390 857 if (nfds == 0)
858 return COMM_SHUTDOWN;
76f87348 859 poll_time = sec > 0 ? 1000 : 0;
0a0bf5db 860#if USE_ASYNC_IO
861 aioCheckCallbacks();
862#endif
dcfe6390 863 for (;;) {
429fdbec 864 num = poll(pfds, nfds, poll_time);
865 select_loops++;
dcfe6390 866 if (num >= 0)
867 break;
868 if (errno == EINTR)
0a0bf5db 869 continue;
a3d5953d 870 debug(5, 0) ("comm_poll: poll failure: %s\n", xstrerror());
03eb2f01 871 assert(errno != EINVAL);
dcfe6390 872 return COMM_ERROR;
873 /* NOTREACHED */
874 }
a3d5953d 875 debug(5, num ? 5 : 8) ("comm_poll: %d sockets ready\n", num);
5c5783a2 876 /* Check timeout handlers ONCE each second. */
dcfe6390 877 if (squid_curtime > last_timeout) {
878 last_timeout = squid_curtime;
879 checkTimeouts();
dcfe6390 880 }
881 if (num == 0)
882 continue;
883 /* scan each socket but the accept socket. Poll this
2c5294ce 884 * more frequently to minimize losses due to the 5 connect
dcfe6390 885 * limit in SunOS */
429fdbec 886 for (i = 0; i < nfds; i++) {
887 int revents;
888 if (((revents = pfds[i].revents) == 0) || ((fd = pfds[i].fd) == -1))
dcfe6390 889 continue;
97c03d3c 890 if ((incoming_counter++ & (lastinc > 0 ? 1 : 7)) == 0)
812ed90c 891 comm_poll_incoming();
892 if (fdIsHttpOrIcp(fd))
996a0a51 893 continue;
429fdbec 894 if (revents & (POLLRDNORM | POLLIN | POLLHUP | POLLERR)) {
a3d5953d 895 debug(5, 6) ("comm_poll: FD %d ready for reading\n", fd);
0b2421ea 896 if ((hdl = fd_table[fd].read_handler)) {
76f87348 897 fd_table[fd].read_handler = NULL;
0b2421ea 898 hdl(fd, fd_table[fd].read_data);
899 }
dcfe6390 900 }
429fdbec 901 if (revents & (POLLWRNORM | POLLOUT | POLLHUP | POLLERR)) {
a3d5953d 902 debug(5, 5) ("comm_poll: FD %d ready for writing\n", fd);
0b2421ea 903 if ((hdl = fd_table[fd].write_handler)) {
76f87348 904 fd_table[fd].write_handler = NULL;
0b2421ea 905 hdl(fd, fd_table[fd].write_data);
906 }
dcfe6390 907 }
429fdbec 908 if (revents & POLLNVAL) {
f1dc9b30 909 close_handler *ch;
910 close_handler *next;
76f87348 911 fde *F = &fd_table[fd];
a3d5953d 912 debug(5, 0) ("WARNING: FD %d has handlers, but it's invalid.\n", fd);
913 debug(5, 0) ("FD %d is a %s\n", fd, fdstatTypeStr[fd_table[fd].type]);
914 debug(5, 0) ("--> %s\n", fd_table[fd].desc);
915 debug(5, 0) ("tmout:%p read:%p write:%p\n",
76f87348 916 F->timeout_handler,
917 F->read_handler,
918 F->write_handler);
76f87348 919 for (ch = F->close_handler; ch; ch = ch->next)
a3d5953d 920 debug(5, 0) (" close handler: %p\n", ch->handler);
76f87348 921 if (F->close_handler) {
922 for (ch = F->close_handler; ch; ch = next) {
dcfe6390 923 next = ch->next;
924 ch->handler(fd, ch->data);
925 safe_free(ch);
926 }
76f87348 927 } else if (F->timeout_handler) {
a3d5953d 928 debug(5, 0) ("comm_poll: Calling Timeout Handler\n");
76f87348 929 F->timeout_handler(fd, F->timeout_data);
dcfe6390 930 }
76f87348 931 F->close_handler = NULL;
932 F->timeout_handler = NULL;
933 F->read_handler = NULL;
934 F->write_handler = NULL;
dcfe6390 935 }
97c03d3c 936 lastinc = polledinc;
dcfe6390 937 }
938 return COMM_OK;
97c03d3c 939 } while (timeout > squid_curtime);
a3d5953d 940 debug(5, 8) ("comm_poll: time out: %d.\n", squid_curtime);
dcfe6390 941 return COMM_TIMEOUT;
055f4d4d 942}
090089c4 943
dcfe6390 944#else
090089c4 945
946/* Select on all sockets; call handlers for those that are ready. */
b8d8561b 947int
948comm_select(time_t sec)
090089c4 949{
090089c4 950 fd_set readfds;
951 fd_set writefds;
582b6456 952 PF *hdl = NULL;
7d49daab 953 int fd;
954 int i;
955 int maxfd;
956 int nfds;
090089c4 957 int num;
97c03d3c 958 static int incoming_counter = 0;
090089c4 959 static time_t last_timeout = 0;
960 struct timeval poll_time;
97c03d3c 961 static int lastinc;
7d49daab 962 time_t timeout;
090089c4 963
964 /* assume all process are very fast (less than 1 second). Call
965 * time() only once */
090089c4 966 /* use only 1 second granularity */
b8de7ebe 967 timeout = squid_curtime + sec;
090089c4 968
f7361640 969 do {
090089c4 970 FD_ZERO(&readfds);
971 FD_ZERO(&writefds);
bbdb774b 972 if (shutdown_pending || reconfigure_pending) {
30a4f2a8 973 serverConnectionsClose();
f88bb09c 974 dnsShutdownServers();
d2af9477 975 redirectShutdownServers();
429fdbec 976 /* shutdown_pending will be set to
977 * +1 for SIGTERM
978 * -1 for SIGINT */
bbdb774b 979 /* reconfigure_pending always == 1 when SIGHUP received */
980 if (shutdown_pending > 0 || reconfigure_pending > 0)
5c5783a2 981 setSocketShutdownLifetimes(Config.shutdownLifetime);
f3753518 982 else
983 setSocketShutdownLifetimes(0);
30a4f2a8 984 }
4d64d74a 985 nfds = 0;
429fdbec 986 maxfd = Biggest_FD + 1;
4d64d74a 987 for (i = 0; i < maxfd; i++) {
090089c4 988 /* Check each open socket for a handler. */
ab1afadb 989 if (fd_table[i].stall_until > squid_curtime)
990 continue;
991 if (fd_table[i].read_handler) {
4d64d74a 992 nfds++;
090089c4 993 FD_SET(i, &readfds);
4d64d74a 994 }
995 if (fd_table[i].write_handler) {
996 nfds++;
090089c4 997 FD_SET(i, &writefds);
4d64d74a 998 }
090089c4 999 }
bbdb774b 1000 if (shutdown_pending || reconfigure_pending)
a3d5953d 1001 debug(5, 2) ("comm_select: Still waiting on %d FDs\n", nfds);
4d64d74a 1002 if (nfds == 0)
1003 return COMM_SHUTDOWN;
0a0bf5db 1004#if USE_ASYNC_IO
1005 aioCheckCallbacks();
1006#endif
7690e8eb 1007 for (;;) {
89fb2544 1008 poll_time.tv_sec = sec > 0 ? 1 : 0;
090089c4 1009 poll_time.tv_usec = 0;
d0217c9b 1010 num = select(maxfd, &readfds, &writefds, NULL, &poll_time);
429fdbec 1011 select_loops++;
090089c4 1012 if (num >= 0)
1013 break;
4d64d74a 1014 if (errno == EINTR)
1015 break;
a3d5953d 1016 debug(50, 0) ("comm_select: select failure: %s\n",
30a4f2a8 1017 xstrerror());
d0217c9b 1018 examine_select(&readfds, &writefds);
bf9f8f2b 1019 return COMM_ERROR;
30a4f2a8 1020 /* NOTREACHED */
090089c4 1021 }
4d64d74a 1022 if (num < 0)
1023 continue;
a3d5953d 1024 debug(5, num ? 5 : 8) ("comm_select: %d sockets ready at %d\n",
30a4f2a8 1025 num, (int) squid_curtime);
090089c4 1026
1027 /* Check lifetime and timeout handlers ONCE each second.
1028 * Replaces brain-dead check every time through the loop! */
b8de7ebe 1029 if (squid_curtime > last_timeout) {
1030 last_timeout = squid_curtime;
090089c4 1031 checkTimeouts();
090089c4 1032 }
7d49daab 1033 if (num == 0)
1034 continue;
1035
090089c4 1036 /* scan each socket but the accept socket. Poll this
2c5294ce 1037 * more frequently to minimize losses due to the 5 connect
090089c4 1038 * limit in SunOS */
1039
5742d7c9 1040 for (fd = 0; fd < maxfd; fd++) {
d0217c9b 1041 if (!FD_ISSET(fd, &readfds) && !FD_ISSET(fd, &writefds))
7d49daab 1042 continue;
cb2f803a 1043 if ((incoming_counter++ & (lastinc > 0 ? 1 : 7)) == 0)
1044 comm_select_incoming();
812ed90c 1045 if (fdIsHttpOrIcp(fd))
7d49daab 1046 continue;
7d49daab 1047 if (FD_ISSET(fd, &readfds)) {
a3d5953d 1048 debug(5, 6) ("comm_select: FD %d ready for reading\n", fd);
7d49daab 1049 if (fd_table[fd].read_handler) {
ff8d0ea6 1050 hdl = fd_table[fd].read_handler;
76f87348 1051 fd_table[fd].read_handler = NULL;
ff8d0ea6 1052 hdl(fd, fd_table[fd].read_data);
090089c4 1053 }
7d49daab 1054 }
1055 if (FD_ISSET(fd, &writefds)) {
a3d5953d 1056 debug(5, 5) ("comm_select: FD %d ready for writing\n", fd);
7d49daab 1057 if (fd_table[fd].write_handler) {
ff8d0ea6 1058 hdl = fd_table[fd].write_handler;
76f87348 1059 fd_table[fd].write_handler = NULL;
ff8d0ea6 1060 hdl(fd, fd_table[fd].write_data);
090089c4 1061 }
7d49daab 1062 }
97c03d3c 1063 lastinc = polledinc;
090089c4 1064 }
7d49daab 1065 return COMM_OK;
97c03d3c 1066 } while (timeout > squid_curtime);
a3d5953d 1067 debug(5, 8) ("comm_select: time out: %d.\n", squid_curtime);
090089c4 1068 return COMM_TIMEOUT;
1069}
dcfe6390 1070#endif
090089c4 1071
b8d8561b 1072void
582b6456 1073commSetSelect(int fd, unsigned int type, PF * handler, void *client_data, time_t timeout)
090089c4 1074{
89de058c 1075 fde *F = &fd_table[fd];
489b22c1 1076 assert(fd >= 0);
89de058c 1077 assert(F->open == FD_OPEN);
62607543 1078 debug(5, 5) ("commSetSelect: FD %d, type=%d, handler=%p, data=%p\n", fd, type, handler, client_data);
090089c4 1079 if (type & COMM_SELECT_READ) {
76f87348 1080 F->read_handler = handler;
1081 F->read_data = client_data;
090089c4 1082 }
1083 if (type & COMM_SELECT_WRITE) {
76f87348 1084 F->write_handler = handler;
1085 F->write_data = client_data;
090089c4 1086 }
5c5783a2 1087 if (timeout)
76f87348 1088 F->timeout = squid_curtime + timeout;
090089c4 1089}
1090
b8d8561b 1091void
582b6456 1092comm_add_close_handler(int fd, PF * handler, void *data)
30a4f2a8 1093{
f1dc9b30 1094 close_handler *new = xmalloc(sizeof(*new));
a3d5953d 1095 debug(5, 5) ("comm_add_close_handler: FD %d, handler=%p, data=%p\n",
e0c42e90 1096 fd, handler, data);
30a4f2a8 1097 new->handler = handler;
1098 new->data = data;
1099 new->next = fd_table[fd].close_handler;
1100 fd_table[fd].close_handler = new;
1101}
1102
b8d8561b 1103void
582b6456 1104comm_remove_close_handler(int fd, PF * handler, void *data)
090089c4 1105{
f1dc9b30 1106 close_handler *p;
1107 close_handler *last = NULL;
30a4f2a8 1108 /* Find handler in list */
1109 for (p = fd_table[fd].close_handler; p != NULL; last = p, p = p->next)
1110 if (p->handler == handler && p->data == data)
1111 break; /* This is our handler */
f88211e8 1112 assert(p != NULL);
30a4f2a8 1113 /* Remove list entry */
1114 if (last)
1115 last->next = p->next;
1116 else
1117 fd_table[fd].close_handler = p->next;
1118 safe_free(p);
1119}
090089c4 1120
b8d8561b 1121static void
1122commSetNoLinger(int fd)
30a4f2a8 1123{
1124 struct linger L;
090089c4 1125 L.l_onoff = 0; /* off */
1126 L.l_linger = 0;
30a4f2a8 1127 if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *) &L, sizeof(L)) < 0)
a3d5953d 1128 debug(50, 0) ("commSetNoLinger: FD %d: %s\n", fd, xstrerror());
090089c4 1129}
1130
b8d8561b 1131static void
1132commSetReuseAddr(int fd)
090089c4 1133{
1134 int on = 1;
30a4f2a8 1135 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)) < 0)
a3d5953d 1136 debug(50, 1) ("commSetReuseAddr: FD %d: %s\n", fd, xstrerror());
090089c4 1137}
1138
b8d8561b 1139static void
1140commSetTcpRcvbuf(int fd, int size)
f868539a 1141{
1142 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *) &size, sizeof(size)) < 0)
a3d5953d 1143 debug(50, 1) ("commSetTcpRcvbuf: FD %d, SIZE %d: %s\n",
b6f794d6 1144 fd, size, xstrerror());
f868539a 1145}
1146
b8d8561b 1147int
1148commSetNonBlocking(int fd)
30a4f2a8 1149{
731e4d49 1150 int flags;
9e205701 1151 int dummy = 0;
95cf2361 1152 if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) {
a3d5953d 1153 debug(50, 0) ("FD %d: fcntl F_GETFL: %s\n", fd, xstrerror());
731e4d49 1154 return COMM_ERROR;
1155 }
4f92c80c 1156 if (fcntl(fd, F_SETFL, flags | SQUID_NONBLOCK) < 0) {
a3d5953d 1157 debug(50, 0) ("commSetNonBlocking: FD %d: %s\n", fd, xstrerror());
30a4f2a8 1158 return COMM_ERROR;
090089c4 1159 }
090089c4 1160 return 0;
1161}
1162
b8d8561b 1163void
1164commSetCloseOnExec(int fd)
3ca60c86 1165{
1166#ifdef FD_CLOEXEC
731e4d49 1167 int flags;
1168 if ((flags = fcntl(fd, F_GETFL)) < 0) {
a3d5953d 1169 debug(50, 0) ("FD %d: fcntl F_GETFL: %s\n", fd, xstrerror());
24382924 1170 return;
3ca60c86 1171 }
24382924 1172 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0)
a3d5953d 1173 debug(50, 0) ("FD %d: set close-on-exec failed: %s\n", fd, xstrerror());
3ca60c86 1174#endif
1175}
1176
e90100aa 1177#ifdef TCP_NODELAY
1178static void
1179commSetTcpNoDelay(int fd)
1180{
1181 int on = 1;
1182 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &on, sizeof(on)) < 0)
a3d5953d 1183 debug(50, 1) ("commSetTcpNoDelay: FD %d: %s\n", fd, xstrerror());
e90100aa 1184}
1185#endif
1186
b8d8561b 1187int
0673c0ba 1188comm_init(void)
090089c4 1189{
f1dc9b30 1190 fd_table = xcalloc(Squid_MaxFD, sizeof(fde));
1191 meta_data.misc += Squid_MaxFD * sizeof(fde);
090089c4 1192 /* Keep a few file descriptors free so that we don't run out of FD's
1193 * after accepting a client but before it opens a socket or a file.
e83892e9 1194 * Since Squid_MaxFD can be as high as several thousand, don't waste them */
1195 RESERVED_FD = min(100, Squid_MaxFD / 4);
090089c4 1196 /* hardwired lifetimes */
e83892e9 1197 meta_data.misc += Squid_MaxFD * sizeof(int);
055f4d4d 1198 zero_tv.tv_sec = 0;
1199 zero_tv.tv_usec = 0;
090089c4 1200 return 0;
1201}
1202
1203
f88211e8 1204#if !HAVE_POLL
090089c4 1205/*
1206 * examine_select - debug routine.
1207 *
1208 * I spend the day chasing this core dump that occurs when both the client
1209 * and the server side of a cache fetch simultaneoulsy abort the
1210 * connection. While I haven't really studied the code to figure out how
1211 * it happens, the snippet below may prevent the cache from exitting:
1212 *
1213 * Call this from where the select loop fails.
1214 */
b8d8561b 1215static int
5742d7c9 1216examine_select(fd_set * readfds, fd_set * writefds)
090089c4 1217{
1218 int fd = 0;
bbc5ea8f 1219 fd_set read_x;
1220 fd_set write_x;
090089c4 1221 int num;
1222 struct timeval tv;
f1dc9b30 1223 close_handler *ch = NULL;
1224 close_handler *next = NULL;
76f87348 1225 fde *F = NULL;
090089c4 1226
a3d5953d 1227 debug(5, 0) ("examine_select: Examining open file descriptors...\n");
e83892e9 1228 for (fd = 0; fd < Squid_MaxFD; fd++) {
090089c4 1229 FD_ZERO(&read_x);
1230 FD_ZERO(&write_x);
090089c4 1231 tv.tv_sec = tv.tv_usec = 0;
af00901c 1232 if (FD_ISSET(fd, readfds))
090089c4 1233 FD_SET(fd, &read_x);
af00901c 1234 else if (FD_ISSET(fd, writefds))
1235 FD_SET(fd, &write_x);
af00901c 1236 else
1237 continue;
e83892e9 1238 num = select(Squid_MaxFD, &read_x, &write_x, NULL, &tv);
af00901c 1239 if (num > -1) {
a3d5953d 1240 debug(5, 5) ("FD %d is valid.\n", fd);
af00901c 1241 continue;
1242 }
76f87348 1243 F = &fd_table[fd];
a3d5953d 1244 debug(5, 0) ("FD %d: %s\n", fd, xstrerror());
1245 debug(5, 0) ("WARNING: FD %d has handlers, but it's invalid.\n", fd);
1246 debug(5, 0) ("FD %d is a %s called '%s'\n",
ca98227c 1247 fd,
95d15928 1248 fdstatTypeStr[fd_table[fd].type],
76f87348 1249 F->desc);
a3d5953d 1250 debug(5, 0) ("tmout:%p read:%p write:%p\n",
76f87348 1251 F->timeout_handler,
1252 F->read_handler,
1253 F->write_handler);
1254 for (ch = F->close_handler; ch; ch = ch->next)
a3d5953d 1255 debug(5, 0) (" close handler: %p\n", ch->handler);
76f87348 1256 if (F->close_handler) {
1257 for (ch = F->close_handler; ch; ch = next) {
af00901c 1258 next = ch->next;
1259 ch->handler(fd, ch->data);
1260 safe_free(ch);
090089c4 1261 }
76f87348 1262 } else if (F->timeout_handler) {
a3d5953d 1263 debug(5, 0) ("examine_select: Calling Timeout Handler\n");
76f87348 1264 F->timeout_handler(fd, F->timeout_data);
090089c4 1265 }
76f87348 1266 F->close_handler = NULL;
1267 F->timeout_handler = NULL;
1268 F->read_handler = NULL;
1269 F->write_handler = NULL;
af00901c 1270 FD_CLR(fd, readfds);
1271 FD_CLR(fd, writefds);
090089c4 1272 }
090089c4 1273 return 0;
1274}
dcfe6390 1275#endif
090089c4 1276
b8d8561b 1277static void
0673c0ba 1278checkTimeouts(void)
090089c4 1279{
1280 int fd;
76f87348 1281 fde *F = NULL;
5c5783a2 1282 PF *callback;
429fdbec 1283 for (fd = 0; fd <= Biggest_FD; fd++) {
76f87348 1284 F = &fd_table[fd];
1285 if (F->open != FD_OPEN)
429fdbec 1286 continue;
76f87348 1287 if (F->timeout == 0)
30a4f2a8 1288 continue;
76f87348 1289 if (F->timeout > squid_curtime)
30a4f2a8 1290 continue;
a3d5953d 1291 debug(5, 5) ("checkTimeouts: FD %d Expired\n", fd);
76f87348 1292 if (F->timeout_handler) {
a3d5953d 1293 debug(5, 5) ("checkTimeouts: FD %d: Call timeout handler\n", fd);
76f87348 1294 callback = F->timeout_handler;
1295 F->timeout_handler = NULL;
1296 callback(fd, F->timeout_data);
30a4f2a8 1297 } else {
a3d5953d 1298 debug(5, 5) ("checkTimeouts: FD %d: Forcing comm_close()\n", fd);
30a4f2a8 1299 comm_close(fd);
090089c4 1300 }
1301 }
1302}
1303
30a4f2a8 1304/* Write to FD. */
b8d8561b 1305static void
582b6456 1306commHandleWrite(int fd, void *data)
30a4f2a8 1307{
f17936ab 1308 CommWriteStateData *state = data;
30a4f2a8 1309 int len = 0;
1310 int nleft;
1311
a3d5953d 1312 debug(5, 5) ("commHandleWrite: FD %d: state=%p, off %d, sz %d.\n",
30a4f2a8 1313 fd, state, state->offset, state->size);
1314
1315 nleft = state->size - state->offset;
1316 len = write(fd, state->buf + state->offset, nleft);
b69f7771 1317 fd_bytes(fd, len, FD_WRITE);
30a4f2a8 1318
1319 if (len == 0) {
1320 /* Note we even call write if nleft == 0 */
1321 /* We're done */
1322 if (nleft != 0)
02be0294 1323 debug(5, 1) ("commHandleWrite: FD %d: write failure: connection closed with %d bytes remaining.\n", fd, nleft);
f17936ab 1324 CommWriteStateCallbackAndFree(fd, nleft ? COMM_ERROR : COMM_OK);
30a4f2a8 1325 } else if (len < 0) {
1326 /* An error */
0a0bf5db 1327 if (errno == EWOULDBLOCK || errno == EAGAIN || errno == EINTR) {
a3d5953d 1328 debug(50, 10) ("commHandleWrite: FD %d: write failure: %s.\n",
30a4f2a8 1329 fd, xstrerror());
b177367b 1330 commSetSelect(fd,
30a4f2a8 1331 COMM_SELECT_WRITE,
cd1fb0eb 1332 commHandleWrite,
b177367b 1333 state,
85d7ea98 1334 0);
9864ee44 1335 } else {
a3d5953d 1336 debug(50, 2) ("commHandleWrite: FD %d: write failure: %s.\n",
9864ee44 1337 fd, xstrerror());
f17936ab 1338 CommWriteStateCallbackAndFree(fd, COMM_ERROR);
30a4f2a8 1339 }
30a4f2a8 1340 } else {
1341 /* A successful write, continue */
1342 state->offset += len;
1343 if (state->offset < state->size) {
1344 /* Not done, reinstall the write handler and write some more */
b177367b 1345 commSetSelect(fd,
30a4f2a8 1346 COMM_SELECT_WRITE,
cd1fb0eb 1347 commHandleWrite,
b177367b 1348 state,
85d7ea98 1349 0);
9864ee44 1350 } else {
f17936ab 1351 CommWriteStateCallbackAndFree(fd, COMM_OK);
30a4f2a8 1352 }
30a4f2a8 1353 }
1354}
1355
1356
1357
1358/* Select for Writing on FD, until SIZE bytes are sent. Call
1359 * * HANDLER when complete. */
b8d8561b 1360void
9e4ad609 1361comm_write(int fd, char *buf, int size, CWCB * handler, void *handler_data, FREE * free_func)
30a4f2a8 1362{
f17936ab 1363 CommWriteStateData *state = NULL;
a3d5953d 1364 debug(5, 5) ("comm_write: FD %d: sz %d: hndl %p: data %p.\n",
787869c5 1365 fd, size, handler, handler_data);
03eb2f01 1366 assert(fd_table[fd].rwstate == NULL);
f17936ab 1367 state = xcalloc(1, sizeof(CommWriteStateData));
30a4f2a8 1368 state->buf = buf;
1369 state->size = size;
1370 state->offset = 0;
1371 state->handler = handler;
30a4f2a8 1372 state->handler_data = handler_data;
86ee2017 1373 state->free = free_func;
a56a3abe 1374 fd_table[fd].rwstate = state;
1a8f5ed6 1375 cbdataLock(handler_data);
b177367b 1376 commSetSelect(fd,
30a4f2a8 1377 COMM_SELECT_WRITE,
cd1fb0eb 1378 commHandleWrite,
b177367b 1379 fd_table[fd].rwstate,
1380 0);
30a4f2a8 1381}