lib537.c 14 KB

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