lib518.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, 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 http://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. #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(_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 *fd = 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. sprintf(msgbuff, "%s", msg);
  53. else
  54. sprintf(msgbuff, "%s, errno %d, %s", msg, err, 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, "r");
  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 nitems, i;
  92. int *memchunk = NULL;
  93. char *fmt;
  94. struct rlimit rl;
  95. char strbuff[256];
  96. char strbuff1[81];
  97. char strbuff2[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. sprintf(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. sprintf(strbuff, fmt, rl.rlim_max);
  127. fprintf(stderr, "initial hard limit: %s\n", strbuff);
  128. /* show our constants */
  129. fprintf(stderr, "test518 FD_SETSIZE: %d\n", FD_SETSIZE);
  130. fprintf(stderr, "test518 NUM_OPEN : %d\n", NUM_OPEN);
  131. fprintf(stderr, "test518 NUM_NEEDED: %d\n", NUM_NEEDED);
  132. /*
  133. * if soft limit and hard limit are different we ask the
  134. * system to raise soft limit all the way up to the hard
  135. * limit. Due to some other system limit the soft limit
  136. * might not be raised up to the hard limit. So from this
  137. * point the resulting soft limit is our limit. Trying to
  138. * open more than soft limit file descriptors will fail.
  139. */
  140. if (rl.rlim_cur != rl.rlim_max) {
  141. #ifdef OPEN_MAX
  142. if ((rl.rlim_cur > 0) &&
  143. (rl.rlim_cur < OPEN_MAX)) {
  144. fprintf(stderr, "raising soft limit up to OPEN_MAX\n");
  145. rl.rlim_cur = OPEN_MAX;
  146. if (setrlimit(RLIMIT_NOFILE, &rl) != 0) {
  147. /* on failure don't abort just issue a warning */
  148. store_errmsg("setrlimit() failed", ERRNO);
  149. fprintf(stderr, "%s\n", msgbuff);
  150. msgbuff[0] = '\0';
  151. }
  152. }
  153. #endif
  154. fprintf(stderr, "raising soft limit up to hard limit\n");
  155. rl.rlim_cur = rl.rlim_max;
  156. if (setrlimit(RLIMIT_NOFILE, &rl) != 0) {
  157. /* on failure don't abort just issue a warning */
  158. store_errmsg("setrlimit() failed", ERRNO);
  159. fprintf(stderr, "%s\n", msgbuff);
  160. msgbuff[0] = '\0';
  161. }
  162. /* get current open file limits */
  163. if (getrlimit(RLIMIT_NOFILE, &rl) != 0) {
  164. store_errmsg("getrlimit() failed", ERRNO);
  165. fprintf(stderr, "%s\n", msgbuff);
  166. return -3;
  167. }
  168. /* show current open file limits */
  169. #ifdef RLIM_INFINITY
  170. if (rl.rlim_cur == RLIM_INFINITY)
  171. strcpy(strbuff, "INFINITY");
  172. else
  173. #endif
  174. sprintf(strbuff, fmt, rl.rlim_cur);
  175. fprintf(stderr, "current soft limit: %s\n", strbuff);
  176. #ifdef RLIM_INFINITY
  177. if (rl.rlim_max == RLIM_INFINITY)
  178. strcpy(strbuff, "INFINITY");
  179. else
  180. #endif
  181. sprintf(strbuff, fmt, rl.rlim_max);
  182. fprintf(stderr, "current hard limit: %s\n", strbuff);
  183. } /* (rl.rlim_cur != rl.rlim_max) */
  184. /*
  185. * test 518 is all about testing libcurl functionality
  186. * when more than FD_SETSIZE file descriptors are open.
  187. * This means that if for any reason we are not able to
  188. * open more than FD_SETSIZE file descriptors then test
  189. * 518 should not be run.
  190. */
  191. /*
  192. * verify that soft limit is higher than NUM_NEEDED,
  193. * which is the number of file descriptors we would
  194. * try to open plus SAFETY_MARGIN to not exhaust the
  195. * file descriptor pool
  196. */
  197. num_open.rlim_cur = NUM_NEEDED;
  198. if ((rl.rlim_cur > 0) &&
  199. #ifdef RLIM_INFINITY
  200. (rl.rlim_cur != RLIM_INFINITY) &&
  201. #endif
  202. (rl.rlim_cur <= num_open.rlim_cur)) {
  203. sprintf(strbuff2, fmt, rl.rlim_cur);
  204. sprintf(strbuff1, fmt, num_open.rlim_cur);
  205. sprintf(strbuff, "fds needed %s > system limit %s",
  206. strbuff1, strbuff2);
  207. store_errmsg(strbuff, 0);
  208. fprintf(stderr, "%s\n", msgbuff);
  209. return -4;
  210. }
  211. /*
  212. * reserve a chunk of memory before opening file descriptors to
  213. * avoid a low memory condition once the file descriptors are
  214. * open. System conditions that could make the test fail should
  215. * be addressed in the precheck phase. This chunk of memory shall
  216. * be always free()ed before exiting the rlimit() function so
  217. * that it becomes available to the test.
  218. */
  219. for (nitems = i = 1; nitems <= i; i *= 2)
  220. nitems = i;
  221. if (nitems > 0x7fff)
  222. nitems = 0x40000;
  223. do {
  224. num_open.rlim_max = sizeof(*memchunk) * (size_t)nitems;
  225. sprintf(strbuff, fmt, num_open.rlim_max);
  226. fprintf(stderr, "allocating memchunk %s byte array\n", strbuff);
  227. memchunk = malloc(sizeof(*memchunk) * (size_t)nitems);
  228. if (!memchunk) {
  229. fprintf(stderr, "memchunk, malloc() failed\n");
  230. nitems /= 2;
  231. }
  232. } while (nitems && !memchunk);
  233. if (!memchunk) {
  234. store_errmsg("memchunk, malloc() failed", ERRNO);
  235. fprintf(stderr, "%s\n", msgbuff);
  236. return -5;
  237. }
  238. /* initialize it to fight lazy allocation */
  239. fprintf(stderr, "initializing memchunk array\n");
  240. for (i = 0; i < nitems; i++)
  241. memchunk[i] = -1;
  242. /* set the number of file descriptors we will try to open */
  243. num_open.rlim_max = NUM_OPEN;
  244. /* verify that we won't overflow size_t in malloc() */
  245. if ((size_t)(num_open.rlim_max) > ((size_t)-1) / sizeof(*fd)) {
  246. sprintf(strbuff1, fmt, num_open.rlim_max);
  247. sprintf(strbuff, "unable to allocate an array for %s "
  248. "file descriptors, would overflow size_t", strbuff1);
  249. store_errmsg(strbuff, 0);
  250. fprintf(stderr, "%s\n", msgbuff);
  251. free(memchunk);
  252. return -6;
  253. }
  254. /* allocate array for file descriptors */
  255. sprintf(strbuff, fmt, num_open.rlim_max);
  256. fprintf(stderr, "allocating array for %s file descriptors\n", strbuff);
  257. fd = malloc(sizeof(*fd) * (size_t)(num_open.rlim_max));
  258. if (!fd) {
  259. store_errmsg("fd, malloc() failed", ERRNO);
  260. fprintf(stderr, "%s\n", msgbuff);
  261. free(memchunk);
  262. return -7;
  263. }
  264. /* initialize it to fight lazy allocation */
  265. fprintf(stderr, "initializing fd array\n");
  266. for (num_open.rlim_cur = 0;
  267. num_open.rlim_cur < num_open.rlim_max;
  268. num_open.rlim_cur++)
  269. fd[num_open.rlim_cur] = -1;
  270. sprintf(strbuff, fmt, num_open.rlim_max);
  271. fprintf(stderr, "trying to open %s file descriptors\n", strbuff);
  272. /* open a dummy descriptor */
  273. fd[0] = open(DEV_NULL, O_RDONLY);
  274. if (fd[0] < 0) {
  275. sprintf(strbuff, "opening of %s failed", DEV_NULL);
  276. store_errmsg(strbuff, ERRNO);
  277. fprintf(stderr, "%s\n", msgbuff);
  278. free(fd);
  279. fd = NULL;
  280. free(memchunk);
  281. return -8;
  282. }
  283. /* create a bunch of file descriptors */
  284. for (num_open.rlim_cur = 1;
  285. num_open.rlim_cur < num_open.rlim_max;
  286. num_open.rlim_cur++) {
  287. fd[num_open.rlim_cur] = dup(fd[0]);
  288. if (fd[num_open.rlim_cur] < 0) {
  289. fd[num_open.rlim_cur] = -1;
  290. sprintf(strbuff1, fmt, num_open.rlim_cur);
  291. sprintf(strbuff, "dup() attempt %s failed", strbuff1);
  292. fprintf(stderr, "%s\n", strbuff);
  293. sprintf(strbuff1, fmt, num_open.rlim_cur);
  294. sprintf(strbuff, "fds system limit seems close to %s", strbuff1);
  295. fprintf(stderr, "%s\n", strbuff);
  296. num_open.rlim_max = NUM_NEEDED;
  297. sprintf(strbuff2, fmt, num_open.rlim_max);
  298. sprintf(strbuff1, fmt, num_open.rlim_cur);
  299. sprintf(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. sprintf(strbuff, fmt, num_open.rlim_max);
  314. fprintf(stderr, "%s file descriptors open\n", strbuff);
  315. #if !defined(HAVE_POLL_FINE) && \
  316. !defined(USE_WINSOCK) && \
  317. !defined(TPF)
  318. /*
  319. * when using select() instead of poll() we cannot test
  320. * libcurl functionality with a socket number equal or
  321. * greater than FD_SETSIZE. In any case, macro VERIFY_SOCK
  322. * in lib/select.c enforces this check and protects libcurl
  323. * from a possible crash. The effect of this protection
  324. * is that test 518 will always fail, since the actual
  325. * call to select() never takes place. We skip test 518
  326. * with an indication that select limit would be exceeded.
  327. */
  328. num_open.rlim_cur = FD_SETSIZE - SAFETY_MARGIN;
  329. if (num_open.rlim_max > num_open.rlim_cur) {
  330. sprintf(strbuff, "select limit is FD_SETSIZE %d", FD_SETSIZE);
  331. store_errmsg(strbuff, 0);
  332. fprintf(stderr, "%s\n", msgbuff);
  333. close_file_descriptors();
  334. free(memchunk);
  335. return -10;
  336. }
  337. num_open.rlim_cur = FD_SETSIZE - SAFETY_MARGIN;
  338. for (rl.rlim_cur = 0;
  339. rl.rlim_cur < num_open.rlim_max;
  340. rl.rlim_cur++) {
  341. if ((fd[rl.rlim_cur] > 0) &&
  342. ((unsigned int)fd[rl.rlim_cur] > num_open.rlim_cur)) {
  343. sprintf(strbuff, "select limit is FD_SETSIZE %d", 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. sprintf(strbuff1, fmt, num_open.rlim_max);
  362. sprintf(strbuff, "stdio fopen() fails with %s fds open()",
  363. strbuff1);
  364. fprintf(stderr, "%s\n", msgbuff);
  365. sprintf(strbuff, "stdio fopen() fails with lots of fds open()");
  366. store_errmsg(strbuff, 0);
  367. close_file_descriptors();
  368. free(memchunk);
  369. return -12;
  370. }
  371. /* free the chunk of memory we were reserving so that it
  372. becomes becomes available to the test */
  373. free(memchunk);
  374. /* close file descriptors unless instructed to keep them */
  375. if (!keep_open) {
  376. close_file_descriptors();
  377. }
  378. return 0;
  379. }
  380. int test(char *URL)
  381. {
  382. CURLcode res;
  383. CURL *curl;
  384. if(!strcmp(URL, "check")) {
  385. /* used by the test script to ask if we can run this test or not */
  386. if(rlimit(FALSE)) {
  387. fprintf(stdout, "rlimit problem: %s\n", msgbuff);
  388. return 1;
  389. }
  390. return 0; /* sure, run this! */
  391. }
  392. if (rlimit(TRUE)) {
  393. /* failure */
  394. return TEST_ERR_MAJOR_BAD;
  395. }
  396. /* run the test with the bunch of open file descriptors
  397. and close them all once the test is over */
  398. if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  399. fprintf(stderr, "curl_global_init() failed\n");
  400. close_file_descriptors();
  401. return TEST_ERR_MAJOR_BAD;
  402. }
  403. if ((curl = curl_easy_init()) == NULL) {
  404. fprintf(stderr, "curl_easy_init() failed\n");
  405. close_file_descriptors();
  406. curl_global_cleanup();
  407. return TEST_ERR_MAJOR_BAD;
  408. }
  409. test_setopt(curl, CURLOPT_URL, URL);
  410. test_setopt(curl, CURLOPT_HEADER, 1L);
  411. res = curl_easy_perform(curl);
  412. test_cleanup:
  413. close_file_descriptors();
  414. curl_easy_cleanup(curl);
  415. curl_global_cleanup();
  416. return (int)res;
  417. }
  418. #else /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */
  419. int test(char *URL)
  420. {
  421. (void)URL;
  422. printf("system lacks necessary system function(s)");
  423. return 1; /* skip test */
  424. }
  425. #endif /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */