1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
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.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
24 #ifdef HAVE_SYS_RESOURCE_H
25 #include <sys/resource.h>
38 #error "this test requires FD_SETSIZE"
41 #define SAFETY_MARGIN (16)
42 #define NUM_OPEN (FD_SETSIZE + 10)
43 #define NUM_NEEDED (NUM_OPEN + SAFETY_MARGIN)
45 #if defined(WIN32) || defined(_WIN32) || defined(MSDOS)
46 #define DEV_NULL "NUL"
48 #define DEV_NULL "/dev/null"
51 #if defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT)
53 static int *fd = NULL;
54 static struct rlimit num_open;
55 static char msgbuff[256];
57 static void store_errmsg(const char *msg, int err)
60 snprintf(msgbuff, sizeof(msgbuff), "%s", msg);
62 snprintf(msgbuff, sizeof(msgbuff), "%s, errno %d, %s", msg,
66 static void close_file_descriptors(void)
68 for(num_open.rlim_cur = 0;
69 num_open.rlim_cur < num_open.rlim_max;
71 if(fd[num_open.rlim_cur] > 0)
72 close(fd[num_open.rlim_cur]);
77 static int fopen_works(void)
83 for(i = 0; i < 3; i++) {
86 for(i = 0; i < 3; i++) {
87 fpa[i] = fopen(DEV_NULL, FOPEN_READTEXT);
89 store_errmsg("fopen failed", ERRNO);
90 fprintf(stderr, "%s\n", msgbuff);
95 for(i = 0; i < 3; i++) {
102 static int rlimit(int keep_open)
105 int *memchunk = NULL;
112 char fmt_lu[] = "%lu";
114 char fmt_llu[] = "%llu";
116 if(sizeof(rl.rlim_max) > sizeof(long))
120 fmt = (sizeof(rl.rlim_max) < sizeof(long))?fmt_u:fmt_lu;
122 /* get initial open file limits */
124 if(getrlimit(RLIMIT_NOFILE, &rl) != 0) {
125 store_errmsg("getrlimit() failed", ERRNO);
126 fprintf(stderr, "%s\n", msgbuff);
130 /* show initial open file limits */
133 if(rl.rlim_cur == RLIM_INFINITY)
134 strcpy(strbuff, "INFINITY");
137 snprintf(strbuff, sizeof(strbuff), fmt, rl.rlim_cur);
138 fprintf(stderr, "initial soft limit: %s\n", strbuff);
141 if(rl.rlim_max == RLIM_INFINITY)
142 strcpy(strbuff, "INFINITY");
145 snprintf(strbuff, sizeof(strbuff), fmt, rl.rlim_max);
146 fprintf(stderr, "initial hard limit: %s\n", strbuff);
148 /* show our constants */
150 fprintf(stderr, "test518 FD_SETSIZE: %d\n", FD_SETSIZE);
151 fprintf(stderr, "test518 NUM_OPEN : %d\n", NUM_OPEN);
152 fprintf(stderr, "test518 NUM_NEEDED: %d\n", NUM_NEEDED);
155 * if soft limit and hard limit are different we ask the
156 * system to raise soft limit all the way up to the hard
157 * limit. Due to some other system limit the soft limit
158 * might not be raised up to the hard limit. So from this
159 * point the resulting soft limit is our limit. Trying to
160 * open more than soft limit file descriptors will fail.
163 if(rl.rlim_cur != rl.rlim_max) {
166 if((rl.rlim_cur > 0) &&
167 (rl.rlim_cur < OPEN_MAX)) {
168 fprintf(stderr, "raising soft limit up to OPEN_MAX\n");
169 rl.rlim_cur = OPEN_MAX;
170 if(setrlimit(RLIMIT_NOFILE, &rl) != 0) {
171 /* on failure don't abort just issue a warning */
172 store_errmsg("setrlimit() failed", ERRNO);
173 fprintf(stderr, "%s\n", msgbuff);
179 fprintf(stderr, "raising soft limit up to hard limit\n");
180 rl.rlim_cur = rl.rlim_max;
181 if(setrlimit(RLIMIT_NOFILE, &rl) != 0) {
182 /* on failure don't abort just issue a warning */
183 store_errmsg("setrlimit() failed", ERRNO);
184 fprintf(stderr, "%s\n", msgbuff);
188 /* get current open file limits */
190 if(getrlimit(RLIMIT_NOFILE, &rl) != 0) {
191 store_errmsg("getrlimit() failed", ERRNO);
192 fprintf(stderr, "%s\n", msgbuff);
196 /* show current open file limits */
199 if(rl.rlim_cur == RLIM_INFINITY)
200 strcpy(strbuff, "INFINITY");
203 snprintf(strbuff, sizeof(strbuff), fmt, rl.rlim_cur);
204 fprintf(stderr, "current soft limit: %s\n", strbuff);
207 if(rl.rlim_max == RLIM_INFINITY)
208 strcpy(strbuff, "INFINITY");
211 snprintf(strbuff, sizeof(strbuff), fmt, rl.rlim_max);
212 fprintf(stderr, "current hard limit: %s\n", strbuff);
214 } /* (rl.rlim_cur != rl.rlim_max) */
217 * test 518 is all about testing libcurl functionality
218 * when more than FD_SETSIZE file descriptors are open.
219 * This means that if for any reason we are not able to
220 * open more than FD_SETSIZE file descriptors then test
221 * 518 should not be run.
225 * verify that soft limit is higher than NUM_NEEDED,
226 * which is the number of file descriptors we would
227 * try to open plus SAFETY_MARGIN to not exhaust the
228 * file descriptor pool
231 num_open.rlim_cur = NUM_NEEDED;
233 if((rl.rlim_cur > 0) &&
235 (rl.rlim_cur != RLIM_INFINITY) &&
237 (rl.rlim_cur <= num_open.rlim_cur)) {
238 snprintf(strbuff2, sizeof(strbuff2), fmt, rl.rlim_cur);
239 snprintf(strbuff1, sizeof(strbuff1), fmt, num_open.rlim_cur);
240 snprintf(strbuff, sizeof(strbuff), "fds needed %s > system limit %s",
242 store_errmsg(strbuff, 0);
243 fprintf(stderr, "%s\n", msgbuff);
248 * reserve a chunk of memory before opening file descriptors to
249 * avoid a low memory condition once the file descriptors are
250 * open. System conditions that could make the test fail should
251 * be addressed in the precheck phase. This chunk of memory shall
252 * be always free()ed before exiting the rlimit() function so
253 * that it becomes available to the test.
256 for(nitems = i = 1; nitems <= i; i *= 2)
261 num_open.rlim_max = sizeof(*memchunk) * (size_t)nitems;
262 snprintf(strbuff, sizeof(strbuff), fmt, num_open.rlim_max);
263 fprintf(stderr, "allocating memchunk %s byte array\n", strbuff);
264 memchunk = malloc(sizeof(*memchunk) * (size_t)nitems);
266 fprintf(stderr, "memchunk, malloc() failed\n");
269 } while(nitems && !memchunk);
271 store_errmsg("memchunk, malloc() failed", ERRNO);
272 fprintf(stderr, "%s\n", msgbuff);
276 /* initialize it to fight lazy allocation */
278 fprintf(stderr, "initializing memchunk array\n");
280 for(i = 0; i < nitems; i++)
283 /* set the number of file descriptors we will try to open */
285 num_open.rlim_max = NUM_OPEN;
287 /* verify that we won't overflow size_t in malloc() */
289 if((size_t)(num_open.rlim_max) > ((size_t)-1) / sizeof(*fd)) {
290 snprintf(strbuff1, sizeof(strbuff1), fmt, num_open.rlim_max);
291 snprintf(strbuff, sizeof(strbuff), "unable to allocate an array for %s "
292 "file descriptors, would overflow size_t", strbuff1);
293 store_errmsg(strbuff, 0);
294 fprintf(stderr, "%s\n", msgbuff);
299 /* allocate array for file descriptors */
301 snprintf(strbuff, sizeof(strbuff), fmt, num_open.rlim_max);
302 fprintf(stderr, "allocating array for %s file descriptors\n", strbuff);
304 fd = malloc(sizeof(*fd) * (size_t)(num_open.rlim_max));
306 store_errmsg("fd, malloc() failed", ERRNO);
307 fprintf(stderr, "%s\n", msgbuff);
312 /* initialize it to fight lazy allocation */
314 fprintf(stderr, "initializing fd array\n");
316 for(num_open.rlim_cur = 0;
317 num_open.rlim_cur < num_open.rlim_max;
319 fd[num_open.rlim_cur] = -1;
321 snprintf(strbuff, sizeof(strbuff), fmt, num_open.rlim_max);
322 fprintf(stderr, "trying to open %s file descriptors\n", strbuff);
324 /* open a dummy descriptor */
326 fd[0] = open(DEV_NULL, O_RDONLY);
328 snprintf(strbuff, sizeof(strbuff), "opening of %s failed", DEV_NULL);
329 store_errmsg(strbuff, ERRNO);
330 fprintf(stderr, "%s\n", msgbuff);
337 /* create a bunch of file descriptors */
339 for(num_open.rlim_cur = 1;
340 num_open.rlim_cur < num_open.rlim_max;
341 num_open.rlim_cur++) {
343 fd[num_open.rlim_cur] = dup(fd[0]);
345 if(fd[num_open.rlim_cur] < 0) {
347 fd[num_open.rlim_cur] = -1;
349 snprintf(strbuff1, sizeof(strbuff1), fmt, num_open.rlim_cur);
350 snprintf(strbuff, sizeof(strbuff), "dup() attempt %s failed", strbuff1);
351 fprintf(stderr, "%s\n", strbuff);
353 snprintf(strbuff1, sizeof(strbuff), fmt, num_open.rlim_cur);
354 snprintf(strbuff, sizeof(strbuff), "fds system limit seems close to %s",
356 fprintf(stderr, "%s\n", strbuff);
358 num_open.rlim_max = NUM_NEEDED;
360 snprintf(strbuff2, sizeof(strbuff2), fmt, num_open.rlim_max);
361 snprintf(strbuff1, sizeof(strbuff1), fmt, num_open.rlim_cur);
362 snprintf(strbuff, sizeof(strbuff), "fds needed %s > system limit %s",
364 store_errmsg(strbuff, 0);
365 fprintf(stderr, "%s\n", msgbuff);
367 for(num_open.rlim_cur = 0;
368 fd[num_open.rlim_cur] >= 0;
370 close(fd[num_open.rlim_cur]);
380 snprintf(strbuff, sizeof(strbuff), fmt, num_open.rlim_max);
381 fprintf(stderr, "%s file descriptors open\n", strbuff);
383 #if !defined(HAVE_POLL_FINE) && \
384 !defined(USE_WINSOCK) && \
388 * when using select() instead of poll() we cannot test
389 * libcurl functionality with a socket number equal or
390 * greater than FD_SETSIZE. In any case, macro VERIFY_SOCK
391 * in lib/select.c enforces this check and protects libcurl
392 * from a possible crash. The effect of this protection
393 * is that test 518 will always fail, since the actual
394 * call to select() never takes place. We skip test 518
395 * with an indication that select limit would be exceeded.
398 num_open.rlim_cur = FD_SETSIZE - SAFETY_MARGIN;
399 if(num_open.rlim_max > num_open.rlim_cur) {
400 snprintf(strbuff, sizeof(strbuff), "select limit is FD_SETSIZE %d",
402 store_errmsg(strbuff, 0);
403 fprintf(stderr, "%s\n", msgbuff);
404 close_file_descriptors();
409 num_open.rlim_cur = FD_SETSIZE - SAFETY_MARGIN;
411 rl.rlim_cur < num_open.rlim_max;
413 if((fd[rl.rlim_cur] > 0) &&
414 ((unsigned int)fd[rl.rlim_cur] > num_open.rlim_cur)) {
415 snprintf(strbuff, sizeof(strbuff), "select limit is FD_SETSIZE %d",
417 store_errmsg(strbuff, 0);
418 fprintf(stderr, "%s\n", msgbuff);
419 close_file_descriptors();
425 #endif /* using a FD_SETSIZE bound select() */
428 * Old or 'backwards compatible' implementations of stdio do not allow
429 * handling of streams with an underlying file descriptor number greater
430 * than 255, even when allowing high numbered file descriptors for sockets.
431 * At this point we have a big number of file descriptors which have been
432 * opened using dup(), so lets test the stdio implementation and discover
433 * if it is capable of fopen()ing some additional files.
437 snprintf(strbuff1, sizeof(strbuff1), fmt, num_open.rlim_max);
438 snprintf(strbuff, sizeof(strbuff),
439 "fopen fails with %s fds open()",
441 fprintf(stderr, "%s\n", msgbuff);
442 snprintf(strbuff, sizeof(strbuff),
443 "fopen fails with lots of fds open()");
444 store_errmsg(strbuff, 0);
445 close_file_descriptors();
450 /* free the chunk of memory we were reserving so that it
451 becomes becomes available to the test */
455 /* close file descriptors unless instructed to keep them */
458 close_file_descriptors();
469 if(!strcmp(URL, "check")) {
470 /* used by the test script to ask if we can run this test or not */
472 fprintf(stdout, "rlimit problem: %s\n", msgbuff);
475 return 0; /* sure, run this! */
480 return TEST_ERR_MAJOR_BAD;
483 /* run the test with the bunch of open file descriptors
484 and close them all once the test is over */
486 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
487 fprintf(stderr, "curl_global_init() failed\n");
488 close_file_descriptors();
489 return TEST_ERR_MAJOR_BAD;
492 if((curl = curl_easy_init()) == NULL) {
493 fprintf(stderr, "curl_easy_init() failed\n");
494 close_file_descriptors();
495 curl_global_cleanup();
496 return TEST_ERR_MAJOR_BAD;
499 test_setopt(curl, CURLOPT_URL, URL);
500 test_setopt(curl, CURLOPT_HEADER, 1L);
502 res = curl_easy_perform(curl);
506 close_file_descriptors();
507 curl_easy_cleanup(curl);
508 curl_global_cleanup();
513 #else /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */
518 printf("system lacks necessary system function(s)");
519 return 1; /* skip test */
522 #endif /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */