annotate src/lib/ioloop-notify-dn.c @ 7086:7ed926ed7aa4 HEAD

Updated copyright notices to include year 2008.
author Timo Sirainen <tss@iki.fi>
date Tue, 01 Jan 2008 22:05:21 +0200
parents 766250fcedd8
children 940641318f12
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7086
7ed926ed7aa4 Updated copyright notices to include year 2008.
Timo Sirainen <tss@iki.fi>
parents: 6817
diff changeset
1 /* Copyright (c) 2003-2008 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
6411
6a64e64fa3a3 Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents: 5937
diff changeset
32 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
33 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
34 {
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
35 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
36 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
37 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
38 int ret;
cc2127b799c5 Ignore SIGIO which gets sent if queue gets full. Also other small
Timo Sirainen <tss@iki.fi>
parents: 2568
diff changeset
39
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
40 ret = write(ctx->event_pipe[1], &si->si_fd, sizeof(int));
2571
cc2127b799c5 Ignore SIGIO which gets sent if queue gets full. Also other small
Timo Sirainen <tss@iki.fi>
parents: 2568
diff changeset
41 if (ret < 0 && errno != EINTR && errno != EAGAIN)
cc2127b799c5 Ignore SIGIO which gets sent if queue gets full. Also other small
Timo Sirainen <tss@iki.fi>
parents: 2568
diff changeset
42 i_fatal("write(event_pipe) failed: %m");
cc2127b799c5 Ignore SIGIO which gets sent if queue gets full. Also other small
Timo Sirainen <tss@iki.fi>
parents: 2568
diff changeset
43
cc2127b799c5 Ignore SIGIO which gets sent if queue gets full. Also other small
Timo Sirainen <tss@iki.fi>
parents: 2568
diff changeset
44 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
45
d39ac5712d2e Make sure errno is restored when exiting SIGRT signal handler.
Timo Sirainen <tss@iki.fi>
parents: 3959
diff changeset
46 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
47 }
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
48
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
49 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
50 {
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
51 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
52 ioloop->notify_handler_context;
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
53 struct io_notify *io;
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
54 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
55
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
56 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
57 if (ret < 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
58 i_fatal("read(event_pipe) failed: %m");
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
59 if ((ret % sizeof(fd_buf[0])) != 0)
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
60 i_fatal("read(event_pipe) returned %d", ret);
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
61 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
62
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
63 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
64 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
65 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
66
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
67 for (i = 0; i < ret; i++) {
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
68 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
69 if (io != NULL)
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
70 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
71 }
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
72 }
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
73
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
74 #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
75 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
76 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
77 {
3482
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
78 struct ioloop_notify_handler_context *ctx =
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
79 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: 5248
diff changeset
80 int fd, ret;
772f4e8bd2a9 Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents: 5248
diff changeset
81
772f4e8bd2a9 Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents: 5248
diff changeset
82 *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
83
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
84 if (ctx == NULL)
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
85 ctx = io_loop_notify_handler_init();
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
86 if (ctx->disabled)
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
87 return IO_NOTIFY_DISABLED;
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
88
3534
a9be1824403b New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents: 3492
diff changeset
89 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
90 if (fd == -1) {
6817
766250fcedd8 Ignore ESTALE errors.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
91 /* ESTALE could happen with NFS. Don't bother giving an error
766250fcedd8 Ignore ESTALE errors.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
92 message then. */
766250fcedd8 Ignore ESTALE errors.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
93 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
94 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
95 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
96 }
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
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
98 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
99 /* 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
100 if (errno != EINVAL)
3634
377f69be914f Fixed fd leak
Timo Sirainen <tss@iki.fi>
parents: 3621
diff changeset
101 i_error("fcntl(F_SETSIG) failed: %m");
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
102 ctx->disabled = TRUE;
3634
377f69be914f Fixed fd leak
Timo Sirainen <tss@iki.fi>
parents: 3621
diff changeset
103 (void)close(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
104 return IO_NOTIFY_DISABLED;
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
105 }
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
106 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
107 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
108 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
109 /* 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
110 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
111 ret = IO_NOTIFY_NOTFOUND;
772f4e8bd2a9 Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents: 5248
diff changeset
112 } 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
113 /* 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
114 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
115 i_error("fcntl(F_NOTIFY) failed: %m");
5018
9b8c3efa85ec Disable dnotify silently if it's not supported by the kernel. We were
Timo Sirainen <tss@iki.fi>
parents: 4903
diff changeset
116 ctx->disabled = TRUE;
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
117 ret = IO_NOTIFY_DISABLED;
5018
9b8c3efa85ec Disable dnotify silently if it's not supported by the kernel. We were
Timo Sirainen <tss@iki.fi>
parents: 4903
diff changeset
118 }
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
119 (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
120 (void)close(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
121 return 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
122 }
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
123
3485
b3e135c37f06 If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents: 3482
diff changeset
124 if (ctx->event_io == NULL) {
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
125 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
126 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
127 }
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: 5248
diff changeset
129 *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
130 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
131 }
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
132
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
133 void io_loop_notify_remove(struct ioloop *ioloop, 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
134 {
3482
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
135 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
136 ioloop->notify_handler_context;
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
137 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
138
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
139 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
140 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
141 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
142 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
143 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
144 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
145
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
146 io_notify_fd_free(&ctx->fd_ctx, io);
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
147
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
148 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
149 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
150 }
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
151
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
152 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
153 {
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
154 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
155 struct sigaction act;
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
156
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
157 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
158 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
159
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
160 if (pipe(ctx->event_pipe) < 0) {
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
161 ctx->disabled = TRUE;
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
162 i_error("dnotify: pipe() failed: %m");
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
163 return ctx;
3482
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
164 }
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
165
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
166 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
167 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
168
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
169 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
170 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
171
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
172 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
173 /* 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
174 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
175
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
176 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
177 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
178 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
179
4550
592a720e8857 Silently disable dnotify if kernel doesn't support RT signals, instead of
Timo Sirainen <tss@iki.fi>
parents: 4156
diff changeset
180 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
181 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
182 /* 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
183 signals, so there's no way dnotify works */
592a720e8857 Silently disable dnotify if kernel doesn't support RT signals, instead of
Timo Sirainen <tss@iki.fi>
parents: 4156
diff changeset
184 ctx->disabled = TRUE;
592a720e8857 Silently disable dnotify if kernel doesn't support RT signals, instead of
Timo Sirainen <tss@iki.fi>
parents: 4156
diff changeset
185 } else {
592a720e8857 Silently disable dnotify if kernel doesn't support RT signals, instead of
Timo Sirainen <tss@iki.fi>
parents: 4156
diff changeset
186 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
187 }
592a720e8857 Silently disable dnotify if kernel doesn't support RT signals, instead of
Timo Sirainen <tss@iki.fi>
parents: 4156
diff changeset
188 }
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
189 }
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
190 return ctx;
3482
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
191 }
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
192
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
193 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
194 {
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
195 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
196 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
197
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
198 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
199 signal(SIGRTMIN, SIG_IGN);
3482
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
200
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
201 if (close(ctx->event_pipe[0]) < 0)
3482
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
202 i_error("close(event_pipe[0]) failed: %m");
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
203 if (close(ctx->event_pipe[1]) < 0)
3482
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
204 i_error("close(event_pipe[1]) failed: %m");
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
205
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
206 i_free(ctx);
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
207 }
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
208
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
209 #endif