test-emfile.c
Go to the documentation of this file.
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 
32 static void connect_cb(uv_connect_t* req, int status);
33 
34 static const int maxfd = 31;
35 static unsigned connect_cb_called;
38 
39 
40 TEST_IMPL(emfile) {
41  struct sockaddr_in addr;
42  struct rlimit limits;
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 
62  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
65  ASSERT(0 == uv_tcp_bind(&server_handle, (const struct sockaddr*) &addr, 0));
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  */
84  (const struct sockaddr*) &addr,
85  connect_cb));
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 
98  return 0;
99 }
100 
101 
103  ASSERT(0 && "connection_cb should not be called.");
104 }
105 
106 
107 static 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;
115 }
116 
117 #else
118 
119 typedef int file_has_no_tests; /* ISO C forbids an empty translation unit. */
120 
121 #endif /* !_WIN32 */
async_greeter_server_with_graceful_shutdown.loop
loop
Definition: async_greeter_server_with_graceful_shutdown.py:59
task.h
uv_connect_s
Definition: uv.h:580
connection_cb
static void connection_cb(uv_stream_t *server_handle, int status)
Definition: test-emfile.c:102
uv_listen
UV_EXTERN int uv_listen(uv_stream_t *stream, int backlog, uv_connection_cb cb)
Definition: unix/stream.c:656
ASSERT
#define ASSERT(expr)
Definition: task.h:102
status
absl::Status status
Definition: rls.cc:251
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
uv_tcp_bind
UV_EXTERN int uv_tcp_bind(uv_tcp_t *handle, const struct sockaddr *addr, unsigned int flags)
Definition: uv-common.c:277
TEST_PORT
#define TEST_PORT
Definition: task.h:53
uv_close
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: unix/core.c:112
uv_stream_s
Definition: uv.h:491
uv_tcp_connect
UV_EXTERN int uv_tcp_connect(uv_connect_t *req, uv_tcp_t *handle, const struct sockaddr *addr, uv_connect_cb cb)
Definition: uv-common.c:315
uv_ip4_addr
UV_EXTERN int uv_ip4_addr(const char *ip, int port, struct sockaddr_in *addr)
Definition: uv-common.c:221
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
req
static uv_connect_t req
Definition: test-connection-fail.c:30
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
TEST_IMPL
TEST_IMPL(emfile)
Definition: test-emfile.c:40
server_handle
static uv_tcp_t server_handle
Definition: test-emfile.c:36
close
#define close
Definition: test-fs.c:48
uv_tcp_init
UV_EXTERN int uv_tcp_init(uv_loop_t *, uv_tcp_t *handle)
Definition: unix/tcp.c:143
uv_tcp_s
Definition: uv.h:544
connect_cb
static void connect_cb(uv_connect_t *req, int status)
Definition: test-emfile.c:107
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
connect_req
static uv_connect_t connect_req
Definition: benchmark-tcp-write-batch.c:39
file_has_no_tests
int file_has_no_tests
Definition: test-fs-fd-hash.c:131
RETURN_SKIP
#define RETURN_SKIP(explanation)
Definition: task.h:262
uv_handle_s
Definition: uv.h:441
uv_loop_s
Definition: uv.h:1767
maxfd
static const int maxfd
Definition: test-emfile.c:34
client_handle
static uv_tcp_t client_handle
Definition: test-emfile.c:37
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
connect_cb_called
static unsigned connect_cb_called
Definition: test-emfile.c:35
errno.h


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:26