lib518.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. #ifndef FD_SETSIZE
  35. #error "this test requires FD_SETSIZE"
  36. #endif
  37. #define SAFETY_MARGIN (16)
  38. #define NUM_OPEN (FD_SETSIZE + 10)
  39. #define NUM_NEEDED (NUM_OPEN + SAFETY_MARGIN)
  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 *testfd = 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,
  55. err, 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(testfd[num_open.rlim_cur] > 0)
  63. close(testfd[num_open.rlim_cur]);
  64. free(testfd);
  65. testfd = 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 void rlim2str(char *buf, size_t len, rlim_t val)
  91. {
  92. #ifdef RLIM_INFINITY
  93. if(val == RLIM_INFINITY) {
  94. msnprintf(buf, len, "INFINITY");
  95. return;
  96. }
  97. #endif
  98. #ifdef HAVE_LONGLONG
  99. if(sizeof(rlim_t) > sizeof(long))
  100. msnprintf(buf, len, "%llu", (unsigned long long)val);
  101. else
  102. #endif
  103. {
  104. if(sizeof(rlim_t) < sizeof(long))
  105. msnprintf(buf, len, "%u", (unsigned int)val);
  106. else
  107. msnprintf(buf, len, "%lu", (unsigned long)val);
  108. }
  109. }
  110. static int test_rlimit(int keep_open)
  111. {
  112. rlim_t nitems, i;
  113. int *memchunk = NULL;
  114. struct rlimit rl;
  115. char strbuff[256];
  116. char strbuff1[81];
  117. char strbuff2[81];
  118. /* get initial open file limits */
  119. if(getrlimit(RLIMIT_NOFILE, &rl) != 0) {
  120. store_errmsg("getrlimit() failed", errno);
  121. fprintf(stderr, "%s\n", msgbuff);
  122. return -1;
  123. }
  124. /* show initial open file limits */
  125. rlim2str(strbuff, sizeof(strbuff), rl.rlim_cur);
  126. fprintf(stderr, "initial soft limit: %s\n", strbuff);
  127. rlim2str(strbuff, sizeof(strbuff), rl.rlim_max);
  128. fprintf(stderr, "initial hard limit: %s\n", strbuff);
  129. /* show our constants */
  130. fprintf(stderr, "test518 FD_SETSIZE: %d\n", FD_SETSIZE);
  131. fprintf(stderr, "test518 NUM_OPEN : %d\n", NUM_OPEN);
  132. fprintf(stderr, "test518 NUM_NEEDED: %d\n", NUM_NEEDED);
  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. rlim2str(strbuff, sizeof(strbuff), rl.rlim_cur);
  171. fprintf(stderr, "current soft limit: %s\n", strbuff);
  172. rlim2str(strbuff, sizeof(strbuff), rl.rlim_max);
  173. fprintf(stderr, "current hard limit: %s\n", strbuff);
  174. } /* (rl.rlim_cur != rl.rlim_max) */
  175. /*
  176. * test 518 is all about testing libcurl functionality
  177. * when more than FD_SETSIZE file descriptors are open.
  178. * This means that if for any reason we are not able to
  179. * open more than FD_SETSIZE file descriptors then test
  180. * 518 should not be run.
  181. */
  182. /*
  183. * verify that soft limit is higher than NUM_NEEDED,
  184. * which is the number of file descriptors we would
  185. * try to open plus SAFETY_MARGIN to not exhaust the
  186. * file descriptor pool
  187. */
  188. num_open.rlim_cur = NUM_NEEDED;
  189. if((rl.rlim_cur > 0) &&
  190. #ifdef RLIM_INFINITY
  191. (rl.rlim_cur != RLIM_INFINITY) &&
  192. #endif
  193. (rl.rlim_cur <= num_open.rlim_cur)) {
  194. rlim2str(strbuff2, sizeof(strbuff2), rl.rlim_cur);
  195. rlim2str(strbuff1, sizeof(strbuff1), num_open.rlim_cur);
  196. msnprintf(strbuff, sizeof(strbuff), "fds needed %s > system limit %s",
  197. strbuff1, strbuff2);
  198. store_errmsg(strbuff, 0);
  199. fprintf(stderr, "%s\n", msgbuff);
  200. return -4;
  201. }
  202. /*
  203. * reserve a chunk of memory before opening file descriptors to
  204. * avoid a low memory condition once the file descriptors are
  205. * open. System conditions that could make the test fail should
  206. * be addressed in the precheck phase. This chunk of memory shall
  207. * be always free()ed before exiting the test_rlimit() function so
  208. * that it becomes available to the test.
  209. */
  210. for(nitems = i = 1; nitems <= i; i *= 2)
  211. nitems = i;
  212. if(nitems > 0x7fff)
  213. nitems = 0x40000;
  214. do {
  215. num_open.rlim_max = sizeof(*memchunk) * nitems;
  216. rlim2str(strbuff, sizeof(strbuff), num_open.rlim_max);
  217. fprintf(stderr, "allocating memchunk %s byte array\n", strbuff);
  218. memchunk = malloc(sizeof(*memchunk) * (size_t)nitems);
  219. if(!memchunk) {
  220. fprintf(stderr, "memchunk, malloc() failed\n");
  221. nitems /= 2;
  222. }
  223. } while(nitems && !memchunk);
  224. if(!memchunk) {
  225. store_errmsg("memchunk, malloc() failed", errno);
  226. fprintf(stderr, "%s\n", msgbuff);
  227. return -5;
  228. }
  229. /* initialize it to fight lazy allocation */
  230. fprintf(stderr, "initializing memchunk array\n");
  231. for(i = 0; i < nitems; i++)
  232. memchunk[i] = -1;
  233. /* set the number of file descriptors we will try to open */
  234. num_open.rlim_max = NUM_OPEN;
  235. /* verify that we won't overflow size_t in malloc() */
  236. if((size_t)(num_open.rlim_max) > ((size_t)-1) / sizeof(*testfd)) {
  237. rlim2str(strbuff1, sizeof(strbuff1), num_open.rlim_max);
  238. msnprintf(strbuff, sizeof(strbuff), "unable to allocate an array for %s "
  239. "file descriptors, would overflow size_t", strbuff1);
  240. store_errmsg(strbuff, 0);
  241. fprintf(stderr, "%s\n", msgbuff);
  242. free(memchunk);
  243. return -6;
  244. }
  245. /* allocate array for file descriptors */
  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. store_errmsg("testfd, malloc() failed", errno);
  251. fprintf(stderr, "%s\n", msgbuff);
  252. free(memchunk);
  253. return -7;
  254. }
  255. /* initialize it to fight lazy allocation */
  256. fprintf(stderr, "initializing testfd array\n");
  257. for(num_open.rlim_cur = 0;
  258. num_open.rlim_cur < num_open.rlim_max;
  259. num_open.rlim_cur++)
  260. testfd[num_open.rlim_cur] = -1;
  261. rlim2str(strbuff, sizeof(strbuff), num_open.rlim_max);
  262. fprintf(stderr, "trying to open %s file descriptors\n", strbuff);
  263. /* open a dummy descriptor */
  264. testfd[0] = open(DEV_NULL, O_RDONLY);
  265. if(testfd[0] < 0) {
  266. msnprintf(strbuff, sizeof(strbuff), "opening of %s failed", DEV_NULL);
  267. store_errmsg(strbuff, errno);
  268. fprintf(stderr, "%s\n", msgbuff);
  269. free(testfd);
  270. testfd = NULL;
  271. free(memchunk);
  272. return -8;
  273. }
  274. /* create a bunch of file descriptors */
  275. for(num_open.rlim_cur = 1;
  276. num_open.rlim_cur < num_open.rlim_max;
  277. num_open.rlim_cur++) {
  278. testfd[num_open.rlim_cur] = dup(testfd[0]);
  279. if(testfd[num_open.rlim_cur] < 0) {
  280. testfd[num_open.rlim_cur] = -1;
  281. rlim2str(strbuff1, sizeof(strbuff1), num_open.rlim_cur);
  282. msnprintf(strbuff, sizeof(strbuff), "dup() attempt %s failed", strbuff1);
  283. fprintf(stderr, "%s\n", strbuff);
  284. rlim2str(strbuff1, sizeof(strbuff1), num_open.rlim_cur);
  285. msnprintf(strbuff, sizeof(strbuff), "fds system limit seems close to %s",
  286. strbuff1);
  287. fprintf(stderr, "%s\n", strbuff);
  288. num_open.rlim_max = NUM_NEEDED;
  289. rlim2str(strbuff2, sizeof(strbuff2), num_open.rlim_max);
  290. rlim2str(strbuff1, sizeof(strbuff1), num_open.rlim_cur);
  291. msnprintf(strbuff, sizeof(strbuff), "fds needed %s > system limit %s",
  292. strbuff2, strbuff1);
  293. store_errmsg(strbuff, 0);
  294. fprintf(stderr, "%s\n", msgbuff);
  295. for(num_open.rlim_cur = 0;
  296. testfd[num_open.rlim_cur] >= 0;
  297. num_open.rlim_cur++)
  298. close(testfd[num_open.rlim_cur]);
  299. free(testfd);
  300. testfd = NULL;
  301. free(memchunk);
  302. return -9;
  303. }
  304. }
  305. rlim2str(strbuff, sizeof(strbuff), num_open.rlim_max);
  306. fprintf(stderr, "%s file descriptors open\n", strbuff);
  307. #if !defined(HAVE_POLL_FINE) && !defined(USE_WINSOCK)
  308. /*
  309. * when using select() instead of poll() we cannot test
  310. * libcurl functionality with a socket number equal or
  311. * greater than FD_SETSIZE. In any case, macro VERIFY_SOCK
  312. * in lib/select.c enforces this check and protects libcurl
  313. * from a possible crash. The effect of this protection
  314. * is that test 518 will always fail, since the actual
  315. * call to select() never takes place. We skip test 518
  316. * with an indication that select limit would be exceeded.
  317. */
  318. num_open.rlim_cur = FD_SETSIZE - SAFETY_MARGIN;
  319. if(num_open.rlim_max > num_open.rlim_cur) {
  320. msnprintf(strbuff, sizeof(strbuff), "select limit is FD_SETSIZE %d",
  321. FD_SETSIZE);
  322. store_errmsg(strbuff, 0);
  323. fprintf(stderr, "%s\n", msgbuff);
  324. close_file_descriptors();
  325. free(memchunk);
  326. return -10;
  327. }
  328. num_open.rlim_cur = FD_SETSIZE - SAFETY_MARGIN;
  329. for(rl.rlim_cur = 0;
  330. rl.rlim_cur < num_open.rlim_max;
  331. rl.rlim_cur++) {
  332. if((testfd[rl.rlim_cur] > 0) &&
  333. ((unsigned int)testfd[rl.rlim_cur] > num_open.rlim_cur)) {
  334. msnprintf(strbuff, sizeof(strbuff), "select limit is FD_SETSIZE %d",
  335. FD_SETSIZE);
  336. store_errmsg(strbuff, 0);
  337. fprintf(stderr, "%s\n", msgbuff);
  338. close_file_descriptors();
  339. free(memchunk);
  340. return -11;
  341. }
  342. }
  343. #endif /* using a FD_SETSIZE bound select() */
  344. /*
  345. * Old or 'backwards compatible' implementations of stdio do not allow
  346. * handling of streams with an underlying file descriptor number greater
  347. * than 255, even when allowing high numbered file descriptors for sockets.
  348. * At this point we have a big number of file descriptors which have been
  349. * opened using dup(), so lets test the stdio implementation and discover
  350. * if it is capable of fopen()ing some additional files.
  351. */
  352. if(!fopen_works()) {
  353. rlim2str(strbuff1, sizeof(strbuff1), num_open.rlim_max);
  354. msnprintf(strbuff, sizeof(strbuff), "fopen fails with %s fds open",
  355. strbuff1);
  356. fprintf(stderr, "%s\n", msgbuff);
  357. msnprintf(strbuff, sizeof(strbuff), "fopen fails with lots of fds open");
  358. store_errmsg(strbuff, 0);
  359. close_file_descriptors();
  360. free(memchunk);
  361. return -12;
  362. }
  363. /* free the chunk of memory we were reserving so that it
  364. becomes available to the test */
  365. free(memchunk);
  366. /* close file descriptors unless instructed to keep them */
  367. if(!keep_open) {
  368. close_file_descriptors();
  369. }
  370. return 0;
  371. }
  372. CURLcode test(char *URL)
  373. {
  374. CURLcode res;
  375. CURL *curl;
  376. if(!strcmp(URL, "check")) {
  377. /* used by the test script to ask if we can run this test or not */
  378. if(test_rlimit(FALSE)) {
  379. fprintf(stdout, "test_rlimit problem: %s\n", msgbuff);
  380. return (CURLcode)1;
  381. }
  382. return CURLE_OK; /* sure, run this! */
  383. }
  384. if(test_rlimit(TRUE)) {
  385. /* failure */
  386. return TEST_ERR_MAJOR_BAD;
  387. }
  388. /* run the test with the bunch of open file descriptors
  389. and close them all once the test is over */
  390. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  391. fprintf(stderr, "curl_global_init() failed\n");
  392. close_file_descriptors();
  393. return TEST_ERR_MAJOR_BAD;
  394. }
  395. curl = curl_easy_init();
  396. if(!curl) {
  397. fprintf(stderr, "curl_easy_init() failed\n");
  398. close_file_descriptors();
  399. curl_global_cleanup();
  400. return TEST_ERR_MAJOR_BAD;
  401. }
  402. test_setopt(curl, CURLOPT_URL, URL);
  403. test_setopt(curl, CURLOPT_HEADER, 1L);
  404. res = curl_easy_perform(curl);
  405. test_cleanup:
  406. close_file_descriptors();
  407. curl_easy_cleanup(curl);
  408. curl_global_cleanup();
  409. return res;
  410. }
  411. #else /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */
  412. CURLcode test(char *URL)
  413. {
  414. (void)URL;
  415. printf("system lacks necessary system function(s)");
  416. return 1; /* skip test */
  417. }
  418. #endif /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */