1 /* Copyright libuv project contributors. All rights reserved.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21 
22 #include "uv.h"
23 #include "internal.h"
24 
25 /* POSIX defines poll() as a portable way to wait on file descriptors.
26  * Here we maintain a dynamically sized array of file descriptors and
27  * events to pass as the first argument to poll().
28  */
29 
30 #include <assert.h>
31 #include <stddef.h>
32 #include <stdint.h>
33 #include <errno.h>
34 #include <unistd.h>
35 
uv__platform_loop_init(uv_loop_t * loop)36 int uv__platform_loop_init(uv_loop_t* loop) {
37   loop->poll_fds = NULL;
38   loop->poll_fds_used = 0;
39   loop->poll_fds_size = 0;
40   loop->poll_fds_iterating = 0;
41   return 0;
42 }
43 
uv__platform_loop_delete(uv_loop_t * loop)44 void uv__platform_loop_delete(uv_loop_t* loop) {
45   uv__free(loop->poll_fds);
46   loop->poll_fds = NULL;
47 }
48 
uv__io_fork(uv_loop_t * loop)49 int uv__io_fork(uv_loop_t* loop) {
50   uv__platform_loop_delete(loop);
51   return uv__platform_loop_init(loop);
52 }
53 
54 /* Allocate or dynamically resize our poll fds array.  */
uv__pollfds_maybe_resize(uv_loop_t * loop)55 static void uv__pollfds_maybe_resize(uv_loop_t* loop) {
56   size_t i;
57   size_t n;
58   struct pollfd* p;
59 
60   if (loop->poll_fds_used < loop->poll_fds_size)
61     return;
62 
63   n = loop->poll_fds_size ? loop->poll_fds_size * 2 : 64;
64   p = uv__realloc(loop->poll_fds, n * sizeof(*loop->poll_fds));
65   if (p == NULL)
66     abort();
67 
68   loop->poll_fds = p;
69   for (i = loop->poll_fds_size; i < n; i++) {
70     loop->poll_fds[i].fd = -1;
71     loop->poll_fds[i].events = 0;
72     loop->poll_fds[i].revents = 0;
73   }
74   loop->poll_fds_size = n;
75 }
76 
77 /* Primitive swap operation on poll fds array elements.  */
uv__pollfds_swap(uv_loop_t * loop,size_t l,size_t r)78 static void uv__pollfds_swap(uv_loop_t* loop, size_t l, size_t r) {
79   struct pollfd pfd;
80   pfd = loop->poll_fds[l];
81   loop->poll_fds[l] = loop->poll_fds[r];
82   loop->poll_fds[r] = pfd;
83 }
84 
85 /* Add a watcher's fd to our poll fds array with its pending events.  */
uv__pollfds_add(uv_loop_t * loop,uv__io_t * w)86 static void uv__pollfds_add(uv_loop_t* loop, uv__io_t* w) {
87   size_t i;
88   struct pollfd* pe;
89 
90   /* If the fd is already in the set just update its events.  */
91   assert(!loop->poll_fds_iterating);
92   for (i = 0; i < loop->poll_fds_used; ++i) {
93     if (loop->poll_fds[i].fd == w->fd) {
94       loop->poll_fds[i].events = w->pevents;
95       return;
96     }
97   }
98 
99   /* Otherwise, allocate a new slot in the set for the fd.  */
100   uv__pollfds_maybe_resize(loop);
101   pe = &loop->poll_fds[loop->poll_fds_used++];
102   pe->fd = w->fd;
103   pe->events = w->pevents;
104 }
105 
106 /* Remove a watcher's fd from our poll fds array.  */
uv__pollfds_del(uv_loop_t * loop,int fd)107 static void uv__pollfds_del(uv_loop_t* loop, int fd) {
108   size_t i;
109   assert(!loop->poll_fds_iterating);
110   for (i = 0; i < loop->poll_fds_used; ++i) {
111     if (loop->poll_fds[i].fd == fd) {
112       /* swap to last position and remove */
113       --loop->poll_fds_used;
114       uv__pollfds_swap(loop, i, loop->poll_fds_used);
115       loop->poll_fds[loop->poll_fds_used].fd = -1;
116       loop->poll_fds[loop->poll_fds_used].events = 0;
117       loop->poll_fds[loop->poll_fds_used].revents = 0;
118       return;
119     }
120   }
121 }
122 
123 
uv__io_poll(uv_loop_t * loop,int timeout)124 void uv__io_poll(uv_loop_t* loop, int timeout) {
125   sigset_t* pset;
126   sigset_t set;
127   uint64_t time_base;
128   uint64_t time_diff;
129   QUEUE* q;
130   uv__io_t* w;
131   size_t i;
132   unsigned int nevents;
133   int nfds;
134   int have_signals;
135   struct pollfd* pe;
136   int fd;
137 
138   if (loop->nfds == 0) {
139     assert(QUEUE_EMPTY(&loop->watcher_queue));
140     return;
141   }
142 
143   /* Take queued watchers and add their fds to our poll fds array.  */
144   while (!QUEUE_EMPTY(&loop->watcher_queue)) {
145     q = QUEUE_HEAD(&loop->watcher_queue);
146     QUEUE_REMOVE(q);
147     QUEUE_INIT(q);
148 
149     w = QUEUE_DATA(q, uv__io_t, watcher_queue);
150     assert(w->pevents != 0);
151     assert(w->fd >= 0);
152     assert(w->fd < (int) loop->nwatchers);
153 
154     uv__pollfds_add(loop, w);
155 
156     w->events = w->pevents;
157   }
158 
159   /* Prepare a set of signals to block around poll(), if any.  */
160   pset = NULL;
161   if (loop->flags & UV_LOOP_BLOCK_SIGPROF) {
162     pset = &set;
163     sigemptyset(pset);
164     sigaddset(pset, SIGPROF);
165   }
166 
167   assert(timeout >= -1);
168   time_base = loop->time;
169 
170   /* Loop calls to poll() and processing of results.  If we get some
171    * results from poll() but they turn out not to be interesting to
172    * our caller then we need to loop around and poll() again.
173    */
174   for (;;) {
175     if (pset != NULL)
176       if (pthread_sigmask(SIG_BLOCK, pset, NULL))
177         abort();
178     nfds = poll(loop->poll_fds, (nfds_t)loop->poll_fds_used, timeout);
179     if (pset != NULL)
180       if (pthread_sigmask(SIG_UNBLOCK, pset, NULL))
181         abort();
182 
183     /* Update loop->time unconditionally. It's tempting to skip the update when
184      * timeout == 0 (i.e. non-blocking poll) but there is no guarantee that the
185      * operating system didn't reschedule our process while in the syscall.
186      */
187     SAVE_ERRNO(uv__update_time(loop));
188 
189     if (nfds == 0) {
190       assert(timeout != -1);
191       return;
192     }
193 
194     if (nfds == -1) {
195       if (errno != EINTR)
196         abort();
197 
198       if (timeout == -1)
199         continue;
200 
201       if (timeout == 0)
202         return;
203 
204       /* Interrupted by a signal. Update timeout and poll again. */
205       goto update_timeout;
206     }
207 
208     /* Tell uv__platform_invalidate_fd not to manipulate our array
209      * while we are iterating over it.
210      */
211     loop->poll_fds_iterating = 1;
212 
213     /* Initialize a count of events that we care about.  */
214     nevents = 0;
215     have_signals = 0;
216 
217     /* Loop over the entire poll fds array looking for returned events.  */
218     for (i = 0; i < loop->poll_fds_used; i++) {
219       pe = loop->poll_fds + i;
220       fd = pe->fd;
221 
222       /* Skip invalidated events, see uv__platform_invalidate_fd.  */
223       if (fd == -1)
224         continue;
225 
226       assert(fd >= 0);
227       assert((unsigned) fd < loop->nwatchers);
228 
229       w = loop->watchers[fd];
230 
231       if (w == NULL) {
232         /* File descriptor that we've stopped watching, ignore.  */
233         uv__platform_invalidate_fd(loop, fd);
234         continue;
235       }
236 
237       /* Filter out events that user has not requested us to watch
238        * (e.g. POLLNVAL).
239        */
240       pe->revents &= w->pevents | POLLERR | POLLHUP;
241 
242       if (pe->revents != 0) {
243         /* Run signal watchers last.  */
244         if (w == &loop->signal_io_watcher) {
245           have_signals = 1;
246         } else {
247           w->cb(loop, w, pe->revents);
248         }
249 
250         nevents++;
251       }
252     }
253 
254     if (have_signals != 0)
255       loop->signal_io_watcher.cb(loop, &loop->signal_io_watcher, POLLIN);
256 
257     loop->poll_fds_iterating = 0;
258 
259     /* Purge invalidated fds from our poll fds array.  */
260     uv__pollfds_del(loop, -1);
261 
262     if (have_signals != 0)
263       return;  /* Event loop should cycle now so don't poll again. */
264 
265     if (nevents != 0)
266       return;
267 
268     if (timeout == 0)
269       return;
270 
271     if (timeout == -1)
272       continue;
273 
274 update_timeout:
275     assert(timeout > 0);
276 
277     time_diff = loop->time - time_base;
278     if (time_diff >= (uint64_t) timeout)
279       return;
280 
281     timeout -= time_diff;
282   }
283 }
284 
285 /* Remove the given fd from our poll fds array because no one
286  * is interested in its events anymore.
287  */
uv__platform_invalidate_fd(uv_loop_t * loop,int fd)288 void uv__platform_invalidate_fd(uv_loop_t* loop, int fd) {
289   size_t i;
290 
291   if (loop->poll_fds_iterating) {
292     /* uv__io_poll is currently iterating.  Just invalidate fd.  */
293     for (i = 0; i < loop->poll_fds_used; i++)
294       if (loop->poll_fds[i].fd == fd) {
295         loop->poll_fds[i].fd = -1;
296         loop->poll_fds[i].events = 0;
297         loop->poll_fds[i].revents = 0;
298       }
299   } else {
300     /* uv__io_poll is not iterating.  Delete fd from the set.  */
301     uv__pollfds_del(loop, fd);
302   }
303 }
304 
305 /* Check whether the given fd is supported by poll().  */
uv__io_check_fd(uv_loop_t * loop,int fd)306 int uv__io_check_fd(uv_loop_t* loop, int fd) {
307   struct pollfd p[1];
308   int rv;
309 
310   p[0].fd = fd;
311   p[0].events = POLLIN;
312 
313   do
314     rv = poll(p, 1, 0);
315   while (rv == -1 && (errno == EINTR || errno == EAGAIN));
316 
317   if (rv == -1)
318     return UV__ERR(errno);
319 
320   if (p[0].revents & POLLNVAL)
321     return UV_EINVAL;
322 
323   return 0;
324 }
325