test-handle-fileno.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 #include "uv.h"
23 #include "task.h"
24 
25 
26 static int get_tty_fd(void) {
27  /* Make sure we have an FD that refers to a tty */
28 #ifdef _WIN32
29  HANDLE handle;
30  handle = CreateFileA("conin$",
31  GENERIC_READ | GENERIC_WRITE,
32  FILE_SHARE_READ | FILE_SHARE_WRITE,
33  NULL,
34  OPEN_EXISTING,
35  FILE_ATTRIBUTE_NORMAL,
36  NULL);
38  return -1;
39  return _open_osfhandle((intptr_t) handle, 0);
40 #else /* unix */
41  return open("/dev/tty", O_RDONLY, 0);
42 #endif
43 }
44 
45 
46 TEST_IMPL(handle_fileno) {
47  int r;
48  int tty_fd;
49  struct sockaddr_in addr;
50  uv_os_fd_t fd;
51  uv_tcp_t tcp;
52  uv_udp_t udp;
53  uv_pipe_t pipe;
54  uv_tty_t tty;
56  uv_loop_t* loop;
57 
59  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
60 
61  r = uv_idle_init(loop, &idle);
62  ASSERT(r == 0);
63  r = uv_fileno((uv_handle_t*) &idle, &fd);
64  ASSERT(r == UV_EINVAL);
65  uv_close((uv_handle_t*) &idle, NULL);
66 
67  r = uv_tcp_init(loop, &tcp);
68  ASSERT(r == 0);
69  r = uv_fileno((uv_handle_t*) &tcp, &fd);
70  ASSERT(r == UV_EBADF);
71  r = uv_tcp_bind(&tcp, (const struct sockaddr*) &addr, 0);
72  ASSERT(r == 0);
73  r = uv_fileno((uv_handle_t*) &tcp, &fd);
74  ASSERT(r == 0);
75  uv_close((uv_handle_t*) &tcp, NULL);
76  r = uv_fileno((uv_handle_t*) &tcp, &fd);
77  ASSERT(r == UV_EBADF);
78 
79  r = uv_udp_init(loop, &udp);
80  ASSERT(r == 0);
81  r = uv_fileno((uv_handle_t*) &udp, &fd);
82  ASSERT(r == UV_EBADF);
83  r = uv_udp_bind(&udp, (const struct sockaddr*) &addr, 0);
84  ASSERT(r == 0);
85  r = uv_fileno((uv_handle_t*) &udp, &fd);
86  ASSERT(r == 0);
87  uv_close((uv_handle_t*) &udp, NULL);
88  r = uv_fileno((uv_handle_t*) &udp, &fd);
89  ASSERT(r == UV_EBADF);
90 
91  r = uv_pipe_init(loop, &pipe, 0);
92  ASSERT(r == 0);
93  r = uv_fileno((uv_handle_t*) &pipe, &fd);
94  ASSERT(r == UV_EBADF);
95  r = uv_pipe_bind(&pipe, TEST_PIPENAME);
96  ASSERT(r == 0);
97  r = uv_fileno((uv_handle_t*) &pipe, &fd);
98  ASSERT(r == 0);
99  uv_close((uv_handle_t*) &pipe, NULL);
100  r = uv_fileno((uv_handle_t*) &pipe, &fd);
101  ASSERT(r == UV_EBADF);
102 
103  tty_fd = get_tty_fd();
104  if (tty_fd < 0) {
105  fprintf(stderr, "Cannot open a TTY fd");
106  fflush(stderr);
107  } else {
108  r = uv_tty_init(loop, &tty, tty_fd, 0);
109  ASSERT(r == 0);
112  r = uv_fileno((uv_handle_t*) &tty, &fd);
113  ASSERT(r == 0);
114  uv_close((uv_handle_t*) &tty, NULL);
115  r = uv_fileno((uv_handle_t*) &tty, &fd);
116  ASSERT(r == UV_EBADF);
119  }
120 
122 
124  return 0;
125 }
async_greeter_server_with_graceful_shutdown.loop
loop
Definition: async_greeter_server_with_graceful_shutdown.py:59
idle
static uv_idle_t idle
Definition: test-poll-oob.c:37
task.h
uv_pipe_init
UV_EXTERN int uv_pipe_init(uv_loop_t *, uv_pipe_t *handle, int ipc)
Definition: unix/pipe.c:33
uv_tty_s
Definition: uv.h:704
ASSERT
#define ASSERT(expr)
Definition: task.h:102
tcp
static uv_tcp_t tcp
Definition: test-connection-fail.c:29
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
get_tty_fd
static int get_tty_fd(void)
Definition: test-handle-fileno.c:26
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
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_ip4_addr
UV_EXTERN int uv_ip4_addr(const char *ip, int port, struct sockaddr_in *addr)
Definition: uv-common.c:221
uv_udp_init
UV_EXTERN int uv_udp_init(uv_loop_t *, uv_udp_t *handle)
Definition: unix/udp.c:988
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
tty
uv_tty_t tty
Definition: libuv/docs/code/tty/main.c:7
uv_is_readable
UV_EXTERN int uv_is_readable(const uv_stream_t *handle)
Definition: unix/stream.c:1606
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
uv_udp_s
Definition: uv.h:629
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
uv_tcp_init
UV_EXTERN int uv_tcp_init(uv_loop_t *, uv_tcp_t *handle)
Definition: unix/tcp.c:143
TEST_IMPL
TEST_IMPL(handle_fileno)
Definition: test-handle-fileno.c:46
uv_tcp_s
Definition: uv.h:544
uv_idle_init
UV_EXTERN int uv_idle_init(uv_loop_t *, uv_idle_t *idle)
udp
static uv_udp_t udp
Definition: test-getsockname.c:38
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
uv_fileno
UV_EXTERN int uv_fileno(const uv_handle_t *handle, uv_os_fd_t *fd)
Definition: unix/core.c:755
uv_udp_bind
UV_EXTERN int uv_udp_bind(uv_udp_t *handle, const struct sockaddr *addr, unsigned int flags)
Definition: uv-common.c:296
uv_os_fd_t
int uv_os_fd_t
Definition: unix.h:128
uv_idle_s
Definition: uv.h:824
fix_build_deps.r
r
Definition: fix_build_deps.py:491
uv_tty_init
UV_EXTERN int uv_tty_init(uv_loop_t *, uv_tty_t *, uv_file fd, int readable)
Definition: unix/tty.c:123
INVALID_HANDLE_VALUE
#define INVALID_HANDLE_VALUE
Definition: bloaty/third_party/zlib/contrib/minizip/iowin32.c:21
uv_pipe_s
Definition: uv.h:757
open
#define open
Definition: test-fs.c:46
handle
static csh handle
Definition: test_arm_regression.c:16
uv_handle_s
Definition: uv.h:441
TEST_PIPENAME
#define TEST_PIPENAME
Definition: task.h:61
uv_loop_s
Definition: uv.h:1767
uv_is_writable
UV_EXTERN int uv_is_writable(const uv_stream_t *handle)
Definition: unix/stream.c:1611
uv_pipe_bind
UV_EXTERN int uv_pipe_bind(uv_pipe_t *handle, const char *name)
Definition: unix/pipe.c:43
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10


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