1 /* MIT License
2 *
3 * Copyright (c) 2024 Brad House
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * SPDX-License-Identifier: MIT
25 */
26 #include "ares_private.h"
27 #include "ares_event.h"
28
29 #if defined(HAVE_POLL) && defined(CARES_THREADS)
30
31 #ifdef HAVE_POLL_H
32 # include <poll.h>
33 #endif
34
ares_evsys_poll_init(ares_event_thread_t * e)35 static ares_bool_t ares_evsys_poll_init(ares_event_thread_t *e)
36 {
37 e->ev_signal = ares_pipeevent_create(e);
38 if (e->ev_signal == NULL) {
39 return ARES_FALSE; /* LCOV_EXCL_LINE: UntestablePath */
40 }
41 return ARES_TRUE;
42 }
43
ares_evsys_poll_destroy(ares_event_thread_t * e)44 static void ares_evsys_poll_destroy(ares_event_thread_t *e)
45 {
46 (void)e;
47 }
48
ares_evsys_poll_event_add(ares_event_t * event)49 static ares_bool_t ares_evsys_poll_event_add(ares_event_t *event)
50 {
51 (void)event;
52 return ARES_TRUE;
53 }
54
ares_evsys_poll_event_del(ares_event_t * event)55 static void ares_evsys_poll_event_del(ares_event_t *event)
56 {
57 (void)event;
58 }
59
ares_evsys_poll_event_mod(ares_event_t * event,ares_event_flags_t new_flags)60 static void ares_evsys_poll_event_mod(ares_event_t *event,
61 ares_event_flags_t new_flags)
62 {
63 (void)event;
64 (void)new_flags;
65 }
66
ares_evsys_poll_wait(ares_event_thread_t * e,unsigned long timeout_ms)67 static size_t ares_evsys_poll_wait(ares_event_thread_t *e,
68 unsigned long timeout_ms)
69 {
70 size_t num_fds = 0;
71 ares_socket_t *fdlist = ares_htable_asvp_keys(e->ev_sock_handles, &num_fds);
72 struct pollfd *pollfd = NULL;
73 int rv;
74 size_t cnt = 0;
75 size_t i;
76
77 if (fdlist != NULL && num_fds) {
78 pollfd = ares_malloc_zero(sizeof(*pollfd) * num_fds);
79 if (pollfd == NULL) {
80 goto done; /* LCOV_EXCL_LINE: OutOfMemory */
81 }
82 for (i = 0; i < num_fds; i++) {
83 const ares_event_t *ev =
84 ares_htable_asvp_get_direct(e->ev_sock_handles, fdlist[i]);
85 pollfd[i].fd = ev->fd;
86 if (ev->flags & ARES_EVENT_FLAG_READ) {
87 pollfd[i].events |= POLLIN;
88 }
89 if (ev->flags & ARES_EVENT_FLAG_WRITE) {
90 pollfd[i].events |= POLLOUT;
91 }
92 }
93 }
94 ares_free(fdlist);
95
96 rv = poll(pollfd, (nfds_t)num_fds, (timeout_ms == 0) ? -1 : (int)timeout_ms);
97 if (rv <= 0) {
98 goto done;
99 }
100
101 for (i = 0; pollfd != NULL && i < num_fds; i++) {
102 ares_event_t *ev;
103 ares_event_flags_t flags = 0;
104
105 if (pollfd[i].revents == 0) {
106 continue;
107 }
108
109 cnt++;
110
111 ev = ares_htable_asvp_get_direct(e->ev_sock_handles, pollfd[i].fd);
112 if (ev == NULL || ev->cb == NULL) {
113 continue; /* LCOV_EXCL_LINE: DefensiveCoding */
114 }
115
116 if (pollfd[i].revents & (POLLERR | POLLHUP | POLLIN)) {
117 flags |= ARES_EVENT_FLAG_READ;
118 }
119
120 if (pollfd[i].revents & POLLOUT) {
121 flags |= ARES_EVENT_FLAG_WRITE;
122 }
123
124 ev->cb(e, pollfd[i].fd, ev->data, flags);
125 }
126
127 done:
128 ares_free(pollfd);
129 return cnt;
130 }
131
132 const ares_event_sys_t ares_evsys_poll = { "poll",
133 ares_evsys_poll_init,
134 ares_evsys_poll_destroy, /* NoOp */
135 ares_evsys_poll_event_add, /* NoOp */
136 ares_evsys_poll_event_del, /* NoOp */
137 ares_evsys_poll_event_mod, /* NoOp */
138 ares_evsys_poll_wait };
139
140 #endif
141