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