1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
28 #include "handle-inl.h"
31 uv_handle_type uv_guess_handle(uv_file file) {
36 return UV_UNKNOWN_HANDLE;
39 handle = uv__get_osfhandle(file);
41 switch (GetFileType(handle)) {
43 if (GetConsoleMode(handle, &mode)) {
56 return UV_UNKNOWN_HANDLE;
61 int uv_is_active(const uv_handle_t* handle) {
62 return (handle->flags & UV_HANDLE_ACTIVE) &&
63 !(handle->flags & UV_HANDLE_CLOSING);
67 void uv_close(uv_handle_t* handle, uv_close_cb cb) {
68 uv_loop_t* loop = handle->loop;
70 if (handle->flags & UV_HANDLE_CLOSING) {
75 handle->close_cb = cb;
77 /* Handle-specific close actions */
78 switch (handle->type) {
80 uv__tcp_close(loop, (uv_tcp_t*)handle);
84 uv__pipe_close(loop, (uv_pipe_t*) handle);
88 uv__tty_close((uv_tty_t*) handle);
92 uv__udp_close(loop, (uv_udp_t*) handle);
96 uv__poll_close(loop, (uv_poll_t*) handle);
100 uv_timer_stop((uv_timer_t*)handle);
101 uv__handle_closing(handle);
102 uv__want_endgame(loop, handle);
106 uv_prepare_stop((uv_prepare_t*)handle);
107 uv__handle_closing(handle);
108 uv__want_endgame(loop, handle);
112 uv_check_stop((uv_check_t*)handle);
113 uv__handle_closing(handle);
114 uv__want_endgame(loop, handle);
118 uv_idle_stop((uv_idle_t*)handle);
119 uv__handle_closing(handle);
120 uv__want_endgame(loop, handle);
124 uv__async_close(loop, (uv_async_t*) handle);
128 uv__signal_close(loop, (uv_signal_t*) handle);
132 uv__process_close(loop, (uv_process_t*) handle);
136 uv__fs_event_close(loop, (uv_fs_event_t*) handle);
140 uv__fs_poll_close((uv_fs_poll_t*) handle);
141 uv__handle_closing(handle);
151 int uv_is_closing(const uv_handle_t* handle) {
152 return !!(handle->flags & (UV_HANDLE_CLOSING | UV_HANDLE_CLOSED));
156 uv_os_fd_t uv_get_osfhandle(int fd) {
157 return uv__get_osfhandle(fd);
160 int uv_open_osfhandle(uv_os_fd_t os_fd) {
161 return _open_osfhandle((intptr_t) os_fd, 0);