Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / tests / libtest / lib518.c
blobab2d08ec900c2b77f6363a7107978e93b782e947
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: lib518.c,v 1.1.1.1 2008-09-23 16:32:06 hoffman Exp $
9 */
11 #include "test.h"
13 #ifdef HAVE_SYS_TYPES_H
14 #include <sys/types.h>
15 #endif
16 #ifdef HAVE_SYS_RESOURCE_H
17 #include <sys/resource.h>
18 #endif
19 #ifdef HAVE_FCNTL_H
20 #include <fcntl.h>
21 #endif
22 #ifdef HAVE_LIMITS_H
23 #include <limits.h>
24 #endif
25 #ifdef HAVE_STRING_H
26 #include <string.h>
27 #endif
29 #ifndef FD_SETSIZE
30 #error "this test requires FD_SETSIZE"
31 #endif
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"
39 #else
40 #define DEV_NULL "/dev/null"
41 #endif
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)
51 if (!err)
52 sprintf(msgbuff, "%s", msg);
53 else
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;
61 num_open.rlim_cur++)
62 if (fd[num_open.rlim_cur] > 0)
63 close(fd[num_open.rlim_cur]);
64 free(fd);
65 fd = NULL;
68 static int fopen_works(void)
70 FILE *fpa[3];
71 int i;
72 int ret = 1;
74 for (i = 0; i < 3; i++) {
75 fpa[i] = NULL;
77 for (i = 0; i < 3; i++) {
78 fpa[i] = fopen(DEV_NULL, "r");
79 if (fpa[i] == NULL) {
80 store_errmsg("fopen() failed", ERRNO);
81 fprintf(stderr, "%s\n", msgbuff);
82 ret = 0;
83 break;
86 for (i = 0; i < 3; i++) {
87 if (fpa[i] != NULL)
88 fclose(fpa[i]);
90 return ret;
93 static int rlimit(int keep_open)
95 int nitems, i;
96 int *memchunk = NULL;
97 char *fmt;
98 struct rlimit rl;
99 char strbuff[256];
100 char strbuff1[81];
101 char strbuff2[81];
102 char fmt_u[] = "%u";
103 char fmt_lu[] = "%lu";
104 #ifdef HAVE_LONGLONG
105 char fmt_llu[] = "%llu";
107 if (sizeof(rl.rlim_max) > sizeof(long))
108 fmt = fmt_llu;
109 else
110 #endif
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);
118 return -1;
121 /* show initial open file limits */
123 #ifdef RLIM_INFINITY
124 if (rl.rlim_cur == RLIM_INFINITY)
125 strcpy(strbuff, "INFINITY");
126 else
127 #endif
128 sprintf(strbuff, fmt, rl.rlim_cur);
129 fprintf(stderr, "initial soft limit: %s\n", strbuff);
131 #ifdef RLIM_INFINITY
132 if (rl.rlim_max == RLIM_INFINITY)
133 strcpy(strbuff, "INFINITY");
134 else
135 #endif
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) {
156 #ifdef OPEN_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);
165 msgbuff[0] = '\0';
168 #endif
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);
176 msgbuff[0] = '\0';
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);
184 return -3;
187 /* show current open file limits */
189 #ifdef RLIM_INFINITY
190 if (rl.rlim_cur == RLIM_INFINITY)
191 strcpy(strbuff, "INFINITY");
192 else
193 #endif
194 sprintf(strbuff, fmt, rl.rlim_cur);
195 fprintf(stderr, "current soft limit: %s\n", strbuff);
197 #ifdef RLIM_INFINITY
198 if (rl.rlim_max == RLIM_INFINITY)
199 strcpy(strbuff, "INFINITY");
200 else
201 #endif
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) &&
225 #ifdef RLIM_INFINITY
226 (rl.rlim_cur != RLIM_INFINITY) &&
227 #endif
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",
232 strbuff1, strbuff2);
233 store_errmsg(strbuff, 0);
234 fprintf(stderr, "%s\n", msgbuff);
235 return -4;
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)
248 nitems = i;
249 if (nitems > 0x7fff)
250 nitems = 0x40000;
251 do {
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);
256 if (!memchunk) {
257 fprintf(stderr, "memchunk, malloc() failed\n");
258 nitems /= 2;
260 } while (nitems && !memchunk);
261 if (!memchunk) {
262 store_errmsg("memchunk, malloc() failed", ERRNO);
263 fprintf(stderr, "%s\n", msgbuff);
264 return -5;
267 /* initialize it to fight lazy allocation */
269 fprintf(stderr, "initializing memchunk array\n");
271 for (i = 0; i < nitems; i++)
272 memchunk[i] = -1;
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);
286 free(memchunk);
287 return -6;
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));
296 if (!fd) {
297 store_errmsg("fd, malloc() failed", ERRNO);
298 fprintf(stderr, "%s\n", msgbuff);
299 free(memchunk);
300 return -7;
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;
309 num_open.rlim_cur++)
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);
318 if (fd[0] < 0) {
319 sprintf(strbuff, "opening of %s failed", DEV_NULL);
320 store_errmsg(strbuff, ERRNO);
321 fprintf(stderr, "%s\n", msgbuff);
322 free(fd);
323 fd = NULL;
324 free(memchunk);
325 return -8;
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",
353 strbuff2, strbuff1);
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;
359 num_open.rlim_cur++)
360 close(fd[num_open.rlim_cur]);
361 free(fd);
362 fd = NULL;
363 free(memchunk);
364 return -9;
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) && \
375 !defined(TPF)
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();
394 free(memchunk);
395 return -10;
398 num_open.rlim_cur = FD_SETSIZE - SAFETY_MARGIN;
399 for (rl.rlim_cur = 0;
400 rl.rlim_cur < num_open.rlim_max;
401 rl.rlim_cur++) {
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();
408 free(memchunk);
409 return -11;
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()",
427 strbuff1);
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();
432 free(memchunk);
433 return -12;
436 /* free the chunk of memory we were reserving so that it
437 becomes becomes available to the test */
439 free(memchunk);
441 /* close file descriptors unless instructed to keep them */
443 if (!keep_open) {
444 close_file_descriptors();
447 return 0;
450 int test(char *URL)
452 CURLcode res;
453 CURL *curl;
455 if(!strcmp(URL, "check")) {
456 /* used by the test script to ask if we can run this test or not */
457 if(rlimit(FALSE)) {
458 fprintf(stdout, "rlimit problem: %s\n", msgbuff);
459 return 1;
461 return 0; /* sure, run this! */
464 if (rlimit(TRUE)) {
465 /* failure */
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();
494 return (int)res;
497 #else /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */
499 int test(char *URL)
501 (void)URL;
502 printf("system lacks necessary system function(s)");
503 return 1; /* skip test */
506 #endif /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */