libuv-svace-build
121 строка · 4.0 Кб
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
22#if !defined(_WIN32)23
24#include "uv.h"25#include "task.h"26
27#include <errno.h>28#include <sys/resource.h>29#include <unistd.h>30
31static void connection_cb(uv_stream_t* server_handle, int status);32static void connect_cb(uv_connect_t* req, int status);33
34static const int maxfd = 31;35static unsigned connect_cb_called;36static uv_tcp_t server_handle;37static uv_tcp_t client_handle;38
39
40TEST_IMPL(emfile) {41struct sockaddr_in addr;42struct rlimit limits;43uv_connect_t connect_req;44uv_loop_t* loop;45int first_fd;46#if defined(_AIX) || defined(__MVS__)47/* On AIX, if a 'accept' call fails ECONNRESET is set on the socket48* which causes uv__emfile_trick to not work as intended and this test
49* to fail.
50*/
51RETURN_SKIP("uv__emfile_trick does not work on this OS");52#endif53
54/* Lower the file descriptor limit and use up all fds save one. */55limits.rlim_cur = limits.rlim_max = maxfd + 1;56if (setrlimit(RLIMIT_NOFILE, &limits)) {57ASSERT_EQ(errno, EPERM); /* Valgrind blocks the setrlimit() call. */58RETURN_SKIP("setrlimit(RLIMIT_NOFILE) failed, running under valgrind?");59}60
61loop = uv_default_loop();62ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));63ASSERT_OK(uv_tcp_init(loop, &server_handle));64ASSERT_OK(uv_tcp_init(loop, &client_handle));65ASSERT_OK(uv_tcp_bind(&server_handle, (const struct sockaddr*) &addr, 0));66ASSERT_OK(uv_listen((uv_stream_t*) &server_handle, 8, connection_cb));67
68/* Remember the first one so we can clean up afterwards. */69do70first_fd = dup(0);71while (first_fd == -1 && errno == EINTR);72ASSERT_GT(first_fd, 0);73
74while (dup(0) != -1 || errno == EINTR);75ASSERT_EQ(errno, EMFILE);76close(maxfd);77
78/* Now connect and use up the last available file descriptor. The EMFILE79* handling logic in src/unix/stream.c should ensure that connect_cb() runs
80* whereas connection_cb() should *not* run.
81*/
82ASSERT_OK(uv_tcp_connect(&connect_req,83&client_handle,84(const struct sockaddr*) &addr,85connect_cb));86ASSERT_OK(uv_run(loop, UV_RUN_DEFAULT));87ASSERT_EQ(1, connect_cb_called);88
89/* Close the dups again. Ignore errors in the unlikely event that the90* file descriptors were not contiguous.
91*/
92while (first_fd < maxfd) {93close(first_fd);94first_fd += 1;95}96
97MAKE_VALGRIND_HAPPY(loop);98return 0;99}
100
101
102static void connection_cb(uv_stream_t* server_handle, int status) {103ASSERT(0 && "connection_cb should not be called.");104}
105
106
107static void connect_cb(uv_connect_t* req, int status) {108/* |status| should equal 0 because the connection should have been accepted,109* it's just that the server immediately closes it again.
110*/
111ASSERT_OK(status);112connect_cb_called += 1;113uv_close((uv_handle_t*) &server_handle, NULL);114uv_close((uv_handle_t*) &client_handle, NULL);115}
116
117#else118
119typedef int file_has_no_tests; /* ISO C forbids an empty translation unit. */120
121#endif /* !_WIN32 */122