test-emfile.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 <stdio.h>
  26. #include <sys/resource.h>
  27. #include <unistd.h>
  28. static void connection_cb(uv_stream_t* server_handle, int status);
  29. static void connect_cb(uv_connect_t* req, int status);
  30. static const int maxfd = 31;
  31. static unsigned connect_cb_called;
  32. static uv_tcp_t server_handle;
  33. static uv_tcp_t client_handle;
  34. TEST_IMPL(emfile) {
  35. struct sockaddr_in addr;
  36. struct rlimit limits;
  37. uv_connect_t connect_req;
  38. uv_loop_t* loop;
  39. int first_fd;
  40. loop = uv_default_loop();
  41. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  42. ASSERT(0 == uv_tcp_init(loop, &server_handle));
  43. ASSERT(0 == uv_tcp_init(loop, &client_handle));
  44. ASSERT(0 == uv_tcp_bind(&server_handle, (const struct sockaddr*) &addr, 0));
  45. ASSERT(0 == uv_listen((uv_stream_t*) &server_handle, 8, connection_cb));
  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. perror("setrlimit(RLIMIT_NOFILE)");
  50. ASSERT(0);
  51. }
  52. /* Remember the first one so we can clean up afterwards. */
  53. do
  54. first_fd = dup(0);
  55. while (first_fd == -1 && errno == EINTR);
  56. ASSERT(first_fd > 0);
  57. while (dup(0) != -1 || errno == EINTR);
  58. ASSERT(errno == EMFILE);
  59. close(maxfd);
  60. /* Now connect and use up the last available file descriptor. The EMFILE
  61. * handling logic in src/unix/stream.c should ensure that connect_cb() runs
  62. * whereas connection_cb() should *not* run.
  63. */
  64. ASSERT(0 == uv_tcp_connect(&connect_req,
  65. &client_handle,
  66. (const struct sockaddr*) &addr,
  67. connect_cb));
  68. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  69. ASSERT(1 == connect_cb_called);
  70. /* Close the dups again. Ignore errors in the unlikely event that the
  71. * file descriptors were not contiguous.
  72. */
  73. while (first_fd < maxfd) {
  74. close(first_fd);
  75. first_fd += 1;
  76. }
  77. MAKE_VALGRIND_HAPPY();
  78. return 0;
  79. }
  80. static void connection_cb(uv_stream_t* server_handle, int status) {
  81. ASSERT(0 && "connection_cb should not be called.");
  82. }
  83. static void connect_cb(uv_connect_t* req, int status) {
  84. /* |status| should equal 0 because the connection should have been accepted,
  85. * it's just that the server immediately closes it again.
  86. */
  87. ASSERT(0 == status);
  88. connect_cb_called += 1;
  89. uv_close((uv_handle_t*) &server_handle, NULL);
  90. uv_close((uv_handle_t*) &client_handle, NULL);
  91. }
  92. #endif /* !defined(_WIN32) */