test-emfile.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #if !defined(_WIN32)
  22. #include "uv.h"
  23. #include "task.h"
  24. #include <errno.h>
  25. #include <sys/resource.h>
  26. #include <unistd.h>
  27. static void connection_cb(uv_stream_t* server_handle, int status);
  28. static void connect_cb(uv_connect_t* req, int status);
  29. static const int maxfd = 31;
  30. static unsigned connect_cb_called;
  31. static uv_tcp_t server_handle;
  32. static uv_tcp_t client_handle;
  33. TEST_IMPL(emfile) {
  34. struct sockaddr_in addr;
  35. struct rlimit limits;
  36. uv_connect_t connect_req;
  37. uv_loop_t* loop;
  38. int first_fd;
  39. #if defined(_AIX) || defined(__MVS__)
  40. /* On AIX, if a 'accept' call fails ECONNRESET is set on the socket
  41. * which causes uv__emfile_trick to not work as intended and this test
  42. * to fail.
  43. */
  44. RETURN_SKIP("uv__emfile_trick does not work on this OS");
  45. #endif
  46. /* Lower the file descriptor limit and use up all fds save one. */
  47. limits.rlim_cur = limits.rlim_max = maxfd + 1;
  48. if (setrlimit(RLIMIT_NOFILE, &limits)) {
  49. ASSERT(errno == EPERM); /* Valgrind blocks the setrlimit() call. */
  50. RETURN_SKIP("setrlimit(RLIMIT_NOFILE) failed, running under valgrind?");
  51. }
  52. loop = uv_default_loop();
  53. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  54. ASSERT(0 == uv_tcp_init(loop, &server_handle));
  55. ASSERT(0 == uv_tcp_init(loop, &client_handle));
  56. ASSERT(0 == uv_tcp_bind(&server_handle, (const struct sockaddr*) &addr, 0));
  57. ASSERT(0 == uv_listen((uv_stream_t*) &server_handle, 8, connection_cb));
  58. /* Remember the first one so we can clean up afterwards. */
  59. do
  60. first_fd = dup(0);
  61. while (first_fd == -1 && errno == EINTR);
  62. ASSERT(first_fd > 0);
  63. while (dup(0) != -1 || errno == EINTR);
  64. ASSERT(errno == EMFILE);
  65. close(maxfd);
  66. /* Now connect and use up the last available file descriptor. The EMFILE
  67. * handling logic in src/unix/stream.c should ensure that connect_cb() runs
  68. * whereas connection_cb() should *not* run.
  69. */
  70. ASSERT(0 == uv_tcp_connect(&connect_req,
  71. &client_handle,
  72. (const struct sockaddr*) &addr,
  73. connect_cb));
  74. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  75. ASSERT(1 == connect_cb_called);
  76. /* Close the dups again. Ignore errors in the unlikely event that the
  77. * file descriptors were not contiguous.
  78. */
  79. while (first_fd < maxfd) {
  80. close(first_fd);
  81. first_fd += 1;
  82. }
  83. MAKE_VALGRIND_HAPPY();
  84. return 0;
  85. }
  86. static void connection_cb(uv_stream_t* server_handle, int status) {
  87. ASSERT(0 && "connection_cb should not be called.");
  88. }
  89. static void connect_cb(uv_connect_t* req, int status) {
  90. /* |status| should equal 0 because the connection should have been accepted,
  91. * it's just that the server immediately closes it again.
  92. */
  93. ASSERT(0 == status);
  94. connect_cb_called += 1;
  95. uv_close((uv_handle_t*) &server_handle, NULL);
  96. uv_close((uv_handle_t*) &client_handle, NULL);
  97. }
  98. #else
  99. typedef int file_has_no_tests; /* ISO C forbids an empty translation unit. */
  100. #endif /* !_WIN32 */