]> git.ipfire.org Git - thirdparty/squid.git/blame - src/fd.cc
Build fails on Linux and FreeBSD (and probably on all others platforms)
[thirdparty/squid.git] / src / fd.cc
CommitLineData
be335c22 1
d892b155 2/*
a1ca9253 3 * $Id: fd.cc,v 1.50 2004/04/03 14:25:59 hno Exp $
d892b155 4 *
5 * DEBUG: section 51 Filedescriptor Functions
6 * AUTHOR: Duane Wessels
7 *
2b6662ba 8 * SQUID Web Proxy Cache http://www.squid-cache.org/
e25c139f 9 * ----------------------------------------------------------
d892b155 10 *
2b6662ba 11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
d892b155 19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
cbdec147 32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
e25c139f 33 *
d892b155 34 */
35
a7870688 36#include "squid.h"
528b2c61 37#include "fde.h"
a7870688 38
1f7c9178 39int default_read_method(int, char *, int);
40int default_write_method(int, const char *, int);
0f15e632 41#ifdef _SQUID_MSWIN_
42int socket_read_method(int, char *, int);
43int socket_write_method(int, const char *, int);
44int file_read_method(int, char *, int);
45int file_write_method(int, const char *, int);
46#endif
1f7c9178 47
22f5d1ca 48const char *fdTypeStr[] =
62e76326 49 {
50 "None",
51 "Log",
52 "File",
53 "Socket",
54 "Pipe",
55 "Unknown"
56 };
22f5d1ca 57
60c0b5a2 58static void fdUpdateBiggest(int fd, int);
a7870688 59
60static void
60c0b5a2 61fdUpdateBiggest(int fd, int opening)
a7870688 62{
63 if (fd < Biggest_FD)
62e76326 64 return;
65
edd1e84c 66 assert(fd < Squid_MaxFD);
62e76326 67
a7870688 68 if (fd > Biggest_FD) {
62e76326 69 /*
70 * assert that we are not closing a FD bigger than
71 * our known biggest FD
72 */
73 assert(opening);
74 Biggest_FD = fd;
75 return;
a7870688 76 }
62e76326 77
a7870688 78 /* if we are here, then fd == Biggest_FD */
60c0b5a2 79 /*
80 * assert that we are closing the biggest FD; we can't be
81 * re-opening it
82 */
83 assert(!opening);
62e76326 84
60c0b5a2 85 while (!fd_table[Biggest_FD].flags.open)
62e76326 86 Biggest_FD--;
a7870688 87}
88
89void
4f92c80c 90fd_close(int fd)
a7870688 91{
76f87348 92 fde *F = &fd_table[fd];
62e76326 93
76f87348 94 if (F->type == FD_FILE) {
62e76326 95 assert(F->read_handler == NULL);
96 assert(F->write_handler == NULL);
9e4ad609 97 }
62e76326 98
d8326d6b 99 debug(51, 3) ("fd_close FD %d %s\n", fd, F->desc);
1b3db6d9 100 commSetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
101 commSetSelect(fd, COMM_SELECT_WRITE, NULL, NULL, 0);
60c0b5a2 102 F->flags.open = 0;
103 fdUpdateBiggest(fd, 0);
2524a4d5 104 Number_FD--;
76f87348 105 memset(F, '\0', sizeof(fde));
106 F->timeout = 0;
a7870688 107}
108
0f15e632 109#ifdef _SQUID_MSWIN_
110
111int
112socket_read_method(int fd, char *buf, int len)
113{
114 return (recv(fd, buf, len, 0));
115}
116
117int
118file_read_method(int fd, char *buf, int len)
119{
120 return (_read(fd, buf, len));
121}
122
123int
124socket_write_method(int fd, const char *buf, int len)
125{
126 return (send(fd, buf, len, 0));
127}
128
129int
130file_write_method(int fd, const char *buf, int len)
131{
132 return (_write(fd, buf, len));
133}
62e76326 134
0f15e632 135#else
1f7c9178 136int
137default_read_method(int fd, char *buf, int len)
138{
139 return (read(fd, buf, len));
140}
141
142int
143default_write_method(int fd, const char *buf, int len)
144{
145 return (write(fd, buf, len));
146}
62e76326 147
0f15e632 148#endif
1f7c9178 149
a7870688 150void
151fd_open(int fd, unsigned int type, const char *desc)
152{
4bc3a4cd 153 fde *F;
ceab4c00 154 assert(fd >= 0);
4bc3a4cd 155 F = &fd_table[fd];
62e76326 156
7197b20d 157 if (F->flags.open) {
62e76326 158 debug(51, 1) ("WARNING: Closing open FD %4d\n", fd);
159 fd_close(fd);
6cf028ab 160 }
62e76326 161
60c0b5a2 162 assert(!F->flags.open);
a7c05555 163 debug(51, 3) ("fd_open FD %d %s\n", fd, desc);
76f87348 164 F->type = type;
60c0b5a2 165 F->flags.open = 1;
751406fe 166 F->epoll_state = 0;
0f15e632 167#ifdef _SQUID_MSWIN_
62e76326 168
0f15e632 169 switch (type) {
62e76326 170
0f15e632 171 case FD_SOCKET:
62e76326 172
0f15e632 173 case FD_PIPE:
62e76326 174 F->read_method = &socket_read_method;
175 F->write_method = &socket_write_method;
176 break;
177
0f15e632 178 case FD_FILE:
62e76326 179
0f15e632 180 case FD_LOG:
62e76326 181 F->read_method = &file_read_method;
182 F->write_method = &file_write_method;
183 break;
184
0f15e632 185 default:
62e76326 186 fatalf("fd_open(): unknown FD type - FD#: %i, type: %u, desc %s\n", fd, type, desc);
0f15e632 187 }
62e76326 188
0f15e632 189#else
1f7c9178 190 F->read_method = &default_read_method;
62e76326 191
1f7c9178 192 F->write_method = &default_write_method;
62e76326 193
0f15e632 194#endif
62e76326 195
60c0b5a2 196 fdUpdateBiggest(fd, 1);
62e76326 197
a7870688 198 if (desc)
62e76326 199 xstrncpy(F->desc, desc, FD_DESC_SZ);
200
2524a4d5 201 Number_FD++;
a7870688 202}
203
204void
205fd_note(int fd, const char *s)
206{
76f87348 207 fde *F = &fd_table[fd];
208 xstrncpy(F->desc, s, FD_DESC_SZ);
a7870688 209}
4f92c80c 210
211void
212fd_bytes(int fd, int len, unsigned int type)
213{
76f87348 214 fde *F = &fd_table[fd];
62e76326 215
4f92c80c 216 if (len < 0)
62e76326 217 return;
218
365e5b34 219 assert(type == FD_READ || type == FD_WRITE);
62e76326 220
4f92c80c 221 if (type == FD_READ)
62e76326 222 F->bytes_read += len;
4f92c80c 223 else
62e76326 224 F->bytes_written += len;
4f92c80c 225}
f17936ab 226
227void
228fdFreeMemory(void)
229{
9e4ad609 230 safe_free(fd_table);
231}
232
233void
234fdDumpOpen(void)
235{
236 int i;
76f87348 237 fde *F;
62e76326 238
9e4ad609 239 for (i = 0; i < Squid_MaxFD; i++) {
62e76326 240 F = &fd_table[i];
241
242 if (!F->flags.open)
243 continue;
244
245 if (i == fileno(debug_log))
246 continue;
247
248 debug(51, 1) ("Open FD %-10s %4d %s\n",
249 F->bytes_read && F->bytes_written ? "READ/WRITE" :
250 F->bytes_read ? "READING" :
251 F->bytes_written ? "WRITING" : null_string,
252 i, F->desc);
9e4ad609 253 }
f17936ab 254}
22f5d1ca 255
256int
257fdNFree(void)
258{
b6a2f15e 259 return Squid_MaxFD - Number_FD - Opening_FD;
22f5d1ca 260}
4b23e114 261
a1ca9253 262int
263fdUsageHigh(void)
264{
265 int nrfree = fdNFree();
266
267 if (nrfree < (RESERVED_FD << 1))
268 return 1;
269
270 if (nrfree < (Number_FD >> 2))
271 return 1;
272
273 return 0;
274}
275
4b23e114 276/* Called when we runs out of file descriptors */
277void
278fdAdjustReserved(void)
279{
e6ccf245 280 int newReserve;
4b23e114 281 int x;
282 static time_t last = 0;
283 /*
284 * don't update too frequently
285 */
62e76326 286
4b23e114 287 if (last + 5 > squid_curtime)
62e76326 288 return;
289
4b23e114 290 /*
291 * Calculate a new reserve, based on current usage and a small extra
292 */
e6ccf245 293 newReserve = Squid_MaxFD - Number_FD + XMIN(25, Squid_MaxFD / 16);
62e76326 294
e6ccf245 295 if (newReserve <= RESERVED_FD)
62e76326 296 return;
297
4b23e114 298 x = Squid_MaxFD - 20 - XMIN(25, Squid_MaxFD / 16);
62e76326 299
e6ccf245 300 if (newReserve > x) {
62e76326 301 /* perhaps this should be fatal()? -DW */
302 debug(51, 0) ("WARNING: This machine has a serious shortage of filedescriptors.\n");
303 newReserve = x;
4b23e114 304 }
62e76326 305
4b23e114 306 debug(51, 0) ("Reserved FD adjusted from %d to %d due to failures\n",
62e76326 307 RESERVED_FD, newReserve);
e6ccf245 308 RESERVED_FD = newReserve;
4b23e114 309}