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_setup.h"
27 #include "ares.h"
28 #include "ares_private.h"
29 #include "ares_event.h"
30 #ifdef HAVE_POLL_H
31 #  include <poll.h>
32 #endif
33 
34 #if defined(HAVE_POLL)
35 
ares_evsys_poll_init(ares_event_thread_t *e)36 static ares_bool_t ares_evsys_poll_init(ares_event_thread_t *e)
37 {
38   e->ev_signal = ares_pipeevent_create(e);
39   if (e->ev_signal == NULL) {
40     return ARES_FALSE;
41   }
42   return ARES_TRUE;
43 }
44 
ares_evsys_poll_destroy(ares_event_thread_t *e)45 static void ares_evsys_poll_destroy(ares_event_thread_t *e)
46 {
47   (void)e;
48 }
49 
ares_evsys_poll_event_add(ares_event_t *event)50 static ares_bool_t ares_evsys_poll_event_add(ares_event_t *event)
51 {
52   (void)event;
53   return ARES_TRUE;
54 }
55 
ares_evsys_poll_event_del(ares_event_t *event)56 static void ares_evsys_poll_event_del(ares_event_t *event)
57 {
58   (void)event;
59 }
60 
ares_evsys_poll_event_mod(ares_event_t *event, ares_event_flags_t new_flags)61 static void ares_evsys_poll_event_mod(ares_event_t      *event,
62                                       ares_event_flags_t new_flags)
63 {
64   (void)event;
65   (void)new_flags;
66 }
67 
ares_evsys_poll_wait(ares_event_thread_t *e, unsigned long timeout_ms)68 static size_t ares_evsys_poll_wait(ares_event_thread_t *e,
69                                    unsigned long        timeout_ms)
70 {
71   size_t         num_fds = 0;
72   ares_socket_t *fdlist  = ares__htable_asvp_keys(e->ev_handles, &num_fds);
73   struct pollfd *pollfd  = NULL;
74   int            rv;
75   size_t         cnt = 0;
76   size_t         i;
77 
78   if (num_fds) {
79     pollfd = ares_malloc_zero(sizeof(*pollfd) * num_fds);
80     for (i = 0; i < num_fds; i++) {
81       const ares_event_t *ev =
82         ares__htable_asvp_get_direct(e->ev_handles, fdlist[i]);
83       pollfd[i].fd = ev->fd;
84       if (ev->flags & ARES_EVENT_FLAG_READ) {
85         pollfd[i].events |= POLLIN;
86       }
87       if (ev->flags & ARES_EVENT_FLAG_WRITE) {
88         pollfd[i].events |= POLLOUT;
89       }
90     }
91   }
92   ares_free(fdlist);
93 
94   rv = poll(pollfd, (nfds_t)num_fds, (timeout_ms == 0) ? -1 : (int)timeout_ms);
95   if (rv <= 0) {
96     goto done;
97   }
98 
99   for (i = 0; i < num_fds; i++) {
100     ares_event_t      *ev;
101     ares_event_flags_t flags = 0;
102 
103     if (pollfd[i].revents == 0) {
104       continue;
105     }
106 
107     cnt++;
108 
109     ev = ares__htable_asvp_get_direct(e->ev_handles, pollfd[i].fd);
110     if (ev == NULL || ev->cb == NULL) {
111       continue;
112     }
113 
114     if (pollfd[i].revents & (POLLERR | POLLHUP | POLLIN)) {
115       flags |= ARES_EVENT_FLAG_READ;
116     }
117 
118     if (pollfd[i].revents & POLLOUT) {
119       flags |= ARES_EVENT_FLAG_WRITE;
120     }
121 
122     ev->cb(e, pollfd[i].fd, ev->data, flags);
123   }
124 
125 done:
126   ares_free(pollfd);
127   return cnt;
128 }
129 
130 const ares_event_sys_t ares_evsys_poll = { "poll",
131                                            ares_evsys_poll_init,
132                                            ares_evsys_poll_destroy,   /* NoOp */
133                                            ares_evsys_poll_event_add, /* NoOp */
134                                            ares_evsys_poll_event_del, /* NoOp */
135                                            ares_evsys_poll_event_mod, /* NoOp */
136                                            ares_evsys_poll_wait };
137 
138 #endif
139