xref: /curl/tests/libtest/lib537.c (revision c72cefea)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
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 https://curl.se/docs/copyright.html.
13  *
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.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 #include "test.h"
25 
26 #ifdef HAVE_SYS_RESOURCE_H
27 #include <sys/resource.h>
28 #endif
29 #ifdef HAVE_FCNTL_H
30 #include <fcntl.h>
31 #endif
32 #include <limits.h>
33 
34 #include "warnless.h"
35 #include "memdebug.h"
36 
37 #if !defined(HAVE_POLL) && !defined(USE_WINSOCK) && !defined(FD_SETSIZE)
38 #error "this test requires FD_SETSIZE"
39 #endif
40 
41 #define SAFETY_MARGIN (11)
42 
43 #if defined(_WIN32) || defined(MSDOS)
44 #define DEV_NULL "NUL"
45 #else
46 #define DEV_NULL "/dev/null"
47 #endif
48 
49 #if defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT)
50 
51 static int *testfd = NULL;
52 static struct rlimit num_open;
53 static char msgbuff[256];
54 
store_errmsg(const char * msg,int err)55 static void store_errmsg(const char *msg, int err)
56 {
57   if(!err)
58     msnprintf(msgbuff, sizeof(msgbuff), "%s", msg);
59   else
60     msnprintf(msgbuff, sizeof(msgbuff), "%s, errno %d, %s", msg,
61               err, strerror(err));
62 }
63 
close_file_descriptors(void)64 static void close_file_descriptors(void)
65 {
66   for(num_open.rlim_cur = 0;
67       num_open.rlim_cur < num_open.rlim_max;
68       num_open.rlim_cur++)
69     if(testfd[num_open.rlim_cur] > 0)
70       close(testfd[num_open.rlim_cur]);
71   free(testfd);
72   testfd = NULL;
73 }
74 
fopen_works(void)75 static int fopen_works(void)
76 {
77   FILE *fpa[3];
78   int i;
79   int ret = 1;
80 
81   for(i = 0; i < 3; i++) {
82     fpa[i] = NULL;
83   }
84   for(i = 0; i < 3; i++) {
85     fpa[i] = fopen(DEV_NULL, FOPEN_READTEXT);
86     if(!fpa[i]) {
87       store_errmsg("fopen failed", errno);
88       fprintf(stderr, "%s\n", msgbuff);
89       ret = 0;
90       break;
91     }
92   }
93   for(i = 0; i < 3; i++) {
94     if(fpa[i])
95       fclose(fpa[i]);
96   }
97   return ret;
98 }
99 
rlim2str(char * buf,size_t len,rlim_t val)100 static void rlim2str(char *buf, size_t len, rlim_t val)
101 {
102 #ifdef RLIM_INFINITY
103   if(val == RLIM_INFINITY) {
104     msnprintf(buf, len, "INFINITY");
105     return;
106   }
107 #endif
108 #ifdef HAVE_LONGLONG
109   if(sizeof(rlim_t) > sizeof(long))
110     msnprintf(buf, len, "%llu", (unsigned long long)val);
111   else
112 #endif
113   {
114     if(sizeof(rlim_t) < sizeof(long))
115       msnprintf(buf, len, "%u", (unsigned int)val);
116     else
117       msnprintf(buf, len, "%lu", (unsigned long)val);
118   }
119 }
120 
test_rlimit(int keep_open)121 static int test_rlimit(int keep_open)
122 {
123   int *tmpfd;
124   rlim_t nitems, i;
125   int *memchunk = NULL;
126   struct rlimit rl;
127   char strbuff[256];
128   char strbuff1[81];
129 
130   /* get initial open file limits */
131 
132   if(getrlimit(RLIMIT_NOFILE, &rl) != 0) {
133     store_errmsg("getrlimit() failed", errno);
134     fprintf(stderr, "%s\n", msgbuff);
135     return -1;
136   }
137 
138   /* show initial open file limits */
139 
140   rlim2str(strbuff, sizeof(strbuff), rl.rlim_cur);
141   fprintf(stderr, "initial soft limit: %s\n", strbuff);
142 
143   rlim2str(strbuff, sizeof(strbuff), rl.rlim_max);
144   fprintf(stderr, "initial hard limit: %s\n", strbuff);
145 
146   /* If the OS allows a HUGE number of open files, we do not run.
147    * Modern debian sid reports a limit of 134217724 and this tests
148    * takes minutes. */
149 #define LIMIT_CAP     (256*1024)
150   if(rl.rlim_cur > LIMIT_CAP) {
151     fprintf(stderr, "soft limit above %ld, not running\n", (long)LIMIT_CAP);
152     return -2;
153   }
154 
155   /*
156    * if soft limit and hard limit are different we ask the
157    * system to raise soft limit all the way up to the hard
158    * limit. Due to some other system limit the soft limit
159    * might not be raised up to the hard limit. So from this
160    * point the resulting soft limit is our limit. Trying to
161    * open more than soft limit file descriptors will fail.
162    */
163 
164   if(rl.rlim_cur != rl.rlim_max) {
165 
166 #ifdef OPEN_MAX
167     if((rl.rlim_cur > 0) &&
168        (rl.rlim_cur < OPEN_MAX)) {
169       fprintf(stderr, "raising soft limit up to OPEN_MAX\n");
170       rl.rlim_cur = OPEN_MAX;
171       if(setrlimit(RLIMIT_NOFILE, &rl) != 0) {
172         /* on failure don't abort just issue a warning */
173         store_errmsg("setrlimit() failed", errno);
174         fprintf(stderr, "%s\n", msgbuff);
175         msgbuff[0] = '\0';
176       }
177     }
178 #endif
179 
180     fprintf(stderr, "raising soft limit up to hard limit\n");
181     rl.rlim_cur = rl.rlim_max;
182     if(setrlimit(RLIMIT_NOFILE, &rl) != 0) {
183       /* on failure don't abort just issue a warning */
184       store_errmsg("setrlimit() failed", errno);
185       fprintf(stderr, "%s\n", msgbuff);
186       msgbuff[0] = '\0';
187     }
188 
189     /* get current open file limits */
190 
191     if(getrlimit(RLIMIT_NOFILE, &rl) != 0) {
192       store_errmsg("getrlimit() failed", errno);
193       fprintf(stderr, "%s\n", msgbuff);
194       return -3;
195     }
196 
197     /* show current open file limits */
198 
199     rlim2str(strbuff, sizeof(strbuff), rl.rlim_cur);
200     fprintf(stderr, "current soft limit: %s\n", strbuff);
201 
202     rlim2str(strbuff, sizeof(strbuff), rl.rlim_max);
203     fprintf(stderr, "current hard limit: %s\n", strbuff);
204 
205   } /* (rl.rlim_cur != rl.rlim_max) */
206 
207   /*
208    * test 537 is all about testing libcurl functionality
209    * when the system has nearly exhausted the number of
210    * available file descriptors. Test 537 will try to run
211    * with a very small number of file descriptors available.
212    * This implies that any file descriptor which is open
213    * when the test runs will have a number in the high range
214    * of whatever the system supports.
215    */
216 
217   /*
218    * reserve a chunk of memory before opening file descriptors to
219    * avoid a low memory condition once the file descriptors are
220    * open. System conditions that could make the test fail should
221    * be addressed in the precheck phase. This chunk of memory shall
222    * be always free()ed before exiting the test_rlimit() function so
223    * that it becomes available to the test.
224    */
225 
226   for(nitems = i = 1; nitems <= i; i *= 2)
227     nitems = i;
228   if(nitems > 0x7fff)
229     nitems = 0x40000;
230   do {
231     num_open.rlim_max = sizeof(*memchunk) * nitems;
232     rlim2str(strbuff, sizeof(strbuff), num_open.rlim_max);
233     fprintf(stderr, "allocating memchunk %s byte array\n", strbuff);
234     memchunk = malloc(sizeof(*memchunk) * (size_t)nitems);
235     if(!memchunk) {
236       fprintf(stderr, "memchunk, malloc() failed\n");
237       nitems /= 2;
238     }
239   } while(nitems && !memchunk);
240   if(!memchunk) {
241     store_errmsg("memchunk, malloc() failed", errno);
242     fprintf(stderr, "%s\n", msgbuff);
243     return -4;
244   }
245 
246   /* initialize it to fight lazy allocation */
247 
248   fprintf(stderr, "initializing memchunk array\n");
249 
250   for(i = 0; i < nitems; i++)
251     memchunk[i] = -1;
252 
253   /* set the number of file descriptors we will try to open */
254 
255 #ifdef RLIM_INFINITY
256   if((rl.rlim_cur > 0) && (rl.rlim_cur != RLIM_INFINITY)) {
257 #else
258   if(rl.rlim_cur > 0) {
259 #endif
260     /* soft limit minus SAFETY_MARGIN */
261     num_open.rlim_max = rl.rlim_cur - SAFETY_MARGIN;
262   }
263   else {
264     /* a huge number of file descriptors */
265     for(nitems = i = 1; nitems <= i; i *= 2)
266       nitems = i;
267     if(nitems > 0x7fff)
268       nitems = 0x40000;
269     num_open.rlim_max = nitems;
270   }
271 
272   /* verify that we won't overflow size_t in malloc() */
273 
274   if((size_t)(num_open.rlim_max) > ((size_t)-1) / sizeof(*testfd)) {
275     rlim2str(strbuff1, sizeof(strbuff1), num_open.rlim_max);
276     msnprintf(strbuff, sizeof(strbuff), "unable to allocate an array for %s "
277               "file descriptors, would overflow size_t", strbuff1);
278     store_errmsg(strbuff, 0);
279     fprintf(stderr, "%s\n", msgbuff);
280     free(memchunk);
281     return -5;
282   }
283 
284   /* allocate array for file descriptors */
285 
286   do {
287     rlim2str(strbuff, sizeof(strbuff), num_open.rlim_max);
288     fprintf(stderr, "allocating array for %s file descriptors\n", strbuff);
289 
290     testfd = malloc(sizeof(*testfd) * (size_t)(num_open.rlim_max));
291     if(!testfd) {
292       fprintf(stderr, "testfd, malloc() failed\n");
293       num_open.rlim_max /= 2;
294     }
295   } while(num_open.rlim_max && !testfd);
296   if(!testfd) {
297     store_errmsg("testfd, malloc() failed", errno);
298     fprintf(stderr, "%s\n", msgbuff);
299     free(memchunk);
300     return -6;
301   }
302 
303   /* initialize it to fight lazy allocation */
304 
305   fprintf(stderr, "initializing testfd array\n");
306 
307   for(num_open.rlim_cur = 0;
308       num_open.rlim_cur < num_open.rlim_max;
309       num_open.rlim_cur++)
310     testfd[num_open.rlim_cur] = -1;
311 
312   rlim2str(strbuff, sizeof(strbuff), num_open.rlim_max);
313   fprintf(stderr, "trying to open %s file descriptors\n", strbuff);
314 
315   /* open a dummy descriptor */
316 
317   testfd[0] = open(DEV_NULL, O_RDONLY);
318   if(testfd[0] < 0) {
319     msnprintf(strbuff, sizeof(strbuff), "opening of %s failed", DEV_NULL);
320     store_errmsg(strbuff, errno);
321     fprintf(stderr, "%s\n", msgbuff);
322     free(testfd);
323     testfd = NULL;
324     free(memchunk);
325     return -7;
326   }
327 
328   /* create a bunch of file descriptors */
329 
330   for(num_open.rlim_cur = 1;
331       num_open.rlim_cur < num_open.rlim_max;
332       num_open.rlim_cur++) {
333 
334     testfd[num_open.rlim_cur] = dup(testfd[0]);
335 
336     if(testfd[num_open.rlim_cur] < 0) {
337 
338       testfd[num_open.rlim_cur] = -1;
339 
340       rlim2str(strbuff1, sizeof(strbuff1), num_open.rlim_cur);
341       msnprintf(strbuff, sizeof(strbuff), "dup() attempt %s failed", strbuff1);
342       fprintf(stderr, "%s\n", strbuff);
343 
344       rlim2str(strbuff1, sizeof(strbuff1), num_open.rlim_cur);
345       msnprintf(strbuff, sizeof(strbuff), "fds system limit seems close to %s",
346                 strbuff1);
347       fprintf(stderr, "%s\n", strbuff);
348 
349       num_open.rlim_max = num_open.rlim_cur - SAFETY_MARGIN;
350 
351       num_open.rlim_cur -= num_open.rlim_max;
352       rlim2str(strbuff1, sizeof(strbuff1), num_open.rlim_cur);
353       msnprintf(strbuff, sizeof(strbuff), "closing %s file descriptors",
354                 strbuff1);
355       fprintf(stderr, "%s\n", strbuff);
356 
357       for(num_open.rlim_cur = num_open.rlim_max;
358           testfd[num_open.rlim_cur] >= 0;
359           num_open.rlim_cur++) {
360         close(testfd[num_open.rlim_cur]);
361         testfd[num_open.rlim_cur] = -1;
362       }
363 
364       rlim2str(strbuff, sizeof(strbuff), num_open.rlim_max);
365       fprintf(stderr, "shrinking array for %s file descriptors\n", strbuff);
366 
367       /* we don't care if we can't shrink it */
368 
369       tmpfd = realloc(testfd, sizeof(*testfd) * (size_t)(num_open.rlim_max));
370       if(tmpfd) {
371         testfd = tmpfd;
372         tmpfd = NULL;
373       }
374 
375       break;
376     }
377   }
378 
379   rlim2str(strbuff, sizeof(strbuff), num_open.rlim_max);
380   fprintf(stderr, "%s file descriptors open\n", strbuff);
381 
382 #if !defined(HAVE_POLL) && !defined(USE_WINSOCK)
383 
384   /*
385    * when using select() instead of poll() we cannot test
386    * libcurl functionality with a socket number equal or
387    * greater than FD_SETSIZE. In any case, macro VERIFY_SOCK
388    * in lib/select.c enforces this check and protects libcurl
389    * from a possible crash. The effect of this protection
390    * is that test 537 will always fail, since the actual
391    * call to select() never takes place. We skip test 537
392    * with an indication that select limit would be exceeded.
393    */
394 
395   num_open.rlim_cur = FD_SETSIZE - SAFETY_MARGIN;
396   if(num_open.rlim_max > num_open.rlim_cur) {
397     msnprintf(strbuff, sizeof(strbuff), "select limit is FD_SETSIZE %d",
398               FD_SETSIZE);
399     store_errmsg(strbuff, 0);
400     fprintf(stderr, "%s\n", msgbuff);
401     close_file_descriptors();
402     free(memchunk);
403     return -8;
404   }
405 
406   num_open.rlim_cur = FD_SETSIZE - SAFETY_MARGIN;
407   for(rl.rlim_cur = 0;
408       rl.rlim_cur < num_open.rlim_max;
409       rl.rlim_cur++) {
410     if((testfd[rl.rlim_cur] > 0) &&
411        ((unsigned int)testfd[rl.rlim_cur] > num_open.rlim_cur)) {
412       msnprintf(strbuff, sizeof(strbuff), "select limit is FD_SETSIZE %d",
413                 FD_SETSIZE);
414       store_errmsg(strbuff, 0);
415       fprintf(stderr, "%s\n", msgbuff);
416       close_file_descriptors();
417       free(memchunk);
418       return -9;
419     }
420   }
421 
422 #endif /* using a FD_SETSIZE bound select() */
423 
424   /*
425    * Old or 'backwards compatible' implementations of stdio do not allow
426    * handling of streams with an underlying file descriptor number greater
427    * than 255, even when allowing high numbered file descriptors for sockets.
428    * At this point we have a big number of file descriptors which have been
429    * opened using dup(), so lets test the stdio implementation and discover
430    * if it is capable of fopen()ing some additional files.
431    */
432 
433   if(!fopen_works()) {
434     rlim2str(strbuff1, sizeof(strbuff1), num_open.rlim_max);
435     msnprintf(strbuff, sizeof(strbuff), "fopen fails with %s fds open",
436               strbuff1);
437     fprintf(stderr, "%s\n", msgbuff);
438     msnprintf(strbuff, sizeof(strbuff), "fopen fails with lots of fds open");
439     store_errmsg(strbuff, 0);
440     close_file_descriptors();
441     free(memchunk);
442     return -10;
443   }
444 
445   /* free the chunk of memory we were reserving so that it
446      becomes available to the test */
447 
448   free(memchunk);
449 
450   /* close file descriptors unless instructed to keep them */
451 
452   if(!keep_open) {
453     close_file_descriptors();
454   }
455 
456   return 0;
457 }
458 
459 CURLcode test(char *URL)
460 {
461   CURLcode res;
462   CURL *curl;
463 
464   if(!strcmp(URL, "check")) {
465     /* used by the test script to ask if we can run this test or not */
466     if(test_rlimit(FALSE)) {
467       fprintf(stdout, "test_rlimit problem: %s\n", msgbuff);
468       return (CURLcode)1;
469     }
470     return CURLE_OK; /* sure, run this! */
471   }
472 
473   if(test_rlimit(TRUE)) {
474     /* failure */
475     return TEST_ERR_MAJOR_BAD;
476   }
477 
478   /* run the test with the bunch of open file descriptors
479      and close them all once the test is over */
480 
481   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
482     fprintf(stderr, "curl_global_init() failed\n");
483     close_file_descriptors();
484     return TEST_ERR_MAJOR_BAD;
485   }
486 
487   curl = curl_easy_init();
488   if(!curl) {
489     fprintf(stderr, "curl_easy_init() failed\n");
490     close_file_descriptors();
491     curl_global_cleanup();
492     return TEST_ERR_MAJOR_BAD;
493   }
494 
495   test_setopt(curl, CURLOPT_URL, URL);
496   test_setopt(curl, CURLOPT_HEADER, 1L);
497 
498   res = curl_easy_perform(curl);
499 
500 test_cleanup:
501 
502   close_file_descriptors();
503   curl_easy_cleanup(curl);
504   curl_global_cleanup();
505 
506   return res;
507 }
508 
509 #else /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */
510 
511 CURLcode test(char *URL)
512 {
513   (void)URL;
514   printf("system lacks necessary system function(s)");
515   return (CURLcode)1; /* skip test */
516 }
517 
518 #endif /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */
519