Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/lib/ioloop-notify-dn.c @ 9191:b340ecb24469 HEAD
Fix VPATH build of RQUOTA support.
Some rpcgen derive #include "..." paths from the infile argument.
This will be off for VPATH builds, as the generated rquota_xdr.c
code will look in $(srcdir), but we'll generate the rquota.h file in
$(builddir). Play safe and copy rquota.x to $(builddir) first.
This fixes the build on openSUSE 11.1.
author | Matthias Andree <matthias.andree@gmx.de> |
---|---|
date | Tue, 07 Jul 2009 21:01:36 +0200 |
parents | b9faf4db2a9f |
children | 00cd9aacd03c |
rev | line source |
---|---|
8590
b9faf4db2a9f
Updated copyright notices to include year 2009.
Timo Sirainen <tss@iki.fi>
parents:
8366
diff
changeset
|
1 /* Copyright (c) 2003-2009 Dovecot authors, see the included COPYING file */ |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
2 |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
3 /* Logic is pretty much based on dnotify by Oskar Liljeblad. */ |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
4 |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
5 #define _GNU_SOURCE |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
6 #include "lib.h" |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
7 |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
8 #ifdef IOLOOP_NOTIFY_DNOTIFY |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
9 |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
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:
5018
diff
changeset
|
11 #include "ioloop-notify-fd.h" |
3621
3ae2df67459c
Added fd_set_nonblock() and changed net_set_nonblock() to use it.
Timo Sirainen <tss@iki.fi>
parents:
3534
diff
changeset
|
12 #include "fd-set-nonblock.h" |
3492
ab0fc2c1d8e1
Set close-on-exec flags for opened file descriptors.
Timo Sirainen <tss@iki.fi>
parents:
3485
diff
changeset
|
13 #include "fd-close-on-exec.h" |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
14 |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
15 #include <signal.h> |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
16 #include <unistd.h> |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
17 #include <fcntl.h> |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
18 |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
2571
diff
changeset
|
19 struct ioloop_notify_handler_context { |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
20 struct ioloop_notify_fd_context fd_ctx; |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
21 |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
2571
diff
changeset
|
22 struct io *event_io; |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
23 int event_pipe[2]; |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
24 |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3851
diff
changeset
|
25 bool disabled; |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
2571
diff
changeset
|
26 }; |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
2571
diff
changeset
|
27 |
3851
4fa8e287486e
Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents:
3649
diff
changeset
|
28 static int sigrt_refcount = 0; |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
29 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
30 static struct ioloop_notify_handler_context *io_loop_notify_handler_init(void); |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
31 |
7459
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
32 static void ioloop_dnotify_disable(struct ioloop_notify_handler_context *ctx) |
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
33 { |
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
34 if (ctx->disabled) |
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
35 return; |
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
36 |
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
37 if (--sigrt_refcount == 0) |
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
38 signal(SIGRTMIN, SIG_IGN); |
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
39 |
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
40 if (close(ctx->event_pipe[0]) < 0) |
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
41 i_error("close(dnotify pipe[0]) failed: %m"); |
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
42 if (close(ctx->event_pipe[1]) < 0) |
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
43 i_error("close(dnotify pipe[1]) failed: %m"); |
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
44 ctx->disabled = TRUE; |
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
45 } |
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
46 |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
5937
diff
changeset
|
47 static void sigrt_handler(int signo ATTR_UNUSED, siginfo_t *si, |
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
5937
diff
changeset
|
48 void *data ATTR_UNUSED) |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
49 { |
3851
4fa8e287486e
Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents:
3649
diff
changeset
|
50 struct ioloop_notify_handler_context *ctx = |
4fa8e287486e
Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents:
3649
diff
changeset
|
51 current_ioloop->notify_handler_context; |
4156
d39ac5712d2e
Make sure errno is restored when exiting SIGRT signal handler.
Timo Sirainen <tss@iki.fi>
parents:
3959
diff
changeset
|
52 int saved_errno = errno; |
2571
cc2127b799c5
Ignore SIGIO which gets sent if queue gets full. Also other small
Timo Sirainen <tss@iki.fi>
parents:
2568
diff
changeset
|
53 int ret; |
cc2127b799c5
Ignore SIGIO which gets sent if queue gets full. Also other small
Timo Sirainen <tss@iki.fi>
parents:
2568
diff
changeset
|
54 |
7459
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
55 if (ctx->disabled) |
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
56 return; |
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
57 |
3851
4fa8e287486e
Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents:
3649
diff
changeset
|
58 ret = write(ctx->event_pipe[1], &si->si_fd, sizeof(int)); |
7459
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
59 if (ret < 0 && errno != EINTR && errno != EAGAIN) { |
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
60 i_error("write(dnotify pipe) failed: %m"); |
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
61 ioloop_dnotify_disable(ctx); |
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
62 } |
2571
cc2127b799c5
Ignore SIGIO which gets sent if queue gets full. Also other small
Timo Sirainen <tss@iki.fi>
parents:
2568
diff
changeset
|
63 |
cc2127b799c5
Ignore SIGIO which gets sent if queue gets full. Also other small
Timo Sirainen <tss@iki.fi>
parents:
2568
diff
changeset
|
64 i_assert(ret <= 0 || ret == sizeof(int)); |
4156
d39ac5712d2e
Make sure errno is restored when exiting SIGRT signal handler.
Timo Sirainen <tss@iki.fi>
parents:
3959
diff
changeset
|
65 |
d39ac5712d2e
Make sure errno is restored when exiting SIGRT signal handler.
Timo Sirainen <tss@iki.fi>
parents:
3959
diff
changeset
|
66 errno = saved_errno; |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
67 } |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
68 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
69 static void dnotify_input(struct ioloop *ioloop) |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
70 { |
3851
4fa8e287486e
Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents:
3649
diff
changeset
|
71 struct ioloop_notify_handler_context *ctx = |
4fa8e287486e
Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents:
3649
diff
changeset
|
72 ioloop->notify_handler_context; |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
73 struct io_notify *io; |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
74 int fd_buf[256], i, ret; |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
75 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
76 ret = read(ctx->event_pipe[0], fd_buf, sizeof(fd_buf)); |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
77 if (ret < 0) |
7459
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
78 i_fatal("read(dnotify pipe) failed: %m"); |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
79 if ((ret % sizeof(fd_buf[0])) != 0) |
7459
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
80 i_fatal("read(dnotify pipe) returned %d", ret); |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
81 ret /= sizeof(fd_buf[0]); |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
82 |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
83 if (gettimeofday(&ioloop_timeval, &ioloop_timezone) < 0) |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
84 i_fatal("gettimeofday(): %m"); |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
85 ioloop_time = ioloop_timeval.tv_sec; |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
86 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
87 for (i = 0; i < ret; i++) { |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
88 io = io_notify_fd_find(&ctx->fd_ctx, fd_buf[i]); |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
89 if (io != NULL) |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
90 io->io.callback(io->io.context); |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
91 } |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
92 } |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
93 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
94 #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:
5248
diff
changeset
|
95 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:
5248
diff
changeset
|
96 void *context, struct io **io_r) |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
97 { |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
2571
diff
changeset
|
98 struct ioloop_notify_handler_context *ctx = |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
99 current_ioloop->notify_handler_context; |
7459
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
100 int fd; |
5937
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
101 |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
102 *io_r = NULL; |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
103 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
104 if (ctx == NULL) |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
105 ctx = io_loop_notify_handler_init(); |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
106 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
|
107 return IO_NOTIFY_NOSUPPORT; |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
108 |
3534
a9be1824403b
New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents:
3492
diff
changeset
|
109 fd = open(path, O_RDONLY); |
a9be1824403b
New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents:
3492
diff
changeset
|
110 if (fd == -1) { |
6817 | 111 /* ESTALE could happen with NFS. Don't bother giving an error |
112 message then. */ | |
113 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:
5248
diff
changeset
|
114 i_error("open(%s) for dnotify failed: %m", path); |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
115 return IO_NOTIFY_NOTFOUND; |
3534
a9be1824403b
New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents:
3492
diff
changeset
|
116 } |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
117 |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
118 if (fcntl(fd, F_SETSIG, SIGRTMIN) < 0) { |
5937
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
119 /* EINVAL means there's no realtime signals and no dnotify */ |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
120 if (errno != EINVAL) |
3634 | 121 i_error("fcntl(F_SETSIG) failed: %m"); |
7459
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
122 ioloop_dnotify_disable(ctx); |
3634 | 123 (void)close(fd); |
7457
940641318f12
Renamed IO_NOTIFY_DISABLED to IO_NOTIFY_NOSUPPORT. IO_NOTIFY_NOSUPPORT is
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
124 return IO_NOTIFY_NOSUPPORT; |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
125 } |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
126 if (fcntl(fd, F_NOTIFY, DN_CREATE | DN_DELETE | DN_RENAME | |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
127 DN_MULTISHOT) < 0) { |
5018
9b8c3efa85ec
Disable dnotify silently if it's not supported by the kernel. We were
Timo Sirainen <tss@iki.fi>
parents:
4903
diff
changeset
|
128 if (errno == ENOTDIR) { |
9b8c3efa85ec
Disable dnotify silently if it's not supported by the kernel. We were
Timo Sirainen <tss@iki.fi>
parents:
4903
diff
changeset
|
129 /* we're trying to add dnotify to a non-directory fd. |
9b8c3efa85ec
Disable dnotify silently if it's not supported by the kernel. We were
Timo Sirainen <tss@iki.fi>
parents:
4903
diff
changeset
|
130 fail silently. */ |
5937
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
131 } else { |
5018
9b8c3efa85ec
Disable dnotify silently if it's not supported by the kernel. We were
Timo Sirainen <tss@iki.fi>
parents:
4903
diff
changeset
|
132 /* dnotify not in kernel. disable it. */ |
5937
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
133 if (errno != EINVAL) |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
134 i_error("fcntl(F_NOTIFY) failed: %m"); |
7459
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
135 ioloop_dnotify_disable(ctx); |
5018
9b8c3efa85ec
Disable dnotify silently if it's not supported by the kernel. We were
Timo Sirainen <tss@iki.fi>
parents:
4903
diff
changeset
|
136 } |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
137 (void)fcntl(fd, F_SETSIG, 0); |
3534
a9be1824403b
New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents:
3492
diff
changeset
|
138 (void)close(fd); |
7457
940641318f12
Renamed IO_NOTIFY_DISABLED to IO_NOTIFY_NOSUPPORT. IO_NOTIFY_NOSUPPORT is
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
139 return IO_NOTIFY_NOSUPPORT; |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
140 } |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
141 |
3485
b3e135c37f06
If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents:
3482
diff
changeset
|
142 if (ctx->event_io == NULL) { |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
143 ctx->event_io = io_add(ctx->event_pipe[0], IO_READ, |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
144 dnotify_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
|
145 } |
b3e135c37f06
If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents:
3482
diff
changeset
|
146 |
5937
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
147 *io_r = io_notify_fd_add(&ctx->fd_ctx, fd, callback, context); |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
148 return IO_NOTIFY_ADDED; |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
149 } |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
150 |
8366
2c111b572eee
Don't break if io_remove() or timeout_remove() are called for non-active ioloops.
Timo Sirainen <tss@iki.fi>
parents:
7459
diff
changeset
|
151 void io_loop_notify_remove(struct io *_io) |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
152 { |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
2571
diff
changeset
|
153 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:
7459
diff
changeset
|
154 _io->ioloop->notify_handler_context; |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
155 struct io_notify *io = (struct io_notify *)_io; |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
156 |
2568
69f1ba731c84
Drop F_NOTIFY before setting F_SETSIG back to SIGIO, otherwise we might get
Timo Sirainen <tss@iki.fi>
parents:
1729
diff
changeset
|
157 if (fcntl(io->fd, F_NOTIFY, 0) < 0) |
69f1ba731c84
Drop F_NOTIFY before setting F_SETSIG back to SIGIO, otherwise we might get
Timo Sirainen <tss@iki.fi>
parents:
1729
diff
changeset
|
158 i_error("fcntl(F_NOTIFY, 0) failed: %m"); |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
159 if (fcntl(io->fd, F_SETSIG, 0) < 0) |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
160 i_error("fcntl(F_SETSIG, 0) failed: %m"); |
3534
a9be1824403b
New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents:
3492
diff
changeset
|
161 if (close(io->fd)) |
a9be1824403b
New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents:
3492
diff
changeset
|
162 i_error("close(dnotify) failed: %m"); |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
163 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
164 io_notify_fd_free(&ctx->fd_ctx, io); |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
165 |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
166 if (ctx->fd_ctx.notifies == NULL) |
3879
928229f8b3e6
deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents:
3863
diff
changeset
|
167 io_remove(&ctx->event_io); |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
168 } |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
169 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
170 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:
2571
diff
changeset
|
171 { |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
2571
diff
changeset
|
172 struct ioloop_notify_handler_context *ctx; |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
2571
diff
changeset
|
173 struct sigaction act; |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
2571
diff
changeset
|
174 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
175 ctx = current_ioloop->notify_handler_context = |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
2571
diff
changeset
|
176 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:
2571
diff
changeset
|
177 |
3851
4fa8e287486e
Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents:
3649
diff
changeset
|
178 if (pipe(ctx->event_pipe) < 0) { |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
179 ctx->disabled = TRUE; |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
180 i_error("dnotify: pipe() failed: %m"); |
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
181 return ctx; |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
2571
diff
changeset
|
182 } |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
2571
diff
changeset
|
183 |
3851
4fa8e287486e
Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents:
3649
diff
changeset
|
184 fd_set_nonblock(ctx->event_pipe[0], TRUE); |
4fa8e287486e
Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents:
3649
diff
changeset
|
185 fd_set_nonblock(ctx->event_pipe[1], TRUE); |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
2571
diff
changeset
|
186 |
3851
4fa8e287486e
Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents:
3649
diff
changeset
|
187 fd_close_on_exec(ctx->event_pipe[0], TRUE); |
4fa8e287486e
Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents:
3649
diff
changeset
|
188 fd_close_on_exec(ctx->event_pipe[1], TRUE); |
3492
ab0fc2c1d8e1
Set close-on-exec flags for opened file descriptors.
Timo Sirainen <tss@iki.fi>
parents:
3485
diff
changeset
|
189 |
3851
4fa8e287486e
Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents:
3649
diff
changeset
|
190 if (sigrt_refcount++ == 0) { |
4fa8e287486e
Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents:
3649
diff
changeset
|
191 /* SIGIO is sent if queue gets full. we'll just ignore it. */ |
4fa8e287486e
Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents:
3649
diff
changeset
|
192 signal(SIGIO, SIG_IGN); |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
2571
diff
changeset
|
193 |
3851
4fa8e287486e
Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents:
3649
diff
changeset
|
194 act.sa_sigaction = sigrt_handler; |
4fa8e287486e
Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents:
3649
diff
changeset
|
195 sigemptyset(&act.sa_mask); |
4fa8e287486e
Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents:
3649
diff
changeset
|
196 act.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER; |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
2571
diff
changeset
|
197 |
4550
592a720e8857
Silently disable dnotify if kernel doesn't support RT signals, instead of
Timo Sirainen <tss@iki.fi>
parents:
4156
diff
changeset
|
198 if (sigaction(SIGRTMIN, &act, NULL) < 0) { |
592a720e8857
Silently disable dnotify if kernel doesn't support RT signals, instead of
Timo Sirainen <tss@iki.fi>
parents:
4156
diff
changeset
|
199 if (errno == EINVAL) { |
592a720e8857
Silently disable dnotify if kernel doesn't support RT signals, instead of
Timo Sirainen <tss@iki.fi>
parents:
4156
diff
changeset
|
200 /* kernel is too old to understand even RT |
592a720e8857
Silently disable dnotify if kernel doesn't support RT signals, instead of
Timo Sirainen <tss@iki.fi>
parents:
4156
diff
changeset
|
201 signals, so there's no way dnotify works */ |
7459
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
202 ioloop_dnotify_disable(ctx); |
4550
592a720e8857
Silently disable dnotify if kernel doesn't support RT signals, instead of
Timo Sirainen <tss@iki.fi>
parents:
4156
diff
changeset
|
203 } else { |
592a720e8857
Silently disable dnotify if kernel doesn't support RT signals, instead of
Timo Sirainen <tss@iki.fi>
parents:
4156
diff
changeset
|
204 i_fatal("sigaction(SIGRTMIN) failed: %m"); |
592a720e8857
Silently disable dnotify if kernel doesn't support RT signals, instead of
Timo Sirainen <tss@iki.fi>
parents:
4156
diff
changeset
|
205 } |
592a720e8857
Silently disable dnotify if kernel doesn't support RT signals, instead of
Timo Sirainen <tss@iki.fi>
parents:
4156
diff
changeset
|
206 } |
3851
4fa8e287486e
Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents:
3649
diff
changeset
|
207 } |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
208 return ctx; |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
2571
diff
changeset
|
209 } |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
2571
diff
changeset
|
210 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
5018
diff
changeset
|
211 void io_loop_notify_handler_deinit(struct ioloop *ioloop) |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
2571
diff
changeset
|
212 { |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
2571
diff
changeset
|
213 struct ioloop_notify_handler_context *ctx = |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
2571
diff
changeset
|
214 ioloop->notify_handler_context; |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
2571
diff
changeset
|
215 |
7459
63cd8ec2007a
Better error handling for dnotify failures.
Timo Sirainen <tss@iki.fi>
parents:
7457
diff
changeset
|
216 ioloop_dnotify_disable(ctx); |
3482
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
2571
diff
changeset
|
217 i_free(ctx); |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
2571
diff
changeset
|
218 } |
465c465c66be
Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents:
2571
diff
changeset
|
219 |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
220 #endif |