1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, 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 http://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 sprintf(msgbuff, "%s", msg);
62 sprintf(msgbuff, "%s, errno %d, %s", msg, err, strerror(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, "r");
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)
104 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 sprintf(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 sprintf(strbuff, fmt, rl.rlim_max);
145 fprintf(stderr, "initial hard limit: %s\n", strbuff);
147 /* show our constants */
149 fprintf(stderr, "test518 FD_SETSIZE: %d\n", FD_SETSIZE);
150 fprintf(stderr, "test518 NUM_OPEN : %d\n", NUM_OPEN);
151 fprintf(stderr, "test518 NUM_NEEDED: %d\n", NUM_NEEDED);
154 * if soft limit and hard limit are different we ask the
155 * system to raise soft limit all the way up to the hard
156 * limit. Due to some other system limit the soft limit
157 * might not be raised up to the hard limit. So from this
158 * point the resulting soft limit is our limit. Trying to
159 * open more than soft limit file descriptors will fail.
162 if (rl.rlim_cur != rl.rlim_max) {
165 if ((rl.rlim_cur > 0) &&
166 (rl.rlim_cur < OPEN_MAX)) {
167 fprintf(stderr, "raising soft limit up to OPEN_MAX\n");
168 rl.rlim_cur = OPEN_MAX;
169 if (setrlimit(RLIMIT_NOFILE, &rl) != 0) {
170 /* on failure don't abort just issue a warning */
171 store_errmsg("setrlimit() failed", ERRNO);
172 fprintf(stderr, "%s\n", msgbuff);
178 fprintf(stderr, "raising soft limit up to hard limit\n");
179 rl.rlim_cur = rl.rlim_max;
180 if (setrlimit(RLIMIT_NOFILE, &rl) != 0) {
181 /* on failure don't abort just issue a warning */
182 store_errmsg("setrlimit() failed", ERRNO);
183 fprintf(stderr, "%s\n", msgbuff);
187 /* get current open file limits */
189 if (getrlimit(RLIMIT_NOFILE, &rl) != 0) {
190 store_errmsg("getrlimit() failed", ERRNO);
191 fprintf(stderr, "%s\n", msgbuff);
195 /* show current open file limits */
198 if (rl.rlim_cur == RLIM_INFINITY)
199 strcpy(strbuff, "INFINITY");
202 sprintf(strbuff, fmt, rl.rlim_cur);
203 fprintf(stderr, "current soft limit: %s\n", strbuff);
206 if (rl.rlim_max == RLIM_INFINITY)
207 strcpy(strbuff, "INFINITY");
210 sprintf(strbuff, fmt, rl.rlim_max);
211 fprintf(stderr, "current hard limit: %s\n", strbuff);
213 } /* (rl.rlim_cur != rl.rlim_max) */
216 * test 518 is all about testing libcurl functionality
217 * when more than FD_SETSIZE file descriptors are open.
218 * This means that if for any reason we are not able to
219 * open more than FD_SETSIZE file descriptors then test
220 * 518 should not be run.
224 * verify that soft limit is higher than NUM_NEEDED,
225 * which is the number of file descriptors we would
226 * try to open plus SAFETY_MARGIN to not exhaust the
227 * file descriptor pool
230 num_open.rlim_cur = NUM_NEEDED;
232 if ((rl.rlim_cur > 0) &&
234 (rl.rlim_cur != RLIM_INFINITY) &&
236 (rl.rlim_cur <= num_open.rlim_cur)) {
237 sprintf(strbuff2, fmt, rl.rlim_cur);
238 sprintf(strbuff1, fmt, num_open.rlim_cur);
239 sprintf(strbuff, "fds needed %s > system limit %s",
241 store_errmsg(strbuff, 0);
242 fprintf(stderr, "%s\n", msgbuff);
247 * reserve a chunk of memory before opening file descriptors to
248 * avoid a low memory condition once the file descriptors are
249 * open. System conditions that could make the test fail should
250 * be addressed in the precheck phase. This chunk of memory shall
251 * be always free()ed before exiting the rlimit() function so
252 * that it becomes available to the test.
255 for (nitems = i = 1; nitems <= i; i *= 2)
260 num_open.rlim_max = sizeof(*memchunk) * (size_t)nitems;
261 sprintf(strbuff, fmt, num_open.rlim_max);
262 fprintf(stderr, "allocating memchunk %s byte array\n", strbuff);
263 memchunk = malloc(sizeof(*memchunk) * (size_t)nitems);
265 fprintf(stderr, "memchunk, malloc() failed\n");
268 } while (nitems && !memchunk);
270 store_errmsg("memchunk, malloc() failed", ERRNO);
271 fprintf(stderr, "%s\n", msgbuff);
275 /* initialize it to fight lazy allocation */
277 fprintf(stderr, "initializing memchunk array\n");
279 for (i = 0; i < nitems; i++)
282 /* set the number of file descriptors we will try to open */
284 num_open.rlim_max = NUM_OPEN;
286 /* verify that we won't overflow size_t in malloc() */
288 if ((size_t)(num_open.rlim_max) > ((size_t)-1) / sizeof(*fd)) {
289 sprintf(strbuff1, fmt, num_open.rlim_max);
290 sprintf(strbuff, "unable to allocate an array for %s "
291 "file descriptors, would overflow size_t", strbuff1);
292 store_errmsg(strbuff, 0);
293 fprintf(stderr, "%s\n", msgbuff);
298 /* allocate array for file descriptors */
300 sprintf(strbuff, fmt, num_open.rlim_max);
301 fprintf(stderr, "allocating array for %s file descriptors\n", strbuff);
303 fd = malloc(sizeof(*fd) * (size_t)(num_open.rlim_max));
305 store_errmsg("fd, malloc() failed", ERRNO);
306 fprintf(stderr, "%s\n", msgbuff);
311 /* initialize it to fight lazy allocation */
313 fprintf(stderr, "initializing fd array\n");
315 for (num_open.rlim_cur = 0;
316 num_open.rlim_cur < num_open.rlim_max;
318 fd[num_open.rlim_cur] = -1;
320 sprintf(strbuff, fmt, num_open.rlim_max);
321 fprintf(stderr, "trying to open %s file descriptors\n", strbuff);
323 /* open a dummy descriptor */
325 fd[0] = open(DEV_NULL, O_RDONLY);
327 sprintf(strbuff, "opening of %s failed", DEV_NULL);
328 store_errmsg(strbuff, ERRNO);
329 fprintf(stderr, "%s\n", msgbuff);
336 /* create a bunch of file descriptors */
338 for (num_open.rlim_cur = 1;
339 num_open.rlim_cur < num_open.rlim_max;
340 num_open.rlim_cur++) {
342 fd[num_open.rlim_cur] = dup(fd[0]);
344 if (fd[num_open.rlim_cur] < 0) {
346 fd[num_open.rlim_cur] = -1;
348 sprintf(strbuff1, fmt, num_open.rlim_cur);
349 sprintf(strbuff, "dup() attempt %s failed", strbuff1);
350 fprintf(stderr, "%s\n", strbuff);
352 sprintf(strbuff1, fmt, num_open.rlim_cur);
353 sprintf(strbuff, "fds system limit seems close to %s", strbuff1);
354 fprintf(stderr, "%s\n", strbuff);
356 num_open.rlim_max = NUM_NEEDED;
358 sprintf(strbuff2, fmt, num_open.rlim_max);
359 sprintf(strbuff1, fmt, num_open.rlim_cur);
360 sprintf(strbuff, "fds needed %s > system limit %s",
362 store_errmsg(strbuff, 0);
363 fprintf(stderr, "%s\n", msgbuff);
365 for (num_open.rlim_cur = 0;
366 fd[num_open.rlim_cur] >= 0;
368 close(fd[num_open.rlim_cur]);
378 sprintf(strbuff, fmt, num_open.rlim_max);
379 fprintf(stderr, "%s file descriptors open\n", strbuff);
381 #if !defined(HAVE_POLL_FINE) && \
382 !defined(USE_WINSOCK) && \
386 * when using select() instead of poll() we cannot test
387 * libcurl functionality with a socket number equal or
388 * greater than FD_SETSIZE. In any case, macro VERIFY_SOCK
389 * in lib/select.c enforces this check and protects libcurl
390 * from a possible crash. The effect of this protection
391 * is that test 518 will always fail, since the actual
392 * call to select() never takes place. We skip test 518
393 * with an indication that select limit would be exceeded.
396 num_open.rlim_cur = FD_SETSIZE - SAFETY_MARGIN;
397 if (num_open.rlim_max > num_open.rlim_cur) {
398 sprintf(strbuff, "select limit is FD_SETSIZE %d", FD_SETSIZE);
399 store_errmsg(strbuff, 0);
400 fprintf(stderr, "%s\n", msgbuff);
401 close_file_descriptors();
406 num_open.rlim_cur = FD_SETSIZE - SAFETY_MARGIN;
407 for (rl.rlim_cur = 0;
408 rl.rlim_cur < num_open.rlim_max;
410 if ((fd[rl.rlim_cur] > 0) &&
411 ((unsigned int)fd[rl.rlim_cur] > num_open.rlim_cur)) {
412 sprintf(strbuff, "select limit is FD_SETSIZE %d", FD_SETSIZE);
413 store_errmsg(strbuff, 0);
414 fprintf(stderr, "%s\n", msgbuff);
415 close_file_descriptors();
421 #endif /* using a FD_SETSIZE bound select() */
424 * Old or 'backwards compatible' implementations of stdio do not allow
425 * handling of streams with an underlying file descriptor number greater
426 * than 255, even when allowing high numbered file descriptors for sockets.
427 * At this point we have a big number of file descriptors which have been
428 * opened using dup(), so lets test the stdio implementation and discover
429 * if it is capable of fopen()ing some additional files.
432 if (!fopen_works()) {
433 sprintf(strbuff1, fmt, num_open.rlim_max);
434 sprintf(strbuff, "stdio fopen() fails with %s fds open()",
436 fprintf(stderr, "%s\n", msgbuff);
437 sprintf(strbuff, "stdio fopen() fails with lots of fds open()");
438 store_errmsg(strbuff, 0);
439 close_file_descriptors();
444 /* free the chunk of memory we were reserving so that it
445 becomes becomes available to the test */
449 /* close file descriptors unless instructed to keep them */
452 close_file_descriptors();
463 if(!strcmp(URL, "check")) {
464 /* used by the test script to ask if we can run this test or not */
466 fprintf(stdout, "rlimit problem: %s\n", msgbuff);
469 return 0; /* sure, run this! */
474 return TEST_ERR_MAJOR_BAD;
477 /* run the test with the bunch of open file descriptors
478 and close them all once the test is over */
480 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
481 fprintf(stderr, "curl_global_init() failed\n");
482 close_file_descriptors();
483 return TEST_ERR_MAJOR_BAD;
486 if ((curl = curl_easy_init()) == NULL) {
487 fprintf(stderr, "curl_easy_init() failed\n");
488 close_file_descriptors();
489 curl_global_cleanup();
490 return TEST_ERR_MAJOR_BAD;
493 test_setopt(curl, CURLOPT_URL, URL);
494 test_setopt(curl, CURLOPT_HEADER, 1L);
496 res = curl_easy_perform(curl);
500 close_file_descriptors();
501 curl_easy_cleanup(curl);
502 curl_global_cleanup();
507 #else /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */
512 printf("system lacks necessary system function(s)");
513 return 1; /* skip test */
516 #endif /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */