lib537.c 14 KB

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