11cb0ef41Sopenharmony_ci/* MIT License 21cb0ef41Sopenharmony_ci * 31cb0ef41Sopenharmony_ci * Copyright (c) 2024 Brad House 41cb0ef41Sopenharmony_ci * 51cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy 61cb0ef41Sopenharmony_ci * of this software and associated documentation files (the "Software"), to deal 71cb0ef41Sopenharmony_ci * in the Software without restriction, including without limitation the rights 81cb0ef41Sopenharmony_ci * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 91cb0ef41Sopenharmony_ci * copies of the Software, and to permit persons to whom the Software is 101cb0ef41Sopenharmony_ci * furnished to do so, subject to the following conditions: 111cb0ef41Sopenharmony_ci * 121cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice (including the next 131cb0ef41Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 141cb0ef41Sopenharmony_ci * Software. 151cb0ef41Sopenharmony_ci * 161cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 171cb0ef41Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 181cb0ef41Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 191cb0ef41Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 201cb0ef41Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 211cb0ef41Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 221cb0ef41Sopenharmony_ci * SOFTWARE. 231cb0ef41Sopenharmony_ci * 241cb0ef41Sopenharmony_ci * SPDX-License-Identifier: MIT 251cb0ef41Sopenharmony_ci */ 261cb0ef41Sopenharmony_ci#include "ares_setup.h" 271cb0ef41Sopenharmony_ci#include "ares.h" 281cb0ef41Sopenharmony_ci#include "ares_private.h" 291cb0ef41Sopenharmony_ci#include "ares_event.h" 301cb0ef41Sopenharmony_ci#ifdef HAVE_SYS_SELECT_H 311cb0ef41Sopenharmony_ci# include <sys/select.h> 321cb0ef41Sopenharmony_ci#endif 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci/* All systems have select(), but not all have a way to wake, so we require 351cb0ef41Sopenharmony_ci * pipe() to wake the select() */ 361cb0ef41Sopenharmony_ci#if defined(HAVE_PIPE) 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_cistatic ares_bool_t ares_evsys_select_init(ares_event_thread_t *e) 391cb0ef41Sopenharmony_ci{ 401cb0ef41Sopenharmony_ci e->ev_signal = ares_pipeevent_create(e); 411cb0ef41Sopenharmony_ci if (e->ev_signal == NULL) { 421cb0ef41Sopenharmony_ci return ARES_FALSE; 431cb0ef41Sopenharmony_ci } 441cb0ef41Sopenharmony_ci return ARES_TRUE; 451cb0ef41Sopenharmony_ci} 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_cistatic void ares_evsys_select_destroy(ares_event_thread_t *e) 481cb0ef41Sopenharmony_ci{ 491cb0ef41Sopenharmony_ci (void)e; 501cb0ef41Sopenharmony_ci} 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_cistatic ares_bool_t ares_evsys_select_event_add(ares_event_t *event) 531cb0ef41Sopenharmony_ci{ 541cb0ef41Sopenharmony_ci (void)event; 551cb0ef41Sopenharmony_ci return ARES_TRUE; 561cb0ef41Sopenharmony_ci} 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_cistatic void ares_evsys_select_event_del(ares_event_t *event) 591cb0ef41Sopenharmony_ci{ 601cb0ef41Sopenharmony_ci (void)event; 611cb0ef41Sopenharmony_ci} 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_cistatic void ares_evsys_select_event_mod(ares_event_t *event, 641cb0ef41Sopenharmony_ci ares_event_flags_t new_flags) 651cb0ef41Sopenharmony_ci{ 661cb0ef41Sopenharmony_ci (void)event; 671cb0ef41Sopenharmony_ci (void)new_flags; 681cb0ef41Sopenharmony_ci} 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_cistatic size_t ares_evsys_select_wait(ares_event_thread_t *e, 711cb0ef41Sopenharmony_ci unsigned long timeout_ms) 721cb0ef41Sopenharmony_ci{ 731cb0ef41Sopenharmony_ci size_t num_fds = 0; 741cb0ef41Sopenharmony_ci ares_socket_t *fdlist = ares__htable_asvp_keys(e->ev_handles, &num_fds); 751cb0ef41Sopenharmony_ci int rv; 761cb0ef41Sopenharmony_ci size_t cnt = 0; 771cb0ef41Sopenharmony_ci size_t i; 781cb0ef41Sopenharmony_ci fd_set read_fds; 791cb0ef41Sopenharmony_ci fd_set write_fds; 801cb0ef41Sopenharmony_ci int nfds = 0; 811cb0ef41Sopenharmony_ci struct timeval tv; 821cb0ef41Sopenharmony_ci struct timeval *tout = NULL; 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ci FD_ZERO(&read_fds); 851cb0ef41Sopenharmony_ci FD_ZERO(&write_fds); 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_ci for (i = 0; i < num_fds; i++) { 881cb0ef41Sopenharmony_ci const ares_event_t *ev = 891cb0ef41Sopenharmony_ci ares__htable_asvp_get_direct(e->ev_handles, fdlist[i]); 901cb0ef41Sopenharmony_ci if (ev->flags & ARES_EVENT_FLAG_READ) { 911cb0ef41Sopenharmony_ci FD_SET(ev->fd, &read_fds); 921cb0ef41Sopenharmony_ci } 931cb0ef41Sopenharmony_ci if (ev->flags & ARES_EVENT_FLAG_WRITE) { 941cb0ef41Sopenharmony_ci FD_SET(ev->fd, &write_fds); 951cb0ef41Sopenharmony_ci } 961cb0ef41Sopenharmony_ci if (ev->fd + 1 > nfds) { 971cb0ef41Sopenharmony_ci nfds = ev->fd + 1; 981cb0ef41Sopenharmony_ci } 991cb0ef41Sopenharmony_ci } 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ci if (timeout_ms) { 1021cb0ef41Sopenharmony_ci tv.tv_sec = (int)(timeout_ms / 1000); 1031cb0ef41Sopenharmony_ci tv.tv_usec = (int)((timeout_ms % 1000) * 1000); 1041cb0ef41Sopenharmony_ci tout = &tv; 1051cb0ef41Sopenharmony_ci } 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ci rv = select(nfds, &read_fds, &write_fds, NULL, tout); 1081cb0ef41Sopenharmony_ci if (rv > 0) { 1091cb0ef41Sopenharmony_ci for (i = 0; i < num_fds; i++) { 1101cb0ef41Sopenharmony_ci ares_event_t *ev; 1111cb0ef41Sopenharmony_ci ares_event_flags_t flags = 0; 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci ev = ares__htable_asvp_get_direct(e->ev_handles, fdlist[i]); 1141cb0ef41Sopenharmony_ci if (ev == NULL || ev->cb == NULL) { 1151cb0ef41Sopenharmony_ci continue; 1161cb0ef41Sopenharmony_ci } 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_ci if (FD_ISSET(fdlist[i], &read_fds)) { 1191cb0ef41Sopenharmony_ci flags |= ARES_EVENT_FLAG_READ; 1201cb0ef41Sopenharmony_ci } 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ci if (FD_ISSET(fdlist[i], &write_fds)) { 1231cb0ef41Sopenharmony_ci flags |= ARES_EVENT_FLAG_WRITE; 1241cb0ef41Sopenharmony_ci } 1251cb0ef41Sopenharmony_ci 1261cb0ef41Sopenharmony_ci if (flags == 0) { 1271cb0ef41Sopenharmony_ci continue; 1281cb0ef41Sopenharmony_ci } 1291cb0ef41Sopenharmony_ci 1301cb0ef41Sopenharmony_ci cnt++; 1311cb0ef41Sopenharmony_ci 1321cb0ef41Sopenharmony_ci ev->cb(e, fdlist[i], ev->data, flags); 1331cb0ef41Sopenharmony_ci } 1341cb0ef41Sopenharmony_ci } 1351cb0ef41Sopenharmony_ci 1361cb0ef41Sopenharmony_ci ares_free(fdlist); 1371cb0ef41Sopenharmony_ci 1381cb0ef41Sopenharmony_ci return cnt; 1391cb0ef41Sopenharmony_ci} 1401cb0ef41Sopenharmony_ci 1411cb0ef41Sopenharmony_ciconst ares_event_sys_t ares_evsys_select = { 1421cb0ef41Sopenharmony_ci "select", 1431cb0ef41Sopenharmony_ci ares_evsys_select_init, 1441cb0ef41Sopenharmony_ci ares_evsys_select_destroy, /* NoOp */ 1451cb0ef41Sopenharmony_ci ares_evsys_select_event_add, /* NoOp */ 1461cb0ef41Sopenharmony_ci ares_evsys_select_event_del, /* NoOp */ 1471cb0ef41Sopenharmony_ci ares_evsys_select_event_mod, /* NoOp */ 1481cb0ef41Sopenharmony_ci ares_evsys_select_wait 1491cb0ef41Sopenharmony_ci}; 1501cb0ef41Sopenharmony_ci 1511cb0ef41Sopenharmony_ci#endif 152