master
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24#include "first.h"
26#if defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT)
28#define T518_SAFETY_MARGIN 16
30#define NUM_OPEN (FD_SETSIZE + 10)
31#define NUM_NEEDED (NUM_OPEN + T518_SAFETY_MARGIN)
33#if defined(_WIN32) || defined(MSDOS)
34#define DEV_NULL "NUL"
35#else
36#define DEV_NULL "/dev/null"
37#endif
39static int *t518_testfd = NULL;
40static struct rlimit t518_num_open;
41static char t518_msgbuff[256];
43static void t518_store_errmsg(const char *msg, int err)
44{
45 if(!err)
46 curl_msnprintf(t518_msgbuff, sizeof(t518_msgbuff), "%s", msg);
47 else {
48 char errbuf[STRERROR_LEN];
49 curl_msnprintf(t518_msgbuff, sizeof(t518_msgbuff), "%s, errno %d, %s", msg,
50 err, curlx_strerror(err, errbuf, sizeof(errbuf)));
51 }
52}
54static void t518_close_file_descriptors(void)
55{
56 for(t518_num_open.rlim_cur = 0;
57 t518_num_open.rlim_cur < t518_num_open.rlim_max;
58 t518_num_open.rlim_cur++)
59 if(t518_testfd[t518_num_open.rlim_cur] > 0)
60 curlx_close(t518_testfd[t518_num_open.rlim_cur]);
61 curlx_safefree(t518_testfd);
62}
64static int t518_fopen_works(void)
65{
66 FILE *fpa[3];
67 int i;
68 int ret = 1;
70 for(i = 0; i < 3; i++) {
71 fpa[i] = NULL;
72 }
73 for(i = 0; i < 3; i++) {
74 fpa[i] = curlx_fopen(DEV_NULL, FOPEN_READTEXT);
75 if(!fpa[i]) {
76 t518_store_errmsg("fopen failed", errno);
77 curl_mfprintf(stderr, "%s\n", t518_msgbuff);
78 ret = 0;
79 break;
80 }
81 }
82 for(i = 0; i < 3; i++) {
83 if(fpa[i])
84 curlx_fclose(fpa[i]);
85 }
86 return ret;
87}
89static int t518_test_rlimit(int keep_open)
90{
91 rlim_t nitems, i;
92 int *memchunk = NULL;
93 struct rlimit rl;
94 char strbuff[256];
95 char strbuff1[81];
96 char strbuff2[81];
98 /* get initial open file limits */
100 if(getrlimit(RLIMIT_NOFILE, &rl)) {
101 t518_store_errmsg("getrlimit() failed", errno);
102 curl_mfprintf(stderr, "%s\n", t518_msgbuff);
103 return -1;
104 }
106 /* show initial open file limits */
108 tutil_rlim2str(strbuff, sizeof(strbuff), rl.rlim_cur);
109 curl_mfprintf(stderr, "initial soft limit: %s\n", strbuff);
111 tutil_rlim2str(strbuff, sizeof(strbuff), rl.rlim_max);
112 curl_mfprintf(stderr, "initial hard limit: %s\n", strbuff);
114 /* show our constants */
116 curl_mfprintf(stderr, "test518 FD_SETSIZE: %d\n", FD_SETSIZE);
117 curl_mfprintf(stderr, "test518 NUM_OPEN : %d\n", NUM_OPEN);
118 curl_mfprintf(stderr, "test518 NUM_NEEDED: %d\n", NUM_NEEDED);
120 /*
121 * if soft limit and hard limit are different we ask the
122 * system to raise soft limit all the way up to the hard
123 * limit. Due to some other system limit the soft limit
124 * might not be raised up to the hard limit. So from this
125 * point the resulting soft limit is our limit. Trying to
126 * open more than soft limit file descriptors does fail.
127 */
129 if(rl.rlim_cur != rl.rlim_max) {
131#ifdef OPEN_MAX
132 if((rl.rlim_cur > 0) &&
133 (rl.rlim_cur < OPEN_MAX)) {
134 curl_mfprintf(stderr, "raising soft limit up to OPEN_MAX\n");
135 rl.rlim_cur = OPEN_MAX;
136 if(setrlimit(RLIMIT_NOFILE, &rl)) {
137 /* on failure do not abort, only issue a warning */
138 t518_store_errmsg("setrlimit() failed", errno);
139 curl_mfprintf(stderr, "%s\n", t518_msgbuff);
140 t518_msgbuff[0] = '\0';
141 }
142 }
143#endif
145 curl_mfprintf(stderr, "raising soft limit up to hard limit\n");
146 rl.rlim_cur = rl.rlim_max;
147 if(setrlimit(RLIMIT_NOFILE, &rl)) {
148 /* on failure do not abort, only issue a warning */
149 t518_store_errmsg("setrlimit() failed", errno);
150 curl_mfprintf(stderr, "%s\n", t518_msgbuff);
151 t518_msgbuff[0] = '\0';
152 }
154 /* get current open file limits */
156 if(getrlimit(RLIMIT_NOFILE, &rl)) {
157 t518_store_errmsg("getrlimit() failed", errno);
158 curl_mfprintf(stderr, "%s\n", t518_msgbuff);
159 return -3;
160 }
162 /* show current open file limits */
164 tutil_rlim2str(strbuff, sizeof(strbuff), rl.rlim_cur);
165 curl_mfprintf(stderr, "current soft limit: %s\n", strbuff);
167 tutil_rlim2str(strbuff, sizeof(strbuff), rl.rlim_max);
168 curl_mfprintf(stderr, "current hard limit: %s\n", strbuff);
170 } /* (rl.rlim_cur != rl.rlim_max) */
172 /*
173 * test 518 is all about testing libcurl functionality
174 * when more than FD_SETSIZE file descriptors are open.
175 * This means that if for any reason we are not able to
176 * open more than FD_SETSIZE file descriptors then test
177 * 518 should not be run.
178 */
180 /*
181 * verify that soft limit is higher than NUM_NEEDED,
182 * which is the number of file descriptors we would
183 * try to open plus T518_SAFETY_MARGIN to not exhaust the
184 * file descriptor pool
185 */
187 t518_num_open.rlim_cur = NUM_NEEDED;
189 if((rl.rlim_cur > 0) &&
190#ifdef RLIM_INFINITY
191 (rl.rlim_cur != RLIM_INFINITY) &&
192#endif
193 (rl.rlim_cur <= t518_num_open.rlim_cur)) {
194 tutil_rlim2str(strbuff2, sizeof(strbuff2), rl.rlim_cur);
195 tutil_rlim2str(strbuff1, sizeof(strbuff1), t518_num_open.rlim_cur);
196 curl_msnprintf(strbuff, sizeof(strbuff), "fds needed %s > system limit %s",
197 strbuff1, strbuff2);
198 t518_store_errmsg(strbuff, 0);
199 curl_mfprintf(stderr, "%s\n", t518_msgbuff);
200 return -4;
201 }
203 /*
204 * reserve a chunk of memory before opening file descriptors to
205 * avoid a low memory condition once the file descriptors are
206 * open. System conditions that could make the test fail should
207 * be addressed in the precheck phase. This chunk of memory shall
208 * be always curlx_free()ed before exiting the t518_test_rlimit()
209 * function so that it becomes available to the test.
210 */
212 for(nitems = i = 1; nitems <= i; i *= 2)
213 nitems = i;
214 if(nitems > 0x7fff)
215 nitems = 0x40000;
216 do {
217 t518_num_open.rlim_max = sizeof(*memchunk) * nitems;
218 tutil_rlim2str(strbuff, sizeof(strbuff), t518_num_open.rlim_max);
219 curl_mfprintf(stderr, "allocating memchunk %s byte array\n", strbuff);
220 memchunk = curlx_malloc(sizeof(*memchunk) * (size_t)nitems);
221 if(!memchunk) {
222 curl_mfprintf(stderr, "memchunk, malloc() failed\n");
223 nitems /= 2;
224 }
225 } while(nitems && !memchunk);
226 if(!memchunk) {
227 t518_store_errmsg("memchunk, malloc() failed", errno);
228 curl_mfprintf(stderr, "%s\n", t518_msgbuff);
229 return -5;
230 }
232 /* initialize it to fight lazy allocation */
234 curl_mfprintf(stderr, "initializing memchunk array\n");
236 for(i = 0; i < nitems; i++)
237 memchunk[i] = -1;
239 /* set the number of file descriptors we try to open */
241 t518_num_open.rlim_max = NUM_OPEN;
243 /* verify that we do not overflow size_t in curlx_malloc() */
245 if((size_t)t518_num_open.rlim_max > ((size_t)-1) / sizeof(*t518_testfd)) {
246 tutil_rlim2str(strbuff1, sizeof(strbuff1), t518_num_open.rlim_max);
247 curl_msnprintf(strbuff, sizeof(strbuff),
248 "unable to allocate an array for %s "
249 "file descriptors, would overflow size_t", strbuff1);
250 t518_store_errmsg(strbuff, 0);
251 curl_mfprintf(stderr, "%s\n", t518_msgbuff);
252 curlx_free(memchunk);
253 return -6;
254 }
256 /* allocate array for file descriptors */
258 tutil_rlim2str(strbuff, sizeof(strbuff), t518_num_open.rlim_max);
259 curl_mfprintf(stderr, "allocating array for %s file descriptors\n", strbuff);
261 t518_testfd = curlx_malloc(sizeof(*t518_testfd) *
262 (size_t)t518_num_open.rlim_max);
263 if(!t518_testfd) {
264 t518_store_errmsg("testfd, malloc() failed", errno);
265 curl_mfprintf(stderr, "%s\n", t518_msgbuff);
266 curlx_free(memchunk);
267 return -7;
268 }
270 /* initialize it to fight lazy allocation */
272 curl_mfprintf(stderr, "initializing testfd array\n");
274 for(t518_num_open.rlim_cur = 0;
275 t518_num_open.rlim_cur < t518_num_open.rlim_max;
276 t518_num_open.rlim_cur++)
277 t518_testfd[t518_num_open.rlim_cur] = -1;
279 tutil_rlim2str(strbuff, sizeof(strbuff), t518_num_open.rlim_max);
280 curl_mfprintf(stderr, "trying to open %s file descriptors\n", strbuff);
282 /* open a dummy descriptor */
284 t518_testfd[0] = curlx_open(DEV_NULL, O_RDONLY);
285 if(t518_testfd[0] < 0) {
286 curl_msnprintf(strbuff, sizeof(strbuff), "opening of %s failed", DEV_NULL);
287 t518_store_errmsg(strbuff, errno);
288 curl_mfprintf(stderr, "%s\n", t518_msgbuff);
289 curlx_safefree(t518_testfd);
290 curlx_free(memchunk);
291 return -8;
292 }
294 /* create a bunch of file descriptors */
296 for(t518_num_open.rlim_cur = 1;
297 t518_num_open.rlim_cur < t518_num_open.rlim_max;
298 t518_num_open.rlim_cur++) {
300 t518_testfd[t518_num_open.rlim_cur] = dup(t518_testfd[0]);
302 if(t518_testfd[t518_num_open.rlim_cur] < 0) {
304 t518_testfd[t518_num_open.rlim_cur] = -1;
306 tutil_rlim2str(strbuff1, sizeof(strbuff1), t518_num_open.rlim_cur);
307 curl_msnprintf(strbuff, sizeof(strbuff), "dup() attempt %s failed",
308 strbuff1);
309 curl_mfprintf(stderr, "%s\n", strbuff);
311 tutil_rlim2str(strbuff1, sizeof(strbuff1), t518_num_open.rlim_cur);
312 curl_msnprintf(strbuff, sizeof(strbuff),
313 "fds system limit seems close to %s", strbuff1);
314 curl_mfprintf(stderr, "%s\n", strbuff);
316 t518_num_open.rlim_max = NUM_NEEDED;
318 tutil_rlim2str(strbuff2, sizeof(strbuff2), t518_num_open.rlim_max);
319 tutil_rlim2str(strbuff1, sizeof(strbuff1), t518_num_open.rlim_cur);
320 curl_msnprintf(strbuff, sizeof(strbuff),
321 "fds needed %s > system limit %s", strbuff2, strbuff1);
322 t518_store_errmsg(strbuff, 0);
323 curl_mfprintf(stderr, "%s\n", t518_msgbuff);
325 for(t518_num_open.rlim_cur = 0;
326 t518_testfd[t518_num_open.rlim_cur] >= 0;
327 t518_num_open.rlim_cur++)
328 curlx_close(t518_testfd[t518_num_open.rlim_cur]);
329 curlx_safefree(t518_testfd);
330 curlx_free(memchunk);
331 return -9;
332 }
333 }
335 tutil_rlim2str(strbuff, sizeof(strbuff), t518_num_open.rlim_max);
336 curl_mfprintf(stderr, "%s file descriptors open\n", strbuff);
338#if !defined(HAVE_POLL) && !defined(USE_WINSOCK)
339 /*
340 * when using select() instead of poll() we cannot test
341 * libcurl functionality with a socket number equal or
342 * greater than FD_SETSIZE. In any case, macro VERIFY_SOCK
343 * in lib/select.c enforces this check and protects libcurl
344 * from a possible crash. The effect of this protection
345 * is that test 518 always fails, since the actual
346 * call to select() never takes place. We skip test 518
347 * with an indication that select limit would be exceeded.
348 */
350 t518_num_open.rlim_cur = FD_SETSIZE - T518_SAFETY_MARGIN;
351 if(t518_num_open.rlim_max > t518_num_open.rlim_cur) {
352 curl_msnprintf(strbuff, sizeof(strbuff), "select limit is FD_SETSIZE %d",
353 FD_SETSIZE);
354 t518_store_errmsg(strbuff, 0);
355 curl_mfprintf(stderr, "%s\n", t518_msgbuff);
356 t518_close_file_descriptors();
357 curlx_free(memchunk);
358 return -10;
359 }
361 t518_num_open.rlim_cur = FD_SETSIZE - T518_SAFETY_MARGIN;
362 for(rl.rlim_cur = 0;
363 rl.rlim_cur < t518_num_open.rlim_max;
364 rl.rlim_cur++) {
365 if((t518_testfd[rl.rlim_cur] > 0) &&
366 ((unsigned int)t518_testfd[rl.rlim_cur] > t518_num_open.rlim_cur)) {
367 curl_msnprintf(strbuff, sizeof(strbuff), "select limit is FD_SETSIZE %d",
368 FD_SETSIZE);
369 t518_store_errmsg(strbuff, 0);
370 curl_mfprintf(stderr, "%s\n", t518_msgbuff);
371 t518_close_file_descriptors();
372 curlx_free(memchunk);
373 return -11;
374 }
375 }
376#endif /* !HAVE_POLL && !USE_WINSOCK */
378 /*
379 * Old or 'backwards compatible' implementations of stdio do not allow
380 * handling of streams with an underlying file descriptor number greater
381 * than 255, even when allowing high numbered file descriptors for sockets.
382 * At this point we have a big number of file descriptors which have been
383 * opened using dup(), so lets test the stdio implementation and discover
384 * if it is capable of fopen()ing some additional files.
385 */
387 if(!t518_fopen_works()) {
388 tutil_rlim2str(strbuff1, sizeof(strbuff1), t518_num_open.rlim_max);
389 curl_msnprintf(strbuff, sizeof(strbuff), "fopen fails with %s fds open",
390 strbuff1);
391 curl_mfprintf(stderr, "%s\n", t518_msgbuff);
392 curl_msnprintf(strbuff, sizeof(strbuff),
393 "fopen fails with lots of fds open");
394 t518_store_errmsg(strbuff, 0);
395 t518_close_file_descriptors();
396 curlx_free(memchunk);
397 return -12;
398 }
400 /* free the chunk of memory we were reserving so that it
401 becomes available to the test */
403 curlx_free(memchunk);
405 /* close file descriptors unless instructed to keep them */
407 if(!keep_open) {
408 t518_close_file_descriptors();
409 }
411 return 0;
412}
414static CURLcode test_lib518(const char *URL)
415{
416 CURLcode result;
417 CURL *curl;
419 if(!strcmp(URL, "check")) {
420 /* used by the test script to ask if we can run this test or not */
421 if(t518_test_rlimit(FALSE)) {
422 curl_mfprintf(stdout, "test_rlimit problem: %s\n", t518_msgbuff);
423 return TEST_ERR_FAILURE;
424 }
425 return CURLE_OK; /* sure, run this! */
426 }
428 if(t518_test_rlimit(TRUE)) {
429 /* failure */
430 return TEST_ERR_MAJOR_BAD;
431 }
433 /* run the test with the bunch of open file descriptors
434 and close them all once the test is over */
436 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
437 curl_mfprintf(stderr, "curl_global_init() failed\n");
438 t518_close_file_descriptors();
439 return TEST_ERR_MAJOR_BAD;
440 }
442 curl = curl_easy_init();
443 if(!curl) {
444 curl_mfprintf(stderr, "curl_easy_init() failed\n");
445 t518_close_file_descriptors();
446 curl_global_cleanup();
447 return TEST_ERR_MAJOR_BAD;
448 }
450 easy_setopt(curl, CURLOPT_URL, URL);
451 easy_setopt(curl, CURLOPT_HEADER, 1L);
453 result = curl_easy_perform(curl);
455test_cleanup:
457 t518_close_file_descriptors();
458 curl_easy_cleanup(curl);
459 curl_global_cleanup();
461 return result;
462}
464#else /* HAVE_GETRLIMIT && HAVE_SETRLIMIT */
466static CURLcode test_lib518(const char *URL)
467{
468 (void)URL;
469 curl_mprintf("system lacks necessary system function(s)");
470 return TEST_ERR_MAJOR_BAD; /* skip test */
471}
473#endif /* HAVE_GETRLIMIT && HAVE_SETRLIMIT */
Diagnostics4 git calls · 587 ms
CallStatusSizeTime0587 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=582 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2004.3 KiB582 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=4406 git.response.cache=MISS git.response.duration_ms=582 git.fetch.wants=c751574f290002b8bec2979fe9e1ffeef05f0282 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=4406 git.pkt.payload.max=4385 git.sideband.band1.bytes=4384 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e334b21dd8f485
- Request
- 24bd1f08…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format