lib537.c 13 KB

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