annotate src/lib/ioloop-notify-dn.c @ 5248:12ac5f685814 HEAD

Various cleanups to ioloop code.
author Timo Sirainen <tss@iki.fi>
date Fri, 09 Mar 2007 00:04:21 +0200
parents 9b8c3efa85ec
children 772f4e8bd2a9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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
1 /* Copyright (C) 2003 Timo Sirainen */
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
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
32 static void sigrt_handler(int signo __attr_unused__, siginfo_t *si,
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
33 void *data __attr_unused__)
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
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
75 struct io *io_add_notify(const char *path, io_callback_t *callback,
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
76 void *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
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;
3534
a9be1824403b New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents: 3492
diff changeset
80 int fd;
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
81
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
82 if (ctx == NULL)
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
83 ctx = io_loop_notify_handler_init();
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
84 if (ctx->disabled)
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
85 return NULL;
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
86
3534
a9be1824403b New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents: 3492
diff changeset
87 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
88 if (fd == -1) {
a9be1824403b New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents: 3492
diff changeset
89 i_error("open(%s) for dnotify failed: %m", path);
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
90 return NULL;
3534
a9be1824403b New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents: 3492
diff changeset
91 }
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
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 if (fcntl(fd, F_SETSIG, SIGRTMIN) < 0) {
3485
b3e135c37f06 If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents: 3482
diff changeset
94 if (errno == EINVAL) {
5018
9b8c3efa85ec Disable dnotify silently if it's not supported by the kernel. We were
Timo Sirainen <tss@iki.fi>
parents: 4903
diff changeset
95 /* not supported, disable dnotify */
3485
b3e135c37f06 If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents: 3482
diff changeset
96 ctx->disabled = TRUE;
3634
377f69be914f Fixed fd leak
Timo Sirainen <tss@iki.fi>
parents: 3621
diff changeset
97 } else {
377f69be914f Fixed fd leak
Timo Sirainen <tss@iki.fi>
parents: 3621
diff changeset
98 i_error("fcntl(F_SETSIG) failed: %m");
3485
b3e135c37f06 If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents: 3482
diff changeset
99 }
3634
377f69be914f Fixed fd leak
Timo Sirainen <tss@iki.fi>
parents: 3621
diff changeset
100 (void)close(fd);
3534
a9be1824403b New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents: 3492
diff changeset
101 return 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
102 }
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 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
104 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
105 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
106 /* 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
107 fail silently. */
9b8c3efa85ec Disable dnotify silently if it's not supported by the kernel. We were
Timo Sirainen <tss@iki.fi>
parents: 4903
diff changeset
108 } else if (errno == EINVAL) {
9b8c3efa85ec Disable dnotify silently if it's not supported by the kernel. We were
Timo Sirainen <tss@iki.fi>
parents: 4903
diff changeset
109 /* dnotify not in kernel. disable it. */
9b8c3efa85ec Disable dnotify silently if it's not supported by the kernel. We were
Timo Sirainen <tss@iki.fi>
parents: 4903
diff changeset
110 ctx->disabled = TRUE;
9b8c3efa85ec Disable dnotify silently if it's not supported by the kernel. We were
Timo Sirainen <tss@iki.fi>
parents: 4903
diff changeset
111 } else {
3534
a9be1824403b New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents: 3492
diff changeset
112 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
113 }
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
114 (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
115 (void)close(fd);
a9be1824403b New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents: 3492
diff changeset
116 return 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
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
3485
b3e135c37f06 If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents: 3482
diff changeset
119 if (ctx->event_io == NULL) {
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
120 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
121 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
122 }
b3e135c37f06 If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents: 3482
diff changeset
123
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
124 return io_notify_fd_add(&ctx->fd_ctx, fd, callback, 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
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
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
127 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
128 {
3482
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
129 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
130 ioloop->notify_handler_context;
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
131 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
132
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
133 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
134 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
135 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
136 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
137 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
138 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
139
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
140 io_notify_fd_free(&ctx->fd_ctx, io);
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
141
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
142 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
143 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
144 }
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 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
147 {
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
148 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
149 struct sigaction act;
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
150
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
151 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
152 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
153
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
154 if (pipe(ctx->event_pipe) < 0) {
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
155 ctx->disabled = TRUE;
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
156 i_error("dnotify: pipe() failed: %m");
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
157 return ctx;
3482
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
158 }
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 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
161 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
162
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
163 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
164 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
165
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
166 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
167 /* 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
168 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
169
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
170 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
171 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
172 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
173
4550
592a720e8857 Silently disable dnotify if kernel doesn't support RT signals, instead of
Timo Sirainen <tss@iki.fi>
parents: 4156
diff changeset
174 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
175 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
176 /* 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
177 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
178 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
179 } else {
592a720e8857 Silently disable dnotify if kernel doesn't support RT signals, instead of
Timo Sirainen <tss@iki.fi>
parents: 4156
diff changeset
180 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
181 }
592a720e8857 Silently disable dnotify if kernel doesn't support RT signals, instead of
Timo Sirainen <tss@iki.fi>
parents: 4156
diff changeset
182 }
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
183 }
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
184 return ctx;
3482
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
185 }
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
186
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 5018
diff changeset
187 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
188 {
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
189 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
190 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
191
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
192 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
193 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
194
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
195 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
196 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
197 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
198 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
199
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
200 i_free(ctx);
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
201 }
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
202
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
203 #endif