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_POLL_H
311cb0ef41Sopenharmony_ci#  include <poll.h>
321cb0ef41Sopenharmony_ci#endif
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci#if defined(HAVE_POLL)
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_cistatic ares_bool_t ares_evsys_poll_init(ares_event_thread_t *e)
371cb0ef41Sopenharmony_ci{
381cb0ef41Sopenharmony_ci  e->ev_signal = ares_pipeevent_create(e);
391cb0ef41Sopenharmony_ci  if (e->ev_signal == NULL) {
401cb0ef41Sopenharmony_ci    return ARES_FALSE;
411cb0ef41Sopenharmony_ci  }
421cb0ef41Sopenharmony_ci  return ARES_TRUE;
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_cistatic void ares_evsys_poll_destroy(ares_event_thread_t *e)
461cb0ef41Sopenharmony_ci{
471cb0ef41Sopenharmony_ci  (void)e;
481cb0ef41Sopenharmony_ci}
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_cistatic ares_bool_t ares_evsys_poll_event_add(ares_event_t *event)
511cb0ef41Sopenharmony_ci{
521cb0ef41Sopenharmony_ci  (void)event;
531cb0ef41Sopenharmony_ci  return ARES_TRUE;
541cb0ef41Sopenharmony_ci}
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_cistatic void ares_evsys_poll_event_del(ares_event_t *event)
571cb0ef41Sopenharmony_ci{
581cb0ef41Sopenharmony_ci  (void)event;
591cb0ef41Sopenharmony_ci}
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_cistatic void ares_evsys_poll_event_mod(ares_event_t      *event,
621cb0ef41Sopenharmony_ci                                      ares_event_flags_t new_flags)
631cb0ef41Sopenharmony_ci{
641cb0ef41Sopenharmony_ci  (void)event;
651cb0ef41Sopenharmony_ci  (void)new_flags;
661cb0ef41Sopenharmony_ci}
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_cistatic size_t ares_evsys_poll_wait(ares_event_thread_t *e,
691cb0ef41Sopenharmony_ci                                   unsigned long        timeout_ms)
701cb0ef41Sopenharmony_ci{
711cb0ef41Sopenharmony_ci  size_t         num_fds = 0;
721cb0ef41Sopenharmony_ci  ares_socket_t *fdlist  = ares__htable_asvp_keys(e->ev_handles, &num_fds);
731cb0ef41Sopenharmony_ci  struct pollfd *pollfd  = NULL;
741cb0ef41Sopenharmony_ci  int            rv;
751cb0ef41Sopenharmony_ci  size_t         cnt = 0;
761cb0ef41Sopenharmony_ci  size_t         i;
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci  if (num_fds) {
791cb0ef41Sopenharmony_ci    pollfd = ares_malloc_zero(sizeof(*pollfd) * num_fds);
801cb0ef41Sopenharmony_ci    for (i = 0; i < num_fds; i++) {
811cb0ef41Sopenharmony_ci      const ares_event_t *ev =
821cb0ef41Sopenharmony_ci        ares__htable_asvp_get_direct(e->ev_handles, fdlist[i]);
831cb0ef41Sopenharmony_ci      pollfd[i].fd = ev->fd;
841cb0ef41Sopenharmony_ci      if (ev->flags & ARES_EVENT_FLAG_READ) {
851cb0ef41Sopenharmony_ci        pollfd[i].events |= POLLIN;
861cb0ef41Sopenharmony_ci      }
871cb0ef41Sopenharmony_ci      if (ev->flags & ARES_EVENT_FLAG_WRITE) {
881cb0ef41Sopenharmony_ci        pollfd[i].events |= POLLOUT;
891cb0ef41Sopenharmony_ci      }
901cb0ef41Sopenharmony_ci    }
911cb0ef41Sopenharmony_ci  }
921cb0ef41Sopenharmony_ci  ares_free(fdlist);
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci  rv = poll(pollfd, (nfds_t)num_fds, (timeout_ms == 0) ? -1 : (int)timeout_ms);
951cb0ef41Sopenharmony_ci  if (rv <= 0) {
961cb0ef41Sopenharmony_ci    goto done;
971cb0ef41Sopenharmony_ci  }
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci  for (i = 0; i < num_fds; i++) {
1001cb0ef41Sopenharmony_ci    ares_event_t      *ev;
1011cb0ef41Sopenharmony_ci    ares_event_flags_t flags = 0;
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci    if (pollfd[i].revents == 0) {
1041cb0ef41Sopenharmony_ci      continue;
1051cb0ef41Sopenharmony_ci    }
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci    cnt++;
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci    ev = ares__htable_asvp_get_direct(e->ev_handles, pollfd[i].fd);
1101cb0ef41Sopenharmony_ci    if (ev == NULL || ev->cb == NULL) {
1111cb0ef41Sopenharmony_ci      continue;
1121cb0ef41Sopenharmony_ci    }
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci    if (pollfd[i].revents & (POLLERR | POLLHUP | POLLIN)) {
1151cb0ef41Sopenharmony_ci      flags |= ARES_EVENT_FLAG_READ;
1161cb0ef41Sopenharmony_ci    }
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci    if (pollfd[i].revents & POLLOUT) {
1191cb0ef41Sopenharmony_ci      flags |= ARES_EVENT_FLAG_WRITE;
1201cb0ef41Sopenharmony_ci    }
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci    ev->cb(e, pollfd[i].fd, ev->data, flags);
1231cb0ef41Sopenharmony_ci  }
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_cidone:
1261cb0ef41Sopenharmony_ci  ares_free(pollfd);
1271cb0ef41Sopenharmony_ci  return cnt;
1281cb0ef41Sopenharmony_ci}
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ciconst ares_event_sys_t ares_evsys_poll = { "poll",
1311cb0ef41Sopenharmony_ci                                           ares_evsys_poll_init,
1321cb0ef41Sopenharmony_ci                                           ares_evsys_poll_destroy,   /* NoOp */
1331cb0ef41Sopenharmony_ci                                           ares_evsys_poll_event_add, /* NoOp */
1341cb0ef41Sopenharmony_ci                                           ares_evsys_poll_event_del, /* NoOp */
1351cb0ef41Sopenharmony_ci                                           ares_evsys_poll_event_mod, /* NoOp */
1361cb0ef41Sopenharmony_ci                                           ares_evsys_poll_wait };
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci#endif
139