11cb0ef41Sopenharmony_ci/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 21cb0ef41Sopenharmony_ci * 31cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy 41cb0ef41Sopenharmony_ci * of this software and associated documentation files (the "Software"), to 51cb0ef41Sopenharmony_ci * deal in the Software without restriction, including without limitation the 61cb0ef41Sopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 71cb0ef41Sopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is 81cb0ef41Sopenharmony_ci * furnished to do so, subject to the following conditions: 91cb0ef41Sopenharmony_ci * 101cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice shall be included in 111cb0ef41Sopenharmony_ci * all copies or substantial portions of the Software. 121cb0ef41Sopenharmony_ci * 131cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 141cb0ef41Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 151cb0ef41Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 161cb0ef41Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 171cb0ef41Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 181cb0ef41Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 191cb0ef41Sopenharmony_ci * IN THE SOFTWARE. 201cb0ef41Sopenharmony_ci */ 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci#include <assert.h> 231cb0ef41Sopenharmony_ci#include <io.h> 241cb0ef41Sopenharmony_ci#include <stdlib.h> 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci#include "uv.h" 271cb0ef41Sopenharmony_ci#include "internal.h" 281cb0ef41Sopenharmony_ci#include "handle-inl.h" 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ciuv_handle_type uv_guess_handle(uv_file file) { 321cb0ef41Sopenharmony_ci HANDLE handle; 331cb0ef41Sopenharmony_ci DWORD mode; 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci if (file < 0) { 361cb0ef41Sopenharmony_ci return UV_UNKNOWN_HANDLE; 371cb0ef41Sopenharmony_ci } 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci handle = uv__get_osfhandle(file); 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci switch (GetFileType(handle)) { 421cb0ef41Sopenharmony_ci case FILE_TYPE_CHAR: 431cb0ef41Sopenharmony_ci if (GetConsoleMode(handle, &mode)) { 441cb0ef41Sopenharmony_ci return UV_TTY; 451cb0ef41Sopenharmony_ci } else { 461cb0ef41Sopenharmony_ci return UV_FILE; 471cb0ef41Sopenharmony_ci } 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci case FILE_TYPE_PIPE: 501cb0ef41Sopenharmony_ci return UV_NAMED_PIPE; 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci case FILE_TYPE_DISK: 531cb0ef41Sopenharmony_ci return UV_FILE; 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci default: 561cb0ef41Sopenharmony_ci return UV_UNKNOWN_HANDLE; 571cb0ef41Sopenharmony_ci } 581cb0ef41Sopenharmony_ci} 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ciint uv_is_active(const uv_handle_t* handle) { 621cb0ef41Sopenharmony_ci return (handle->flags & UV_HANDLE_ACTIVE) && 631cb0ef41Sopenharmony_ci !(handle->flags & UV_HANDLE_CLOSING); 641cb0ef41Sopenharmony_ci} 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_civoid uv_close(uv_handle_t* handle, uv_close_cb cb) { 681cb0ef41Sopenharmony_ci uv_loop_t* loop = handle->loop; 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci if (handle->flags & UV_HANDLE_CLOSING) { 711cb0ef41Sopenharmony_ci assert(0); 721cb0ef41Sopenharmony_ci return; 731cb0ef41Sopenharmony_ci } 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci handle->close_cb = cb; 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci /* Handle-specific close actions */ 781cb0ef41Sopenharmony_ci switch (handle->type) { 791cb0ef41Sopenharmony_ci case UV_TCP: 801cb0ef41Sopenharmony_ci uv__tcp_close(loop, (uv_tcp_t*)handle); 811cb0ef41Sopenharmony_ci return; 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci case UV_NAMED_PIPE: 841cb0ef41Sopenharmony_ci uv__pipe_close(loop, (uv_pipe_t*) handle); 851cb0ef41Sopenharmony_ci return; 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_ci case UV_TTY: 881cb0ef41Sopenharmony_ci uv__tty_close((uv_tty_t*) handle); 891cb0ef41Sopenharmony_ci return; 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci case UV_UDP: 921cb0ef41Sopenharmony_ci uv__udp_close(loop, (uv_udp_t*) handle); 931cb0ef41Sopenharmony_ci return; 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ci case UV_POLL: 961cb0ef41Sopenharmony_ci uv__poll_close(loop, (uv_poll_t*) handle); 971cb0ef41Sopenharmony_ci return; 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci case UV_TIMER: 1001cb0ef41Sopenharmony_ci uv_timer_stop((uv_timer_t*)handle); 1011cb0ef41Sopenharmony_ci uv__handle_closing(handle); 1021cb0ef41Sopenharmony_ci uv__want_endgame(loop, handle); 1031cb0ef41Sopenharmony_ci return; 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_ci case UV_PREPARE: 1061cb0ef41Sopenharmony_ci uv_prepare_stop((uv_prepare_t*)handle); 1071cb0ef41Sopenharmony_ci uv__handle_closing(handle); 1081cb0ef41Sopenharmony_ci uv__want_endgame(loop, handle); 1091cb0ef41Sopenharmony_ci return; 1101cb0ef41Sopenharmony_ci 1111cb0ef41Sopenharmony_ci case UV_CHECK: 1121cb0ef41Sopenharmony_ci uv_check_stop((uv_check_t*)handle); 1131cb0ef41Sopenharmony_ci uv__handle_closing(handle); 1141cb0ef41Sopenharmony_ci uv__want_endgame(loop, handle); 1151cb0ef41Sopenharmony_ci return; 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ci case UV_IDLE: 1181cb0ef41Sopenharmony_ci uv_idle_stop((uv_idle_t*)handle); 1191cb0ef41Sopenharmony_ci uv__handle_closing(handle); 1201cb0ef41Sopenharmony_ci uv__want_endgame(loop, handle); 1211cb0ef41Sopenharmony_ci return; 1221cb0ef41Sopenharmony_ci 1231cb0ef41Sopenharmony_ci case UV_ASYNC: 1241cb0ef41Sopenharmony_ci uv__async_close(loop, (uv_async_t*) handle); 1251cb0ef41Sopenharmony_ci return; 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ci case UV_SIGNAL: 1281cb0ef41Sopenharmony_ci uv__signal_close(loop, (uv_signal_t*) handle); 1291cb0ef41Sopenharmony_ci return; 1301cb0ef41Sopenharmony_ci 1311cb0ef41Sopenharmony_ci case UV_PROCESS: 1321cb0ef41Sopenharmony_ci uv__process_close(loop, (uv_process_t*) handle); 1331cb0ef41Sopenharmony_ci return; 1341cb0ef41Sopenharmony_ci 1351cb0ef41Sopenharmony_ci case UV_FS_EVENT: 1361cb0ef41Sopenharmony_ci uv__fs_event_close(loop, (uv_fs_event_t*) handle); 1371cb0ef41Sopenharmony_ci return; 1381cb0ef41Sopenharmony_ci 1391cb0ef41Sopenharmony_ci case UV_FS_POLL: 1401cb0ef41Sopenharmony_ci uv__fs_poll_close((uv_fs_poll_t*) handle); 1411cb0ef41Sopenharmony_ci uv__handle_closing(handle); 1421cb0ef41Sopenharmony_ci return; 1431cb0ef41Sopenharmony_ci 1441cb0ef41Sopenharmony_ci default: 1451cb0ef41Sopenharmony_ci /* Not supported */ 1461cb0ef41Sopenharmony_ci abort(); 1471cb0ef41Sopenharmony_ci } 1481cb0ef41Sopenharmony_ci} 1491cb0ef41Sopenharmony_ci 1501cb0ef41Sopenharmony_ci 1511cb0ef41Sopenharmony_ciint uv_is_closing(const uv_handle_t* handle) { 1521cb0ef41Sopenharmony_ci return !!(handle->flags & (UV_HANDLE_CLOSING | UV_HANDLE_CLOSED)); 1531cb0ef41Sopenharmony_ci} 1541cb0ef41Sopenharmony_ci 1551cb0ef41Sopenharmony_ci 1561cb0ef41Sopenharmony_ciuv_os_fd_t uv_get_osfhandle(int fd) { 1571cb0ef41Sopenharmony_ci return uv__get_osfhandle(fd); 1581cb0ef41Sopenharmony_ci} 1591cb0ef41Sopenharmony_ci 1601cb0ef41Sopenharmony_ciint uv_open_osfhandle(uv_os_fd_t os_fd) { 1611cb0ef41Sopenharmony_ci return _open_osfhandle((intptr_t) os_fd, 0); 1621cb0ef41Sopenharmony_ci} 163