annotate src/lib/ioloop-notify-dn.c @ 3621:3ae2df67459c HEAD

Added fd_set_nonblock() and changed net_set_nonblock() to use it.
author Timo Sirainen <tss@iki.fi>
date Sun, 25 Sep 2005 14:12:24 +0300
parents a9be1824403b
children 377f69be914f
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;
3485
b3e135c37f06 If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents: 3482
diff changeset
20 int disabled;
3482
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
21 };
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
22
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
23 static int event_pipe[2] = { -1, -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
24
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
25 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
26 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
27 {
2571
cc2127b799c5 Ignore SIGIO which gets sent if queue gets full. Also other small
Timo Sirainen <tss@iki.fi>
parents: 2568
diff changeset
28 int ret;
cc2127b799c5 Ignore SIGIO which gets sent if queue gets full. Also other small
Timo Sirainen <tss@iki.fi>
parents: 2568
diff changeset
29
cc2127b799c5 Ignore SIGIO which gets sent if queue gets full. Also other small
Timo Sirainen <tss@iki.fi>
parents: 2568
diff changeset
30 ret = write(event_pipe[1], &si->si_fd, sizeof(int));
cc2127b799c5 Ignore SIGIO which gets sent if queue gets full. Also other small
Timo Sirainen <tss@iki.fi>
parents: 2568
diff changeset
31 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
32 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
33
cc2127b799c5 Ignore SIGIO which gets sent if queue gets full. Also other small
Timo Sirainen <tss@iki.fi>
parents: 2568
diff changeset
34 i_assert(ret <= 0 || ret == sizeof(int));
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
35 }
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
36
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
37 static void event_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
38 {
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
39 struct ioloop *ioloop = 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
40 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
41 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
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 ret = read(event_pipe[0], &fd, sizeof(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
44 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
45 i_fatal("read(event_pipe) failed: %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
46 if (ret != sizeof(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
47 i_fatal("read(event_pipe) returned %d != %d", ret, sizeof(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
48
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 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
50 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
51 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
52
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 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
54 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
55 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
56 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
57 }
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 }
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
3534
a9be1824403b New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents: 3492
diff changeset
61 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
62 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
63 {
3482
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
64 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
65 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
66 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
67 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
68
3534
a9be1824403b New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents: 3492
diff changeset
69 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
70 if (fd == -1) {
a9be1824403b New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents: 3492
diff changeset
71 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
72 return NULL;
3534
a9be1824403b New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents: 3492
diff changeset
73 }
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
74
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
75 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
76 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
77 /* 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
78 ctx->disabled = TRUE;
b3e135c37f06 If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents: 3482
diff changeset
79 return NULL;
b3e135c37f06 If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents: 3482
diff changeset
80 }
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 i_error("fcntl(F_SETSIG) failed: %m");
3534
a9be1824403b New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents: 3492
diff changeset
82 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
83 }
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 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
85 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
86 /* 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
87 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
88 if (errno != ENOTDIR)
a9be1824403b New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents: 3492
diff changeset
89 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
90 (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
91 (void)close(fd);
a9be1824403b New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents: 3492
diff changeset
92 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
93 }
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
3485
b3e135c37f06 If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents: 3482
diff changeset
95 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
96 ctx->event_io =
b3e135c37f06 If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents: 3482
diff changeset
97 io_add(event_pipe[0], IO_READ, event_callback, ioloop);
b3e135c37f06 If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents: 3482
diff changeset
98 }
b3e135c37f06 If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents: 3482
diff changeset
99
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
100 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
101 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
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 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
104 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
105
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
106 io->next = ioloop->notifys;
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
107 ioloop->notifys = 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
108 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
109 }
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
110
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
111 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
112 {
3482
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
113 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
114 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
115 struct io **io_p;
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
3485
b3e135c37f06 If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents: 3482
diff changeset
117 if (ctx->disabled)
b3e135c37f06 If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents: 3482
diff changeset
118 return;
b3e135c37f06 If dnotify/inotify isn't in kernel, handle the errors silently and without
Timo Sirainen <tss@iki.fi>
parents: 3482
diff changeset
119
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 for (io_p = &ioloop->notifys; *io_p != NULL; io_p = &(*io_p)->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
121 if (*io_p == 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
122 *io_p = 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
123 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
124 }
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
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
127 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
128 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
129 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
130 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
131 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
132 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
133
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 p_free(ioloop->pool, 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
135
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 if (ioloop->notifys == NULL) {
3482
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
137 io_remove(ctx->event_io);
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
138 ctx->event_io = 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
139 }
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
140 }
5bf22d6bb65e Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
141
3482
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
142 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
143 {
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
144 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
145 struct sigaction act;
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
146
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
147 i_assert(event_pipe[0] == -1);
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
148
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
149 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
150 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
151
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
152 if (pipe(event_pipe) < 0) {
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
153 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
154 return;
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
155 }
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
156
3621
3ae2df67459c Added fd_set_nonblock() and changed net_set_nonblock() to use it.
Timo Sirainen <tss@iki.fi>
parents: 3534
diff changeset
157 fd_set_nonblock(event_pipe[0], TRUE);
3ae2df67459c Added fd_set_nonblock() and changed net_set_nonblock() to use it.
Timo Sirainen <tss@iki.fi>
parents: 3534
diff changeset
158 fd_set_nonblock(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
159
3492
ab0fc2c1d8e1 Set close-on-exec flags for opened file descriptors.
Timo Sirainen <tss@iki.fi>
parents: 3485
diff changeset
160 fd_close_on_exec(event_pipe[0], TRUE);
ab0fc2c1d8e1 Set close-on-exec flags for opened file descriptors.
Timo Sirainen <tss@iki.fi>
parents: 3485
diff changeset
161 fd_close_on_exec(event_pipe[1], TRUE);
ab0fc2c1d8e1 Set close-on-exec flags for opened file descriptors.
Timo Sirainen <tss@iki.fi>
parents: 3485
diff changeset
162
3482
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
163 /* SIGIO is sent if queue gets full. we'll just ignore it. */
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
164 signal(SIGIO, SIG_IGN);
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
165
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
166 act.sa_sigaction = sigrt_handler;
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
167 sigemptyset(&act.sa_mask);
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
168 act.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
169
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
170 if (sigaction(SIGRTMIN, &act, NULL) < 0)
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
171 i_fatal("sigaction(SIGRTMIN) failed: %m");
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
172 }
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 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
175 {
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
176 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
177 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
178
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
179 signal(SIGRTMIN, SIG_IGN);
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
180
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
181 if (close(event_pipe[0]) < 0)
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
182 i_error("close(event_pipe[0]) failed: %m");
465c465c66be Added inotify patch by Johannes Berg and did some restructuring to
Timo Sirainen <tss@iki.fi>
parents: 2571
diff changeset
183 if (close(event_pipe[1]) < 0)
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[1]) failed: %m");
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 i_free(ctx);
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
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
189 #endif