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) {
41 struct sockaddr_in addr;
42 struct rlimit limits;
43 uv_connect_t connect_req;
44 uv_loop_t* loop;
45 int first_fd;
46#if defined(_AIX) || defined(__MVS__)
47 /* On AIX, if a 'accept' call fails ECONNRESET is set on the socket
48 * which causes uv__emfile_trick to not work as intended and this test
49 * to fail.
50 */
51 RETURN_SKIP("uv__emfile_trick does not work on this OS");
52#endif
53
54 /* Lower the file descriptor limit and use up all fds save one. */
55 limits.rlim_cur = limits.rlim_max = maxfd + 1;
56 if (setrlimit(RLIMIT_NOFILE, &limits)) {
57 ASSERT(errno == EPERM); /* Valgrind blocks the setrlimit() call. */
58 RETURN_SKIP("setrlimit(RLIMIT_NOFILE) failed, running under valgrind?");
59 }
60
61 loop = uv_default_loop();
62 ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
63 ASSERT(0 == uv_tcp_init(loop, &server_handle));
64 ASSERT(0 == uv_tcp_init(loop, &client_handle));
65 ASSERT(0 == uv_tcp_bind(&server_handle, (const struct sockaddr*) &addr, 0));
66 ASSERT(0 == uv_listen((uv_stream_t*) &server_handle, 8, connection_cb));
67
68 /* Remember the first one so we can clean up afterwards. */
69 do
70 first_fd = dup(0);
71 while (first_fd == -1 && errno == EINTR);
72 ASSERT(first_fd > 0);
73
74 while (dup(0) != -1 || errno == EINTR);
75 ASSERT(errno == EMFILE);
76 close(maxfd);
77
78 /* Now connect and use up the last available file descriptor. The EMFILE
79 * handling logic in src/unix/stream.c should ensure that connect_cb() runs
80 * whereas connection_cb() should *not* run.
81 */
82 ASSERT(0 == uv_tcp_connect(&connect_req,
83 &client_handle,
84 (const struct sockaddr*) &addr,
85 connect_cb));
86 ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
87 ASSERT(1 == connect_cb_called);
88
89 /* Close the dups again. Ignore errors in the unlikely event that the
90 * file descriptors were not contiguous.
91 */
92 while (first_fd < maxfd) {
93 close(first_fd);
94 first_fd += 1;
95 }
96
97 MAKE_VALGRIND_HAPPY();
98 return 0;
99}
100
101
102static void connection_cb(uv_stream_t* server_handle, int status) {
103 ASSERT(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 */
111 ASSERT(0 == status);
112 connect_cb_called += 1;
113 uv_close((uv_handle_t*) &server_handle, NULL);
114 uv_close((uv_handle_t*) &client_handle, NULL);
115}
116
117#else
118
119typedef int file_has_no_tests; /* ISO C forbids an empty translation unit. */
120
121#endif /* !_WIN32 */
122