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>
35 #if !defined(HAVE_POLL_FINE) && \
36 !defined(USE_WINSOCK) && \
39 #error "this test requires FD_SETSIZE"
42 #define SAFETY_MARGIN (11)
44 #if defined(WIN32) || defined(_WIN32) || defined(MSDOS)
45 #define DEV_NULL "NUL"
47 #define DEV_NULL "/dev/null"
50 #if defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT)
52 static int *fd = NULL;
53 static struct rlimit num_open;
54 static char msgbuff[256];
56 static void store_errmsg(const char *msg, int err)
59 snprintf(msgbuff, sizeof(msgbuff), "%s", msg);
61 snprintf(msgbuff, sizeof(msgbuff), "%s, errno %d, %s", msg, err,
65 static void close_file_descriptors(void)
67 for(num_open.rlim_cur = 0;
68 num_open.rlim_cur < num_open.rlim_max;
70 if(fd[num_open.rlim_cur] > 0)
71 close(fd[num_open.rlim_cur]);
76 static int fopen_works(void)
82 for(i = 0; i < 3; i++) {
85 for(i = 0; i < 3; i++) {
86 fpa[i] = fopen(DEV_NULL, FOPEN_READTEXT);
88 store_errmsg("fopen failed", errno);
89 fprintf(stderr, "%s\n", msgbuff);
94 for(i = 0; i < 3; i++) {
101 static int rlimit(int keep_open)
105 int *memchunk = NULL;
111 char fmt_lu[] = "%lu";
113 char fmt_llu[] = "%llu";
115 if(sizeof(rl.rlim_max) > sizeof(long))
119 fmt = (sizeof(rl.rlim_max) < sizeof(long))?fmt_u:fmt_lu;
121 /* get initial open file limits */
123 if(getrlimit(RLIMIT_NOFILE, &rl) != 0) {
124 store_errmsg("getrlimit() failed", errno);
125 fprintf(stderr, "%s\n", msgbuff);
129 /* show initial open file limits */
132 if(rl.rlim_cur == RLIM_INFINITY)
133 strcpy(strbuff, "INFINITY");
136 snprintf(strbuff, sizeof(strbuff), fmt, rl.rlim_cur);
137 fprintf(stderr, "initial soft limit: %s\n", strbuff);
140 if(rl.rlim_max == RLIM_INFINITY)
141 strcpy(strbuff, "INFINITY");
144 snprintf(strbuff, sizeof(strbuff), fmt, rl.rlim_max);
145 fprintf(stderr, "initial hard limit: %s\n", strbuff);
148 * if soft limit and hard limit are different we ask the
149 * system to raise soft limit all the way up to the hard
150 * limit. Due to some other system limit the soft limit
151 * might not be raised up to the hard limit. So from this
152 * point the resulting soft limit is our limit. Trying to
153 * open more than soft limit file descriptors will fail.
156 if(rl.rlim_cur != rl.rlim_max) {
159 if((rl.rlim_cur > 0) &&
160 (rl.rlim_cur < OPEN_MAX)) {
161 fprintf(stderr, "raising soft limit up to OPEN_MAX\n");
162 rl.rlim_cur = OPEN_MAX;
163 if(setrlimit(RLIMIT_NOFILE, &rl) != 0) {
164 /* on failure don't abort just issue a warning */
165 store_errmsg("setrlimit() failed", errno);
166 fprintf(stderr, "%s\n", msgbuff);
172 fprintf(stderr, "raising soft limit up to hard limit\n");
173 rl.rlim_cur = rl.rlim_max;
174 if(setrlimit(RLIMIT_NOFILE, &rl) != 0) {
175 /* on failure don't abort just issue a warning */
176 store_errmsg("setrlimit() failed", errno);
177 fprintf(stderr, "%s\n", msgbuff);
181 /* get current open file limits */
183 if(getrlimit(RLIMIT_NOFILE, &rl) != 0) {
184 store_errmsg("getrlimit() failed", errno);
185 fprintf(stderr, "%s\n", msgbuff);
189 /* show current open file limits */
192 if(rl.rlim_cur == RLIM_INFINITY)
193 strcpy(strbuff, "INFINITY");
196 snprintf(strbuff, sizeof(strbuff), fmt, rl.rlim_cur);
197 fprintf(stderr, "current soft limit: %s\n", strbuff);
200 if(rl.rlim_max == RLIM_INFINITY)
201 strcpy(strbuff, "INFINITY");
204 snprintf(strbuff, sizeof(strbuff), fmt, rl.rlim_max);
205 fprintf(stderr, "current hard limit: %s\n", strbuff);
207 } /* (rl.rlim_cur != rl.rlim_max) */
210 * test 537 is all about testing libcurl functionality
211 * when the system has nearly exhausted the number of
212 * available file descriptors. Test 537 will try to run
213 * with a very small number of file descriptors available.
214 * This implies that any file descriptor which is open
215 * when the test runs will have a number in the high range
216 * of whatever the system supports.
220 * reserve a chunk of memory before opening file descriptors to
221 * avoid a low memory condition once the file descriptors are
222 * open. System conditions that could make the test fail should
223 * be addressed in the precheck phase. This chunk of memory shall
224 * be always free()ed before exiting the rlimit() function so
225 * that it becomes available to the test.
228 for(nitems = i = 1; nitems <= i; i *= 2)
233 num_open.rlim_max = sizeof(*memchunk) * nitems;
234 snprintf(strbuff, sizeof(strbuff), fmt, num_open.rlim_max);
235 fprintf(stderr, "allocating memchunk %s byte array\n", strbuff);
236 memchunk = malloc(sizeof(*memchunk) * (size_t)nitems);
238 fprintf(stderr, "memchunk, malloc() failed\n");
241 } while(nitems && !memchunk);
243 store_errmsg("memchunk, malloc() failed", errno);
244 fprintf(stderr, "%s\n", msgbuff);
248 /* initialize it to fight lazy allocation */
250 fprintf(stderr, "initializing memchunk array\n");
252 for(i = 0; i < nitems; i++)
255 /* set the number of file descriptors we will try to open */
258 if((rl.rlim_cur > 0) && (rl.rlim_cur != RLIM_INFINITY)) {
260 if(rl.rlim_cur > 0) {
262 /* soft limit minus SAFETY_MARGIN */
263 num_open.rlim_max = rl.rlim_cur - SAFETY_MARGIN;
266 /* a huge number of file descriptors */
267 for(nitems = i = 1; nitems <= i; i *= 2)
271 num_open.rlim_max = nitems;
274 /* verify that we won't overflow size_t in malloc() */
276 if((size_t)(num_open.rlim_max) > ((size_t)-1) / sizeof(*fd)) {
277 snprintf(strbuff1, sizeof(strbuff1), fmt, num_open.rlim_max);
278 snprintf(strbuff, sizeof(strbuff), "unable to allocate an array for %s "
279 "file descriptors, would overflow size_t", strbuff1);
280 store_errmsg(strbuff, 0);
281 fprintf(stderr, "%s\n", msgbuff);
286 /* allocate array for file descriptors */
289 snprintf(strbuff, sizeof(strbuff), fmt, num_open.rlim_max);
290 fprintf(stderr, "allocating array for %s file descriptors\n", strbuff);
291 fd = malloc(sizeof(*fd) * (size_t)(num_open.rlim_max));
293 fprintf(stderr, "fd, malloc() failed\n");
294 num_open.rlim_max /= 2;
296 } while(num_open.rlim_max && !fd);
298 store_errmsg("fd, malloc() failed", errno);
299 fprintf(stderr, "%s\n", msgbuff);
304 /* initialize it to fight lazy allocation */
306 fprintf(stderr, "initializing fd array\n");
308 for(num_open.rlim_cur = 0;
309 num_open.rlim_cur < num_open.rlim_max;
311 fd[num_open.rlim_cur] = -1;
313 snprintf(strbuff, sizeof(strbuff), fmt, num_open.rlim_max);
314 fprintf(stderr, "trying to open %s file descriptors\n", strbuff);
316 /* open a dummy descriptor */
318 fd[0] = open(DEV_NULL, O_RDONLY);
320 snprintf(strbuff, sizeof(strbuff), "opening of %s failed", DEV_NULL);
321 store_errmsg(strbuff, errno);
322 fprintf(stderr, "%s\n", msgbuff);
329 /* create a bunch of file descriptors */
331 for(num_open.rlim_cur = 1;
332 num_open.rlim_cur < num_open.rlim_max;
333 num_open.rlim_cur++) {
335 fd[num_open.rlim_cur] = dup(fd[0]);
337 if(fd[num_open.rlim_cur] < 0) {
339 fd[num_open.rlim_cur] = -1;
341 snprintf(strbuff1, sizeof(strbuff1), fmt, num_open.rlim_cur);
342 snprintf(strbuff, sizeof(strbuff), "dup() attempt %s failed", strbuff1);
343 fprintf(stderr, "%s\n", strbuff);
345 snprintf(strbuff1, sizeof(strbuff1), fmt, num_open.rlim_cur);
346 snprintf(strbuff, sizeof(strbuff), "fds system limit seems close to %s",
348 fprintf(stderr, "%s\n", strbuff);
350 num_open.rlim_max = num_open.rlim_cur - SAFETY_MARGIN;
352 num_open.rlim_cur -= num_open.rlim_max;
353 snprintf(strbuff1, sizeof(strbuff1), fmt, num_open.rlim_cur);
354 snprintf(strbuff, sizeof(strbuff), "closing %s file descriptors",
356 fprintf(stderr, "%s\n", strbuff);
358 for(num_open.rlim_cur = num_open.rlim_max;
359 fd[num_open.rlim_cur] >= 0;
360 num_open.rlim_cur++) {
361 close(fd[num_open.rlim_cur]);
362 fd[num_open.rlim_cur] = -1;
365 snprintf(strbuff, sizeof(strbuff), fmt, num_open.rlim_max);
366 fprintf(stderr, "shrinking array for %s file descriptors\n", strbuff);
368 /* we don't care if we can't shrink it */
370 tmpfd = realloc(fd, sizeof(*fd) * (size_t)(num_open.rlim_max));
382 snprintf(strbuff, sizeof(strbuff), fmt, num_open.rlim_max);
383 fprintf(stderr, "%s file descriptors open\n", strbuff);
385 #if !defined(HAVE_POLL_FINE) && \
386 !defined(USE_WINSOCK) && \
390 * when using select() instead of poll() we cannot test
391 * libcurl functionality with a socket number equal or
392 * greater than FD_SETSIZE. In any case, macro VERIFY_SOCK
393 * in lib/select.c enforces this check and protects libcurl
394 * from a possible crash. The effect of this protection
395 * is that test 537 will always fail, since the actual
396 * call to select() never takes place. We skip test 537
397 * with an indication that select limit would be exceeded.
400 num_open.rlim_cur = FD_SETSIZE - SAFETY_MARGIN;
401 if(num_open.rlim_max > num_open.rlim_cur) {
402 snprintf(strbuff, sizeof(strbuff), "select limit is FD_SETSIZE %d",
404 store_errmsg(strbuff, 0);
405 fprintf(stderr, "%s\n", msgbuff);
406 close_file_descriptors();
411 num_open.rlim_cur = FD_SETSIZE - SAFETY_MARGIN;
413 rl.rlim_cur < num_open.rlim_max;
415 if((fd[rl.rlim_cur] > 0) &&
416 ((unsigned int)fd[rl.rlim_cur] > num_open.rlim_cur)) {
417 snprintf(strbuff, sizeof(strbuff), "select limit is FD_SETSIZE %d",
419 store_errmsg(strbuff, 0);
420 fprintf(stderr, "%s\n", msgbuff);
421 close_file_descriptors();
427 #endif /* using a FD_SETSIZE bound select() */
430 * Old or 'backwards compatible' implementations of stdio do not allow
431 * handling of streams with an underlying file descriptor number greater
432 * than 255, even when allowing high numbered file descriptors for sockets.
433 * At this point we have a big number of file descriptors which have been
434 * opened using dup(), so lets test the stdio implementation and discover
435 * if it is capable of fopen()ing some additional files.
439 snprintf(strbuff1, sizeof(strbuff1), fmt, num_open.rlim_max);
440 snprintf(strbuff, sizeof(strbuff), "fopen fails with %s fds open",
442 fprintf(stderr, "%s\n", msgbuff);
443 snprintf(strbuff, sizeof(strbuff), "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 curl = curl_easy_init();
494 fprintf(stderr, "curl_easy_init() failed\n");
495 close_file_descriptors();
496 curl_global_cleanup();
497 return TEST_ERR_MAJOR_BAD;
500 test_setopt(curl, CURLOPT_URL, URL);
501 test_setopt(curl, CURLOPT_HEADER, 1L);
503 res = curl_easy_perform(curl);
507 close_file_descriptors();
508 curl_easy_cleanup(curl);
509 curl_global_cleanup();
514 #else /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */
519 printf("system lacks necessary system function(s)");
520 return 1; /* skip test */
523 #endif /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */