Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/lib/ioloop-notify-inotify.c @ 8590:b9faf4db2a9f HEAD
Updated copyright notices to include year 2009.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Tue, 06 Jan 2009 09:25:38 -0500 |
parents | 2c111b572eee |
children | 5cc26d4fc00f |
rev | line source |
---|---|
8590
b9faf4db2a9f
Updated copyright notices to include year 2009.
Timo Sirainen <tss@iki.fi>
parents:
8366
diff
changeset
|
1 /* Copyright (c) 2005-2009 Dovecot authors, see the included COPYING file */ |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
2 |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
3 #define _GNU_SOURCE |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
4 #include "lib.h" |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
5 |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
6 #ifdef IOLOOP_NOTIFY_INOTIFY |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
7 |
3534
a9be1824403b
New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents:
3492
diff
changeset
|
8 #include "fd-close-on-exec.h" |
5937
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5814
diff
changeset
|
9 #include "fd-set-nonblock.h" |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
10 #include "ioloop-internal.h" |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
11 #include "ioloop-notify-fd.h" |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
12 #include "buffer.h" |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
13 #include "network.h" |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
14 |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
15 #include <stdio.h> |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
16 #include <unistd.h> |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
17 #include <fcntl.h> |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
18 #include <sys/ioctl.h> |
3905
b694c27ad1a8
Changed linux/inotify* includes to sys/inotify.h (I thought I had done this
Timo Sirainen <tss@iki.fi>
parents:
3882
diff
changeset
|
19 #include <sys/inotify.h> |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
20 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
21 #define INOTIFY_BUFLEN (32*1024) |
3959
ef5595d6ddec
Cleanups: Make io_remove() do the linked list updating also for notify
Timo Sirainen <tss@iki.fi>
parents:
3905
diff
changeset
|
22 |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
23 struct ioloop_notify_handler_context { |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
24 struct ioloop_notify_fd_context fd_ctx; |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
25 |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
26 int inotify_fd; |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
27 struct io *event_io; |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
28 |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3534
diff
changeset
|
29 bool disabled; |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
30 }; |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
31 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
32 static struct ioloop_notify_handler_context *io_loop_notify_handler_init(void); |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
33 |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
34 static bool inotify_input_more(struct ioloop *ioloop) |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
35 { |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
36 struct ioloop_notify_handler_context *ctx = |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
37 ioloop->notify_handler_context; |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
38 const struct inotify_event *event; |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
39 unsigned char event_buf[INOTIFY_BUFLEN]; |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
40 struct io_notify *io; |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
41 ssize_t ret, pos; |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
42 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
43 /* read as many events as there is available and fit into our buffer. |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
44 only full events are returned by the kernel. */ |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
45 ret = read(ctx->inotify_fd, event_buf, sizeof(event_buf)); |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
46 if (ret <= 0) { |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
47 if (ret == 0) { |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
48 /* nothing more to read */ |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
49 return FALSE; |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
50 } |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
51 i_fatal("read(inotify) failed: %m"); |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
52 } |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
53 |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
54 if (gettimeofday(&ioloop_timeval, &ioloop_timezone) < 0) |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
55 i_fatal("gettimeofday(): %m"); |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
56 ioloop_time = ioloop_timeval.tv_sec; |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
57 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
58 for (pos = 0; pos < ret; ) { |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
59 if ((size_t)(ret - pos) < sizeof(*event)) |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
60 break; |
3959
ef5595d6ddec
Cleanups: Make io_remove() do the linked list updating also for notify
Timo Sirainen <tss@iki.fi>
parents:
3905
diff
changeset
|
61 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
62 event = (struct inotify_event *)(event_buf + pos); |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
63 pos += sizeof(*event) + event->len; |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
64 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
65 io = io_notify_fd_find(&ctx->fd_ctx, event->wd); |
5937
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5814
diff
changeset
|
66 if (io != NULL) { |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5814
diff
changeset
|
67 if ((event->mask & IN_IGNORED) != 0) { |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5814
diff
changeset
|
68 /* calling inotify_rm_watch() would now give |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5814
diff
changeset
|
69 EINVAL */ |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5814
diff
changeset
|
70 io->fd = -1; |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5814
diff
changeset
|
71 } |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
72 io->io.callback(io->io.context); |
5937
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5814
diff
changeset
|
73 } |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
74 } |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
75 if (pos != ret) |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
76 i_error("read(inotify) returned partial event"); |
5937
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5814
diff
changeset
|
77 return (size_t)ret >= sizeof(event_buf)-512; |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
78 } |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
79 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
80 static void inotify_input(struct ioloop *ioloop) |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
81 { |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
82 while (inotify_input_more(ioloop)) ; |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
83 } |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
84 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
85 #undef io_add_notify |
5937
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5814
diff
changeset
|
86 enum io_notify_result io_add_notify(const char *path, io_callback_t *callback, |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5814
diff
changeset
|
87 void *context, struct io **io_r) |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
88 { |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
89 struct ioloop_notify_handler_context *ctx = |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
90 current_ioloop->notify_handler_context; |
5937
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5814
diff
changeset
|
91 int wd; |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5814
diff
changeset
|
92 |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5814
diff
changeset
|
93 *io_r = NULL; |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
94 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
95 if (ctx == NULL) |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
96 ctx = io_loop_notify_handler_init(); |
3485
b3e135c37f06
If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents:
3482
diff
changeset
|
97 if (ctx->disabled) |
7457
940641318f12
Renamed IO_NOTIFY_DISABLED to IO_NOTIFY_NOSUPPORT. IO_NOTIFY_NOSUPPORT is
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
98 return IO_NOTIFY_NOSUPPORT; |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
99 |
5937
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5814
diff
changeset
|
100 wd = inotify_add_watch(ctx->inotify_fd, path, |
5940
b985e2127a2d
Listen for IN_DELETE_SELF too. Ignore EINVAL for inotify_rm_watch().
Timo Sirainen <tss@iki.fi>
parents:
5937
diff
changeset
|
101 IN_CREATE | IN_DELETE | IN_DELETE_SELF | |
b985e2127a2d
Listen for IN_DELETE_SELF too. Ignore EINVAL for inotify_rm_watch().
Timo Sirainen <tss@iki.fi>
parents:
5937
diff
changeset
|
102 IN_MOVE | IN_CLOSE | IN_MODIFY); |
5937
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5814
diff
changeset
|
103 if (wd < 0) { |
6818
f637a8a1aae7
Don't disable inotify when seeing ESTALE errors.
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
104 /* ESTALE could happen with NFS. Don't bother giving an error |
f637a8a1aae7
Don't disable inotify when seeing ESTALE errors.
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
105 message then. */ |
f637a8a1aae7
Don't disable inotify when seeing ESTALE errors.
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
106 if (errno == ENOENT || errno == ESTALE) |
5937
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5814
diff
changeset
|
107 return IO_NOTIFY_NOTFOUND; |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5814
diff
changeset
|
108 |
7505
3c0dd5c29aca
If inotify_add_watch() fails, log an error.
Timo Sirainen <tss@iki.fi>
parents:
7504
diff
changeset
|
109 i_error("inotify_add_watch(%s) failed: %m", path); |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
110 ctx->disabled = TRUE; |
7457
940641318f12
Renamed IO_NOTIFY_DISABLED to IO_NOTIFY_NOSUPPORT. IO_NOTIFY_NOSUPPORT is
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
111 return IO_NOTIFY_NOSUPPORT; |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
112 } |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
113 |
3485
b3e135c37f06
If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents:
3482
diff
changeset
|
114 if (ctx->event_io == NULL) { |
b3e135c37f06
If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents:
3482
diff
changeset
|
115 ctx->event_io = io_add(ctx->inotify_fd, IO_READ, |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
116 inotify_input, current_ioloop); |
3485
b3e135c37f06
If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents:
3482
diff
changeset
|
117 } |
b3e135c37f06
If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents:
3482
diff
changeset
|
118 |
5937
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5814
diff
changeset
|
119 *io_r = io_notify_fd_add(&ctx->fd_ctx, wd, callback, context); |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5814
diff
changeset
|
120 return IO_NOTIFY_ADDED; |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
121 } |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
122 |
8366
2c111b572eee
Don't break if io_remove() or timeout_remove() are called for non-active ioloops.
Timo Sirainen <tss@iki.fi>
parents:
7505
diff
changeset
|
123 void io_loop_notify_remove(struct io *_io) |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
124 { |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
125 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:
7505
diff
changeset
|
126 _io->ioloop->notify_handler_context; |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
127 struct io_notify *io = (struct io_notify *)_io; |
3485
b3e135c37f06
If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents:
3482
diff
changeset
|
128 |
5937
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5814
diff
changeset
|
129 if (io->fd != -1) { |
5940
b985e2127a2d
Listen for IN_DELETE_SELF too. Ignore EINVAL for inotify_rm_watch().
Timo Sirainen <tss@iki.fi>
parents:
5937
diff
changeset
|
130 /* ernro=EINVAL happens if the file itself is deleted and |
b985e2127a2d
Listen for IN_DELETE_SELF too. Ignore EINVAL for inotify_rm_watch().
Timo Sirainen <tss@iki.fi>
parents:
5937
diff
changeset
|
131 kernel has sent IN_IGNORED event which we haven't read. */ |
b985e2127a2d
Listen for IN_DELETE_SELF too. Ignore EINVAL for inotify_rm_watch().
Timo Sirainen <tss@iki.fi>
parents:
5937
diff
changeset
|
132 if (inotify_rm_watch(ctx->inotify_fd, io->fd) < 0 && |
b985e2127a2d
Listen for IN_DELETE_SELF too. Ignore EINVAL for inotify_rm_watch().
Timo Sirainen <tss@iki.fi>
parents:
5937
diff
changeset
|
133 errno != EINVAL) |
5937
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5814
diff
changeset
|
134 i_error("inotify_rm_watch() failed: %m"); |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5814
diff
changeset
|
135 } |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
136 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
137 io_notify_fd_free(&ctx->fd_ctx, io); |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
138 |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
139 if (ctx->fd_ctx.notifies == NULL) |
3882 | 140 io_remove(&ctx->event_io); |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
141 } |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
142 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
143 static struct ioloop_notify_handler_context *io_loop_notify_handler_init(void) |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
144 { |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
145 struct ioloop *ioloop = current_ioloop; |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
146 struct ioloop_notify_handler_context *ctx; |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
147 |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
148 ctx = ioloop->notify_handler_context = |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
149 i_new(struct ioloop_notify_handler_context, 1); |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
150 |
3534
a9be1824403b
New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents:
3492
diff
changeset
|
151 ctx->inotify_fd = inotify_init(); |
a9be1824403b
New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents:
3492
diff
changeset
|
152 if (ctx->inotify_fd == -1) { |
5814
498ef9bc33f1
If inotify_init() fails with EMFILE, give an understandable error message.
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
153 if (errno != EMFILE) |
498ef9bc33f1
If inotify_init() fails with EMFILE, give an understandable error message.
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
154 i_error("inotify_init() failed: %m"); |
498ef9bc33f1
If inotify_init() fails with EMFILE, give an understandable error message.
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
155 else { |
498ef9bc33f1
If inotify_init() fails with EMFILE, give an understandable error message.
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
156 i_warning("Inotify instance limit for user exceeded, " |
7504
31e1382b315f
If inotify instance limit is reached, give a hint about max_user_instances
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
157 "disabling. Increase " |
31e1382b315f
If inotify instance limit is reached, give a hint about max_user_instances
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
158 "/proc/sys/fs/inotify/max_user_instances"); |
5814
498ef9bc33f1
If inotify_init() fails with EMFILE, give an understandable error message.
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
159 } |
3485
b3e135c37f06
If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents:
3482
diff
changeset
|
160 ctx->disabled = TRUE; |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
161 } else { |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
162 fd_close_on_exec(ctx->inotify_fd, TRUE); |
5937
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5814
diff
changeset
|
163 fd_set_nonblock(ctx->inotify_fd, TRUE); |
3485
b3e135c37f06
If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents:
3482
diff
changeset
|
164 } |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
165 return ctx; |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
166 } |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
167 |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
168 void io_loop_notify_handler_deinit(struct ioloop *ioloop) |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
169 { |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
170 struct ioloop_notify_handler_context *ctx = |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
171 ioloop->notify_handler_context; |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
172 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
173 if (ctx->inotify_fd != -1) { |
3534
a9be1824403b
New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents:
3492
diff
changeset
|
174 if (close(ctx->inotify_fd) < 0) |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
175 i_error("close(inotify) failed: %m"); |
5937
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5814
diff
changeset
|
176 ctx->inotify_fd = -1; |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5030
diff
changeset
|
177 } |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
178 i_free(ctx); |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
179 } |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
180 |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
181 #endif |