9e683d9563a5f53b5c7993bb85e4ee25711941b7
[external/curl.git] / tests / libtest / lib518.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  */
9
10 #include "test.h"
11
12 #ifdef HAVE_SYS_TYPES_H
13 #include <sys/types.h>
14 #endif
15 #ifdef HAVE_SYS_RESOURCE_H
16 #include <sys/resource.h>
17 #endif
18 #ifdef HAVE_FCNTL_H
19 #include <fcntl.h>
20 #endif
21 #ifdef HAVE_LIMITS_H
22 #include <limits.h>
23 #endif
24 #ifdef HAVE_STRING_H
25 #include <string.h>
26 #endif
27
28 #include "memdebug.h"
29
30 #ifndef FD_SETSIZE
31 #error "this test requires FD_SETSIZE"
32 #endif
33
34 #define SAFETY_MARGIN (16)
35 #define NUM_OPEN      (FD_SETSIZE + 10)
36 #define NUM_NEEDED    (NUM_OPEN + SAFETY_MARGIN)
37
38 #if defined(WIN32) || defined(_WIN32) || defined(MSDOS)
39 #define DEV_NULL "NUL"
40 #else
41 #define DEV_NULL "/dev/null"
42 #endif
43
44 #if defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT)
45
46 static int *fd = NULL;
47 static struct rlimit num_open;
48 static char msgbuff[256];
49
50 static void store_errmsg(const char *msg, int err)
51 {
52   if (!err)
53     sprintf(msgbuff, "%s", msg);
54   else
55     sprintf(msgbuff, "%s, errno %d, %s", msg, err, strerror(err));
56 }
57
58 static void close_file_descriptors(void)
59 {
60   for (num_open.rlim_cur = 0;
61        num_open.rlim_cur < num_open.rlim_max;
62        num_open.rlim_cur++)
63     if (fd[num_open.rlim_cur] > 0)
64       close(fd[num_open.rlim_cur]);
65   free(fd);
66   fd = NULL;
67 }
68
69 static int fopen_works(void)
70 {
71   FILE *fpa[3];
72   int i;
73   int ret = 1;
74
75   for (i = 0; i < 3; i++) {
76     fpa[i] = NULL;
77   }
78   for (i = 0; i < 3; i++) {
79     fpa[i] = fopen(DEV_NULL, "r");
80     if (fpa[i] == NULL) {
81       store_errmsg("fopen() failed", ERRNO);
82       fprintf(stderr, "%s\n", msgbuff);
83       ret = 0;
84       break;
85     }
86   }
87   for (i = 0; i < 3; i++) {
88     if (fpa[i] != NULL)
89       fclose(fpa[i]);
90   }
91   return ret;
92 }
93
94 static int rlimit(int keep_open)
95 {
96   int nitems, i;
97   int *memchunk = NULL;
98   char *fmt;
99   struct rlimit rl;
100   char strbuff[256];
101   char strbuff1[81];
102   char strbuff2[81];
103   char fmt_u[] = "%u";
104   char fmt_lu[] = "%lu";
105 #ifdef HAVE_LONGLONG
106   char fmt_llu[] = "%llu";
107
108   if (sizeof(rl.rlim_max) > sizeof(long))
109     fmt = fmt_llu;
110   else
111 #endif
112     fmt = (sizeof(rl.rlim_max) < sizeof(long))?fmt_u:fmt_lu;
113
114   /* get initial open file limits */
115
116   if (getrlimit(RLIMIT_NOFILE, &rl) != 0) {
117     store_errmsg("getrlimit() failed", ERRNO);
118     fprintf(stderr, "%s\n", msgbuff);
119     return -1;
120   }
121
122   /* show initial open file limits */
123
124 #ifdef RLIM_INFINITY
125   if (rl.rlim_cur == RLIM_INFINITY)
126     strcpy(strbuff, "INFINITY");
127   else
128 #endif
129     sprintf(strbuff, fmt, rl.rlim_cur);
130   fprintf(stderr, "initial soft limit: %s\n", strbuff);
131
132 #ifdef RLIM_INFINITY
133   if (rl.rlim_max == RLIM_INFINITY)
134     strcpy(strbuff, "INFINITY");
135   else
136 #endif
137     sprintf(strbuff, fmt, rl.rlim_max);
138   fprintf(stderr, "initial hard limit: %s\n", strbuff);
139
140   /* show our constants */
141
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);
145
146   /*
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.
153    */
154
155   if (rl.rlim_cur != rl.rlim_max) {
156
157 #ifdef OPEN_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);
166         msgbuff[0] = '\0';
167       }
168     }
169 #endif
170
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);
177       msgbuff[0] = '\0';
178     }
179
180     /* get current open file limits */
181
182     if (getrlimit(RLIMIT_NOFILE, &rl) != 0) {
183       store_errmsg("getrlimit() failed", ERRNO);
184       fprintf(stderr, "%s\n", msgbuff);
185       return -3;
186     }
187
188     /* show current open file limits */
189
190 #ifdef RLIM_INFINITY
191     if (rl.rlim_cur == RLIM_INFINITY)
192       strcpy(strbuff, "INFINITY");
193     else
194 #endif
195       sprintf(strbuff, fmt, rl.rlim_cur);
196     fprintf(stderr, "current soft limit: %s\n", strbuff);
197
198 #ifdef RLIM_INFINITY
199     if (rl.rlim_max == RLIM_INFINITY)
200       strcpy(strbuff, "INFINITY");
201     else
202 #endif
203       sprintf(strbuff, fmt, rl.rlim_max);
204     fprintf(stderr, "current hard limit: %s\n", strbuff);
205
206   } /* (rl.rlim_cur != rl.rlim_max) */
207
208   /*
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.
214    */
215
216   /*
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
221    */
222
223   num_open.rlim_cur = NUM_NEEDED;
224
225   if ((rl.rlim_cur > 0) &&
226 #ifdef RLIM_INFINITY
227      (rl.rlim_cur != RLIM_INFINITY) &&
228 #endif
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",
233             strbuff1, strbuff2);
234     store_errmsg(strbuff, 0);
235     fprintf(stderr, "%s\n", msgbuff);
236     return -4;
237   }
238
239   /*
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.
246    */
247
248   for (nitems = i = 1; nitems <= i; i *= 2)
249     nitems = i;
250   if (nitems > 0x7fff)
251     nitems = 0x40000;
252   do {
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);
257     if (!memchunk) {
258       fprintf(stderr, "memchunk, malloc() failed\n");
259       nitems /= 2;
260     }
261   } while (nitems && !memchunk);
262   if (!memchunk) {
263     store_errmsg("memchunk, malloc() failed", ERRNO);
264     fprintf(stderr, "%s\n", msgbuff);
265     return -5;
266   }
267
268   /* initialize it to fight lazy allocation */
269
270   fprintf(stderr, "initializing memchunk array\n");
271
272   for (i = 0; i < nitems; i++)
273     memchunk[i] = -1;
274
275   /* set the number of file descriptors we will try to open */
276
277   num_open.rlim_max = NUM_OPEN;
278
279   /* verify that we won't overflow size_t in malloc() */
280
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);
287     free(memchunk);
288     return -6;
289   }
290
291   /* allocate array for file descriptors */
292
293   sprintf(strbuff, fmt, num_open.rlim_max);
294   fprintf(stderr, "allocating array for %s file descriptors\n", strbuff);
295
296   fd = malloc(sizeof(*fd) * (size_t)(num_open.rlim_max));
297   if (!fd) {
298     store_errmsg("fd, malloc() failed", ERRNO);
299     fprintf(stderr, "%s\n", msgbuff);
300     free(memchunk);
301     return -7;
302   }
303
304   /* initialize it to fight lazy allocation */
305
306   fprintf(stderr, "initializing fd array\n");
307
308   for (num_open.rlim_cur = 0;
309        num_open.rlim_cur < num_open.rlim_max;
310        num_open.rlim_cur++)
311     fd[num_open.rlim_cur] = -1;
312
313   sprintf(strbuff, fmt, num_open.rlim_max);
314   fprintf(stderr, "trying to open %s file descriptors\n", strbuff);
315
316   /* open a dummy descriptor */
317
318   fd[0] = open(DEV_NULL, O_RDONLY);
319   if (fd[0] < 0) {
320     sprintf(strbuff, "opening of %s failed", DEV_NULL);
321     store_errmsg(strbuff, ERRNO);
322     fprintf(stderr, "%s\n", msgbuff);
323     free(fd);
324     fd = NULL;
325     free(memchunk);
326     return -8;
327   }
328
329   /* create a bunch of file descriptors */
330
331   for (num_open.rlim_cur = 1;
332        num_open.rlim_cur < num_open.rlim_max;
333        num_open.rlim_cur++) {
334
335     fd[num_open.rlim_cur] = dup(fd[0]);
336
337     if (fd[num_open.rlim_cur] < 0) {
338
339       fd[num_open.rlim_cur] = -1;
340
341       sprintf(strbuff1, fmt, num_open.rlim_cur);
342       sprintf(strbuff, "dup() attempt %s failed", strbuff1);
343       fprintf(stderr, "%s\n", strbuff);
344
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);
348
349       num_open.rlim_max = NUM_NEEDED;
350
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",
354               strbuff2, strbuff1);
355       store_errmsg(strbuff, 0);
356       fprintf(stderr, "%s\n", msgbuff);
357
358       for (num_open.rlim_cur = 0;
359            fd[num_open.rlim_cur] >= 0;
360            num_open.rlim_cur++)
361         close(fd[num_open.rlim_cur]);
362       free(fd);
363       fd = NULL;
364       free(memchunk);
365       return -9;
366
367     }
368
369   }
370
371   sprintf(strbuff, fmt, num_open.rlim_max);
372   fprintf(stderr, "%s file descriptors open\n", strbuff);
373
374 #if !defined(HAVE_POLL_FINE)    && \
375     !defined(USE_WINSOCK)       && \
376     !defined(TPF)
377
378   /*
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.
387    */
388
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();
395     free(memchunk);
396     return -10;
397   }
398
399   num_open.rlim_cur = FD_SETSIZE - SAFETY_MARGIN;
400   for (rl.rlim_cur = 0;
401        rl.rlim_cur < num_open.rlim_max;
402        rl.rlim_cur++) {
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();
409       free(memchunk);
410       return -11;
411     }
412   }
413
414 #endif /* using a FD_SETSIZE bound select() */
415
416   /*
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.
423    */
424
425   if (!fopen_works()) {
426     sprintf(strbuff1, fmt, num_open.rlim_max);
427     sprintf(strbuff, "stdio fopen() fails with %s fds open()",
428             strbuff1);
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();
433     free(memchunk);
434     return -12;
435   }
436
437   /* free the chunk of memory we were reserving so that it
438      becomes becomes available to the test */
439
440   free(memchunk);
441
442   /* close file descriptors unless instructed to keep them */
443
444   if (!keep_open) {
445     close_file_descriptors();
446   }
447
448   return 0;
449 }
450
451 int test(char *URL)
452 {
453   CURLcode res;
454   CURL *curl;
455
456   if(!strcmp(URL, "check")) {
457     /* used by the test script to ask if we can run this test or not */
458     if(rlimit(FALSE)) {
459       fprintf(stdout, "rlimit problem: %s\n", msgbuff);
460       return 1;
461     }
462     return 0; /* sure, run this! */
463   }
464
465   if (rlimit(TRUE)) {
466     /* failure */
467     return TEST_ERR_MAJOR_BAD;
468   }
469
470   /* run the test with the bunch of open file descriptors
471      and close them all once the test is over */
472
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;
477   }
478
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;
484   }
485
486   test_setopt(curl, CURLOPT_URL, URL);
487   test_setopt(curl, CURLOPT_HEADER, 1L);
488
489   res = curl_easy_perform(curl);
490
491 test_cleanup:
492
493   close_file_descriptors();
494   curl_easy_cleanup(curl);
495   curl_global_cleanup();
496
497   return (int)res;
498 }
499
500 #else /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */
501
502 int test(char *URL)
503 {
504   (void)URL;
505   printf("system lacks necessary system function(s)");
506   return 1; /* skip test */
507 }
508
509 #endif /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */