1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
12 #ifdef HAVE_SYS_TYPES_H
13 #include <sys/types.h>
15 #ifdef HAVE_SYS_RESOURCE_H
16 #include <sys/resource.h>
31 #error "this test requires FD_SETSIZE"
34 #define SAFETY_MARGIN (16)
35 #define NUM_OPEN (FD_SETSIZE + 10)
36 #define NUM_NEEDED (NUM_OPEN + SAFETY_MARGIN)
38 #if defined(WIN32) || defined(_WIN32) || defined(MSDOS)
39 #define DEV_NULL "NUL"
41 #define DEV_NULL "/dev/null"
44 #if defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT)
46 static int *fd = NULL;
47 static struct rlimit num_open;
48 static char msgbuff[256];
50 static void store_errmsg(const char *msg, int err)
53 sprintf(msgbuff, "%s", msg);
55 sprintf(msgbuff, "%s, errno %d, %s", msg, err, strerror(err));
58 static void close_file_descriptors(void)
60 for (num_open.rlim_cur = 0;
61 num_open.rlim_cur < num_open.rlim_max;
63 if (fd[num_open.rlim_cur] > 0)
64 close(fd[num_open.rlim_cur]);
69 static int fopen_works(void)
75 for (i = 0; i < 3; i++) {
78 for (i = 0; i < 3; i++) {
79 fpa[i] = fopen(DEV_NULL, "r");
81 store_errmsg("fopen() failed", ERRNO);
82 fprintf(stderr, "%s\n", msgbuff);
87 for (i = 0; i < 3; i++) {
94 static int rlimit(int keep_open)
104 char fmt_lu[] = "%lu";
106 char fmt_llu[] = "%llu";
108 if (sizeof(rl.rlim_max) > sizeof(long))
112 fmt = (sizeof(rl.rlim_max) < sizeof(long))?fmt_u:fmt_lu;
114 /* get initial open file limits */
116 if (getrlimit(RLIMIT_NOFILE, &rl) != 0) {
117 store_errmsg("getrlimit() failed", ERRNO);
118 fprintf(stderr, "%s\n", msgbuff);
122 /* show initial open file limits */
125 if (rl.rlim_cur == RLIM_INFINITY)
126 strcpy(strbuff, "INFINITY");
129 sprintf(strbuff, fmt, rl.rlim_cur);
130 fprintf(stderr, "initial soft limit: %s\n", strbuff);
133 if (rl.rlim_max == RLIM_INFINITY)
134 strcpy(strbuff, "INFINITY");
137 sprintf(strbuff, fmt, rl.rlim_max);
138 fprintf(stderr, "initial hard limit: %s\n", strbuff);
140 /* show our constants */
142 fprintf(stderr, "test518 FD_SETSIZE: %d\n", FD_SETSIZE);
143 fprintf(stderr, "test518 NUM_OPEN : %d\n", NUM_OPEN);
144 fprintf(stderr, "test518 NUM_NEEDED: %d\n", NUM_NEEDED);
147 * if soft limit and hard limit are different we ask the
148 * system to raise soft limit all the way up to the hard
149 * limit. Due to some other system limit the soft limit
150 * might not be raised up to the hard limit. So from this
151 * point the resulting soft limit is our limit. Trying to
152 * open more than soft limit file descriptors will fail.
155 if (rl.rlim_cur != rl.rlim_max) {
158 if ((rl.rlim_cur > 0) &&
159 (rl.rlim_cur < OPEN_MAX)) {
160 fprintf(stderr, "raising soft limit up to OPEN_MAX\n");
161 rl.rlim_cur = OPEN_MAX;
162 if (setrlimit(RLIMIT_NOFILE, &rl) != 0) {
163 /* on failure don't abort just issue a warning */
164 store_errmsg("setrlimit() failed", ERRNO);
165 fprintf(stderr, "%s\n", msgbuff);
171 fprintf(stderr, "raising soft limit up to hard limit\n");
172 rl.rlim_cur = rl.rlim_max;
173 if (setrlimit(RLIMIT_NOFILE, &rl) != 0) {
174 /* on failure don't abort just issue a warning */
175 store_errmsg("setrlimit() failed", ERRNO);
176 fprintf(stderr, "%s\n", msgbuff);
180 /* get current open file limits */
182 if (getrlimit(RLIMIT_NOFILE, &rl) != 0) {
183 store_errmsg("getrlimit() failed", ERRNO);
184 fprintf(stderr, "%s\n", msgbuff);
188 /* show current open file limits */
191 if (rl.rlim_cur == RLIM_INFINITY)
192 strcpy(strbuff, "INFINITY");
195 sprintf(strbuff, fmt, rl.rlim_cur);
196 fprintf(stderr, "current soft limit: %s\n", strbuff);
199 if (rl.rlim_max == RLIM_INFINITY)
200 strcpy(strbuff, "INFINITY");
203 sprintf(strbuff, fmt, rl.rlim_max);
204 fprintf(stderr, "current hard limit: %s\n", strbuff);
206 } /* (rl.rlim_cur != rl.rlim_max) */
209 * test 518 is all about testing libcurl functionality
210 * when more than FD_SETSIZE file descriptors are open.
211 * This means that if for any reason we are not able to
212 * open more than FD_SETSIZE file descriptors then test
213 * 518 should not be run.
217 * verify that soft limit is higher than NUM_NEEDED,
218 * which is the number of file descriptors we would
219 * try to open plus SAFETY_MARGIN to not exhaust the
220 * file descriptor pool
223 num_open.rlim_cur = NUM_NEEDED;
225 if ((rl.rlim_cur > 0) &&
227 (rl.rlim_cur != RLIM_INFINITY) &&
229 (rl.rlim_cur <= num_open.rlim_cur)) {
230 sprintf(strbuff2, fmt, rl.rlim_cur);
231 sprintf(strbuff1, fmt, num_open.rlim_cur);
232 sprintf(strbuff, "fds needed %s > system limit %s",
234 store_errmsg(strbuff, 0);
235 fprintf(stderr, "%s\n", msgbuff);
240 * reserve a chunk of memory before opening file descriptors to
241 * avoid a low memory condition once the file descriptors are
242 * open. System conditions that could make the test fail should
243 * be addressed in the precheck phase. This chunk of memory shall
244 * be always free()ed before exiting the rlimit() function so
245 * that it becomes available to the test.
248 for (nitems = i = 1; nitems <= i; i *= 2)
253 num_open.rlim_max = sizeof(*memchunk) * (size_t)nitems;
254 sprintf(strbuff, fmt, num_open.rlim_max);
255 fprintf(stderr, "allocating memchunk %s byte array\n", strbuff);
256 memchunk = malloc(sizeof(*memchunk) * (size_t)nitems);
258 fprintf(stderr, "memchunk, malloc() failed\n");
261 } while (nitems && !memchunk);
263 store_errmsg("memchunk, malloc() failed", ERRNO);
264 fprintf(stderr, "%s\n", msgbuff);
268 /* initialize it to fight lazy allocation */
270 fprintf(stderr, "initializing memchunk array\n");
272 for (i = 0; i < nitems; i++)
275 /* set the number of file descriptors we will try to open */
277 num_open.rlim_max = NUM_OPEN;
279 /* verify that we won't overflow size_t in malloc() */
281 if ((size_t)(num_open.rlim_max) > ((size_t)-1) / sizeof(*fd)) {
282 sprintf(strbuff1, fmt, num_open.rlim_max);
283 sprintf(strbuff, "unable to allocate an array for %s "
284 "file descriptors, would overflow size_t", strbuff1);
285 store_errmsg(strbuff, 0);
286 fprintf(stderr, "%s\n", msgbuff);
291 /* allocate array for file descriptors */
293 sprintf(strbuff, fmt, num_open.rlim_max);
294 fprintf(stderr, "allocating array for %s file descriptors\n", strbuff);
296 fd = malloc(sizeof(*fd) * (size_t)(num_open.rlim_max));
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 sprintf(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 sprintf(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 sprintf(strbuff1, fmt, num_open.rlim_cur);
342 sprintf(strbuff, "dup() attempt %s failed", strbuff1);
343 fprintf(stderr, "%s\n", strbuff);
345 sprintf(strbuff1, fmt, num_open.rlim_cur);
346 sprintf(strbuff, "fds system limit seems close to %s", strbuff1);
347 fprintf(stderr, "%s\n", strbuff);
349 num_open.rlim_max = NUM_NEEDED;
351 sprintf(strbuff2, fmt, num_open.rlim_max);
352 sprintf(strbuff1, fmt, num_open.rlim_cur);
353 sprintf(strbuff, "fds needed %s > system limit %s",
355 store_errmsg(strbuff, 0);
356 fprintf(stderr, "%s\n", msgbuff);
358 for (num_open.rlim_cur = 0;
359 fd[num_open.rlim_cur] >= 0;
361 close(fd[num_open.rlim_cur]);
371 sprintf(strbuff, fmt, num_open.rlim_max);
372 fprintf(stderr, "%s file descriptors open\n", strbuff);
374 #if !defined(HAVE_POLL_FINE) && \
375 !defined(USE_WINSOCK) && \
379 * when using select() instead of poll() we cannot test
380 * libcurl functionality with a socket number equal or
381 * greater than FD_SETSIZE. In any case, macro VERIFY_SOCK
382 * in lib/select.c enforces this check and protects libcurl
383 * from a possible crash. The effect of this protection
384 * is that test 518 will always fail, since the actual
385 * call to select() never takes place. We skip test 518
386 * with an indication that select limit would be exceeded.
389 num_open.rlim_cur = FD_SETSIZE - SAFETY_MARGIN;
390 if (num_open.rlim_max > num_open.rlim_cur) {
391 sprintf(strbuff, "select limit is FD_SETSIZE %d", FD_SETSIZE);
392 store_errmsg(strbuff, 0);
393 fprintf(stderr, "%s\n", msgbuff);
394 close_file_descriptors();
399 num_open.rlim_cur = FD_SETSIZE - SAFETY_MARGIN;
400 for (rl.rlim_cur = 0;
401 rl.rlim_cur < num_open.rlim_max;
403 if ((fd[rl.rlim_cur] > 0) &&
404 ((unsigned int)fd[rl.rlim_cur] > num_open.rlim_cur)) {
405 sprintf(strbuff, "select limit is FD_SETSIZE %d", FD_SETSIZE);
406 store_errmsg(strbuff, 0);
407 fprintf(stderr, "%s\n", msgbuff);
408 close_file_descriptors();
414 #endif /* using a FD_SETSIZE bound select() */
417 * Old or 'backwards compatible' implementations of stdio do not allow
418 * handling of streams with an underlying file descriptor number greater
419 * than 255, even when allowing high numbered file descriptors for sockets.
420 * At this point we have a big number of file descriptors which have been
421 * opened using dup(), so lets test the stdio implementation and discover
422 * if it is capable of fopen()ing some additional files.
425 if (!fopen_works()) {
426 sprintf(strbuff1, fmt, num_open.rlim_max);
427 sprintf(strbuff, "stdio fopen() fails with %s fds open()",
429 fprintf(stderr, "%s\n", msgbuff);
430 sprintf(strbuff, "stdio fopen() fails with lots of fds open()");
431 store_errmsg(strbuff, 0);
432 close_file_descriptors();
437 /* free the chunk of memory we were reserving so that it
438 becomes becomes available to the test */
442 /* close file descriptors unless instructed to keep them */
445 close_file_descriptors();
456 if(!strcmp(URL, "check")) {
457 /* used by the test script to ask if we can run this test or not */
459 fprintf(stdout, "rlimit problem: %s\n", msgbuff);
462 return 0; /* sure, run this! */
467 return TEST_ERR_MAJOR_BAD;
470 /* run the test with the bunch of open file descriptors
471 and close them all once the test is over */
473 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
474 fprintf(stderr, "curl_global_init() failed\n");
475 close_file_descriptors();
476 return TEST_ERR_MAJOR_BAD;
479 if ((curl = curl_easy_init()) == NULL) {
480 fprintf(stderr, "curl_easy_init() failed\n");
481 close_file_descriptors();
482 curl_global_cleanup();
483 return TEST_ERR_MAJOR_BAD;
486 test_setopt(curl, CURLOPT_URL, URL);
487 test_setopt(curl, CURLOPT_HEADER, 1L);
489 res = curl_easy_perform(curl);
493 close_file_descriptors();
494 curl_easy_cleanup(curl);
495 curl_global_cleanup();
500 #else /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */
505 printf("system lacks necessary system function(s)");
506 return 1; /* skip test */
509 #endif /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */