lib518.c 14 KB

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