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
311cb0ef41Sopenharmony_ci#ifdef HAVE_SYS_TYPES_H
321cb0ef41Sopenharmony_ci#  include <sys/types.h>
331cb0ef41Sopenharmony_ci#endif
341cb0ef41Sopenharmony_ci#ifdef HAVE_SYS_EVENT_H
351cb0ef41Sopenharmony_ci#  include <sys/event.h>
361cb0ef41Sopenharmony_ci#endif
371cb0ef41Sopenharmony_ci#ifdef HAVE_SYS_TIME_H
381cb0ef41Sopenharmony_ci#  include <sys/time.h>
391cb0ef41Sopenharmony_ci#endif
401cb0ef41Sopenharmony_ci#ifdef HAVE_FCNTL_H
411cb0ef41Sopenharmony_ci#  include <fcntl.h>
421cb0ef41Sopenharmony_ci#endif
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci#ifdef HAVE_KQUEUE
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_citypedef struct {
471cb0ef41Sopenharmony_ci  int            kqueue_fd;
481cb0ef41Sopenharmony_ci  struct kevent *changelist;
491cb0ef41Sopenharmony_ci  size_t         nchanges;
501cb0ef41Sopenharmony_ci  size_t         nchanges_alloc;
511cb0ef41Sopenharmony_ci} ares_evsys_kqueue_t;
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_cistatic void ares_evsys_kqueue_destroy(ares_event_thread_t *e)
541cb0ef41Sopenharmony_ci{
551cb0ef41Sopenharmony_ci  ares_evsys_kqueue_t *kq = NULL;
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  if (e == NULL) {
581cb0ef41Sopenharmony_ci    return;
591cb0ef41Sopenharmony_ci  }
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  kq = e->ev_sys_data;
621cb0ef41Sopenharmony_ci  if (kq == NULL) {
631cb0ef41Sopenharmony_ci    return;
641cb0ef41Sopenharmony_ci  }
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  if (kq->kqueue_fd != -1) {
671cb0ef41Sopenharmony_ci    close(kq->kqueue_fd);
681cb0ef41Sopenharmony_ci  }
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  ares_free(kq->changelist);
711cb0ef41Sopenharmony_ci  ares_free(kq);
721cb0ef41Sopenharmony_ci  e->ev_sys_data = NULL;
731cb0ef41Sopenharmony_ci}
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_cistatic ares_bool_t ares_evsys_kqueue_init(ares_event_thread_t *e)
761cb0ef41Sopenharmony_ci{
771cb0ef41Sopenharmony_ci  ares_evsys_kqueue_t *kq = NULL;
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci  kq = ares_malloc_zero(sizeof(*kq));
801cb0ef41Sopenharmony_ci  if (kq == NULL) {
811cb0ef41Sopenharmony_ci    return ARES_FALSE;
821cb0ef41Sopenharmony_ci  }
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci  e->ev_sys_data = kq;
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci  kq->kqueue_fd = kqueue();
871cb0ef41Sopenharmony_ci  if (kq->kqueue_fd == -1) {
881cb0ef41Sopenharmony_ci    ares_evsys_kqueue_destroy(e);
891cb0ef41Sopenharmony_ci    return ARES_FALSE;
901cb0ef41Sopenharmony_ci  }
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci#  ifdef FD_CLOEXEC
931cb0ef41Sopenharmony_ci  fcntl(kq->kqueue_fd, F_SETFD, FD_CLOEXEC);
941cb0ef41Sopenharmony_ci#  endif
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci  kq->nchanges_alloc = 8;
971cb0ef41Sopenharmony_ci  kq->changelist =
981cb0ef41Sopenharmony_ci    ares_malloc_zero(sizeof(*kq->changelist) * kq->nchanges_alloc);
991cb0ef41Sopenharmony_ci  if (kq->changelist == NULL) {
1001cb0ef41Sopenharmony_ci    ares_evsys_kqueue_destroy(e);
1011cb0ef41Sopenharmony_ci    return ARES_FALSE;
1021cb0ef41Sopenharmony_ci  }
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci  e->ev_signal = ares_pipeevent_create(e);
1051cb0ef41Sopenharmony_ci  if (e->ev_signal == NULL) {
1061cb0ef41Sopenharmony_ci    ares_evsys_kqueue_destroy(e);
1071cb0ef41Sopenharmony_ci    return ARES_FALSE;
1081cb0ef41Sopenharmony_ci  }
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci  return ARES_TRUE;
1111cb0ef41Sopenharmony_ci}
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_cistatic void ares_evsys_kqueue_enqueue(ares_evsys_kqueue_t *kq, int fd,
1141cb0ef41Sopenharmony_ci                                      int16_t filter, uint16_t flags)
1151cb0ef41Sopenharmony_ci{
1161cb0ef41Sopenharmony_ci  size_t idx;
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci  if (kq == NULL) {
1191cb0ef41Sopenharmony_ci    return;
1201cb0ef41Sopenharmony_ci  }
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci  idx = kq->nchanges;
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci  kq->nchanges++;
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ci  if (kq->nchanges > kq->nchanges_alloc) {
1271cb0ef41Sopenharmony_ci    kq->nchanges_alloc <<= 1;
1281cb0ef41Sopenharmony_ci    kq->changelist = ares_realloc_zero(kq->changelist, kq->nchanges_alloc >> 1,
1291cb0ef41Sopenharmony_ci                                       kq->nchanges_alloc);
1301cb0ef41Sopenharmony_ci  }
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci  EV_SET(&kq->changelist[idx], fd, filter, flags, 0, 0, 0);
1331cb0ef41Sopenharmony_ci}
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_cistatic void ares_evsys_kqueue_event_process(ares_event_t      *event,
1361cb0ef41Sopenharmony_ci                                            ares_event_flags_t old_flags,
1371cb0ef41Sopenharmony_ci                                            ares_event_flags_t new_flags)
1381cb0ef41Sopenharmony_ci{
1391cb0ef41Sopenharmony_ci  ares_event_thread_t *e = event->e;
1401cb0ef41Sopenharmony_ci  ares_evsys_kqueue_t *kq;
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci  if (e == NULL) {
1431cb0ef41Sopenharmony_ci    return;
1441cb0ef41Sopenharmony_ci  }
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_ci  kq = e->ev_sys_data;
1471cb0ef41Sopenharmony_ci  if (kq == NULL) {
1481cb0ef41Sopenharmony_ci    return;
1491cb0ef41Sopenharmony_ci  }
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ci  if (new_flags & ARES_EVENT_FLAG_READ && !(old_flags & ARES_EVENT_FLAG_READ)) {
1521cb0ef41Sopenharmony_ci    ares_evsys_kqueue_enqueue(kq, event->fd, EVFILT_READ, EV_ADD | EV_ENABLE);
1531cb0ef41Sopenharmony_ci  }
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci  if (!(new_flags & ARES_EVENT_FLAG_READ) && old_flags & ARES_EVENT_FLAG_READ) {
1561cb0ef41Sopenharmony_ci    ares_evsys_kqueue_enqueue(kq, event->fd, EVFILT_READ, EV_DELETE);
1571cb0ef41Sopenharmony_ci  }
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci  if (new_flags & ARES_EVENT_FLAG_WRITE &&
1601cb0ef41Sopenharmony_ci      !(old_flags & ARES_EVENT_FLAG_WRITE)) {
1611cb0ef41Sopenharmony_ci    ares_evsys_kqueue_enqueue(kq, event->fd, EVFILT_WRITE, EV_ADD | EV_ENABLE);
1621cb0ef41Sopenharmony_ci  }
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ci  if (!(new_flags & ARES_EVENT_FLAG_WRITE) &&
1651cb0ef41Sopenharmony_ci      old_flags & ARES_EVENT_FLAG_WRITE) {
1661cb0ef41Sopenharmony_ci    ares_evsys_kqueue_enqueue(kq, event->fd, EVFILT_WRITE, EV_DELETE);
1671cb0ef41Sopenharmony_ci  }
1681cb0ef41Sopenharmony_ci}
1691cb0ef41Sopenharmony_ci
1701cb0ef41Sopenharmony_cistatic ares_bool_t ares_evsys_kqueue_event_add(ares_event_t *event)
1711cb0ef41Sopenharmony_ci{
1721cb0ef41Sopenharmony_ci  ares_evsys_kqueue_event_process(event, 0, event->flags);
1731cb0ef41Sopenharmony_ci  return ARES_TRUE;
1741cb0ef41Sopenharmony_ci}
1751cb0ef41Sopenharmony_ci
1761cb0ef41Sopenharmony_cistatic void ares_evsys_kqueue_event_del(ares_event_t *event)
1771cb0ef41Sopenharmony_ci{
1781cb0ef41Sopenharmony_ci  ares_evsys_kqueue_event_process(event, event->flags, 0);
1791cb0ef41Sopenharmony_ci}
1801cb0ef41Sopenharmony_ci
1811cb0ef41Sopenharmony_cistatic void ares_evsys_kqueue_event_mod(ares_event_t      *event,
1821cb0ef41Sopenharmony_ci                                        ares_event_flags_t new_flags)
1831cb0ef41Sopenharmony_ci{
1841cb0ef41Sopenharmony_ci  ares_evsys_kqueue_event_process(event, event->flags, new_flags);
1851cb0ef41Sopenharmony_ci}
1861cb0ef41Sopenharmony_ci
1871cb0ef41Sopenharmony_cistatic size_t ares_evsys_kqueue_wait(ares_event_thread_t *e,
1881cb0ef41Sopenharmony_ci                                     unsigned long        timeout_ms)
1891cb0ef41Sopenharmony_ci{
1901cb0ef41Sopenharmony_ci  struct kevent        events[8];
1911cb0ef41Sopenharmony_ci  size_t               nevents = sizeof(events) / sizeof(*events);
1921cb0ef41Sopenharmony_ci  ares_evsys_kqueue_t *kq      = e->ev_sys_data;
1931cb0ef41Sopenharmony_ci  int                  rv;
1941cb0ef41Sopenharmony_ci  size_t               i;
1951cb0ef41Sopenharmony_ci  struct timespec      ts;
1961cb0ef41Sopenharmony_ci  struct timespec     *timeout = NULL;
1971cb0ef41Sopenharmony_ci  size_t               cnt     = 0;
1981cb0ef41Sopenharmony_ci
1991cb0ef41Sopenharmony_ci  if (timeout_ms != 0) {
2001cb0ef41Sopenharmony_ci    ts.tv_sec  = timeout_ms / 1000;
2011cb0ef41Sopenharmony_ci    ts.tv_nsec = (timeout_ms % 1000) * 1000 * 1000;
2021cb0ef41Sopenharmony_ci    timeout    = &ts;
2031cb0ef41Sopenharmony_ci  }
2041cb0ef41Sopenharmony_ci
2051cb0ef41Sopenharmony_ci  memset(events, 0, sizeof(events));
2061cb0ef41Sopenharmony_ci
2071cb0ef41Sopenharmony_ci  rv = kevent(kq->kqueue_fd, kq->changelist, (int)kq->nchanges, events,
2081cb0ef41Sopenharmony_ci              (int)nevents, timeout);
2091cb0ef41Sopenharmony_ci  if (rv < 0) {
2101cb0ef41Sopenharmony_ci    return 0;
2111cb0ef41Sopenharmony_ci  }
2121cb0ef41Sopenharmony_ci
2131cb0ef41Sopenharmony_ci  /* Changelist was consumed */
2141cb0ef41Sopenharmony_ci  kq->nchanges = 0;
2151cb0ef41Sopenharmony_ci  nevents      = (size_t)rv;
2161cb0ef41Sopenharmony_ci
2171cb0ef41Sopenharmony_ci  for (i = 0; i < nevents; i++) {
2181cb0ef41Sopenharmony_ci    ares_event_t      *ev;
2191cb0ef41Sopenharmony_ci    ares_event_flags_t flags = 0;
2201cb0ef41Sopenharmony_ci
2211cb0ef41Sopenharmony_ci    ev = ares__htable_asvp_get_direct(e->ev_handles,
2221cb0ef41Sopenharmony_ci                                      (ares_socket_t)events[i].ident);
2231cb0ef41Sopenharmony_ci    if (ev == NULL || ev->cb == NULL) {
2241cb0ef41Sopenharmony_ci      continue;
2251cb0ef41Sopenharmony_ci    }
2261cb0ef41Sopenharmony_ci
2271cb0ef41Sopenharmony_ci    cnt++;
2281cb0ef41Sopenharmony_ci
2291cb0ef41Sopenharmony_ci    if (events[i].filter == EVFILT_READ ||
2301cb0ef41Sopenharmony_ci        events[i].flags & (EV_EOF | EV_ERROR)) {
2311cb0ef41Sopenharmony_ci      flags |= ARES_EVENT_FLAG_READ;
2321cb0ef41Sopenharmony_ci    } else {
2331cb0ef41Sopenharmony_ci      flags |= ARES_EVENT_FLAG_WRITE;
2341cb0ef41Sopenharmony_ci    }
2351cb0ef41Sopenharmony_ci
2361cb0ef41Sopenharmony_ci    ev->cb(e, ev->fd, ev->data, flags);
2371cb0ef41Sopenharmony_ci  }
2381cb0ef41Sopenharmony_ci
2391cb0ef41Sopenharmony_ci  return cnt;
2401cb0ef41Sopenharmony_ci}
2411cb0ef41Sopenharmony_ci
2421cb0ef41Sopenharmony_ciconst ares_event_sys_t ares_evsys_kqueue = { "kqueue",
2431cb0ef41Sopenharmony_ci                                             ares_evsys_kqueue_init,
2441cb0ef41Sopenharmony_ci                                             ares_evsys_kqueue_destroy,
2451cb0ef41Sopenharmony_ci                                             ares_evsys_kqueue_event_add,
2461cb0ef41Sopenharmony_ci                                             ares_evsys_kqueue_event_del,
2471cb0ef41Sopenharmony_ci                                             ares_evsys_kqueue_event_mod,
2481cb0ef41Sopenharmony_ci                                             ares_evsys_kqueue_wait };
2491cb0ef41Sopenharmony_ci#endif
250