]> git.ipfire.org Git - thirdparty/squid.git/blame - src/fd.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / fd.cc
CommitLineData
d892b155 1/*
4ac4a490 2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
e25c139f 3 *
bbc27441
AJ
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
d892b155 7 */
bbc27441
AJ
8
9/* DEBUG: section 51 Filedescriptor Functions */
d892b155 10
582c2af2 11#include "squid.h"
d841c88d 12#include "comm/Loops.h"
582c2af2 13#include "Debug.h"
ed6e9fb9 14#include "fatal.h"
c4ad1349 15#include "fd.h"
528b2c61 16#include "fde.h"
582c2af2
FC
17#include "globals.h"
18#include "profiler/Profiler.h"
985c86bc 19#include "SquidTime.h"
a7870688 20
2a857320
AR
21// Solaris and possibly others lack MSG_NOSIGNAL optimization
22// TODO: move this into compat/? Use a dedicated compat file to avoid dragging
9a90d1dd 23// sys/socket.h into the rest of Squid??
2a857320
AR
24#ifndef MSG_NOSIGNAL
25#define MSG_NOSIGNAL 0
26#endif
27
1f7c9178 28int default_read_method(int, char *, int);
29int default_write_method(int, const char *, int);
7aa9bb3e 30#if _SQUID_WINDOWS_
0f15e632 31int socket_read_method(int, char *, int);
32int socket_write_method(int, const char *, int);
33int file_read_method(int, char *, int);
34int file_write_method(int, const char *, int);
1bac0258
AR
35#else
36int msghdr_read_method(int, char *, int);
37int msghdr_write_method(int, const char *, int);
0f15e632 38#endif
1f7c9178 39
26ac0430
AJ
40const char *fdTypeStr[] = {
41 "None",
42 "Log",
43 "File",
44 "Socket",
45 "Pipe",
1bac0258 46 "MsgHdr",
26ac0430
AJ
47 "Unknown"
48};
22f5d1ca 49
60c0b5a2 50static void fdUpdateBiggest(int fd, int);
a7870688 51
52static void
60c0b5a2 53fdUpdateBiggest(int fd, int opening)
a7870688 54{
55 if (fd < Biggest_FD)
62e76326 56 return;
57
edd1e84c 58 assert(fd < Squid_MaxFD);
62e76326 59
a7870688 60 if (fd > Biggest_FD) {
62e76326 61 /*
62 * assert that we are not closing a FD bigger than
63 * our known biggest FD
64 */
65 assert(opening);
66 Biggest_FD = fd;
67 return;
a7870688 68 }
62e76326 69
a7870688 70 /* if we are here, then fd == Biggest_FD */
60c0b5a2 71 /*
72 * assert that we are closing the biggest FD; we can't be
73 * re-opening it
74 */
75 assert(!opening);
62e76326 76
a8214fc7 77 while (Biggest_FD >= 0 && !fd_table[Biggest_FD].flags.open)
5e263176 78 --Biggest_FD;
a7870688 79}
80
81void
4f92c80c 82fd_close(int fd)
a7870688 83{
76f87348 84 fde *F = &fd_table[fd];
62e76326 85
78611e17 86 assert(fd >= 0);
be4d35dc 87 assert(F->flags.open);
78611e17 88
76f87348 89 if (F->type == FD_FILE) {
62e76326 90 assert(F->read_handler == NULL);
91 assert(F->write_handler == NULL);
9e4ad609 92 }
62e76326 93
bf8fe701 94 debugs(51, 3, "fd_close FD " << fd << " " << F->desc);
d841c88d
AJ
95 Comm::SetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
96 Comm::SetSelect(fd, COMM_SELECT_WRITE, NULL, NULL, 0);
be4d35dc 97 F->flags.open = false;
60c0b5a2 98 fdUpdateBiggest(fd, 0);
5e263176 99 --Number_FD;
33cc0629 100 F->clear();
a7870688 101}
102
7aa9bb3e 103#if _SQUID_WINDOWS_
0f15e632 104
105int
106socket_read_method(int fd, char *buf, int len)
107{
fe86a3bf 108 int i;
0242b721 109 PROF_start(recv);
fe86a3bf 110 i = recv(fd, (void *) buf, len, 0);
0242b721 111 PROF_stop(recv);
fe86a3bf 112 return i;
0f15e632 113}
114
115int
116file_read_method(int fd, char *buf, int len)
117{
fe86a3bf 118 int i;
119 PROF_start(read);
ce8db2ee 120 i = _read(fd, buf, len);
fe86a3bf 121 PROF_stop(read);
122 return i;
0f15e632 123}
124
125int
126socket_write_method(int fd, const char *buf, int len)
127{
fe86a3bf 128 int i;
129 PROF_start(send);
130 i = send(fd, (const void *) buf, len, 0);
131 PROF_stop(send);
132 return i;
0f15e632 133}
134
135int
136file_write_method(int fd, const char *buf, int len)
137{
0242b721 138 int i;
139 PROF_start(write);
140 i = (_write(fd, buf, len));
141 PROF_stop(write);
142 return i;
0f15e632 143}
62e76326 144
0f15e632 145#else
1f7c9178 146int
147default_read_method(int fd, char *buf, int len)
148{
fe86a3bf 149 int i;
150 PROF_start(read);
151 i = read(fd, buf, len);
152 PROF_stop(read);
153 return i;
1f7c9178 154}
155
156int
157default_write_method(int fd, const char *buf, int len)
158{
fe86a3bf 159 int i;
160 PROF_start(write);
161 i = write(fd, buf, len);
162 PROF_stop(write);
163 return i;
1f7c9178 164}
62e76326 165
1bac0258 166int
ced8def3 167msghdr_read_method(int fd, char *buf, int)
1bac0258
AR
168{
169 PROF_start(read);
170 const int i = recvmsg(fd, reinterpret_cast<msghdr*>(buf), MSG_DONTWAIT);
171 PROF_stop(read);
172 return i;
173}
174
175int
176msghdr_write_method(int fd, const char *buf, int len)
177{
178 PROF_start(write);
179 const int i = sendmsg(fd, reinterpret_cast<const msghdr*>(buf), MSG_NOSIGNAL);
180 PROF_stop(write);
181 return i > 0 ? len : i; // len is imprecise but the caller expects a match
182}
183
0f15e632 184#endif
1f7c9178 185
a7870688 186void
187fd_open(int fd, unsigned int type, const char *desc)
188{
4bc3a4cd 189 fde *F;
ceab4c00 190 assert(fd >= 0);
4bc3a4cd 191 F = &fd_table[fd];
62e76326 192
7197b20d 193 if (F->flags.open) {
e0236918 194 debugs(51, DBG_IMPORTANT, "WARNING: Closing open FD " << std::setw(4) << fd);
62e76326 195 fd_close(fd);
6cf028ab 196 }
62e76326 197
60c0b5a2 198 assert(!F->flags.open);
cc192b50 199 debugs(51, 3, "fd_open() FD " << fd << " " << desc);
76f87348 200 F->type = type;
be4d35dc 201 F->flags.open = true;
751406fe 202 F->epoll_state = 0;
7aa9bb3e 203#if _SQUID_WINDOWS_
62e76326 204
629b5f75 205 F->win32.handle = _get_osfhandle(fd);
206
0f15e632 207 switch (type) {
62e76326 208
0f15e632 209 case FD_SOCKET:
62e76326 210
0f15e632 211 case FD_PIPE:
62e76326 212 F->read_method = &socket_read_method;
213 F->write_method = &socket_write_method;
214 break;
215
0f15e632 216 case FD_FILE:
62e76326 217
0f15e632 218 case FD_LOG:
62e76326 219 F->read_method = &file_read_method;
220 F->write_method = &file_write_method;
221 break;
222
0f15e632 223 default:
62e76326 224 fatalf("fd_open(): unknown FD type - FD#: %i, type: %u, desc %s\n", fd, type, desc);
0f15e632 225 }
62e76326 226
0f15e632 227#else
1bac0258 228 switch (type) {
62e76326 229
1bac0258
AR
230 case FD_MSGHDR:
231 F->read_method = &msghdr_read_method;
232 F->write_method = &msghdr_write_method;
233 break;
234
235 default:
236 F->read_method = &default_read_method;
237 F->write_method = &default_write_method;
238 break;
239 }
62e76326 240
0f15e632 241#endif
62e76326 242
60c0b5a2 243 fdUpdateBiggest(fd, 1);
62e76326 244
528feb29 245 fd_note(fd, desc);
62e76326 246
95dc7ff4 247 ++Number_FD;
a7870688 248}
249
250void
251fd_note(int fd, const char *s)
252{
76f87348 253 fde *F = &fd_table[fd];
528feb29
AD
254 if (s)
255 xstrncpy(F->desc, s, FD_DESC_SZ);
256 else
257 *(F->desc) = 0; // ""-string
a7870688 258}
4f92c80c 259
260void
261fd_bytes(int fd, int len, unsigned int type)
262{
76f87348 263 fde *F = &fd_table[fd];
62e76326 264
4f92c80c 265 if (len < 0)
62e76326 266 return;
267
365e5b34 268 assert(type == FD_READ || type == FD_WRITE);
62e76326 269
4f92c80c 270 if (type == FD_READ)
62e76326 271 F->bytes_read += len;
4f92c80c 272 else
62e76326 273 F->bytes_written += len;
4f92c80c 274}
f17936ab 275
9e4ad609 276void
277fdDumpOpen(void)
278{
279 int i;
76f87348 280 fde *F;
62e76326 281
95dc7ff4 282 for (i = 0; i < Squid_MaxFD; ++i) {
62e76326 283 F = &fd_table[i];
284
285 if (!F->flags.open)
286 continue;
287
288 if (i == fileno(debug_log))
289 continue;
290
e0236918 291 debugs(51, DBG_IMPORTANT, "Open FD "<< std::left<< std::setw(10) <<
bf8fe701 292 (F->bytes_read && F->bytes_written ? "READ/WRITE" :
26ac0430 293 F->bytes_read ? "READING" : F->bytes_written ? "WRITING" :
86905e1f 294 "UNSTARTED") <<
bf8fe701 295 " "<< std::right << std::setw(4) << i << " " << F->desc);
9e4ad609 296 }
f17936ab 297}
22f5d1ca 298
299int
300fdNFree(void)
301{
b6a2f15e 302 return Squid_MaxFD - Number_FD - Opening_FD;
22f5d1ca 303}
4b23e114 304
a1ca9253 305int
306fdUsageHigh(void)
307{
308 int nrfree = fdNFree();
309
310 if (nrfree < (RESERVED_FD << 1))
311 return 1;
312
313 if (nrfree < (Number_FD >> 2))
314 return 1;
315
316 return 0;
317}
318
4b23e114 319/* Called when we runs out of file descriptors */
320void
321fdAdjustReserved(void)
322{
e6ccf245 323 int newReserve;
4b23e114 324 int x;
325 static time_t last = 0;
326 /*
327 * don't update too frequently
328 */
62e76326 329
4b23e114 330 if (last + 5 > squid_curtime)
62e76326 331 return;
332
4b23e114 333 /*
334 * Calculate a new reserve, based on current usage and a small extra
335 */
d85c3078 336 newReserve = Squid_MaxFD - Number_FD + min(25, Squid_MaxFD / 16);
62e76326 337
e6ccf245 338 if (newReserve <= RESERVED_FD)
62e76326 339 return;
340
d85c3078 341 x = Squid_MaxFD - 20 - min(25, Squid_MaxFD / 16);
62e76326 342
e6ccf245 343 if (newReserve > x) {
62e76326 344 /* perhaps this should be fatal()? -DW */
fa84c01d 345 debugs(51, DBG_CRITICAL, "WARNING: This machine has a serious shortage of filedescriptors.");
62e76326 346 newReserve = x;
4b23e114 347 }
62e76326 348
d85c3078 349 if (Squid_MaxFD - newReserve < min(256, Squid_MaxFD / 2))
26ac0430 350 fatalf("Too few filedescriptors available in the system (%d usable of %d).\n", Squid_MaxFD - newReserve, Squid_MaxFD);
375295eb 351
62a48c90
AJ
352 debugs(51, DBG_CRITICAL, "Reserved FD adjusted from " << RESERVED_FD << " to " << newReserve <<
353 " due to failures (" << (Squid_MaxFD - newReserve) << "/" << Squid_MaxFD << " file descriptors available)");
e6ccf245 354 RESERVED_FD = newReserve;
4b23e114 355}
f53969cc 356