summaryrefslogtreecommitdiff
path: root/devel/gamin/files/patch-server_gam_kqueue.c
blob: 9edf541d131be8ccbd26af35c9b2f1c460f3704e (plain) (blame)
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
--- server/gam_kqueue.c.orig	Sat Apr 30 12:26:31 2005
+++ server/gam_kqueue.c	Sun May  1 08:18:47 2005
@@ -0,0 +1,1262 @@
+/*
+ * gam_kqueue.c - a kqueue(2) Gamin backend
+ *
+ * Notes:
+ *
+ *     * 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
+ *       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).
+ *
+ *     * kqueue cannot monitor files residing on anything but a UFS
+ *       file system. If kqueue cannot monitor a file, this backend
+ *       will poll it periodically.
+ *
+ *     * Monitoring a file with kqueue prevents the file system it
+ *       resides on from being unmounted, because kqueue can only
+ *       monitor open files.
+ *
+ *     * The creation of missing monitored files is detected by
+ *       performing a lstat() every second. Although using kqueue to
+ *       monitor the parent directory is technically feasible, it
+ *       would introduce a lot of complexity in the code.
+ *
+ *     * In light of the previous points, it appears that:
+ *
+ *           - 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>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free
+ * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include "config.h"
+#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#include <sys/stat.h>
+#include <sys/event.h>
+#include <sys/time.h>
+#include <errno.h>
+#include "gam_error.h"
+#include "gam_kqueue.h"
+#include "gam_event.h"
+#include "gam_server.h"
+
+/*** tunable constants, modify to tweak the backend aggressivity *************/
+
+/*
+ * The backend will use at most n file descriptors, where n is the
+ * minimum value between (kern.maxfiles * CFG_GLOBAL_FILE_RESERVE_RATIO)
+ * and (kern.maxfilesperproc - CFG_SELF_FILE_RESERVE).
+ */
+#define CFG_GLOBAL_FILE_RESERVE_RATIO			0.7
+#define CFG_SELF_FILE_RESERVE				200
+
+/*
+ * If a SubMonitor or FileMonitor is not supported by kqueue and has
+ * to be polled, the backend will re-attempt to enable kqueue
+ * notification every n polls.
+ */
+#define CFG_UNSUPPORTED_SMON_KQUEUE_RETRY_FREQUENCY	10
+#define CFG_UNSUPPORTED_FMON_KQUEUE_RETRY_FREQUENCY	10
+
+/*
+ * The various poll intervals, in milliseconds. The default interval
+ * for each poller is based on the poller's expected usage.
+ */
+#define CFG_MISSING_SMON_POLL_INTERVAL			1000
+#define CFG_UNSUPPORTED_SMON_POLL_INTERVAL		3000
+#define CFG_UNSUPPORTED_FMON_POLL_INTERVAL		3000
+
+/*** end of tunable constants ************************************************/
+
+#define VN_NOTE_CHANGED	(NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB | NOTE_LINK)
+#define VN_NOTE_DELETED (NOTE_DELETE | NOTE_REVOKE)
+#define VN_NOTE_ALL	(VN_NOTE_CHANGED | VN_NOTE_DELETED | NOTE_RENAME)
+
+/*
+ * A barebone stat structure, only containing the fields we need.
+ */
+typedef struct
+{
+  gboolean	exists;
+  ino_t		ino;
+  mode_t	mode;
+  uid_t		uid;
+  gid_t		gid;
+  time_t	mtime;
+  time_t	ctime;
+  off_t		size;
+} MiniStat;
+
+typedef void (*HashTableAddFunc) (GHashTable *table,
+				  gpointer item,
+				  gpointer user_data);
+typedef void (*HashTablePostAddFunc) (GHashTable *table,
+				      gpointer user_data);
+typedef void (*HashTableRemoveFunc) (GHashTable *table,
+				     gpointer item,
+				     gpointer user_data);
+typedef void (*HashTablePostRemoveFunc) (GHashTable *table,
+					 gpointer user_data);
+
+typedef struct
+{
+  HashTableAddFunc		add;
+  HashTablePostAddFunc		post_add;
+  HashTableRemoveFunc		remove;
+  HashTablePostRemoveFunc	post_remove;
+} HashTableMethods;
+  
+/*
+ * A hash table which can be modified while iterating over it.
+ */
+typedef struct
+{
+  GHashTable		*main;
+  gboolean		iterating;
+  GSList		*pending_additions;
+  GSList		*pending_removals;
+  HashTableMethods	*methods;
+  gpointer		user_data;
+} HashTable;
+
+/* the base monitor class */
+typedef struct _Monitor Monitor;
+struct _Monitor
+{
+  void (*handle_kevent) (Monitor *monitor, struct kevent *event);
+  char		*pathname;
+  int		fd;		/* for kqueue */
+  MiniStat	sb;		/* for poll */
+  unsigned int	poll_count;
+};
+#define MONITOR(ptr)		((Monitor *) ptr)
+
+typedef enum
+{
+  MONITOR_ISDIR		= 1 << 0,
+  MONITOR_ISNOTDIR	= 1 << 1
+} MonitorFlags;
+#define MONITOR_FLAGS_SHIFT	2
+
+/* monitor for Gamin subscriptions */
+typedef struct
+{
+  Monitor	base;
+  GList		*subs;
+  HashTable	*fmons;			/* FileMonitor objects */
+  HashTable	*unsupported_fmons;	/* subset of fmons to poll */
+  gboolean	isdir;			/* is a directory subscription? */
+} SubMonitor;
+#define SUB_MONITOR(ptr)	((SubMonitor *) ptr)
+
+typedef enum
+{
+  SUB_MONITOR_WAS_MISSING		= 1 << MONITOR_FLAGS_SHIFT
+} SubMonitorFlags;
+
+/* monitor for files within directory subscriptions */
+typedef struct
+{
+  Monitor	base;
+  SubMonitor	*smon;		/* the SubMonitor this fmon belongs to */
+  char		*filename;	/* pointer into base.pathname */
+} FileMonitor;
+#define FILE_MONITOR(ptr)	((FileMonitor *) ptr)
+
+typedef enum
+{
+  FILE_MONITOR_POSSIBLY_RECREATED	= 1 << MONITOR_FLAGS_SHIFT
+} FileMonitorFlags;
+
+typedef void (*PollerFunc) (SubMonitor *smon);
+typedef struct
+{
+  PollerFunc	func;
+  unsigned int	interval;
+  unsigned int	timeout_id;
+  HashTable	*smons;		/* SubMonitor objects */
+} Poller;
+
+static int kq = -1;
+
+static unsigned int open_files = 0;
+static unsigned int max_open_files = 0;
+
+static GHashTable *dir_hash = NULL;
+static GHashTable *file_hash = NULL;
+
+static Poller missing_smon_poller;
+static Poller unsupported_smon_poller;
+static Poller unsupported_fmon_poller;
+
+static void gam_kqueue_hash_table_default_add_cb (GHashTable *table,
+						  gpointer item,
+						  gpointer user_data);
+static void gam_kqueue_hash_table_default_remove_cb (GHashTable *table,
+						     gpointer item,
+						     gpointer user_data);
+
+static void gam_kqueue_poller_post_add_cb (GHashTable *table, Poller *poller);
+static void gam_kqueue_poller_post_remove_cb (GHashTable *table, Poller *poller);
+
+static HashTableMethods poller_hash_table_methods =
+  {
+    gam_kqueue_hash_table_default_add_cb,
+    (HashTablePostAddFunc) gam_kqueue_poller_post_add_cb,
+    gam_kqueue_hash_table_default_remove_cb,
+    (HashTablePostRemoveFunc) gam_kqueue_poller_post_remove_cb
+  };
+
+static void gam_kqueue_sub_monitor_add_fmon_cb (GHashTable *table,
+						FileMonitor *fmon,
+						SubMonitor *smon);
+static void gam_kqueue_sub_monitor_remove_fmon_cb (GHashTable *table,
+						   FileMonitor *fmon,
+						   SubMonitor *smon);
+
+static HashTableMethods sub_monitor_fmons_hash_table_methods =
+  {
+    (HashTableAddFunc) gam_kqueue_sub_monitor_add_fmon_cb,
+    (HashTablePostAddFunc) NULL,
+    (HashTableRemoveFunc) gam_kqueue_sub_monitor_remove_fmon_cb,
+    (HashTablePostRemoveFunc) NULL
+  };
+
+static void gam_kqueue_sub_monitor_post_add_unsupported_fmon_cb (GHashTable *table,
+								 SubMonitor *smon);
+static void gam_kqueue_sub_monitor_post_remove_unsupported_fmon_cb (GHashTable *table,
+								    SubMonitor *smon);
+
+static HashTableMethods sub_monitor_unsupported_fmons_hash_table_methods =
+  {
+    gam_kqueue_hash_table_default_add_cb,
+    (HashTablePostAddFunc) gam_kqueue_sub_monitor_post_add_unsupported_fmon_cb,
+    gam_kqueue_hash_table_default_remove_cb,
+    (HashTablePostRemoveFunc) gam_kqueue_sub_monitor_post_remove_unsupported_fmon_cb
+  };
+
+static void gam_kqueue_sub_monitor_emit_event (SubMonitor *smon,
+					       GaminEventType event,
+					       SubMonitorFlags flags);
+static void gam_kqueue_sub_monitor_handle_kevent (Monitor *mon,
+						  struct kevent *event);
+
+static FileMonitor *gam_kqueue_file_monitor_new (SubMonitor *smon,
+						 const char *filename,
+						 FileMonitorFlags *flags);
+static void gam_kqueue_file_monitor_free (FileMonitor *fmon);
+static void gam_kqueue_file_monitor_emit_event (FileMonitor *fmon,
+						GaminEventType event,
+						FileMonitorFlags flags);
+static void gam_kqueue_file_monitor_handle_kevent (Monitor *mon, struct kevent *event);
+
+/*** helpers *****************************************************************/
+
+static void
+gam_kqueue_mini_lstat (const char *pathname, MiniStat *mini_sb)
+{
+  struct stat sb;
+  
+  if (lstat(pathname, &sb) < 0)
+    memset(mini_sb, 0, sizeof(*mini_sb));
+  else
+    {
+      mini_sb->exists = TRUE;
+      mini_sb->ino = sb.st_ino;
+      mini_sb->mode = sb.st_mode;
+      mini_sb->uid = sb.st_uid;
+      mini_sb->gid = sb.st_gid;
+      mini_sb->mtime = sb.st_mtime;
+      mini_sb->ctime = sb.st_ctime;
+      mini_sb->size = sb.st_size;
+    }
+}
+
+static gboolean
+gam_kqueue_differs (const MiniStat *sb1, const MiniStat *sb2)
+{
+  return sb1->mtime != sb2->mtime
+    || sb1->ctime != sb2->ctime
+    || sb1->size != sb2->size
+    || sb1->mode != sb2->mode
+    || sb1->uid != sb2->uid
+    || sb1->gid != sb2->gid
+    || sb1->ino != sb2->ino;
+}
+
+static gboolean
+gam_kqueue_isdir (const char *pathname, MonitorFlags flags)
+{
+  if ((flags & MONITOR_ISDIR) != 0)
+    return TRUE;
+  else if ((flags & MONITOR_ISNOTDIR) != 0)
+    return FALSE;
+  else
+    {
+      struct stat sb;
+      return lstat(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);
+
+  if (sysctlbyname(name, value, &value_len, NULL, 0) < 0)
+    {
+      gam_error(DEBUG_INFO, "unable to retrieve %s: %s\n", name, g_strerror(errno));
+      return FALSE;
+    }
+  else
+    return TRUE;
+}
+
+/*** HashTable ***************************************************************/
+
+static HashTable *
+gam_kqueue_hash_table_new (GHashTable *main,
+			   HashTableMethods *methods,
+			   gpointer user_data)
+{
+  HashTable *table;
+
+  table = g_new0(HashTable, 1);
+  table->main = main;
+  table->methods = methods;
+  table->user_data = user_data;
+
+  return table;
+}
+
+static void
+gam_kqueue_hash_table_default_add_cb (GHashTable *table,
+				      gpointer item,
+				      gpointer user_data)
+{
+  g_hash_table_insert(table, item, GINT_TO_POINTER(TRUE));
+}
+
+static void
+gam_kqueue_hash_table_default_remove_cb (GHashTable *table,
+					 gpointer item,
+					 gpointer user_data)
+{
+  g_hash_table_remove(table, item);
+}
+
+static void
+gam_kqueue_hash_table_add (HashTable *table, gpointer item)
+{
+  if (table->iterating)
+    table->pending_additions = g_slist_prepend(table->pending_additions, item);
+  else
+    {
+      table->methods->add(table->main, item, table->user_data);
+      if (table->methods->post_add)
+	table->methods->post_add(table->main, table->user_data);
+    }
+}
+
+static void
+gam_kqueue_hash_table_remove (HashTable *table, gpointer item)
+{
+  if (table->iterating)
+    table->pending_removals = g_slist_prepend(table->pending_removals, item);
+  else
+    {
+      table->methods->remove(table->main, item, table->user_data);
+      if (table->methods->post_remove)
+	table->methods->post_remove(table->main, table->user_data);
+    }
+}
+
+static void
+gam_kqueue_hash_table_foreach (HashTable *table,
+			       GHFunc func,
+			       gpointer user_data)
+{
+  g_assert(table->iterating == FALSE);
+
+  table->iterating = TRUE;
+  g_hash_table_foreach(table->main, func, user_data);
+  table->iterating = FALSE;
+  
+  if (table->pending_additions)
+    {
+      GSList *l;
+
+      for (l = table->pending_additions; l != NULL; l = l->next)
+	table->methods->add(table->main, l->data, table->user_data);
+
+      g_slist_free(table->pending_additions);
+      table->pending_additions = NULL;
+
+      if (table->methods->post_add)
+	table->methods->post_add(table->main, table->user_data);
+    }
+
+  if (table->pending_removals)
+    {
+      GSList *l;
+
+      for (l = table->pending_removals; l != NULL; l = l->next)
+	table->methods->remove(table->main, l->data, table->user_data);
+
+      g_slist_free(table->pending_removals);
+      table->pending_removals = NULL;
+
+      if (table->methods->post_remove)
+	table->methods->post_remove(table->main, table->user_data);
+    }
+}
+
+static void
+gam_kqueue_hash_table_destroy (HashTable *table)
+{
+  g_assert(table->iterating == FALSE);
+
+  g_hash_table_destroy(table->main);
+  g_free(table);
+}
+
+/*** Poller ******************************************************************/
+
+static void
+gam_kqueue_poller_init (Poller *poller, PollerFunc func, unsigned int interval)
+{
+  poller->func = func;
+  poller->interval = interval;
+  poller->timeout_id = 0;
+  poller->smons = gam_kqueue_hash_table_new(g_hash_table_new(g_direct_hash, g_direct_equal),
+					    &poller_hash_table_methods,
+					    poller);
+}
+
+static void
+gam_kqueue_poller_foreach_cb (SubMonitor *smon,
+			      gpointer unused,
+			      Poller *poller)
+{
+  poller->func(smon);
+}
+
+static gboolean
+gam_kqueue_poller_timeout_cb (Poller *poller)
+{
+  gam_kqueue_hash_table_foreach(poller->smons, (GHFunc) gam_kqueue_poller_foreach_cb, poller);
+
+  return TRUE;			/* keep source */
+}
+
+static void
+gam_kqueue_poller_post_add_cb (GHashTable *table, Poller *poller)
+{
+  if (! poller->timeout_id)
+    poller->timeout_id = g_timeout_add(poller->interval, (GSourceFunc) gam_kqueue_poller_timeout_cb, poller);
+}
+
+static void
+gam_kqueue_poller_post_remove_cb (GHashTable *table, Poller *poller)
+{
+  if (g_hash_table_size(table) == 0 && poller->timeout_id)
+    {
+      g_source_remove(poller->timeout_id);
+      poller->timeout_id = 0;
+    }
+}
+
+static void
+gam_kqueue_poller_add_sub_monitor (Poller *poller, SubMonitor *smon)
+{
+  gam_kqueue_hash_table_add(poller->smons, smon);
+}
+
+static void
+gam_kqueue_poller_remove_sub_monitor (Poller *poller, SubMonitor *smon)
+{
+  gam_kqueue_hash_table_remove(poller->smons, smon);
+}
+
+/*** Monitor *****************************************************************/
+
+static gboolean
+gam_kqueue_monitor_enable_kqueue (Monitor *mon)
+{
+  struct kevent ev[1];
+
+  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 (mon->fd < 0)
+    {
+      GAM_DEBUG(DEBUG_INFO, "cannot open %s (%s), falling back to poll\n", mon->pathname, g_strerror(errno));
+      return FALSE;
+    }
+
+  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;
+    }
+
+  open_files++;
+  return TRUE;
+}
+
+static void
+gam_kqueue_monitor_set_unsupported (Monitor *mon)
+{
+  if (mon->fd >= 0)
+    {
+      close(mon->fd);
+      mon->fd = -1;
+
+      open_files--;
+    }
+
+  gam_kqueue_mini_lstat(mon->pathname, &mon->sb);
+}
+
+static void
+gam_kqueue_monitor_free (Monitor *mon)
+{
+  g_free(mon->pathname);
+  if (mon->fd >= 0)
+    {
+      close(mon->fd);
+      open_files--;
+    }
+  g_free(mon);
+}
+
+/*** SubMonitor **************************************************************/
+
+static void
+gam_kqueue_sub_monitor_init_fmons (SubMonitor *smon)
+{
+  smon->fmons = gam_kqueue_hash_table_new(g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify) gam_kqueue_file_monitor_free),
+					  &sub_monitor_fmons_hash_table_methods,
+					  smon);
+  smon->unsupported_fmons = gam_kqueue_hash_table_new(g_hash_table_new(g_direct_hash, g_direct_equal),
+						      &sub_monitor_unsupported_fmons_hash_table_methods,
+						      smon);
+}
+
+static SubMonitor *
+gam_kqueue_sub_monitor_new (GamSubscription *sub)
+{
+  SubMonitor *smon;
+  Monitor *mon;
+
+  smon = g_new0(SubMonitor, 1);
+
+  mon = MONITOR(smon);
+  mon->handle_kevent = gam_kqueue_sub_monitor_handle_kevent;
+  mon->pathname = g_strdup(gam_subscription_get_path(sub));
+  mon->fd = -1;
+
+  smon->isdir = gam_subscription_is_dir(sub);
+  gam_kqueue_sub_monitor_init_fmons(smon);
+
+  return smon;
+}
+
+static void
+gam_kqueue_sub_monitor_clear_fmons (SubMonitor *smon)
+{
+  if (g_hash_table_size(smon->unsupported_fmons->main) > 0)
+    gam_kqueue_poller_remove_sub_monitor(&unsupported_fmon_poller, smon);
+
+  gam_kqueue_hash_table_destroy(smon->unsupported_fmons);
+  gam_kqueue_hash_table_destroy(smon->fmons);
+}
+
+static void
+gam_kqueue_sub_monitor_free (SubMonitor *smon)
+{
+  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));
+}
+
+static void
+gam_kqueue_sub_monitor_add_fmon_cb (GHashTable *table,
+				    FileMonitor *fmon,
+				    SubMonitor *smon)
+{
+  g_hash_table_replace(table, fmon->filename, fmon);
+}
+
+static void
+gam_kqueue_sub_monitor_remove_fmon_cb (GHashTable *table,
+				       FileMonitor *fmon,
+				       SubMonitor *smon)
+{
+  g_hash_table_remove(table, fmon->filename);
+}
+
+static void
+gam_kqueue_sub_monitor_post_add_unsupported_fmon_cb (GHashTable *table,
+						     SubMonitor *smon)
+{
+  gam_kqueue_poller_add_sub_monitor(&unsupported_fmon_poller, smon);
+}
+
+static void
+gam_kqueue_sub_monitor_post_remove_unsupported_fmon_cb (GHashTable *table,
+							SubMonitor *smon)
+{
+  if (g_hash_table_size(table) == 0)
+    gam_kqueue_poller_remove_sub_monitor(&unsupported_fmon_poller, smon);
+}
+
+static void
+gam_kqueue_sub_monitor_set_missing (SubMonitor *smon)
+{
+  Monitor *mon = MONITOR(smon);
+
+  if (mon->fd >= 0)
+    {
+      close(mon->fd);
+      mon->fd = -1;
+
+      open_files--;
+    }
+
+  /*
+   * A removed directory will normally not contain files, but we must
+   * not assume it (we might receive events out of order, etc). We
+   * therefore check if files are remaining, and if yes, clear them.
+   */
+  if (g_hash_table_size(smon->fmons->main) > 0)
+    {
+      gam_kqueue_sub_monitor_clear_fmons(smon);
+      gam_kqueue_sub_monitor_init_fmons(smon);
+    }
+
+  gam_kqueue_poller_remove_sub_monitor(&unsupported_smon_poller, smon);
+  gam_kqueue_poller_add_sub_monitor(&missing_smon_poller, smon);
+}
+
+static void
+gam_kqueue_sub_monitor_set_unsupported (SubMonitor *smon)
+{
+  Monitor *mon = MONITOR(smon);
+
+  gam_kqueue_monitor_set_unsupported(mon);
+  gam_kqueue_poller_add_sub_monitor(&unsupported_smon_poller, smon);
+}
+
+static void
+gam_kqueue_sub_monitor_enable_notification (SubMonitor *smon,
+					    SubMonitorFlags flags)
+{
+  Monitor *mon = MONITOR(smon);
+  gboolean exists;
+
+  /* we first send CREATED or EXISTS/DELETED+ENDEXISTS events */
+
+  if ((flags & SUB_MONITOR_WAS_MISSING) != 0)
+    exists = TRUE;
+  else
+    {
+      struct stat sb;
+
+      exists = lstat(mon->pathname, &sb) >= 0;
+      flags |= (exists && (sb.st_mode & S_IFDIR) != 0) ? MONITOR_ISDIR : MONITOR_ISNOTDIR;
+    }
+
+  if (exists)
+    {
+      GaminEventType gevent;
+
+      gevent = (flags & SUB_MONITOR_WAS_MISSING) != 0 ? GAMIN_EVENT_CREATED : GAMIN_EVENT_EXISTS;
+      gam_kqueue_sub_monitor_emit_event(smon, gevent, flags);
+
+      if (smon->isdir && (flags & MONITOR_ISDIR) != 0)
+	{
+	  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
+	    {
+	      GAM_DEBUG(DEBUG_INFO, "unable to open directory %s: %s\n", mon->pathname, err->message);
+	      g_error_free(err);
+	    }
+	}
+
+      if ((flags & SUB_MONITOR_WAS_MISSING) == 0)
+	gam_kqueue_sub_monitor_emit_event(smon, GAMIN_EVENT_ENDEXISTS, flags);
+    }
+  else
+    {
+      gam_kqueue_sub_monitor_emit_event(smon, GAMIN_EVENT_DELETED, flags);
+      gam_kqueue_sub_monitor_emit_event(smon, GAMIN_EVENT_ENDEXISTS, flags);
+
+      return;
+    }
+    
+  /* then we enable kqueue notification, falling back to poll if necessary */
+
+  if (! gam_kqueue_monitor_enable_kqueue(mon))
+    gam_kqueue_sub_monitor_set_unsupported(smon);
+}
+
+static void
+gam_kqueue_sub_monitor_handle_directory_change_removal_cb (const char *filename,
+							   FileMonitor *fmon,
+							   GHashTable *filenames)
+{
+  if (! g_hash_table_lookup(filenames, filename))
+    gam_kqueue_file_monitor_emit_event(fmon, GAMIN_EVENT_DELETED, MONITOR_ISNOTDIR);
+}
+
+static void
+gam_kqueue_sub_monitor_handle_directory_change (SubMonitor *smon,
+						gboolean isdir)
+{
+  Monitor *mon = MONITOR(smon);
+  GHashTable *filenames;
+
+  filenames = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
+
+  if (isdir)			/* do not follow symbolic links */
+    {
+      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)))
+	    {
+	      g_hash_table_insert(filenames, g_strdup(filename), GINT_TO_POINTER(TRUE));
+
+	      /* handle file creation */
+	      if (! g_hash_table_lookup(smon->fmons->main, filename))
+		{
+		  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
+	{
+	  GAM_DEBUG(DEBUG_INFO, "unable to open directory %s: %s\n", mon->pathname, err->message);
+	  g_error_free(err);
+	}
+    }
+
+  /*
+   * Handle deleted files (they are also handled at the FileMonitor
+   * level, but we must use whichever event comes first).
+   */
+  gam_kqueue_hash_table_foreach(smon->fmons, (GHFunc) gam_kqueue_sub_monitor_handle_directory_change_removal_cb, filenames);
+  g_hash_table_destroy(filenames);
+}
+
+static void
+gam_kqueue_sub_monitor_emit_event (SubMonitor *smon,
+				   GaminEventType event,
+				   SubMonitorFlags flags)
+{
+  Monitor *mon = MONITOR(smon);
+  gboolean isdir;
+
+  isdir = gam_kqueue_isdir(mon->pathname, flags);
+
+  switch (event)
+    {
+    case GAMIN_EVENT_CHANGED:
+      if (smon->isdir)
+	{
+	  gam_kqueue_sub_monitor_handle_directory_change(smon, isdir);
+	  return;
+	}
+      break;
+
+    case GAMIN_EVENT_DELETED:
+    case GAMIN_EVENT_MOVED:
+      gam_kqueue_sub_monitor_set_missing(smon);
+      break;
+    }
+
+  gam_server_emit_event(mon->pathname, isdir, event, smon->subs, 1);
+}
+
+static void
+gam_kqueue_sub_monitor_handle_kevent (Monitor *mon, struct kevent *event)
+{
+  SubMonitor *smon = SUB_MONITOR(mon);
+
+  if ((event->flags & EV_ERROR) != 0)
+    {
+      /* kqueue failed, fallback to poll */
+      GAM_DEBUG(DEBUG_INFO, "kqueue failed for %s, falling back to poll\n", mon->pathname);
+      gam_kqueue_sub_monitor_set_unsupported(smon);
+      return;
+    }
+
+  /*
+   * kevent aggregates events, so we must handle a fflags with
+   * multiple event bits set.
+   */
+  if ((event->fflags & VN_NOTE_CHANGED) != 0)
+    gam_kqueue_sub_monitor_emit_event(smon, GAMIN_EVENT_CHANGED, 0);
+
+  /* emitting the following events will add the smon to the missing poller */
+  if ((event->fflags & VN_NOTE_DELETED) != 0)
+    gam_kqueue_sub_monitor_emit_event(smon, GAMIN_EVENT_DELETED, MONITOR_ISNOTDIR);
+  else if ((event->fflags & NOTE_RENAME) != 0)
+    gam_kqueue_sub_monitor_emit_event(smon, GAMIN_EVENT_MOVED, MONITOR_ISNOTDIR);
+}
+
+/*** FileMonitor *************************************************************/
+
+static void
+gam_kqueue_file_monitor_set_unsupported (FileMonitor *fmon)
+{
+  Monitor *mon = MONITOR(fmon);
+
+  gam_kqueue_monitor_set_unsupported(mon);
+  gam_kqueue_hash_table_add(fmon->smon->unsupported_fmons, fmon);
+}
+
+static FileMonitor *
+gam_kqueue_file_monitor_new (SubMonitor *smon,
+			     const char *filename,
+			     FileMonitorFlags *flags)
+{
+  FileMonitor *fmon;
+  Monitor *mon;
+
+  fmon = g_new0(FileMonitor, 1);
+
+  mon = MONITOR(fmon);
+  mon->handle_kevent = gam_kqueue_file_monitor_handle_kevent;
+  mon->pathname = g_build_filename(MONITOR(smon)->pathname, filename, NULL);
+  mon->fd = -1;
+
+  fmon->smon = smon;
+  fmon->filename = strrchr(mon->pathname, G_DIR_SEPARATOR);
+  fmon->filename = fmon->filename ? fmon->filename + 1 : mon->pathname;
+
+  gam_kqueue_hash_table_add(fmon->smon->fmons, fmon);
+
+  if (gam_kqueue_monitor_enable_kqueue(mon))
+    *flags = 0;
+  else
+    {
+      gam_kqueue_file_monitor_set_unsupported(fmon);
+      *flags = (mon->sb.mode & S_IFDIR) != 0 ? MONITOR_ISDIR : MONITOR_ISNOTDIR;
+    }
+
+  return fmon;
+}
+
+static void
+gam_kqueue_file_monitor_free (FileMonitor *fmon)
+{
+  gam_kqueue_monitor_free(MONITOR(fmon));
+}
+
+static void
+gam_kqueue_file_monitor_emit_event (FileMonitor *fmon,
+				    GaminEventType event,
+				    FileMonitorFlags flags)
+{
+  Monitor *mon = MONITOR(fmon);
+  struct stat sb;
+  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;
+      isdir = stat_succeeded && (sb.st_mode & S_IFDIR) != 0;
+    }
+  else
+    {
+      stat_done = FALSE;
+      isdir = (flags & MONITOR_ISDIR) != 0;
+    }
+  
+  gam_server_emit_event(mon->pathname, isdir, event, fmon->smon->subs, 1);
+
+  switch (event)
+    {
+    case GAMIN_EVENT_DELETED:
+    case GAMIN_EVENT_MOVED:
+      if (mon->fd < 0)
+	gam_kqueue_hash_table_remove(fmon->smon->unsupported_fmons, fmon);
+
+      if ((flags & FILE_MONITOR_POSSIBLY_RECREATED) != 0)
+	{
+	  if (! stat_done)
+	    stat_succeeded = lstat(mon->pathname, &sb) >= 0;
+
+	  if (stat_succeeded)
+	    {
+	      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
+	       * must therefore create a new fmon and emit a
+	       * GAMIN_EVENT_CREATED event, because
+	       * gam_kqueue_sub_monitor_handle_directory_change() did
+	       * not detect the removal+creation.
+	       */
+
+	      new_fmon = gam_kqueue_file_monitor_new(fmon->smon, fmon->filename, &new_fmon_flags);
+	      gam_kqueue_file_monitor_emit_event(new_fmon, GAMIN_EVENT_CREATED, new_fmon_flags);
+
+	      break;		/* do not remove the fmon we've just created */
+	    }
+	}
+	
+      gam_kqueue_hash_table_remove(fmon->smon->fmons, fmon);
+      break;
+    }
+}
+
+static void
+gam_kqueue_file_monitor_handle_kevent (Monitor *mon, struct kevent *event)
+{
+  FileMonitor *fmon = FILE_MONITOR(mon);
+
+  if ((event->flags & EV_ERROR) != 0)
+    {
+      /* kqueue failed, fallback to poll */
+      GAM_DEBUG(DEBUG_INFO, "kqueue failed for %s, falling back to poll\n", mon->pathname);
+      gam_kqueue_file_monitor_set_unsupported(fmon);
+      return;
+    }
+
+  /*
+   * kevent aggregates events, so we must handle a fflags with
+   * multiple event bits set.
+   */
+
+  if ((event->fflags & VN_NOTE_CHANGED) != 0)
+    gam_kqueue_file_monitor_emit_event(fmon, GAMIN_EVENT_CHANGED, 0);
+
+  /* emitting the following events will free the fmon */
+  if ((event->fflags & VN_NOTE_DELETED) != 0)
+    gam_kqueue_file_monitor_emit_event(fmon, GAMIN_EVENT_DELETED, MONITOR_ISNOTDIR | FILE_MONITOR_POSSIBLY_RECREATED);
+  else if ((event->fflags & NOTE_RENAME) != 0)
+    gam_kqueue_file_monitor_emit_event(fmon, GAMIN_EVENT_MOVED, MONITOR_ISNOTDIR | FILE_MONITOR_POSSIBLY_RECREATED);
+}
+
+/*** kevent/poll callbacks ***************************************************/
+
+static gboolean
+gam_kqueue_kevent_cb (GIOChannel *source,
+		      GIOCondition condition,
+		      gpointer user_data)
+{
+  int nevents;
+  struct kevent ev[1];
+  struct timespec timeout = { 0, 0 };
+  int i;
+
+  nevents = kevent(kq, NULL, 0, ev, G_N_ELEMENTS(ev), &timeout);
+  if (nevents < 0)
+    {
+      GAM_DEBUG(DEBUG_INFO, "kevent() failure: %s\n", g_strerror(errno));
+      return TRUE;		/* keep source */
+    }
+
+  for (i = 0; i < nevents; i++)
+    MONITOR(ev[i].udata)->handle_kevent(ev[i].udata, &ev[i]);
+  
+  return TRUE;			/* keep source */
+}
+
+static void
+gam_kqueue_missing_smon_poll (SubMonitor *smon)
+{
+  struct stat sb;
+
+  if (lstat(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));
+    }
+}
+
+static void
+gam_kqueue_unsupported_smon_poll (SubMonitor *smon)
+{
+  Monitor *mon = MONITOR(smon);
+  MiniStat sb;
+  GaminEventType event;
+
+  if (++mon->poll_count == CFG_UNSUPPORTED_SMON_KQUEUE_RETRY_FREQUENCY)
+    {
+      mon->poll_count = 0;
+      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);
+}
+
+static void
+gam_kqueue_unsupported_fmon_poll_foreach_cb (FileMonitor *fmon,
+					     gpointer unused,
+					     gpointer user_data)
+{
+  Monitor *mon = MONITOR(fmon);
+  MiniStat sb;
+  GaminEventType event;
+
+  if (++mon->poll_count == CFG_UNSUPPORTED_FMON_KQUEUE_RETRY_FREQUENCY)
+    {
+      mon->poll_count = 0;
+      if (gam_kqueue_monitor_enable_kqueue(mon))
+	gam_kqueue_hash_table_remove(fmon->smon->unsupported_fmons, fmon);
+    }
+
+  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_file_monitor_emit_event(fmon, event, (sb.mode & S_IFDIR) != 0 ? MONITOR_ISDIR : MONITOR_ISNOTDIR);
+}
+
+static void
+gam_kqueue_unsupported_fmon_poll (SubMonitor *smon)
+{
+  gam_kqueue_hash_table_foreach(smon->unsupported_fmons, (GHFunc) gam_kqueue_unsupported_fmon_poll_foreach_cb, NULL);
+}
+
+/*** Gamin backend implementation ********************************************/
+
+/**
+ * Initializes the kqueue system.  This must be called before
+ * any other functions in this module.
+ *
+ * @returns TRUE if initialization succeeded, FALSE otherwise
+ */
+gboolean
+gam_kqueue_init (void)
+{
+  GIOChannel *channel;
+  unsigned int maxfiles;
+  unsigned int maxfilesperproc;
+
+  kq = kqueue();
+  if (kq < 0)
+    {
+      gam_error(DEBUG_INFO, "kqueue initialization failure: %s\n", g_strerror(errno));
+      return FALSE;
+    }
+
+  if (! gam_kqueue_get_uint_sysctl("kern.maxfiles", &maxfiles))
+    return FALSE;
+  if (! gam_kqueue_get_uint_sysctl("kern.maxfilesperproc", &maxfilesperproc))
+    return FALSE;
+
+  /*
+   * We make sure to:
+   *	- never paralyze the system (CFG_GLOBAL_FILE_RESERVE_RATIO)
+   *	- never paralyze our own process (CFG_SELF_FILE_RESERVE)
+   */
+
+  maxfiles *= CFG_GLOBAL_FILE_RESERVE_RATIO;
+  maxfilesperproc = maxfilesperproc > CFG_SELF_FILE_RESERVE
+    ? maxfilesperproc - CFG_SELF_FILE_RESERVE
+    : 0;
+
+  max_open_files = MIN(maxfiles, maxfilesperproc);
+
+  dir_hash = g_hash_table_new(g_str_hash, g_str_equal);
+  file_hash = g_hash_table_new(g_str_hash, g_str_equal);
+
+  gam_kqueue_poller_init(&missing_smon_poller,
+			 gam_kqueue_missing_smon_poll,
+			 CFG_MISSING_SMON_POLL_INTERVAL);
+  gam_kqueue_poller_init(&unsupported_smon_poller,
+			 gam_kqueue_unsupported_smon_poll,
+			 CFG_UNSUPPORTED_SMON_POLL_INTERVAL);
+  gam_kqueue_poller_init(&unsupported_fmon_poller,
+			 gam_kqueue_unsupported_fmon_poll,
+			 CFG_UNSUPPORTED_FMON_POLL_INTERVAL);
+
+  channel = g_io_channel_unix_new(kq);
+  g_io_add_watch(channel, G_IO_IN, gam_kqueue_kevent_cb, NULL);
+
+  gam_backend_add_subscription = gam_kqueue_add_subscription;
+  gam_backend_remove_subscription = gam_kqueue_remove_subscription;
+  gam_backend_remove_all_for = gam_kqueue_remove_all_for;
+
+  return TRUE;
+}
+
+/**
+ * Adds a subscription to be monitored.
+ *
+ * @param sub a #GamSubscription to be polled
+ * @returns TRUE if adding the subscription succeeded, FALSE otherwise
+ */
+gboolean
+gam_kqueue_add_subscription (GamSubscription *sub)
+{
+  const char *path;
+  GHashTable *hash;
+  SubMonitor *smon;
+
+  gam_listener_add_subscription(gam_subscription_get_listener(sub), sub);
+
+  path = gam_subscription_get_path(sub);
+  hash = gam_subscription_is_dir(sub) ? dir_hash : file_hash;
+  smon = g_hash_table_lookup(hash, path);
+
+  if (smon)
+    {
+      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);
+
+  g_hash_table_insert(hash, MONITOR(smon)->pathname, smon);
+  gam_kqueue_sub_monitor_enable_notification(smon, 0);
+
+  return TRUE;
+}
+
+/**
+ * Removes a subscription which was being monitored.
+ *
+ * @param sub a #GamSubscription to remove
+ * @returns TRUE if removing the subscription succeeded, FALSE otherwise
+ */
+gboolean
+gam_kqueue_remove_subscription (GamSubscription *sub)
+{
+  GHashTable *hash;
+  SubMonitor *smon;
+
+  hash = gam_subscription_is_dir(sub) ? dir_hash : file_hash;
+  smon = g_hash_table_lookup(hash, gam_subscription_get_path(sub));
+
+  if (! smon)
+    return FALSE;
+
+  smon->subs = g_list_remove_all(smon->subs, sub);
+  if (! smon->subs)
+    {
+      g_hash_table_remove(hash, MONITOR(smon)->pathname);
+      gam_kqueue_sub_monitor_free(smon);
+    }
+
+  gam_subscription_cancel(sub);
+  gam_listener_remove_subscription(gam_subscription_get_listener(sub), sub);
+
+  return TRUE;
+}
+
+/**
+ * Stop monitoring all subscriptions for a given listener.
+ *
+ * @param listener a #GamListener
+ * @returns TRUE if removing the subscriptions succeeded, FALSE otherwise
+ */
+gboolean
+gam_kqueue_remove_all_for (GamListener *listener)
+{
+  GList *subs;
+  GList *l;
+  gboolean success = TRUE;
+
+  subs = gam_listener_get_subscriptions(listener);
+
+  for (l = subs; l != NULL; l = l->next)
+    if (! gam_kqueue_remove_subscription(l->data))
+      success = FALSE;
+
+  g_list_free(subs);
+  
+  return success;
+}