1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, 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>
37 #if !defined(HAVE_POLL_FINE) && \
38 !defined(USE_WINSOCK) && \
41 #error "this test requires FD_SETSIZE"
44 #define SAFETY_MARGIN (11)
46 #if defined(WIN32) || defined(_WIN32) || defined(MSDOS)
47 #define DEV_NULL "NUL"
49 #define DEV_NULL "/dev/null"
52 #if defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT)
54 static int *fd = NULL;
55 static struct rlimit num_open;
56 static char msgbuff[256];
58 static void store_errmsg(const char *msg, int err)
61 sprintf(msgbuff, "%s", msg);
63 sprintf(msgbuff, "%s, errno %d, %s", msg, err, strerror(err));
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, "r");
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)
106 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 sprintf(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 sprintf(strbuff, fmt, rl.rlim_max);
146 fprintf(stderr, "initial hard limit: %s\n", strbuff);
149 * if soft limit and hard limit are different we ask the
150 * system to raise soft limit all the way up to the hard
151 * limit. Due to some other system limit the soft limit
152 * might not be raised up to the hard limit. So from this
153 * point the resulting soft limit is our limit. Trying to
154 * open more than soft limit file descriptors will fail.
157 if (rl.rlim_cur != rl.rlim_max) {
160 if ((rl.rlim_cur > 0) &&
161 (rl.rlim_cur < OPEN_MAX)) {
162 fprintf(stderr, "raising soft limit up to OPEN_MAX\n");
163 rl.rlim_cur = OPEN_MAX;
164 if (setrlimit(RLIMIT_NOFILE, &rl) != 0) {
165 /* on failure don't abort just issue a warning */
166 store_errmsg("setrlimit() failed", ERRNO);
167 fprintf(stderr, "%s\n", msgbuff);
173 fprintf(stderr, "raising soft limit up to hard limit\n");
174 rl.rlim_cur = rl.rlim_max;
175 if (setrlimit(RLIMIT_NOFILE, &rl) != 0) {
176 /* on failure don't abort just issue a warning */
177 store_errmsg("setrlimit() failed", ERRNO);
178 fprintf(stderr, "%s\n", msgbuff);
182 /* get current open file limits */
184 if (getrlimit(RLIMIT_NOFILE, &rl) != 0) {
185 store_errmsg("getrlimit() failed", ERRNO);
186 fprintf(stderr, "%s\n", msgbuff);
190 /* show current open file limits */
193 if (rl.rlim_cur == RLIM_INFINITY)
194 strcpy(strbuff, "INFINITY");
197 sprintf(strbuff, fmt, rl.rlim_cur);
198 fprintf(stderr, "current soft limit: %s\n", strbuff);
201 if (rl.rlim_max == RLIM_INFINITY)
202 strcpy(strbuff, "INFINITY");
205 sprintf(strbuff, fmt, rl.rlim_max);
206 fprintf(stderr, "current hard limit: %s\n", strbuff);
208 } /* (rl.rlim_cur != rl.rlim_max) */
211 * test 537 is all about testing libcurl functionality
212 * when the system has nearly exhausted the number of
213 * available file descriptors. Test 537 will try to run
214 * with a very small number of file descriptors available.
215 * This implies that any file descriptor which is open
216 * when the test runs will have a number in the high range
217 * of whatever the system supports.
221 * reserve a chunk of memory before opening file descriptors to
222 * avoid a low memory condition once the file descriptors are
223 * open. System conditions that could make the test fail should
224 * be addressed in the precheck phase. This chunk of memory shall
225 * be always free()ed before exiting the rlimit() function so
226 * that it becomes available to the test.
229 for (nitems = i = 1; nitems <= i; i *= 2)
234 num_open.rlim_max = sizeof(*memchunk) * (size_t)nitems;
235 sprintf(strbuff, fmt, num_open.rlim_max);
236 fprintf(stderr, "allocating memchunk %s byte array\n", strbuff);
237 memchunk = malloc(sizeof(*memchunk) * (size_t)nitems);
239 fprintf(stderr, "memchunk, malloc() failed\n");
242 } while (nitems && !memchunk);
244 store_errmsg("memchunk, malloc() failed", ERRNO);
245 fprintf(stderr, "%s\n", msgbuff);
249 /* initialize it to fight lazy allocation */
251 fprintf(stderr, "initializing memchunk array\n");
253 for (i = 0; i < nitems; i++)
256 /* set the number of file descriptors we will try to open */
259 if ((rl.rlim_cur > 0) && (rl.rlim_cur != RLIM_INFINITY)) {
261 if (rl.rlim_cur > 0) {
263 /* soft limit minus SAFETY_MARGIN */
264 num_open.rlim_max = rl.rlim_cur - SAFETY_MARGIN;
267 /* a huge number of file descriptors */
268 for (nitems = i = 1; nitems <= i; i *= 2)
272 num_open.rlim_max = nitems;
275 /* verify that we won't overflow size_t in malloc() */
277 if ((size_t)(num_open.rlim_max) > ((size_t)-1) / sizeof(*fd)) {
278 sprintf(strbuff1, fmt, num_open.rlim_max);
279 sprintf(strbuff, "unable to allocate an array for %s "
280 "file descriptors, would overflow size_t", strbuff1);
281 store_errmsg(strbuff, 0);
282 fprintf(stderr, "%s\n", msgbuff);
287 /* allocate array for file descriptors */
290 sprintf(strbuff, fmt, num_open.rlim_max);
291 fprintf(stderr, "allocating array for %s file descriptors\n", strbuff);
292 fd = malloc(sizeof(*fd) * (size_t)(num_open.rlim_max));
294 fprintf(stderr, "fd, malloc() failed\n");
295 num_open.rlim_max /= 2;
297 } while (num_open.rlim_max && !fd);
299 store_errmsg("fd, malloc() failed", ERRNO);
300 fprintf(stderr, "%s\n", msgbuff);
305 /* initialize it to fight lazy allocation */
307 fprintf(stderr, "initializing fd array\n");
309 for (num_open.rlim_cur = 0;
310 num_open.rlim_cur < num_open.rlim_max;
312 fd[num_open.rlim_cur] = -1;
314 sprintf(strbuff, fmt, num_open.rlim_max);
315 fprintf(stderr, "trying to open %s file descriptors\n", strbuff);
317 /* open a dummy descriptor */
319 fd[0] = open(DEV_NULL, O_RDONLY);
321 sprintf(strbuff, "opening of %s failed", DEV_NULL);
322 store_errmsg(strbuff, ERRNO);
323 fprintf(stderr, "%s\n", msgbuff);
330 /* create a bunch of file descriptors */
332 for (num_open.rlim_cur = 1;
333 num_open.rlim_cur < num_open.rlim_max;
334 num_open.rlim_cur++) {
336 fd[num_open.rlim_cur] = dup(fd[0]);
338 if (fd[num_open.rlim_cur] < 0) {
340 fd[num_open.rlim_cur] = -1;
342 sprintf(strbuff1, fmt, num_open.rlim_cur);
343 sprintf(strbuff, "dup() attempt %s failed", strbuff1);
344 fprintf(stderr, "%s\n", strbuff);
346 sprintf(strbuff1, fmt, num_open.rlim_cur);
347 sprintf(strbuff, "fds system limit seems close to %s", strbuff1);
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 sprintf(strbuff1, fmt, num_open.rlim_cur);
354 sprintf(strbuff, "closing %s file descriptors", strbuff1);
355 fprintf(stderr, "%s\n", strbuff);
357 for (num_open.rlim_cur = num_open.rlim_max;
358 fd[num_open.rlim_cur] >= 0;
359 num_open.rlim_cur++) {
360 close(fd[num_open.rlim_cur]);
361 fd[num_open.rlim_cur] = -1;
364 sprintf(strbuff, fmt, num_open.rlim_max);
365 fprintf(stderr, "shrinking array for %s file descriptors\n", strbuff);
367 /* we don't care if we can't shrink it */
369 tmpfd = realloc(fd, sizeof(*fd) * (size_t)(num_open.rlim_max));
381 sprintf(strbuff, fmt, num_open.rlim_max);
382 fprintf(stderr, "%s file descriptors open\n", strbuff);
384 #if !defined(HAVE_POLL_FINE) && \
385 !defined(USE_WINSOCK) && \
389 * when using select() instead of poll() we cannot test
390 * libcurl functionality with a socket number equal or
391 * greater than FD_SETSIZE. In any case, macro VERIFY_SOCK
392 * in lib/select.c enforces this check and protects libcurl
393 * from a possible crash. The effect of this protection
394 * is that test 537 will always fail, since the actual
395 * call to select() never takes place. We skip test 537
396 * with an indication that select limit would be exceeded.
399 num_open.rlim_cur = FD_SETSIZE - SAFETY_MARGIN;
400 if (num_open.rlim_max > num_open.rlim_cur) {
401 sprintf(strbuff, "select limit is FD_SETSIZE %d", FD_SETSIZE);
402 store_errmsg(strbuff, 0);
403 fprintf(stderr, "%s\n", msgbuff);
404 close_file_descriptors();
409 num_open.rlim_cur = FD_SETSIZE - SAFETY_MARGIN;
410 for (rl.rlim_cur = 0;
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 sprintf(strbuff, "select limit is FD_SETSIZE %d", FD_SETSIZE);
416 store_errmsg(strbuff, 0);
417 fprintf(stderr, "%s\n", msgbuff);
418 close_file_descriptors();
424 #endif /* using a FD_SETSIZE bound select() */
427 * Old or 'backwards compatible' implementations of stdio do not allow
428 * handling of streams with an underlying file descriptor number greater
429 * than 255, even when allowing high numbered file descriptors for sockets.
430 * At this point we have a big number of file descriptors which have been
431 * opened using dup(), so lets test the stdio implementation and discover
432 * if it is capable of fopen()ing some additional files.
435 if (!fopen_works()) {
436 sprintf(strbuff1, fmt, num_open.rlim_max);
437 sprintf(strbuff, "stdio fopen() fails with %s fds open()",
439 fprintf(stderr, "%s\n", msgbuff);
440 sprintf(strbuff, "stdio fopen() fails with lots of fds open()");
441 store_errmsg(strbuff, 0);
442 close_file_descriptors();
447 /* free the chunk of memory we were reserving so that it
448 becomes becomes available to the test */
452 /* close file descriptors unless instructed to keep them */
455 close_file_descriptors();
466 if(!strcmp(URL, "check")) {
467 /* used by the test script to ask if we can run this test or not */
469 fprintf(stdout, "rlimit problem: %s\n", msgbuff);
472 return 0; /* sure, run this! */
477 return TEST_ERR_MAJOR_BAD;
480 /* run the test with the bunch of open file descriptors
481 and close them all once the test is over */
483 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
484 fprintf(stderr, "curl_global_init() failed\n");
485 close_file_descriptors();
486 return TEST_ERR_MAJOR_BAD;
489 if ((curl = curl_easy_init()) == NULL) {
490 fprintf(stderr, "curl_easy_init() failed\n");
491 close_file_descriptors();
492 curl_global_cleanup();
493 return TEST_ERR_MAJOR_BAD;
496 test_setopt(curl, CURLOPT_URL, URL);
497 test_setopt(curl, CURLOPT_HEADER, 1L);
499 res = curl_easy_perform(curl);
503 close_file_descriptors();
504 curl_easy_cleanup(curl);
505 curl_global_cleanup();
510 #else /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */
515 printf("system lacks necessary system function(s)");
516 return 1; /* skip test */
519 #endif /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */