1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
--- server/gam_kqueue.c.orig 2007-07-04 09:50:41.000000000 -0400
+++ server/gam_kqueue.c 2009-06-03 13:43:38.000000000 -0400
@@ -5,7 +5,8 @@
*
* * http://techpubs.sgi.com/library/tpl/cgi-bin/getdoc.cgi?coll=0650&db=bks&fname=/SGI_Developer/books/IIDsktp_IG/sgi_html/ch08.html
* states that FAM does not follow monitored symbolic links: we
- * do the same (note that regarding
+ * do NOT do the same to preserve compatibility with SGI FAM (note
+ * that regarding
* http://oss.sgi.com/bugzilla/show_bug.cgi?id=405, we do what
* FAM should do: we do not call g_dir_open() if the file is a
* symbolic link).
@@ -28,10 +29,9 @@
* - kqueue needs to be augmented with a filename-based
* monitoring facility;
*
- * - kqueue needs to be moved out the UFS code.
- *
* Copyright (C) 2005 Joe Marcus Clarke <marcus@FreeBSD.org>
- * Copyright (C) 2005 Jean-Yves Lefort <jylefort@FreeBSD.org>
+ * Copyright (C) 2005, 2006 Jean-Yves Lefort <jylefort@FreeBSD.org>
+ * Copyright (C) 2006 Alex Dupre <ale@FreeBSD.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -53,6 +53,9 @@
#include <fcntl.h>
#include <unistd.h>
#include <sys/param.h>
+#ifndef USE_GAMIN_POLLER
+#include <sys/mount.h>
+#endif
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/stat.h>
@@ -62,6 +65,7 @@
#include "gam_error.h"
#include "gam_kqueue.h"
#include "gam_event.h"
+#include "gam_excludes.h"
#include "gam_server.h"
#include "gam_poll_basic.h"
@@ -130,7 +134,7 @@ typedef struct
HashTableRemoveFunc remove;
HashTablePostRemoveFunc post_remove;
} HashTableMethods;
-
+
/*
* A hash table which can be modified while iterating over it.
*/
@@ -281,8 +285,8 @@ static void
gam_kqueue_mini_lstat (const char *pathname, MiniStat *mini_sb)
{
struct stat sb;
-
- if (lstat(pathname, &sb) < 0)
+
+ if (stat(pathname, &sb) < 0)
memset(mini_sb, 0, sizeof(*mini_sb));
else
{
@@ -319,14 +323,14 @@ gam_kqueue_isdir (const char *pathname,
else
{
struct stat sb;
- return lstat(pathname, &sb) >= 0 && (sb.st_mode & S_IFDIR) != 0;
+ return stat(pathname, &sb) >= 0 && (sb.st_mode & S_IFDIR) != 0;
}
}
static gboolean
gam_kqueue_get_uint_sysctl (const char *name, unsigned int *value)
{
- unsigned int value_len = sizeof(*value);
+ size_t value_len = sizeof(*value);
if (sysctlbyname(name, value, &value_len, (void *)NULL, 0) < 0)
{
@@ -406,7 +410,7 @@ gam_kqueue_hash_table_foreach (HashTable
table->iterating = TRUE;
g_hash_table_foreach(table->main, func, user_data);
table->iterating = FALSE;
-
+
if (table->pending_additions)
{
GSList *l;
@@ -509,33 +513,52 @@ static gboolean
gam_kqueue_monitor_enable_kqueue (Monitor *mon)
{
struct kevent ev[1];
+#ifndef USE_GAMIN_POLLER
+ struct statfs sb;
+#endif
if (open_files == max_open_files)
{
GAM_DEBUG(DEBUG_INFO, "cannot open %s (max_open_files limit reached), falling back to poll\n", mon->pathname);
return FALSE;
}
-
- mon->fd = open(mon->pathname, O_RDONLY | O_NOFOLLOW);
+
+ if (gam_exclude_check(mon->pathname))
+ {
+ GAM_DEBUG(DEBUG_INFO, "not using kqueue for %s since it is excluded, falling back to poll\n", mon->pathname);
+ return FALSE;
+ }
+
+ mon->fd = open(mon->pathname, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
if (mon->fd < 0)
{
GAM_DEBUG(DEBUG_INFO, "cannot open %s (%s), falling back to poll\n", mon->pathname, g_strerror(errno));
return FALSE;
}
+#ifndef USE_GAMIN_POLLER
+ if (fstatfs(mon->fd, &sb) == 0 && (sb.f_flags & MNT_LOCAL) == 0)
+ {
+ GAM_DEBUG(DEBUG_INFO, "%s resides on a remote file system, falling back to poll\n", mon->pathname);
+ goto poll;
+ }
+#endif
+
EV_SET(ev, mon->fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR, VN_NOTE_ALL, 0, mon);
if (kevent(kq, ev, G_N_ELEMENTS(ev), NULL, 0, NULL) < 0)
{
GAM_DEBUG(DEBUG_INFO, "cannot enable kqueue notification for %s (%s), falling back to poll\n", mon->pathname, g_strerror(errno));
-
- close(mon->fd);
- mon->fd = -1;
-
- return FALSE;
+ goto poll;
}
open_files++;
return TRUE;
+
+ poll:
+ close(mon->fd);
+ mon->fd = -1;
+
+ return FALSE;
}
static void
@@ -612,7 +635,7 @@ gam_kqueue_sub_monitor_free (SubMonitor
gam_kqueue_poller_remove_sub_monitor(&missing_smon_poller, smon);
gam_kqueue_poller_remove_sub_monitor(&unsupported_smon_poller, smon);
/* unsupported_dirs_poller is handled by _clear_fmons() below */
-
+
gam_kqueue_sub_monitor_clear_fmons(smon);
gam_kqueue_monitor_free(MONITOR(smon));
}
@@ -700,7 +723,7 @@ gam_kqueue_sub_monitor_enable_notificati
{
struct stat sb;
- exists = lstat(mon->pathname, &sb) >= 0;
+ exists = stat(mon->pathname, &sb) >= 0;
flags |= (exists && (sb.st_mode & S_IFDIR) != 0) ? MONITOR_ISDIR : MONITOR_ISNOTDIR;
}
@@ -715,21 +738,21 @@ gam_kqueue_sub_monitor_enable_notificati
{
GDir *dir;
GError *err = NULL;
-
+
dir = g_dir_open(mon->pathname, 0, &err);
if (dir)
{
const char *filename;
-
+
while ((filename = g_dir_read_name(dir)))
{
FileMonitor *fmon;
FileMonitorFlags fmon_flags;
-
+
fmon = gam_kqueue_file_monitor_new(smon, filename, &fmon_flags);
gam_kqueue_file_monitor_emit_event(fmon, gevent, fmon_flags);
}
-
+
g_dir_close(dir);
}
else
@@ -749,7 +772,7 @@ gam_kqueue_sub_monitor_enable_notificati
return;
}
-
+
/* then we enable kqueue notification, falling back to poll if necessary */
if (! gam_kqueue_monitor_enable_kqueue(mon))
@@ -774,7 +797,7 @@ gam_kqueue_sub_monitor_handle_directory_
filenames = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
- if (isdir) /* do not follow symbolic links */
+ if (isdir)
{
GDir *dir;
GError *err = NULL;
@@ -783,7 +806,7 @@ gam_kqueue_sub_monitor_handle_directory_
if (dir)
{
const char *filename;
-
+
while ((filename = g_dir_read_name(dir)))
{
g_hash_table_insert(filenames, g_strdup(filename), GINT_TO_POINTER(TRUE));
@@ -793,12 +816,12 @@ gam_kqueue_sub_monitor_handle_directory_
{
FileMonitor *fmon;
FileMonitorFlags fmon_flags;
-
+
fmon = gam_kqueue_file_monitor_new(smon, filename, &fmon_flags);
gam_kqueue_file_monitor_emit_event(fmon, GAMIN_EVENT_CREATED, fmon_flags);
}
}
-
+
g_dir_close(dir);
}
else
@@ -840,6 +863,8 @@ gam_kqueue_sub_monitor_emit_event (SubMo
case GAMIN_EVENT_MOVED:
gam_kqueue_sub_monitor_set_missing(smon);
break;
+ default:
+ break;
}
gam_server_emit_event(mon->pathname, isdir, event, smon->subs, 1);
@@ -931,11 +956,11 @@ gam_kqueue_file_monitor_emit_event (File
gboolean isdir;
gboolean stat_done;
gboolean stat_succeeded;
-
+
if ((flags & MONITOR_ISDIR) == 0 && (flags & MONITOR_ISNOTDIR) == 0)
{
stat_done = TRUE;
- stat_succeeded = lstat(mon->pathname, &sb) >= 0;
+ stat_succeeded = stat(mon->pathname, &sb) >= 0;
isdir = stat_succeeded && (sb.st_mode & S_IFDIR) != 0;
}
else
@@ -943,7 +968,7 @@ gam_kqueue_file_monitor_emit_event (File
stat_done = FALSE;
isdir = (flags & MONITOR_ISDIR) != 0;
}
-
+
gam_server_emit_event(mon->pathname, isdir, event, fmon->smon->subs, 1);
switch (event)
@@ -962,7 +987,7 @@ gam_kqueue_file_monitor_emit_event (File
{
FileMonitor *new_fmon;
FileMonitorFlags new_fmon_flags;
-
+
/*
* The file exists again. It means that kqueue has
* aggregated a removal+creation into a single event. We
@@ -978,9 +1003,11 @@ gam_kqueue_file_monitor_emit_event (File
break; /* do not remove the fmon we've just created */
}
}
-
+
gam_kqueue_hash_table_remove(fmon->smon->fmons, fmon);
break;
+ default:
+ break;
}
}
@@ -1033,7 +1060,7 @@ gam_kqueue_kevent_cb (GIOChannel *source
for (i = 0; i < nevents; i++)
MONITOR(ev[i].udata)->handle_kevent(MONITOR(ev[i].udata), &ev[i]);
-
+
return TRUE; /* keep source */
}
@@ -1042,7 +1069,7 @@ gam_kqueue_missing_smon_poll (SubMonitor
{
struct stat sb;
- if (lstat(MONITOR(smon)->pathname, &sb) >= 0)
+ if (stat(MONITOR(smon)->pathname, &sb) >= 0)
{
gam_kqueue_poller_remove_sub_monitor(&missing_smon_poller, smon);
gam_kqueue_sub_monitor_enable_notification(smon, SUB_MONITOR_WAS_MISSING | ((sb.st_mode & S_IFDIR) != 0 ? MONITOR_ISDIR : MONITOR_ISNOTDIR));
@@ -1062,16 +1089,16 @@ gam_kqueue_unsupported_smon_poll (SubMon
if (gam_kqueue_monitor_enable_kqueue(mon))
gam_kqueue_poller_remove_sub_monitor(&missing_smon_poller, smon);
}
-
+
gam_kqueue_mini_lstat(mon->pathname, &sb);
-
+
if (! sb.exists && mon->sb.exists)
event = GAMIN_EVENT_DELETED;
else if (gam_kqueue_differs(&sb, &mon->sb))
event = GAMIN_EVENT_CHANGED;
else
return;
-
+
memcpy(&mon->sb, &sb, sizeof(sb));
gam_kqueue_sub_monitor_emit_event(smon, event, (sb.mode & S_IFDIR) != 0 ? MONITOR_ISDIR : MONITOR_ISNOTDIR);
}
@@ -1167,7 +1194,10 @@ gam_kqueue_init (void)
channel = g_io_channel_unix_new(kq);
g_io_add_watch(channel, G_IO_IN, gam_kqueue_kevent_cb, NULL);
-
+#ifdef USE_GAMIN_POLLER
+ gam_poll_basic_init ();
+#endif
+
gam_server_install_kernel_hooks(GAMIN_K_KQUEUE,
gam_kqueue_add_subscription,
gam_kqueue_remove_subscription,
@@ -1200,7 +1230,7 @@ gam_kqueue_add_subscription (GamSubscrip
smon->subs = g_list_append(smon->subs, sub);
return TRUE;
}
-
+
smon = gam_kqueue_sub_monitor_new(sub);
smon->subs = g_list_append(smon->subs, sub);
@@ -1260,6 +1290,6 @@ gam_kqueue_remove_all_for (GamListener *
success = FALSE;
g_list_free(subs);
-
+
return success;
}
|