1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: lib518.c,v 1.1.1.1 2008-09-23 16:32:06 hoffman Exp $
13 #ifdef HAVE_SYS_TYPES_H
14 #include <sys/types.h>
16 #ifdef HAVE_SYS_RESOURCE_H
17 #include <sys/resource.h>
30 #error "this test requires FD_SETSIZE"
33 #define SAFETY_MARGIN (16)
34 #define NUM_OPEN (FD_SETSIZE + 10)
35 #define NUM_NEEDED (NUM_OPEN + SAFETY_MARGIN)
37 #if defined(WIN32) || defined(_WIN32) || defined(MSDOS)
38 #define DEV_NULL "NUL"
40 #define DEV_NULL "/dev/null"
43 #if defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT)
45 static int *fd
= NULL
;
46 static struct rlimit num_open
;
47 static char msgbuff
[256];
49 static void store_errmsg(const char *msg
, int err
)
52 sprintf(msgbuff
, "%s", msg
);
54 sprintf(msgbuff
, "%s, errno %d, %s", msg
, err
, strerror(err
));
57 static void close_file_descriptors(void)
59 for (num_open
.rlim_cur
= 0;
60 num_open
.rlim_cur
< num_open
.rlim_max
;
62 if (fd
[num_open
.rlim_cur
] > 0)
63 close(fd
[num_open
.rlim_cur
]);
68 static int fopen_works(void)
74 for (i
= 0; i
< 3; i
++) {
77 for (i
= 0; i
< 3; i
++) {
78 fpa
[i
] = fopen(DEV_NULL
, "r");
80 store_errmsg("fopen() failed", ERRNO
);
81 fprintf(stderr
, "%s\n", msgbuff
);
86 for (i
= 0; i
< 3; i
++) {
93 static int rlimit(int keep_open
)
103 char fmt_lu
[] = "%lu";
105 char fmt_llu
[] = "%llu";
107 if (sizeof(rl
.rlim_max
) > sizeof(long))
111 fmt
= (sizeof(rl
.rlim_max
) < sizeof(long))?fmt_u
:fmt_lu
;
113 /* get initial open file limits */
115 if (getrlimit(RLIMIT_NOFILE
, &rl
) != 0) {
116 store_errmsg("getrlimit() failed", ERRNO
);
117 fprintf(stderr
, "%s\n", msgbuff
);
121 /* show initial open file limits */
124 if (rl
.rlim_cur
== RLIM_INFINITY
)
125 strcpy(strbuff
, "INFINITY");
128 sprintf(strbuff
, fmt
, rl
.rlim_cur
);
129 fprintf(stderr
, "initial soft limit: %s\n", strbuff
);
132 if (rl
.rlim_max
== RLIM_INFINITY
)
133 strcpy(strbuff
, "INFINITY");
136 sprintf(strbuff
, fmt
, rl
.rlim_max
);
137 fprintf(stderr
, "initial hard limit: %s\n", strbuff
);
139 /* show our constants */
141 fprintf(stderr
, "test518 FD_SETSIZE: %d\n", FD_SETSIZE
);
142 fprintf(stderr
, "test518 NUM_OPEN : %d\n", NUM_OPEN
);
143 fprintf(stderr
, "test518 NUM_NEEDED: %d\n", NUM_NEEDED
);
146 * if soft limit and hard limit are different we ask the
147 * system to raise soft limit all the way up to the hard
148 * limit. Due to some other system limit the soft limit
149 * might not be raised up to the hard limit. So from this
150 * point the resulting soft limit is our limit. Trying to
151 * open more than soft limit file descriptors will fail.
154 if (rl
.rlim_cur
!= rl
.rlim_max
) {
157 if ((rl
.rlim_cur
> 0) &&
158 (rl
.rlim_cur
< OPEN_MAX
)) {
159 fprintf(stderr
, "raising soft limit up to OPEN_MAX\n");
160 rl
.rlim_cur
= OPEN_MAX
;
161 if (setrlimit(RLIMIT_NOFILE
, &rl
) != 0) {
162 /* on failure don't abort just issue a warning */
163 store_errmsg("setrlimit() failed", ERRNO
);
164 fprintf(stderr
, "%s\n", msgbuff
);
170 fprintf(stderr
, "raising soft limit up to hard limit\n");
171 rl
.rlim_cur
= rl
.rlim_max
;
172 if (setrlimit(RLIMIT_NOFILE
, &rl
) != 0) {
173 /* on failure don't abort just issue a warning */
174 store_errmsg("setrlimit() failed", ERRNO
);
175 fprintf(stderr
, "%s\n", msgbuff
);
179 /* get current open file limits */
181 if (getrlimit(RLIMIT_NOFILE
, &rl
) != 0) {
182 store_errmsg("getrlimit() failed", ERRNO
);
183 fprintf(stderr
, "%s\n", msgbuff
);
187 /* show current open file limits */
190 if (rl
.rlim_cur
== RLIM_INFINITY
)
191 strcpy(strbuff
, "INFINITY");
194 sprintf(strbuff
, fmt
, rl
.rlim_cur
);
195 fprintf(stderr
, "current soft limit: %s\n", strbuff
);
198 if (rl
.rlim_max
== RLIM_INFINITY
)
199 strcpy(strbuff
, "INFINITY");
202 sprintf(strbuff
, fmt
, rl
.rlim_max
);
203 fprintf(stderr
, "current hard limit: %s\n", strbuff
);
205 } /* (rl.rlim_cur != rl.rlim_max) */
208 * test 518 is all about testing libcurl functionality
209 * when more than FD_SETSIZE file descriptors are open.
210 * This means that if for any reason we are not able to
211 * open more than FD_SETSIZE file descriptors then test
212 * 518 should not be run.
216 * verify that soft limit is higher than NUM_NEEDED,
217 * which is the number of file descriptors we would
218 * try to open plus SAFETY_MARGIN to not exhaust the
219 * file descriptor pool
222 num_open
.rlim_cur
= NUM_NEEDED
;
224 if ((rl
.rlim_cur
> 0) &&
226 (rl
.rlim_cur
!= RLIM_INFINITY
) &&
228 (rl
.rlim_cur
<= num_open
.rlim_cur
)) {
229 sprintf(strbuff2
, fmt
, rl
.rlim_cur
);
230 sprintf(strbuff1
, fmt
, num_open
.rlim_cur
);
231 sprintf(strbuff
, "fds needed %s > system limit %s",
233 store_errmsg(strbuff
, 0);
234 fprintf(stderr
, "%s\n", msgbuff
);
239 * reserve a chunk of memory before opening file descriptors to
240 * avoid a low memory condition once the file descriptors are
241 * open. System conditions that could make the test fail should
242 * be addressed in the precheck phase. This chunk of memory shall
243 * be always free()ed before exiting the rlimit() function so
244 * that it becomes available to the test.
247 for (nitems
= i
= 1; nitems
<= i
; i
*= 2)
252 num_open
.rlim_max
= sizeof(*memchunk
) * (size_t)nitems
;
253 sprintf(strbuff
, fmt
, num_open
.rlim_max
);
254 fprintf(stderr
, "allocating memchunk %s byte array\n", strbuff
);
255 memchunk
= malloc(sizeof(*memchunk
) * (size_t)nitems
);
257 fprintf(stderr
, "memchunk, malloc() failed\n");
260 } while (nitems
&& !memchunk
);
262 store_errmsg("memchunk, malloc() failed", ERRNO
);
263 fprintf(stderr
, "%s\n", msgbuff
);
267 /* initialize it to fight lazy allocation */
269 fprintf(stderr
, "initializing memchunk array\n");
271 for (i
= 0; i
< nitems
; i
++)
274 /* set the number of file descriptors we will try to open */
276 num_open
.rlim_max
= NUM_OPEN
;
278 /* verify that we won't overflow size_t in malloc() */
280 if ((size_t)(num_open
.rlim_max
) > ((size_t)-1) / sizeof(*fd
)) {
281 sprintf(strbuff1
, fmt
, num_open
.rlim_max
);
282 sprintf(strbuff
, "unable to allocate an array for %s "
283 "file descriptors, would overflow size_t", strbuff1
);
284 store_errmsg(strbuff
, 0);
285 fprintf(stderr
, "%s\n", msgbuff
);
290 /* allocate array for file descriptors */
292 sprintf(strbuff
, fmt
, num_open
.rlim_max
);
293 fprintf(stderr
, "allocating array for %s file descriptors\n", strbuff
);
295 fd
= malloc(sizeof(*fd
) * (size_t)(num_open
.rlim_max
));
297 store_errmsg("fd, malloc() failed", ERRNO
);
298 fprintf(stderr
, "%s\n", msgbuff
);
303 /* initialize it to fight lazy allocation */
305 fprintf(stderr
, "initializing fd array\n");
307 for (num_open
.rlim_cur
= 0;
308 num_open
.rlim_cur
< num_open
.rlim_max
;
310 fd
[num_open
.rlim_cur
] = -1;
312 sprintf(strbuff
, fmt
, num_open
.rlim_max
);
313 fprintf(stderr
, "trying to open %s file descriptors\n", strbuff
);
315 /* open a dummy descriptor */
317 fd
[0] = open(DEV_NULL
, O_RDONLY
);
319 sprintf(strbuff
, "opening of %s failed", DEV_NULL
);
320 store_errmsg(strbuff
, ERRNO
);
321 fprintf(stderr
, "%s\n", msgbuff
);
328 /* create a bunch of file descriptors */
330 for (num_open
.rlim_cur
= 1;
331 num_open
.rlim_cur
< num_open
.rlim_max
;
332 num_open
.rlim_cur
++) {
334 fd
[num_open
.rlim_cur
] = dup(fd
[0]);
336 if (fd
[num_open
.rlim_cur
] < 0) {
338 fd
[num_open
.rlim_cur
] = -1;
340 sprintf(strbuff1
, fmt
, num_open
.rlim_cur
);
341 sprintf(strbuff
, "dup() attempt %s failed", strbuff1
);
342 fprintf(stderr
, "%s\n", strbuff
);
344 sprintf(strbuff1
, fmt
, num_open
.rlim_cur
);
345 sprintf(strbuff
, "fds system limit seems close to %s", strbuff1
);
346 fprintf(stderr
, "%s\n", strbuff
);
348 num_open
.rlim_max
= NUM_NEEDED
;
350 sprintf(strbuff2
, fmt
, num_open
.rlim_max
);
351 sprintf(strbuff1
, fmt
, num_open
.rlim_cur
);
352 sprintf(strbuff
, "fds needed %s > system limit %s",
354 store_errmsg(strbuff
, 0);
355 fprintf(stderr
, "%s\n", msgbuff
);
357 for (num_open
.rlim_cur
= 0;
358 fd
[num_open
.rlim_cur
] >= 0;
360 close(fd
[num_open
.rlim_cur
]);
370 sprintf(strbuff
, fmt
, num_open
.rlim_max
);
371 fprintf(stderr
, "%s file descriptors open\n", strbuff
);
373 #if !defined(HAVE_POLL_FINE) && \
374 !defined(USE_WINSOCK) && \
378 * when using select() instead of poll() we cannot test
379 * libcurl functionality with a socket number equal or
380 * greater than FD_SETSIZE. In any case, macro VERIFY_SOCK
381 * in lib/select.c enforces this check and protects libcurl
382 * from a possible crash. The effect of this protection
383 * is that test 518 will always fail, since the actual
384 * call to select() never takes place. We skip test 518
385 * with an indication that select limit would be exceeded.
388 num_open
.rlim_cur
= FD_SETSIZE
- SAFETY_MARGIN
;
389 if (num_open
.rlim_max
> num_open
.rlim_cur
) {
390 sprintf(strbuff
, "select limit is FD_SETSIZE %d", FD_SETSIZE
);
391 store_errmsg(strbuff
, 0);
392 fprintf(stderr
, "%s\n", msgbuff
);
393 close_file_descriptors();
398 num_open
.rlim_cur
= FD_SETSIZE
- SAFETY_MARGIN
;
399 for (rl
.rlim_cur
= 0;
400 rl
.rlim_cur
< num_open
.rlim_max
;
402 if ((fd
[rl
.rlim_cur
] > 0) &&
403 ((unsigned int)fd
[rl
.rlim_cur
] > num_open
.rlim_cur
)) {
404 sprintf(strbuff
, "select limit is FD_SETSIZE %d", FD_SETSIZE
);
405 store_errmsg(strbuff
, 0);
406 fprintf(stderr
, "%s\n", msgbuff
);
407 close_file_descriptors();
413 #endif /* using a FD_SETSIZE bound select() */
416 * Old or 'backwards compatible' implementations of stdio do not allow
417 * handling of streams with an underlying file descriptor number greater
418 * than 255, even when allowing high numbered file descriptors for sockets.
419 * At this point we have a big number of file descriptors which have been
420 * opened using dup(), so lets test the stdio implementation and discover
421 * if it is capable of fopen()ing some additional files.
424 if (!fopen_works()) {
425 sprintf(strbuff1
, fmt
, num_open
.rlim_max
);
426 sprintf(strbuff
, "stdio fopen() fails with %s fds open()",
428 fprintf(stderr
, "%s\n", msgbuff
);
429 sprintf(strbuff
, "stdio fopen() fails with lots of fds open()");
430 store_errmsg(strbuff
, 0);
431 close_file_descriptors();
436 /* free the chunk of memory we were reserving so that it
437 becomes becomes available to the test */
441 /* close file descriptors unless instructed to keep them */
444 close_file_descriptors();
455 if(!strcmp(URL
, "check")) {
456 /* used by the test script to ask if we can run this test or not */
458 fprintf(stdout
, "rlimit problem: %s\n", msgbuff
);
461 return 0; /* sure, run this! */
466 return TEST_ERR_MAJOR_BAD
;
469 /* run the test with the bunch of open file descriptors
470 and close them all once the test is over */
472 if (curl_global_init(CURL_GLOBAL_ALL
) != CURLE_OK
) {
473 fprintf(stderr
, "curl_global_init() failed\n");
474 close_file_descriptors();
475 return TEST_ERR_MAJOR_BAD
;
478 if ((curl
= curl_easy_init()) == NULL
) {
479 fprintf(stderr
, "curl_easy_init() failed\n");
480 close_file_descriptors();
481 curl_global_cleanup();
482 return TEST_ERR_MAJOR_BAD
;
485 curl_easy_setopt(curl
, CURLOPT_URL
, URL
);
486 curl_easy_setopt(curl
, CURLOPT_HEADER
, 1L);
488 res
= curl_easy_perform(curl
);
490 close_file_descriptors();
491 curl_easy_cleanup(curl
);
492 curl_global_cleanup();
497 #else /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */
502 printf("system lacks necessary system function(s)");
503 return 1; /* skip test */
506 #endif /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */