lib537.c 14 KB

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