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