378903a62706089e80fee300fa9a5273706845f2
[platform/upstream/nodejs.git] / deps / uv / src / unix / kqueue.c
1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2  * Permission is hereby granted, free of charge, to any person obtaining a copy
3  * of this software and associated documentation files (the "Software"), to
4  * deal in the Software without restriction, including without limitation the
5  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6  * sell copies of the Software, and to permit persons to whom the Software is
7  * furnished to do so, subject to the following conditions:
8  *
9  * The above copyright notice and this permission notice shall be included in
10  * all copies or substantial portions of the Software.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18  * IN THE SOFTWARE.
19  */
20
21 #include "uv.h"
22 #include "internal.h"
23
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28
29 #include <sys/sysctl.h>
30 #include <sys/types.h>
31 #include <sys/event.h>
32 #include <sys/time.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <time.h>
36
37 static void uv__fs_event(uv_loop_t* loop, uv__io_t* w, unsigned int fflags);
38
39
40 int uv__kqueue_init(uv_loop_t* loop) {
41   loop->backend_fd = kqueue();
42
43   if (loop->backend_fd == -1)
44     return -1;
45
46   uv__cloexec(loop->backend_fd, 1);
47
48   return 0;
49 }
50
51
52 void uv__io_poll(uv_loop_t* loop, int timeout) {
53   struct kevent events[1024];
54   struct kevent* ev;
55   struct timespec spec;
56   unsigned int nevents;
57   unsigned int revents;
58   ngx_queue_t* q;
59   uint64_t base;
60   uint64_t diff;
61   uv__io_t* w;
62   int filter;
63   int fflags;
64   int count;
65   int nfds;
66   int fd;
67   int op;
68   int i;
69
70   if (loop->nfds == 0) {
71     assert(ngx_queue_empty(&loop->watcher_queue));
72     return;
73   }
74
75   nevents = 0;
76
77   while (!ngx_queue_empty(&loop->watcher_queue)) {
78     q = ngx_queue_head(&loop->watcher_queue);
79     ngx_queue_remove(q);
80     ngx_queue_init(q);
81
82     w = ngx_queue_data(q, uv__io_t, watcher_queue);
83     assert(w->pevents != 0);
84     assert(w->fd >= 0);
85     assert(w->fd < (int) loop->nwatchers);
86
87     if ((w->events & UV__POLLIN) == 0 && (w->pevents & UV__POLLIN) != 0) {
88       filter = EVFILT_READ;
89       fflags = 0;
90       op = EV_ADD;
91
92       if (w->cb == uv__fs_event) {
93         filter = EVFILT_VNODE;
94         fflags = NOTE_ATTRIB | NOTE_WRITE  | NOTE_RENAME
95                | NOTE_DELETE | NOTE_EXTEND | NOTE_REVOKE;
96         op = EV_ADD | EV_ONESHOT; /* Stop the event from firing repeatedly. */
97       }
98
99       EV_SET(events + nevents, w->fd, filter, op, fflags, 0, 0);
100
101       if (++nevents == ARRAY_SIZE(events)) {
102         if (kevent(loop->backend_fd, events, nevents, NULL, 0, NULL))
103           abort();
104         nevents = 0;
105       }
106     }
107
108     if ((w->events & UV__POLLOUT) == 0 && (w->pevents & UV__POLLOUT) != 0) {
109       EV_SET(events + nevents, w->fd, EVFILT_WRITE, EV_ADD, 0, 0, 0);
110
111       if (++nevents == ARRAY_SIZE(events)) {
112         if (kevent(loop->backend_fd, events, nevents, NULL, 0, NULL))
113           abort();
114         nevents = 0;
115       }
116     }
117
118     w->events = w->pevents;
119   }
120
121   assert(timeout >= -1);
122   base = loop->time;
123   count = 48; /* Benchmarks suggest this gives the best throughput. */
124
125   for (;; nevents = 0) {
126     if (timeout != -1) {
127       spec.tv_sec = timeout / 1000;
128       spec.tv_nsec = (timeout % 1000) * 1000000;
129     }
130
131     nfds = kevent(loop->backend_fd,
132                   events,
133                   nevents,
134                   events,
135                   ARRAY_SIZE(events),
136                   timeout == -1 ? NULL : &spec);
137
138     /* Update loop->time unconditionally. It's tempting to skip the update when
139      * timeout == 0 (i.e. non-blocking poll) but there is no guarantee that the
140      * operating system didn't reschedule our process while in the syscall.
141      */
142     SAVE_ERRNO(uv__update_time(loop));
143
144     if (nfds == 0) {
145       assert(timeout != -1);
146       return;
147     }
148
149     if (nfds == -1) {
150       if (errno != EINTR)
151         abort();
152
153       if (timeout == 0)
154         return;
155
156       if (timeout == -1)
157         continue;
158
159       /* Interrupted by a signal. Update timeout and poll again. */
160       goto update_timeout;
161     }
162
163     nevents = 0;
164
165     for (i = 0; i < nfds; i++) {
166       ev = events + i;
167       fd = ev->ident;
168       w = loop->watchers[fd];
169
170       if (w == NULL) {
171         /* File descriptor that we've stopped watching, disarm it. */
172         /* TODO batch up */
173         struct kevent events[1];
174
175         EV_SET(events + 0, fd, ev->filter, EV_DELETE, 0, 0, 0);
176         if (kevent(loop->backend_fd, events, 1, NULL, 0, NULL))
177           if (errno != EBADF && errno != ENOENT)
178             abort();
179
180         continue;
181       }
182
183       if (ev->filter == EVFILT_VNODE) {
184         assert(w->events == UV__POLLIN);
185         assert(w->pevents == UV__POLLIN);
186         w->cb(loop, w, ev->fflags); /* XXX always uv__fs_event() */
187         nevents++;
188         continue;
189       }
190
191       revents = 0;
192
193       if (ev->filter == EVFILT_READ) {
194         if (w->events & UV__POLLIN) {
195           revents |= UV__POLLIN;
196           w->rcount = ev->data;
197         } else {
198           /* TODO batch up */
199           struct kevent events[1];
200           EV_SET(events + 0, fd, ev->filter, EV_DELETE, 0, 0, 0);
201           if (kevent(loop->backend_fd, events, 1, NULL, 0, NULL))
202             if (errno != ENOENT)
203               abort();
204         }
205       }
206
207       if (ev->filter == EVFILT_WRITE) {
208         if (w->events & UV__POLLOUT) {
209           revents |= UV__POLLOUT;
210           w->wcount = ev->data;
211         } else {
212           /* TODO batch up */
213           struct kevent events[1];
214           EV_SET(events + 0, fd, ev->filter, EV_DELETE, 0, 0, 0);
215           if (kevent(loop->backend_fd, events, 1, NULL, 0, NULL))
216             if (errno != ENOENT)
217               abort();
218         }
219       }
220
221       if (ev->flags & EV_ERROR)
222         revents |= UV__POLLERR;
223
224       if (revents == 0)
225         continue;
226
227       w->cb(loop, w, revents);
228       nevents++;
229     }
230
231     if (nevents != 0) {
232       if (nfds == ARRAY_SIZE(events) && --count != 0) {
233         /* Poll for more events but don't block this time. */
234         timeout = 0;
235         continue;
236       }
237       return;
238     }
239
240     if (timeout == 0)
241       return;
242
243     if (timeout == -1)
244       continue;
245
246 update_timeout:
247     assert(timeout > 0);
248
249     diff = loop->time - base;
250     if (diff >= (uint64_t) timeout)
251       return;
252
253     timeout -= diff;
254   }
255 }
256
257
258 static void uv__fs_event(uv_loop_t* loop, uv__io_t* w, unsigned int fflags) {
259   uv_fs_event_t* handle;
260   struct kevent ev;
261   int events;
262
263   handle = container_of(w, uv_fs_event_t, event_watcher);
264
265   if (fflags & (NOTE_ATTRIB | NOTE_EXTEND))
266     events = UV_CHANGE;
267   else
268     events = UV_RENAME;
269
270   handle->cb(handle, NULL, events, 0);
271
272   if (handle->event_watcher.fd == -1)
273     return;
274
275   /* Watcher operates in one-shot mode, re-arm it. */
276   fflags = NOTE_ATTRIB | NOTE_WRITE  | NOTE_RENAME
277          | NOTE_DELETE | NOTE_EXTEND | NOTE_REVOKE;
278
279   EV_SET(&ev, w->fd, EVFILT_VNODE, EV_ADD | EV_ONESHOT, fflags, 0, 0);
280
281   if (kevent(loop->backend_fd, &ev, 1, NULL, 0, NULL))
282     abort();
283 }
284
285
286 int uv_fs_event_init(uv_loop_t* loop,
287                      uv_fs_event_t* handle,
288                      const char* filename,
289                      uv_fs_event_cb cb,
290                      int flags) {
291 #if defined(__APPLE__)
292   struct stat statbuf;
293 #endif /* defined(__APPLE__) */
294   int fd;
295
296   /* TODO open asynchronously - but how do we report back errors? */
297   if ((fd = open(filename, O_RDONLY)) == -1) {
298     uv__set_sys_error(loop, errno);
299     return -1;
300   }
301
302   uv__handle_init(loop, (uv_handle_t*)handle, UV_FS_EVENT);
303   uv__handle_start(handle); /* FIXME shouldn't start automatically */
304   uv__io_init(&handle->event_watcher, uv__fs_event, fd);
305   handle->filename = strdup(filename);
306   handle->cb = cb;
307
308 #if defined(__APPLE__)
309   /* Nullify field to perform checks later */
310   handle->cf_eventstream = NULL;
311   handle->realpath = NULL;
312   handle->realpath_len = 0;
313   handle->cf_flags = flags;
314
315   if (fstat(fd, &statbuf))
316     goto fallback;
317   /* FSEvents works only with directories */
318   if (!(statbuf.st_mode & S_IFDIR))
319     goto fallback;
320
321   return uv__fsevents_init(handle);
322
323 fallback:
324 #endif /* defined(__APPLE__) */
325
326   uv__io_start(loop, &handle->event_watcher, UV__POLLIN);
327
328   return 0;
329 }
330
331
332 void uv__fs_event_close(uv_fs_event_t* handle) {
333 #if defined(__APPLE__)
334   if (uv__fsevents_close(handle))
335     uv__io_stop(handle->loop, &handle->event_watcher, UV__POLLIN);
336 #else
337   uv__io_stop(handle->loop, &handle->event_watcher, UV__POLLIN);
338 #endif /* defined(__APPLE__) */
339
340   uv__handle_stop(handle);
341
342   free(handle->filename);
343   handle->filename = NULL;
344
345   close(handle->event_watcher.fd);
346   handle->event_watcher.fd = -1;
347 }