Mercurial > dovecot > core-2.2
annotate src/lib/ioloop-notify-kqueue.c @ 23007:36e01285b5b8
lib: buffer - Improve header comment for buffer_insert() and buffer_delete().
author | Stephan Bosch <stephan.bosch@dovecot.fi> |
---|---|
date | Mon, 18 Mar 2019 00:52:37 +0100 |
parents | 98652f62dbf5 |
children |
rev | line source |
---|---|
3782
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1 /* |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
2 * BSD kqueue() based ioloop notify handler. |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
3 * |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
4 * Copyright (c) 2005 Vaclav Haisman <v.haisman@sh.cvut.cz> |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
5 */ |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
6 |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
7 #define _GNU_SOURCE |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
8 #include "lib.h" |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
9 |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
10 #ifdef IOLOOP_NOTIFY_KQUEUE |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
11 |
13529
cf77e448295c
Renamed lib/*-internal.h files to lib/*-private.h for consistency.
Timo Sirainen <tss@iki.fi>
parents:
13252
diff
changeset
|
12 #include "ioloop-private.h" |
18985
818c1a77c025
lib: Log notify IO leaks when ioloop is destroyed.
Timo Sirainen <tss@iki.fi>
parents:
18984
diff
changeset
|
13 #include "llist.h" |
3958
3e9b43d0cd80
kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
3782
diff
changeset
|
14 #include "fd-close-on-exec.h" |
3782
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
15 #include <unistd.h> |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
16 #include <fcntl.h> |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
17 #include <sys/types.h> |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
18 #include <sys/event.h> |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
19 #include <sys/time.h> |
3958
3e9b43d0cd80
kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
3782
diff
changeset
|
20 #include <sys/stat.h> |
3782
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
21 |
4737
fd0d7e9e0e72
Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents:
4590
diff
changeset
|
22 /* kevent.udata's type just has to be different in NetBSD than in |
fd0d7e9e0e72
Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents:
4590
diff
changeset
|
23 FreeBSD and OpenBSD.. */ |
fd0d7e9e0e72
Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents:
4590
diff
changeset
|
24 #ifdef __NetBSD__ |
fd0d7e9e0e72
Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents:
4590
diff
changeset
|
25 # define MY_EV_SET(a, b, c, d, e, f, g) \ |
fd0d7e9e0e72
Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents:
4590
diff
changeset
|
26 EV_SET(a, b, c, d, e, f, (intptr_t)g) |
fd0d7e9e0e72
Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents:
4590
diff
changeset
|
27 #else |
fd0d7e9e0e72
Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents:
4590
diff
changeset
|
28 # define MY_EV_SET(a, b, c, d, e, f, g) \ |
fd0d7e9e0e72
Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents:
4590
diff
changeset
|
29 EV_SET(a, b, c, d, e, f, g) |
fd0d7e9e0e72
Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents:
4590
diff
changeset
|
30 #endif |
fd0d7e9e0e72
Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents:
4590
diff
changeset
|
31 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
32 struct io_notify { |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
33 struct io io; |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
34 int refcount; |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
35 int fd; |
18985
818c1a77c025
lib: Log notify IO leaks when ioloop is destroyed.
Timo Sirainen <tss@iki.fi>
parents:
18984
diff
changeset
|
36 struct io_notify *prev, *next; |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
37 }; |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
38 |
3782
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
39 struct ioloop_notify_handler_context { |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
40 int kq; |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
41 struct io *event_io; |
18985
818c1a77c025
lib: Log notify IO leaks when ioloop is destroyed.
Timo Sirainen <tss@iki.fi>
parents:
18984
diff
changeset
|
42 struct io_notify *notifies; |
3782
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
43 }; |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
44 |
18985
818c1a77c025
lib: Log notify IO leaks when ioloop is destroyed.
Timo Sirainen <tss@iki.fi>
parents:
18984
diff
changeset
|
45 static void |
818c1a77c025
lib: Log notify IO leaks when ioloop is destroyed.
Timo Sirainen <tss@iki.fi>
parents:
18984
diff
changeset
|
46 io_loop_notify_free(struct ioloop_notify_handler_context *ctx, |
818c1a77c025
lib: Log notify IO leaks when ioloop is destroyed.
Timo Sirainen <tss@iki.fi>
parents:
18984
diff
changeset
|
47 struct io_notify *io) |
818c1a77c025
lib: Log notify IO leaks when ioloop is destroyed.
Timo Sirainen <tss@iki.fi>
parents:
18984
diff
changeset
|
48 { |
818c1a77c025
lib: Log notify IO leaks when ioloop is destroyed.
Timo Sirainen <tss@iki.fi>
parents:
18984
diff
changeset
|
49 DLLIST_REMOVE(&ctx->notifies, io); |
818c1a77c025
lib: Log notify IO leaks when ioloop is destroyed.
Timo Sirainen <tss@iki.fi>
parents:
18984
diff
changeset
|
50 i_free(io); |
818c1a77c025
lib: Log notify IO leaks when ioloop is destroyed.
Timo Sirainen <tss@iki.fi>
parents:
18984
diff
changeset
|
51 } |
818c1a77c025
lib: Log notify IO leaks when ioloop is destroyed.
Timo Sirainen <tss@iki.fi>
parents:
18984
diff
changeset
|
52 |
4907
5b4c9b20eba0
Replaced void *context from a lot of callbacks with the actual context
Timo Sirainen <tss@iki.fi>
parents:
4737
diff
changeset
|
53 static void event_callback(struct ioloop_notify_handler_context *ctx) |
3782
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
54 { |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
55 struct io_notify *io; |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
56 struct kevent events[64]; |
4573
8d977716f449
Rewrote much of the kqueue code. It didn't work correctly if there were both
Timo Sirainen <tss@iki.fi>
parents:
3959
diff
changeset
|
57 struct timespec ts; |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
58 int i, ret; |
3782
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
59 |
4573
8d977716f449
Rewrote much of the kqueue code. It didn't work correctly if there were both
Timo Sirainen <tss@iki.fi>
parents:
3959
diff
changeset
|
60 ts.tv_sec = 0; |
8d977716f449
Rewrote much of the kqueue code. It didn't work correctly if there were both
Timo Sirainen <tss@iki.fi>
parents:
3959
diff
changeset
|
61 ts.tv_nsec = 0; |
8d977716f449
Rewrote much of the kqueue code. It didn't work correctly if there were both
Timo Sirainen <tss@iki.fi>
parents:
3959
diff
changeset
|
62 |
6494
59490181469e
Use N_ELEMENTS() macro instead of doing sizeof()/sizeof([0]) ourself.
Timo Sirainen <tss@iki.fi>
parents:
5937
diff
changeset
|
63 ret = kevent(ctx->kq, NULL, 0, events, N_ELEMENTS(events), &ts); |
4573
8d977716f449
Rewrote much of the kqueue code. It didn't work correctly if there were both
Timo Sirainen <tss@iki.fi>
parents:
3959
diff
changeset
|
64 if (ret <= 0) { |
8d977716f449
Rewrote much of the kqueue code. It didn't work correctly if there were both
Timo Sirainen <tss@iki.fi>
parents:
3959
diff
changeset
|
65 if (ret == 0 || errno == EINTR) |
8d977716f449
Rewrote much of the kqueue code. It didn't work correctly if there were both
Timo Sirainen <tss@iki.fi>
parents:
3959
diff
changeset
|
66 return; |
8d977716f449
Rewrote much of the kqueue code. It didn't work correctly if there were both
Timo Sirainen <tss@iki.fi>
parents:
3959
diff
changeset
|
67 |
8d977716f449
Rewrote much of the kqueue code. It didn't work correctly if there were both
Timo Sirainen <tss@iki.fi>
parents:
3959
diff
changeset
|
68 i_fatal("kevent(notify) failed: %m"); |
8d977716f449
Rewrote much of the kqueue code. It didn't work correctly if there were both
Timo Sirainen <tss@iki.fi>
parents:
3959
diff
changeset
|
69 } |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
70 |
9400
b59c6c30115e
Removed ioloop_timezone. It's not working nowadays.
Timo Sirainen <tss@iki.fi>
parents:
8557
diff
changeset
|
71 if (gettimeofday(&ioloop_timeval, NULL) < 0) |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
72 i_fatal("gettimeofday() failed: %m"); |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
73 ioloop_time = ioloop_timeval.tv_sec; |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
74 |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
75 for (i = 0; i < ret; i++) { |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
76 io = (void *)events[i].udata; |
8557
076ccdcbbeb2
kqueue notify: Don't assert-cras if kevent() returns multiple events for the same io.
Timo Sirainen <tss@iki.fi>
parents:
8366
diff
changeset
|
77 i_assert(io->refcount >= 1); |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
78 io->refcount++; |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
79 } |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
80 for (i = 0; i < ret; i++) { |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
81 io = (void *)events[i].udata; |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
82 /* there can be multiple events for a single io. |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
83 call the callback only once if that happens. */ |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
84 if (io->refcount == 2 && io->io.callback != NULL) |
13252
32315c24992c
Replaced "ioloop log" with a more generic "ioloop context".
Timo Sirainen <tss@iki.fi>
parents:
9400
diff
changeset
|
85 io_loop_call_io(&io->io); |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
86 |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
87 if (--io->refcount == 0) |
18985
818c1a77c025
lib: Log notify IO leaks when ioloop is destroyed.
Timo Sirainen <tss@iki.fi>
parents:
18984
diff
changeset
|
88 io_loop_notify_free(ctx, io); |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
89 } |
3782
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
90 } |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
91 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
92 static struct ioloop_notify_handler_context *io_loop_notify_handler_init(void) |
3782
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
93 { |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
94 struct ioloop_notify_handler_context *ctx; |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
95 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
96 ctx = current_ioloop->notify_handler_context = |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
97 i_new(struct ioloop_notify_handler_context, 1); |
3782
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
98 ctx->kq = kqueue(); |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
99 if (ctx->kq < 0) |
4573
8d977716f449
Rewrote much of the kqueue code. It didn't work correctly if there were both
Timo Sirainen <tss@iki.fi>
parents:
3959
diff
changeset
|
100 i_fatal("kqueue(notify) failed: %m"); |
3958
3e9b43d0cd80
kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
3782
diff
changeset
|
101 fd_close_on_exec(ctx->kq, TRUE); |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
102 return ctx; |
3782
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
103 } |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
104 |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
105 void io_loop_notify_handler_deinit(struct ioloop *ioloop) |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
106 { |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
107 struct ioloop_notify_handler_context *ctx = |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
108 ioloop->notify_handler_context; |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
109 |
18985
818c1a77c025
lib: Log notify IO leaks when ioloop is destroyed.
Timo Sirainen <tss@iki.fi>
parents:
18984
diff
changeset
|
110 while (ctx->notifies != NULL) { |
818c1a77c025
lib: Log notify IO leaks when ioloop is destroyed.
Timo Sirainen <tss@iki.fi>
parents:
18984
diff
changeset
|
111 struct io_notify *io = ctx->notifies; |
818c1a77c025
lib: Log notify IO leaks when ioloop is destroyed.
Timo Sirainen <tss@iki.fi>
parents:
18984
diff
changeset
|
112 struct io *_io = &io->io; |
818c1a77c025
lib: Log notify IO leaks when ioloop is destroyed.
Timo Sirainen <tss@iki.fi>
parents:
18984
diff
changeset
|
113 |
20811 | 114 i_warning("I/O notify leak: %p (%s:%u, fd %d)", |
18985
818c1a77c025
lib: Log notify IO leaks when ioloop is destroyed.
Timo Sirainen <tss@iki.fi>
parents:
18984
diff
changeset
|
115 (void *)_io->callback, |
20811 | 116 _io->source_filename, |
18985
818c1a77c025
lib: Log notify IO leaks when ioloop is destroyed.
Timo Sirainen <tss@iki.fi>
parents:
18984
diff
changeset
|
117 _io->source_linenum, io->fd); |
818c1a77c025
lib: Log notify IO leaks when ioloop is destroyed.
Timo Sirainen <tss@iki.fi>
parents:
18984
diff
changeset
|
118 io_remove(&_io); |
818c1a77c025
lib: Log notify IO leaks when ioloop is destroyed.
Timo Sirainen <tss@iki.fi>
parents:
18984
diff
changeset
|
119 } |
818c1a77c025
lib: Log notify IO leaks when ioloop is destroyed.
Timo Sirainen <tss@iki.fi>
parents:
18984
diff
changeset
|
120 |
3958
3e9b43d0cd80
kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
3782
diff
changeset
|
121 if (ctx->event_io) |
3e9b43d0cd80
kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
3782
diff
changeset
|
122 io_remove(&ctx->event_io); |
3782
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
123 if (close(ctx->kq) < 0) |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
124 i_error("close(kqueue notify) failed: %m"); |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
125 i_free(ctx); |
3782
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
126 } |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
127 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
128 #undef io_add_notify |
18984
d26178d0557a
lib: io_add_notify() wasn't setting struct io.source_linenum
Timo Sirainen <tss@iki.fi>
parents:
18977
diff
changeset
|
129 enum io_notify_result |
20811 | 130 io_add_notify(const char *path, const char *source_filename, |
131 unsigned int source_linenum, | |
18984
d26178d0557a
lib: io_add_notify() wasn't setting struct io.source_linenum
Timo Sirainen <tss@iki.fi>
parents:
18977
diff
changeset
|
132 io_callback_t *callback, void *context, struct io **io_r) |
3782
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
133 { |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
134 struct ioloop_notify_handler_context *ctx = |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
135 current_ioloop->notify_handler_context; |
4573
8d977716f449
Rewrote much of the kqueue code. It didn't work correctly if there were both
Timo Sirainen <tss@iki.fi>
parents:
3959
diff
changeset
|
136 struct kevent ev; |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
137 struct io_notify *io; |
3782
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
138 int fd; |
3958
3e9b43d0cd80
kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
3782
diff
changeset
|
139 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
140 if (ctx == NULL) |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
141 ctx = io_loop_notify_handler_init(); |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
142 |
3782
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
143 fd = open(path, O_RDONLY); |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
144 if (fd == -1) { |
7527
20c6e9545d56
kqueue: If opening a file fails with ESTALE, don't log an error. It probably
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
145 /* ESTALE could happen with NFS. Don't bother giving an error |
20c6e9545d56
kqueue: If opening a file fails with ESTALE, don't log an error. It probably
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
146 message then. */ |
20c6e9545d56
kqueue: If opening a file fails with ESTALE, don't log an error. It probably
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
147 if (errno != ENOENT && errno != ESTALE) |
4573
8d977716f449
Rewrote much of the kqueue code. It didn't work correctly if there were both
Timo Sirainen <tss@iki.fi>
parents:
3959
diff
changeset
|
148 i_error("open(%s) for kq notify failed: %m", path); |
5937
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5935
diff
changeset
|
149 return IO_NOTIFY_NOTFOUND; |
3782
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
150 } |
3958
3e9b43d0cd80
kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
3782
diff
changeset
|
151 fd_close_on_exec(fd, TRUE); |
3782
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
152 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
153 io = i_new(struct io_notify, 1); |
6588
fd01ec16611f
Don't crash when removing kqueue notify.
Timo Sirainen <tss@iki.fi>
parents:
6494
diff
changeset
|
154 io->io.condition = IO_NOTIFY; |
21054
0cc16f2b23cf
lib: ioloop-notify-kqueue wasn't storing source filename.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20811
diff
changeset
|
155 io->io.source_filename = source_filename; |
18984
d26178d0557a
lib: io_add_notify() wasn't setting struct io.source_linenum
Timo Sirainen <tss@iki.fi>
parents:
18977
diff
changeset
|
156 io->io.source_linenum = source_linenum; |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
157 io->io.callback = callback; |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
158 io->io.context = context; |
8366
2c111b572eee
Don't break if io_remove() or timeout_remove() are called for non-active ioloops.
Timo Sirainen <tss@iki.fi>
parents:
7527
diff
changeset
|
159 io->io.ioloop = current_ioloop; |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
160 io->refcount = 1; |
3782
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
161 io->fd = fd; |
4573
8d977716f449
Rewrote much of the kqueue code. It didn't work correctly if there were both
Timo Sirainen <tss@iki.fi>
parents:
3959
diff
changeset
|
162 |
4590
4061c612a402
Fix 100% CPU usage looping when a vnode change event came but it was never
Timo Sirainen <tss@iki.fi>
parents:
4574
diff
changeset
|
163 /* EV_CLEAR flag is needed because the EVFILT_VNODE filter reports |
4061c612a402
Fix 100% CPU usage looping when a vnode change event came but it was never
Timo Sirainen <tss@iki.fi>
parents:
4574
diff
changeset
|
164 event state transitions and not the current state. With this flag, |
4061c612a402
Fix 100% CPU usage looping when a vnode change event came but it was never
Timo Sirainen <tss@iki.fi>
parents:
4574
diff
changeset
|
165 the same event is only returned once. */ |
4737
fd0d7e9e0e72
Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents:
4590
diff
changeset
|
166 MY_EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, |
18977
412de6dea139
lib: kqueue notification should trigger also on file renames.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
167 NOTE_DELETE | NOTE_RENAME | NOTE_WRITE | NOTE_EXTEND | |
412de6dea139
lib: kqueue notification should trigger also on file renames.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
168 NOTE_REVOKE, 0, io); |
3958
3e9b43d0cd80
kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
3782
diff
changeset
|
169 if (kevent(ctx->kq, &ev, 1, NULL, 0, NULL) < 0) { |
3e9b43d0cd80
kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
3782
diff
changeset
|
170 i_error("kevent(%d, %s) for notify failed: %m", fd, path); |
14691
3945a3646c67
Changed i_close_fd() API to set the fd to -1 after closing.
Timo Sirainen <tss@iki.fi>
parents:
14687
diff
changeset
|
171 i_close_fd(&fd); |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
172 i_free(io); |
7457
940641318f12
Renamed IO_NOTIFY_DISABLED to IO_NOTIFY_NOSUPPORT. IO_NOTIFY_NOSUPPORT is
Timo Sirainen <tss@iki.fi>
parents:
6588
diff
changeset
|
173 return IO_NOTIFY_NOSUPPORT; |
3958
3e9b43d0cd80
kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
3782
diff
changeset
|
174 } |
3782
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
175 |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
176 if (ctx->event_io == NULL) { |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
177 ctx->event_io = io_add(ctx->kq, IO_READ, event_callback, |
8366
2c111b572eee
Don't break if io_remove() or timeout_remove() are called for non-active ioloops.
Timo Sirainen <tss@iki.fi>
parents:
7527
diff
changeset
|
178 io->io.ioloop->notify_handler_context); |
3782
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
179 } |
18985
818c1a77c025
lib: Log notify IO leaks when ioloop is destroyed.
Timo Sirainen <tss@iki.fi>
parents:
18984
diff
changeset
|
180 DLLIST_PREPEND(&ctx->notifies, io); |
5937
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5935
diff
changeset
|
181 *io_r = &io->io; |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5935
diff
changeset
|
182 return IO_NOTIFY_ADDED; |
3782
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
183 } |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
184 |
8366
2c111b572eee
Don't break if io_remove() or timeout_remove() are called for non-active ioloops.
Timo Sirainen <tss@iki.fi>
parents:
7527
diff
changeset
|
185 void io_loop_notify_remove(struct io *_io) |
3782
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
186 { |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
187 struct ioloop_notify_handler_context *ctx = |
8366
2c111b572eee
Don't break if io_remove() or timeout_remove() are called for non-active ioloops.
Timo Sirainen <tss@iki.fi>
parents:
7527
diff
changeset
|
188 _io->ioloop->notify_handler_context; |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
189 struct io_notify *io = (struct io_notify *)_io; |
4573
8d977716f449
Rewrote much of the kqueue code. It didn't work correctly if there were both
Timo Sirainen <tss@iki.fi>
parents:
3959
diff
changeset
|
190 struct kevent ev; |
3958
3e9b43d0cd80
kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
3782
diff
changeset
|
191 |
4737
fd0d7e9e0e72
Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents:
4590
diff
changeset
|
192 MY_EV_SET(&ev, io->fd, EVFILT_VNODE, EV_DELETE, 0, 0, NULL); |
3958
3e9b43d0cd80
kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
3782
diff
changeset
|
193 if (kevent(ctx->kq, &ev, 1, NULL, 0, 0) < 0) |
3e9b43d0cd80
kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
3782
diff
changeset
|
194 i_error("kevent(%d) for notify remove failed: %m", io->fd); |
3e9b43d0cd80
kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
3782
diff
changeset
|
195 if (close(io->fd) < 0) |
4573
8d977716f449
Rewrote much of the kqueue code. It didn't work correctly if there were both
Timo Sirainen <tss@iki.fi>
parents:
3959
diff
changeset
|
196 i_error("close(%d) for notify remove failed: %m", io->fd); |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
197 io->fd = -1; |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
198 |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
199 if (--io->refcount == 0) |
18985
818c1a77c025
lib: Log notify IO leaks when ioloop is destroyed.
Timo Sirainen <tss@iki.fi>
parents:
18984
diff
changeset
|
200 io_loop_notify_free(ctx, io); |
3782
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
201 } |
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
202 |
18994
3083424cafcd
lib: Added io_loop_extract_notify_fd()
Timo Sirainen <tss@iki.fi>
parents:
18985
diff
changeset
|
203 int io_loop_extract_notify_fd(struct ioloop *ioloop) |
3083424cafcd
lib: Added io_loop_extract_notify_fd()
Timo Sirainen <tss@iki.fi>
parents:
18985
diff
changeset
|
204 { |
3083424cafcd
lib: Added io_loop_extract_notify_fd()
Timo Sirainen <tss@iki.fi>
parents:
18985
diff
changeset
|
205 struct ioloop_notify_handler_context *ctx = |
3083424cafcd
lib: Added io_loop_extract_notify_fd()
Timo Sirainen <tss@iki.fi>
parents:
18985
diff
changeset
|
206 ioloop->notify_handler_context; |
3083424cafcd
lib: Added io_loop_extract_notify_fd()
Timo Sirainen <tss@iki.fi>
parents:
18985
diff
changeset
|
207 struct io_notify *io; |
3083424cafcd
lib: Added io_loop_extract_notify_fd()
Timo Sirainen <tss@iki.fi>
parents:
18985
diff
changeset
|
208 int fd, new_kq; |
3083424cafcd
lib: Added io_loop_extract_notify_fd()
Timo Sirainen <tss@iki.fi>
parents:
18985
diff
changeset
|
209 |
22369
98652f62dbf5
lib: io_loop_extract_notify_fd() - Don't crash if no notifys have been added
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21054
diff
changeset
|
210 if (ctx == NULL || ctx->kq == -1) |
18994
3083424cafcd
lib: Added io_loop_extract_notify_fd()
Timo Sirainen <tss@iki.fi>
parents:
18985
diff
changeset
|
211 return -1; |
3083424cafcd
lib: Added io_loop_extract_notify_fd()
Timo Sirainen <tss@iki.fi>
parents:
18985
diff
changeset
|
212 |
19002
f3e6256d3762
lib: Fixed io_loop_extract_notify_fd() to compile with kqueue.
Timo Sirainen <tss@iki.fi>
parents:
18994
diff
changeset
|
213 new_kq = kqueue(); |
f3e6256d3762
lib: Fixed io_loop_extract_notify_fd() to compile with kqueue.
Timo Sirainen <tss@iki.fi>
parents:
18994
diff
changeset
|
214 if (new_kq < 0) { |
f3e6256d3762
lib: Fixed io_loop_extract_notify_fd() to compile with kqueue.
Timo Sirainen <tss@iki.fi>
parents:
18994
diff
changeset
|
215 i_error("kqueue(notify) failed: %m"); |
18994
3083424cafcd
lib: Added io_loop_extract_notify_fd()
Timo Sirainen <tss@iki.fi>
parents:
18985
diff
changeset
|
216 return -1; |
3083424cafcd
lib: Added io_loop_extract_notify_fd()
Timo Sirainen <tss@iki.fi>
parents:
18985
diff
changeset
|
217 } |
19002
f3e6256d3762
lib: Fixed io_loop_extract_notify_fd() to compile with kqueue.
Timo Sirainen <tss@iki.fi>
parents:
18994
diff
changeset
|
218 for (io = ctx->notifies; io != NULL; io = io->next) |
18994
3083424cafcd
lib: Added io_loop_extract_notify_fd()
Timo Sirainen <tss@iki.fi>
parents:
18985
diff
changeset
|
219 io->fd = -1; |
3083424cafcd
lib: Added io_loop_extract_notify_fd()
Timo Sirainen <tss@iki.fi>
parents:
18985
diff
changeset
|
220 if (ctx->event_io != NULL) |
3083424cafcd
lib: Added io_loop_extract_notify_fd()
Timo Sirainen <tss@iki.fi>
parents:
18985
diff
changeset
|
221 io_remove(&ctx->event_io); |
3083424cafcd
lib: Added io_loop_extract_notify_fd()
Timo Sirainen <tss@iki.fi>
parents:
18985
diff
changeset
|
222 fd = ctx->kq; |
3083424cafcd
lib: Added io_loop_extract_notify_fd()
Timo Sirainen <tss@iki.fi>
parents:
18985
diff
changeset
|
223 ctx->kq = new_kq; |
3083424cafcd
lib: Added io_loop_extract_notify_fd()
Timo Sirainen <tss@iki.fi>
parents:
18985
diff
changeset
|
224 return fd; |
3083424cafcd
lib: Added io_loop_extract_notify_fd()
Timo Sirainen <tss@iki.fi>
parents:
18985
diff
changeset
|
225 } |
3083424cafcd
lib: Added io_loop_extract_notify_fd()
Timo Sirainen <tss@iki.fi>
parents:
18985
diff
changeset
|
226 |
3782
c67f77647a6e
Added kqueue notification support. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
227 #endif |