annotate src/lib/ioloop-notify-dn.c @ 6429:65c69a53a7be HEAD

Replaced my Copyright notices. The year range always ends with 2007 now. My name was replaced with "Dovecot authors". In many cases I didn't really even own the copyright, so this is more correct.
author Timo Sirainen <tss@iki.fi>
date Sun, 16 Sep 2007 14:34:22 +0300
parents 6a64e64fa3a3
children 766250fcedd8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6429
65c69a53a7be Replaced my Copyright notices. The year range always ends with 2007 now.
Timo Sirainen <tss@iki.fi>
parents: 6411
diff changeset
1 /* Copyright (c) 2003-2007 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) {
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
91 if (errno != ENOENT)
772f4e8bd2a9 Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents: 5248
diff changeset
92 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
93 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
94 }
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
95
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
96 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
97 /* 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
98 if (errno != EINVAL)
3634
377f69be914f Fixed fd leak
Timo Sirainen <tss@iki.fi>
parents: 3621
diff changeset
99 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
100 ctx->disabled = TRUE;
3634
377f69be914f Fixed fd leak
Timo Sirainen <tss@iki.fi>
parents: 3621
diff changeset
101 (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
102 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
103 }
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
104 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
105 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
106 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
107 /* 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
108 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
109 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
110 } 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
111 /* 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
112 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
113 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
114 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
115 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
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 (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
118 (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
119 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
120 }
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
121
3485
b3e135c37f06 If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents: 3482
diff changeset
122 if (ctx->event_io == NULL) {
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
123 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
124 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
125 }
b3e135c37f06 If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents: 3482
diff changeset
126
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
127 *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
128 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
129 }
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
130
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
131 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
132 {
3482
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
133 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
134 ioloop->notify_handler_context;
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
135 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
136
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
137 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
138 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
139 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
140 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
141 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
142 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
143
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
144 io_notify_fd_free(&ctx->fd_ctx, io);
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
145
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
146 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
147 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
148 }
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
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
150 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
151 {
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
152 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
153 struct sigaction act;
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
154
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
155 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
156 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
157
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
158 if (pipe(ctx->event_pipe) < 0) {
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
159 ctx->disabled = TRUE;
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
160 i_error("dnotify: pipe() failed: %m");
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
161 return ctx;
3482
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
162 }
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
163
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
164 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
165 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
166
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
167 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
168 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
169
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
170 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
171 /* 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
172 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
173
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
174 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
175 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
176 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
177
4550
592a720e8857 Silently disable dnotify if kernel doesn't support RT signals, instead of
Timo Sirainen <tss@iki.fi>
parents: 4156
diff changeset
178 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
179 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
180 /* 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
181 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
182 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
183 } else {
592a720e8857 Silently disable dnotify if kernel doesn't support RT signals, instead of
Timo Sirainen <tss@iki.fi>
parents: 4156
diff changeset
184 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
185 }
592a720e8857 Silently disable dnotify if kernel doesn't support RT signals, instead of
Timo Sirainen <tss@iki.fi>
parents: 4156
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 }
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
188 return ctx;
3482
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
189 }
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
190
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
191 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
192 {
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
193 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
194 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
195
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
196 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
197 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
198
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
199 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
200 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
201 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
202 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
203
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
204 i_free(ctx);
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
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
207 #endif