lib518.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * $Id$
  9. */
  10. #include "test.h"
  11. #ifdef HAVE_SYS_TYPES_H
  12. #include <sys/types.h>
  13. #endif
  14. #ifdef HAVE_SYS_RESOURCE_H
  15. #include <sys/resource.h>
  16. #endif
  17. #ifdef HAVE_FCNTL_H
  18. #include <fcntl.h>
  19. #endif
  20. #ifdef HAVE_LIMITS_H
  21. #include <limits.h>
  22. #endif
  23. #ifdef HAVE_STRING_H
  24. #include <string.h>
  25. #endif
  26. #ifndef FD_SETSIZE
  27. #error "this test requires FD_SETSIZE"
  28. #endif
  29. #define SAFETY_MARGIN (16)
  30. #define NUM_OPEN (FD_SETSIZE + 10)
  31. #define NUM_NEEDED (NUM_OPEN + SAFETY_MARGIN)
  32. #if defined(WIN32) || defined(_WIN32) || defined(MSDOS)
  33. #define DEV_NULL "NUL"
  34. #else
  35. #define DEV_NULL "/dev/null"
  36. #endif
  37. #if defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT)
  38. static int *fd = NULL;
  39. static struct rlimit num_open;
  40. static char msgbuff[256];
  41. static void store_errmsg(const char *msg, int err)
  42. {
  43. if (!err)
  44. sprintf(msgbuff, "%s", msg);
  45. else
  46. sprintf(msgbuff, "%s, errno %d, %s", msg, err, strerror(err));
  47. }
  48. static void close_file_descriptors(void)
  49. {
  50. for (num_open.rlim_cur = 0;
  51. num_open.rlim_cur < num_open.rlim_max;
  52. num_open.rlim_cur++)
  53. if (fd[num_open.rlim_cur] > 0)
  54. close(fd[num_open.rlim_cur]);
  55. free(fd);
  56. fd = NULL;
  57. }
  58. static int fopen_works(void)
  59. {
  60. FILE *fpa[3];
  61. int i;
  62. int ret = 1;
  63. for (i = 0; i < 3; i++) {
  64. fpa[i] = NULL;
  65. }
  66. for (i = 0; i < 3; i++) {
  67. fpa[i] = fopen(DEV_NULL, "r");
  68. if (fpa[i] == NULL) {
  69. store_errmsg("fopen() failed", ERRNO);
  70. fprintf(stderr, "%s\n", msgbuff);
  71. ret = 0;
  72. break;
  73. }
  74. }
  75. for (i = 0; i < 3; i++) {
  76. if (fpa[i] != NULL)
  77. fclose(fpa[i]);
  78. }
  79. return ret;
  80. }
  81. static int rlimit(int keep_open)
  82. {
  83. int nitems, i;
  84. int *memchunk = NULL;
  85. char *fmt;
  86. struct rlimit rl;
  87. char strbuff[256];
  88. char strbuff1[81];
  89. char strbuff2[81];
  90. char fmt_u[] = "%u";
  91. char fmt_lu[] = "%lu";
  92. #ifdef HAVE_LONGLONG
  93. char fmt_llu[] = "%llu";
  94. if (sizeof(rl.rlim_max) > sizeof(long))
  95. fmt = fmt_llu;
  96. else
  97. #endif
  98. fmt = (sizeof(rl.rlim_max) < sizeof(long))?fmt_u:fmt_lu;
  99. /* get initial open file limits */
  100. if (getrlimit(RLIMIT_NOFILE, &rl) != 0) {
  101. store_errmsg("getrlimit() failed", ERRNO);
  102. fprintf(stderr, "%s\n", msgbuff);
  103. return -1;
  104. }
  105. /* show initial open file limits */
  106. #ifdef RLIM_INFINITY
  107. if (rl.rlim_cur == RLIM_INFINITY)
  108. strcpy(strbuff, "INFINITY");
  109. else
  110. #endif
  111. sprintf(strbuff, fmt, rl.rlim_cur);
  112. fprintf(stderr, "initial soft limit: %s\n", strbuff);
  113. #ifdef RLIM_INFINITY
  114. if (rl.rlim_max == RLIM_INFINITY)
  115. strcpy(strbuff, "INFINITY");
  116. else
  117. #endif
  118. sprintf(strbuff, fmt, rl.rlim_max);
  119. fprintf(stderr, "initial hard limit: %s\n", strbuff);
  120. /* show our constants */
  121. fprintf(stderr, "test518 FD_SETSIZE: %d\n", FD_SETSIZE);
  122. fprintf(stderr, "test518 NUM_OPEN : %d\n", NUM_OPEN);
  123. fprintf(stderr, "test518 NUM_NEEDED: %d\n", NUM_NEEDED);
  124. /*
  125. * if soft limit and hard limit are different we ask the
  126. * system to raise soft limit all the way up to the hard
  127. * limit. Due to some other system limit the soft limit
  128. * might not be raised up to the hard limit. So from this
  129. * point the resulting soft limit is our limit. Trying to
  130. * open more than soft limit file descriptors will fail.
  131. */
  132. if (rl.rlim_cur != rl.rlim_max) {
  133. #ifdef OPEN_MAX
  134. if ((rl.rlim_cur > 0) &&
  135. (rl.rlim_cur < OPEN_MAX)) {
  136. fprintf(stderr, "raising soft limit up to OPEN_MAX\n");
  137. rl.rlim_cur = OPEN_MAX;
  138. if (setrlimit(RLIMIT_NOFILE, &rl) != 0) {
  139. /* on failure don't abort just issue a warning */
  140. store_errmsg("setrlimit() failed", ERRNO);
  141. fprintf(stderr, "%s\n", msgbuff);
  142. msgbuff[0] = '\0';
  143. }
  144. }
  145. #endif
  146. fprintf(stderr, "raising soft limit up to hard limit\n");
  147. rl.rlim_cur = rl.rlim_max;
  148. if (setrlimit(RLIMIT_NOFILE, &rl) != 0) {
  149. /* on failure don't abort just issue a warning */
  150. store_errmsg("setrlimit() failed", ERRNO);
  151. fprintf(stderr, "%s\n", msgbuff);
  152. msgbuff[0] = '\0';
  153. }
  154. /* get current open file limits */
  155. if (getrlimit(RLIMIT_NOFILE, &rl) != 0) {
  156. store_errmsg("getrlimit() failed", ERRNO);
  157. fprintf(stderr, "%s\n", msgbuff);
  158. return -3;
  159. }
  160. /* show current open file limits */
  161. #ifdef RLIM_INFINITY
  162. if (rl.rlim_cur == RLIM_INFINITY)
  163. strcpy(strbuff, "INFINITY");
  164. else
  165. #endif
  166. sprintf(strbuff, fmt, rl.rlim_cur);
  167. fprintf(stderr, "current soft limit: %s\n", strbuff);
  168. #ifdef RLIM_INFINITY
  169. if (rl.rlim_max == RLIM_INFINITY)
  170. strcpy(strbuff, "INFINITY");
  171. else
  172. #endif
  173. sprintf(strbuff, fmt, rl.rlim_max);
  174. fprintf(stderr, "current hard limit: %s\n", strbuff);
  175. } /* (rl.rlim_cur != rl.rlim_max) */
  176. /*
  177. * test 518 is all about testing libcurl functionality
  178. * when more than FD_SETSIZE file descriptors are open.
  179. * This means that if for any reason we are not able to
  180. * open more than FD_SETSIZE file descriptors then test
  181. * 518 should not be run.
  182. */
  183. /*
  184. * verify that soft limit is higher than NUM_NEEDED,
  185. * which is the number of file descriptors we would
  186. * try to open plus SAFETY_MARGIN to not exhaust the
  187. * file descriptor pool
  188. */
  189. num_open.rlim_cur = NUM_NEEDED;
  190. if ((rl.rlim_cur > 0) &&
  191. #ifdef RLIM_INFINITY
  192. (rl.rlim_cur != RLIM_INFINITY) &&
  193. #endif
  194. (rl.rlim_cur <= num_open.rlim_cur)) {
  195. sprintf(strbuff2, fmt, rl.rlim_cur);
  196. sprintf(strbuff1, fmt, num_open.rlim_cur);
  197. sprintf(strbuff, "fds needed %s > system limit %s",
  198. strbuff1, strbuff2);
  199. store_errmsg(strbuff, 0);
  200. fprintf(stderr, "%s\n", msgbuff);
  201. return -4;
  202. }
  203. /*
  204. * reserve a chunk of memory before opening file descriptors to
  205. * avoid a low memory condition once the file descriptors are
  206. * open. System conditions that could make the test fail should
  207. * be addressed in the precheck phase. This chunk of memory shall
  208. * be always free()ed before exiting the rlimit() function so
  209. * that it becomes available to the test.
  210. */
  211. for (nitems = i = 1; nitems <= i; i *= 2)
  212. nitems = i;
  213. if (nitems > 0x7fff)
  214. nitems = 0x40000;
  215. do {
  216. num_open.rlim_max = sizeof(*memchunk) * (size_t)nitems;
  217. sprintf(strbuff, fmt, num_open.rlim_max);
  218. fprintf(stderr, "allocating memchunk %s byte array\n", strbuff);
  219. memchunk = malloc(sizeof(*memchunk) * (size_t)nitems);
  220. if (!memchunk) {
  221. fprintf(stderr, "memchunk, malloc() failed\n");
  222. nitems /= 2;
  223. }
  224. } while (nitems && !memchunk);
  225. if (!memchunk) {
  226. store_errmsg("memchunk, malloc() failed", ERRNO);
  227. fprintf(stderr, "%s\n", msgbuff);
  228. return -5;
  229. }
  230. /* initialize it to fight lazy allocation */
  231. fprintf(stderr, "initializing memchunk array\n");
  232. for (i = 0; i < nitems; i++)
  233. memchunk[i] = -1;
  234. /* set the number of file descriptors we will try to open */
  235. num_open.rlim_max = NUM_OPEN;
  236. /* verify that we won't overflow size_t in malloc() */
  237. if ((size_t)(num_open.rlim_max) > ((size_t)-1) / sizeof(*fd)) {
  238. sprintf(strbuff1, fmt, num_open.rlim_max);
  239. sprintf(strbuff, "unable to allocate an array for %s "
  240. "file descriptors, would overflow size_t", strbuff1);
  241. store_errmsg(strbuff, 0);
  242. fprintf(stderr, "%s\n", msgbuff);
  243. free(memchunk);
  244. return -6;
  245. }
  246. /* allocate array for file descriptors */
  247. sprintf(strbuff, fmt, num_open.rlim_max);
  248. fprintf(stderr, "allocating array for %s file descriptors\n", strbuff);
  249. fd = malloc(sizeof(*fd) * (size_t)(num_open.rlim_max));
  250. if (!fd) {
  251. store_errmsg("fd, malloc() failed", ERRNO);
  252. fprintf(stderr, "%s\n", msgbuff);
  253. free(memchunk);
  254. return -7;
  255. }
  256. /* initialize it to fight lazy allocation */
  257. fprintf(stderr, "initializing fd array\n");
  258. for (num_open.rlim_cur = 0;
  259. num_open.rlim_cur < num_open.rlim_max;
  260. num_open.rlim_cur++)
  261. fd[num_open.rlim_cur] = -1;
  262. sprintf(strbuff, fmt, num_open.rlim_max);
  263. fprintf(stderr, "trying to open %s file descriptors\n", strbuff);
  264. /* open a dummy descriptor */
  265. fd[0] = open(DEV_NULL, O_RDONLY);
  266. if (fd[0] < 0) {
  267. sprintf(strbuff, "opening of %s failed", DEV_NULL);
  268. store_errmsg(strbuff, ERRNO);
  269. fprintf(stderr, "%s\n", msgbuff);
  270. free(fd);
  271. fd = NULL;
  272. free(memchunk);
  273. return -8;
  274. }
  275. /* create a bunch of file descriptors */
  276. for (num_open.rlim_cur = 1;
  277. num_open.rlim_cur < num_open.rlim_max;
  278. num_open.rlim_cur++) {
  279. fd[num_open.rlim_cur] = dup(fd[0]);
  280. if (fd[num_open.rlim_cur] < 0) {
  281. fd[num_open.rlim_cur] = -1;
  282. sprintf(strbuff1, fmt, num_open.rlim_cur);
  283. sprintf(strbuff, "dup() attempt %s failed", strbuff1);
  284. fprintf(stderr, "%s\n", strbuff);
  285. sprintf(strbuff1, fmt, num_open.rlim_cur);
  286. sprintf(strbuff, "fds system limit seems close to %s", strbuff1);
  287. fprintf(stderr, "%s\n", strbuff);
  288. num_open.rlim_max = NUM_NEEDED;
  289. sprintf(strbuff2, fmt, num_open.rlim_max);
  290. sprintf(strbuff1, fmt, num_open.rlim_cur);
  291. sprintf(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. fd[num_open.rlim_cur] >= 0;
  297. num_open.rlim_cur++)
  298. close(fd[num_open.rlim_cur]);
  299. free(fd);
  300. fd = NULL;
  301. free(memchunk);
  302. return -9;
  303. }
  304. }
  305. sprintf(strbuff, fmt, num_open.rlim_max);
  306. fprintf(stderr, "%s file descriptors open\n", strbuff);
  307. #if !defined(HAVE_POLL_FINE) && \
  308. !defined(USE_WINSOCK) && \
  309. !defined(TPF)
  310. /*
  311. * when using select() instead of poll() we cannot test
  312. * libcurl functionality with a socket number equal or
  313. * greater than FD_SETSIZE. In any case, macro VERIFY_SOCK
  314. * in lib/select.c enforces this check and protects libcurl
  315. * from a possible crash. The effect of this protection
  316. * is that test 518 will always fail, since the actual
  317. * call to select() never takes place. We skip test 518
  318. * with an indication that select limit would be exceeded.
  319. */
  320. num_open.rlim_cur = FD_SETSIZE - SAFETY_MARGIN;
  321. if (num_open.rlim_max > num_open.rlim_cur) {
  322. sprintf(strbuff, "select limit is FD_SETSIZE %d", FD_SETSIZE);
  323. store_errmsg(strbuff, 0);
  324. fprintf(stderr, "%s\n", msgbuff);
  325. close_file_descriptors();
  326. free(memchunk);
  327. return -10;
  328. }
  329. num_open.rlim_cur = FD_SETSIZE - SAFETY_MARGIN;
  330. for (rl.rlim_cur = 0;
  331. rl.rlim_cur < num_open.rlim_max;
  332. rl.rlim_cur++) {
  333. if ((fd[rl.rlim_cur] > 0) &&
  334. ((unsigned int)fd[rl.rlim_cur] > num_open.rlim_cur)) {
  335. sprintf(strbuff, "select limit is FD_SETSIZE %d", 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. sprintf(strbuff1, fmt, num_open.rlim_max);
  354. sprintf(strbuff, "stdio fopen() fails with %s fds open()",
  355. strbuff1);
  356. fprintf(stderr, "%s\n", msgbuff);
  357. sprintf(strbuff, "stdio 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 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. int 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(rlimit(FALSE)) {
  379. fprintf(stdout, "rlimit problem: %s\n", msgbuff);
  380. return 1;
  381. }
  382. return 0; /* sure, run this! */
  383. }
  384. if (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. if ((curl = curl_easy_init()) == NULL) {
  396. fprintf(stderr, "curl_easy_init() failed\n");
  397. close_file_descriptors();
  398. curl_global_cleanup();
  399. return TEST_ERR_MAJOR_BAD;
  400. }
  401. curl_easy_setopt(curl, CURLOPT_URL, URL);
  402. curl_easy_setopt(curl, CURLOPT_HEADER, 1L);
  403. res = curl_easy_perform(curl);
  404. close_file_descriptors();
  405. curl_easy_cleanup(curl);
  406. curl_global_cleanup();
  407. return (int)res;
  408. }
  409. #else /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */
  410. int test(char *URL)
  411. {
  412. (void)URL;
  413. printf("system lacks necessary system function(s)");
  414. return 1; /* skip test */
  415. }
  416. #endif /* defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) */