annotate src/lib/ioloop-notify-dn.c @ 4903:204d7edc7cdc HEAD

Added context parameter type safety checks for most callback APIs.
author Timo Sirainen <tss@iki.fi>
date Fri, 15 Dec 2006 18:55:29 +0200
parents 592a720e8857
children 9b8c3efa85ec
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"
3621
3ae2df67459c Added fd_set_nonblock() and changed net_set_nonblock() to use it.
Timo Sirainen <tss@iki.fi>
parents: 3534
diff changeset
11 #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
12 #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
13
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 #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
15 #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
16 #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
17
3482
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
18 struct 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
19 struct io *event_io;
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
20 bool disabled;
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
21
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
22 int event_pipe[2];
3482
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
23 };
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
24
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
25 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
26
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
27 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
28 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
29 {
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
30 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
31 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
32 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
33 int ret;
cc2127b799c5 Ignore SIGIO which gets sent if queue gets full. Also other small
Timo Sirainen <tss@iki.fi>
parents: 2568
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 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
36 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
37 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
38
cc2127b799c5 Ignore SIGIO which gets sent if queue gets full. Also other small
Timo Sirainen <tss@iki.fi>
parents: 2568
diff changeset
39 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
40
d39ac5712d2e Make sure errno is restored when exiting SIGRT signal handler.
Timo Sirainen <tss@iki.fi>
parents: 3959
diff changeset
41 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
42 }
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
43
4903
204d7edc7cdc Added context parameter type safety checks for most callback APIs.
Timo Sirainen <tss@iki.fi>
parents: 4550
diff changeset
44 static void event_callback(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
45 {
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
46 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
47 ioloop->notify_handler_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
48 struct io *io;
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
49 int fd, ret;
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 ret = read(ctx->event_pipe[0], &fd, sizeof(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
52 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
53 i_fatal("read(event_pipe) failed: %m");
3649
8c63af0ab91a Fixed some printf-type mismatches
Timo Sirainen <tss@iki.fi>
parents: 3634
diff changeset
54 if (ret != sizeof(fd)) {
8c63af0ab91a Fixed some printf-type mismatches
Timo Sirainen <tss@iki.fi>
parents: 3634
diff changeset
55 i_fatal("read(event_pipe) returned %d != %"PRIuSIZE_T,
8c63af0ab91a Fixed some printf-type mismatches
Timo Sirainen <tss@iki.fi>
parents: 3634
diff changeset
56 ret, sizeof(fd));
8c63af0ab91a Fixed some printf-type mismatches
Timo Sirainen <tss@iki.fi>
parents: 3634
diff changeset
57 }
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
58
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
59 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
60 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
61 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
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 for (io = ioloop->notifys; io != NULL; io = io->next) {
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 if (io->fd == fd) {
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 io->callback(io->context);
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 break;
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
67 }
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
68 }
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
69 }
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
70
3534
a9be1824403b New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents: 3492
diff changeset
71 struct io *io_loop_notify_add(struct ioloop *ioloop, const char *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
72 io_callback_t *callback, void *context)
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 {
3482
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
74 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
75 ioloop->notify_handler_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
76 struct io *io;
3534
a9be1824403b New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents: 3492
diff changeset
77 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
78
3534
a9be1824403b New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents: 3492
diff changeset
79 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
80 if (fd == -1) {
a9be1824403b New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents: 3492
diff changeset
81 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
82 return NULL;
3534
a9be1824403b New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents: 3492
diff changeset
83 }
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
84
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
85 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
86 if (errno == EINVAL) {
b3e135c37f06 If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents: 3482
diff changeset
87 /* dnotify not in kernel. disable it. */
b3e135c37f06 If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents: 3482
diff changeset
88 ctx->disabled = TRUE;
3634
377f69be914f Fixed fd leak
Timo Sirainen <tss@iki.fi>
parents: 3621
diff changeset
89 } else {
377f69be914f Fixed fd leak
Timo Sirainen <tss@iki.fi>
parents: 3621
diff changeset
90 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
91 }
3634
377f69be914f Fixed fd leak
Timo Sirainen <tss@iki.fi>
parents: 3621
diff changeset
92 (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
93 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
94 }
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 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
96 DN_MULTISHOT) < 0) {
3534
a9be1824403b New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents: 3492
diff changeset
97 /* we fail here if we're trying to add dnotify to
a9be1824403b New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents: 3492
diff changeset
98 non-directory fd. fail silently in that case. */
a9be1824403b New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents: 3492
diff changeset
99 if (errno != ENOTDIR)
a9be1824403b New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents: 3492
diff changeset
100 i_error("fcntl(F_NOTIFY) 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
101 (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
102 (void)close(fd);
a9be1824403b New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents: 3492
diff changeset
103 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
104 }
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
3485
b3e135c37f06 If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents: 3482
diff changeset
106 if (ctx->event_io == NULL) {
b3e135c37f06 If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents: 3482
diff changeset
107 ctx->event_io =
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
108 io_add(ctx->event_pipe[0], IO_READ,
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
109 event_callback, 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
110 }
b3e135c37f06 If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents: 3482
diff changeset
111
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
112 io = p_new(ioloop->pool, struct io, 1);
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
113 io->fd = fd;
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
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
115 io->callback = callback;
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
116 io->context = context;
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 return io;
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 }
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
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 void io_loop_notify_remove(struct ioloop *ioloop, struct io *io)
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 {
3482
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
122 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
123 ioloop->notify_handler_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
124
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
125 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
126 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
127 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
128 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
129 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
130 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
131
3879
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3863
diff changeset
132 if (ioloop->notifys == NULL)
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3863
diff changeset
133 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
134 }
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
3482
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
136 void io_loop_notify_handler_init(struct ioloop *ioloop)
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
137 {
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
138 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
139 struct sigaction act;
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
140
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
141 ctx = 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
142 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
143
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
144 if (pipe(ctx->event_pipe) < 0) {
3482
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
145 i_fatal("pipe() failed: %m");
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
146 return;
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
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
149 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
150 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
151
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
152 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
153 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
154
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
155 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
156 /* 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
157 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
158
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
159 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
160 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
161 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
162
4550
592a720e8857 Silently disable dnotify if kernel doesn't support RT signals, instead of
Timo Sirainen <tss@iki.fi>
parents: 4156
diff changeset
163 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
164 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
165 /* 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
166 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
167 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
168 } else {
592a720e8857 Silently disable dnotify if kernel doesn't support RT signals, instead of
Timo Sirainen <tss@iki.fi>
parents: 4156
diff changeset
169 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
170 }
592a720e8857 Silently disable dnotify if kernel doesn't support RT signals, instead of
Timo Sirainen <tss@iki.fi>
parents: 4156
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 }
3482
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
173 }
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
174
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
175 void io_loop_notify_handler_deinit(struct ioloop *ioloop __attr_unused__)
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
176 {
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
177 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
178 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
179
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
180 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
181 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
182
3851
4fa8e287486e Dnotify crashed if we tried to use multiple I/O loops.
Timo Sirainen <tss@iki.fi>
parents: 3649
diff changeset
183 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
184 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
185 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
186 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
187
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
188 i_free(ctx);
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
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
191 #endif