lib537.c 14 KB

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