lib537.c 14 KB

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