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