Blame test/test-emfile.c

Packit b5b901
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
Packit b5b901
 *
Packit b5b901
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit b5b901
 * of this software and associated documentation files (the "Software"), to
Packit b5b901
 * deal in the Software without restriction, including without limitation the
Packit b5b901
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
Packit b5b901
 * sell copies of the Software, and to permit persons to whom the Software is
Packit b5b901
 * furnished to do so, subject to the following conditions:
Packit b5b901
 *
Packit b5b901
 * The above copyright notice and this permission notice shall be included in
Packit b5b901
 * all copies or substantial portions of the Software.
Packit b5b901
 *
Packit b5b901
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit b5b901
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit b5b901
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit b5b901
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit b5b901
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit b5b901
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
Packit b5b901
 * IN THE SOFTWARE.
Packit b5b901
 */
Packit b5b901
Packit b5b901
#if !defined(_WIN32)
Packit b5b901
Packit b5b901
#include "uv.h"
Packit b5b901
#include "task.h"
Packit b5b901
Packit b5b901
#include <errno.h>
Packit b5b901
#include <sys/resource.h>
Packit b5b901
#include <unistd.h>
Packit b5b901
Packit b5b901
static void connection_cb(uv_stream_t* server_handle, int status);
Packit b5b901
static void connect_cb(uv_connect_t* req, int status);
Packit b5b901
Packit b5b901
static const int maxfd = 31;
Packit b5b901
static unsigned connect_cb_called;
Packit b5b901
static uv_tcp_t server_handle;
Packit b5b901
static uv_tcp_t client_handle;
Packit b5b901
Packit b5b901
Packit b5b901
TEST_IMPL(emfile) {
Packit Service e08953
  struct sockaddr_in addr;
Packit Service e08953
  struct rlimit limits;
Packit Service e08953
  uv_connect_t connect_req;
Packit Service e08953
  uv_loop_t* loop;
Packit Service e08953
  int first_fd;
Packit b5b901
#if defined(_AIX) || defined(__MVS__)
Packit b5b901
  /* On AIX, if a 'accept' call fails ECONNRESET is set on the socket
Packit b5b901
   * which causes uv__emfile_trick to not work as intended and this test
Packit b5b901
   * to fail.
Packit b5b901
   */
Packit b5b901
  RETURN_SKIP("uv__emfile_trick does not work on this OS");
Packit b5b901
#endif
Packit b5b901
Packit b5b901
  /* Lower the file descriptor limit and use up all fds save one. */
Packit b5b901
  limits.rlim_cur = limits.rlim_max = maxfd + 1;
Packit b5b901
  if (setrlimit(RLIMIT_NOFILE, &limits)) {
Packit b5b901
    ASSERT(errno == EPERM);  /* Valgrind blocks the setrlimit() call. */
Packit b5b901
    RETURN_SKIP("setrlimit(RLIMIT_NOFILE) failed, running under valgrind?");
Packit b5b901
  }
Packit b5b901
Packit b5b901
  loop = uv_default_loop();
Packit b5b901
  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
Packit b5b901
  ASSERT(0 == uv_tcp_init(loop, &server_handle));
Packit b5b901
  ASSERT(0 == uv_tcp_init(loop, &client_handle));
Packit b5b901
  ASSERT(0 == uv_tcp_bind(&server_handle, (const struct sockaddr*) &addr, 0));
Packit b5b901
  ASSERT(0 == uv_listen((uv_stream_t*) &server_handle, 8, connection_cb));
Packit b5b901
Packit b5b901
  /* Remember the first one so we can clean up afterwards. */
Packit b5b901
  do
Packit b5b901
    first_fd = dup(0);
Packit b5b901
  while (first_fd == -1 && errno == EINTR);
Packit b5b901
  ASSERT(first_fd > 0);
Packit b5b901
Packit b5b901
  while (dup(0) != -1 || errno == EINTR);
Packit b5b901
  ASSERT(errno == EMFILE);
Packit b5b901
  close(maxfd);
Packit b5b901
Packit b5b901
  /* Now connect and use up the last available file descriptor.  The EMFILE
Packit b5b901
   * handling logic in src/unix/stream.c should ensure that connect_cb() runs
Packit b5b901
   * whereas connection_cb() should *not* run.
Packit b5b901
   */
Packit b5b901
  ASSERT(0 == uv_tcp_connect(&connect_req,
Packit b5b901
                             &client_handle,
Packit b5b901
                             (const struct sockaddr*) &addr,
Packit b5b901
                             connect_cb));
Packit b5b901
  ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
Packit b5b901
  ASSERT(1 == connect_cb_called);
Packit b5b901
Packit b5b901
  /* Close the dups again. Ignore errors in the unlikely event that the
Packit b5b901
   * file descriptors were not contiguous.
Packit b5b901
   */
Packit b5b901
  while (first_fd < maxfd) {
Packit b5b901
    close(first_fd);
Packit b5b901
    first_fd += 1;
Packit b5b901
  }
Packit b5b901
Packit b5b901
  MAKE_VALGRIND_HAPPY();
Packit b5b901
  return 0;
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
static void connection_cb(uv_stream_t* server_handle, int status) {
Packit b5b901
  ASSERT(0 && "connection_cb should not be called.");
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
static void connect_cb(uv_connect_t* req, int status) {
Packit b5b901
  /* |status| should equal 0 because the connection should have been accepted,
Packit b5b901
   * it's just that the server immediately closes it again.
Packit b5b901
   */
Packit b5b901
  ASSERT(0 == status);
Packit b5b901
  connect_cb_called += 1;
Packit b5b901
  uv_close((uv_handle_t*) &server_handle, NULL);
Packit b5b901
  uv_close((uv_handle_t*) &client_handle, NULL);
Packit b5b901
}
Packit b5b901
Packit Service e08953
#else
Packit Service e08953
Packit Service e08953
typedef int file_has_no_tests; /* ISO C forbids an empty translation unit. */
Packit Service e08953
Packit Service e08953
#endif /* !_WIN32 */