View | Details | Raw Unified | Return to bug 226920 | Differences between
and this patch

Collapse All | Expand All

(-)Makefile (-1 / +1 lines)
Lines 3-9 Link Here
3
3
4
PORTNAME=	glib
4
PORTNAME=	glib
5
PORTVERSION=	2.50.3
5
PORTVERSION=	2.50.3
6
PORTREVISION=	2
6
PORTREVISION=	3
7
PORTEPOCH=	1
7
PORTEPOCH=	1
8
CATEGORIES=	devel
8
CATEGORIES=	devel
9
MASTER_SITES=	GNOME
9
MASTER_SITES=	GNOME
(-)files/patch-gio_filemonitor (+1037 lines)
Line 0 Link Here
1
# gio: Always purge kqueue subs from missing list
2
# https://gitlab.gnome.org/GNOME/glib/commit/e305fe971e4647d971428a772b7290b9c308a96f
3
# kqueue: Fix invalid emission of G_FILE_MONITOR_EVENT_MOVED event
4
# https://gitlab.gnome.org/GNOME/glib/commit/76072a2dde4a4acc8be8d3c47efbc6811ebe0c1e
5
# kqueue: Multiple fixes and simplifications
6
# https://gitlab.gnome.org/GNOME/glib/commit/aa39a0557c679fc345b0ba72a87c33152eb8ebcd
7
# gpollfilemonitor: Fix use-after-free caused by leaking GSource
8
# https://gitlab.gnome.org/GNOME/glib/commit/ba4a9538e14e8ba0ea037cab5f4b23aa47272a4c
9
# Reorder operations in _kqsub_cancel() to prevent races.
10
# https://gitlab.gnome.org/GNOME/glib/commit/ab179184b883ad378a420223f378071821f0c8b9
11
12
--- gio/gpollfilemonitor.c.orig	2016-10-22 05:17:34 UTC
13
+++ gio/gpollfilemonitor.c
14
@@ -50,7 +50,9 @@ g_poll_file_monitor_finalize (GObject* o
15
   
16
   poll_monitor = G_POLL_FILE_MONITOR (object);
17
 
18
+  g_poll_file_monitor_cancel (G_FILE_MONITOR (poll_monitor));
19
   g_object_unref (poll_monitor->file);
20
+  g_clear_object (&poll_monitor->last_info);
21
 
22
   G_OBJECT_CLASS (g_poll_file_monitor_parent_class)->finalize (object);
23
 }
24
--- gio/kqueue/Makefile.in.orig	2017-02-13 15:22:04 UTC
25
+++ gio/kqueue/Makefile.in
26
@@ -183,9 +183,8 @@ LTLIBRARIES = $(installed_test_LTLIBRARI
27
 libkqueue_la_LIBADD =
28
 am__objects_1 =
29
 am_libkqueue_la_OBJECTS = libkqueue_la-gkqueuefilemonitor.lo \
30
-	libkqueue_la-kqueue-helper.lo libkqueue_la-kqueue-thread.lo \
31
-	libkqueue_la-kqueue-sub.lo libkqueue_la-kqueue-missing.lo \
32
-	libkqueue_la-kqueue-utils.lo libkqueue_la-kqueue-exclusions.lo \
33
+	libkqueue_la-kqueue-helper.lo \
34
+	libkqueue_la-kqueue-missing.lo \
35
 	libkqueue_la-dep-list.lo $(am__objects_1)
36
 libkqueue_la_OBJECTS = $(am_libkqueue_la_OBJECTS)
37
 AM_V_lt = $(am__v_lt_@AM_V@)
38
--- gio/kqueue/gkqueuefilemonitor.c.orig	2016-10-22 05:18:22 UTC
39
+++ gio/kqueue/gkqueuefilemonitor.c
40
@@ -22,33 +22,73 @@
41
 
42
 #include "config.h"
43
 
44
-#include "gkqueuefilemonitor.h"
45
-#include "kqueue-helper.h"
46
-#include "kqueue-exclusions.h"
47
+#include <sys/types.h>
48
+#include <sys/event.h>
49
+#include <sys/time.h>
50
+#include <sys/socket.h>
51
+#include <sys/stat.h>
52
+
53
+#include <errno.h>
54
+#include <fcntl.h>
55
+#include <string.h>
56
+
57
+#include <glib-object.h>
58
+#include <gio/gfilemonitor.h>
59
+#include <gio/glocalfilemonitor.h>
60
+#include <gio/giomodule.h>
61
 #include <gio/gpollfilemonitor.h>
62
 #include <gio/gfile.h>
63
-#include <gio/giomodule.h>
64
+#include <glib-unix.h>
65
+#include "glib-private.h"
66
 
67
+#include "kqueue-helper.h"
68
+#include "dep-list.h"
69
 
70
-struct _GKqueueFileMonitor
71
+G_LOCK_DEFINE_STATIC (kq_lock);
72
+static GSource       *kq_source;
73
+static int	      kq_queue = -1;
74
+
75
+#define G_TYPE_KQUEUE_FILE_MONITOR	(g_kqueue_file_monitor_get_type ())
76
+#define G_KQUEUE_FILE_MONITOR(inst)	(G_TYPE_CHECK_INSTANCE_CAST ((inst), \
77
+					G_TYPE_KQUEUE_FILE_MONITOR, GKqueueFileMonitor))
78
+
79
+typedef GLocalFileMonitorClass GKqueueFileMonitorClass;
80
+
81
+typedef struct
82
 {
83
   GLocalFileMonitor parent_instance;
84
 
85
   kqueue_sub *sub;
86
-
87
+#ifndef O_EVTONLY
88
   GFileMonitor *fallback;
89
   GFile *fbfile;
90
-};
91
+#endif
92
+} GKqueueFileMonitor;
93
+
94
+GType g_kqueue_file_monitor_get_type (void);
95
+G_DEFINE_TYPE_WITH_CODE (GKqueueFileMonitor, g_kqueue_file_monitor, G_TYPE_LOCAL_FILE_MONITOR,
96
+	g_io_extension_point_implement (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME,
97
+		g_define_type_id,
98
+                "kqueue",
99
+		20))
100
+
101
+#ifndef O_EVTONLY
102
+#define O_KQFLAG O_RDONLY
103
+#else
104
+#define O_KQFLAG O_EVTONLY
105
+#endif
106
+
107
+#define NOTE_ALL (NOTE_DELETE|NOTE_WRITE|NOTE_EXTEND|NOTE_ATTRIB|NOTE_RENAME)
108
 
109
 static gboolean g_kqueue_file_monitor_cancel (GFileMonitor* monitor);
110
+static gboolean g_kqueue_file_monitor_is_supported (void);
111
 
112
-G_DEFINE_TYPE_WITH_CODE (GKqueueFileMonitor, g_kqueue_file_monitor, G_TYPE_LOCAL_FILE_MONITOR,
113
-       g_io_extension_point_implement (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME,
114
-               g_define_type_id,
115
-               "kqueue",
116
-               20))
117
+static kqueue_sub	*_kqsub_new (const gchar *, GLocalFileMonitor *, GFileMonitorSource *);
118
+static void		 _kqsub_free (kqueue_sub *);
119
+static gboolean		 _kqsub_cancel (kqueue_sub *);
120
 
121
 
122
+#ifndef O_EVTONLY
123
 static void
124
 _fallback_callback (GFileMonitor      *unused,
125
                     GFile             *first,
126
@@ -57,21 +97,41 @@ _fallback_callback (GFileMonitor      *u
127
                     gpointer           udata)
128
 {
129
   GKqueueFileMonitor *kq_mon = G_KQUEUE_FILE_MONITOR (udata);
130
-  GFileMonitor *mon = G_FILE_MONITOR (kq_mon);
131
-  g_assert (kq_mon != NULL);
132
-  g_assert (mon != NULL);
133
-  (void) unused;
134
-
135
-  if (event == G_FILE_MONITOR_EVENT_CHANGED)
136
-    {
137
-      GLocalFileMonitor *local_monitor = G_LOCAL_FILE_MONITOR (kq_mon);
138
 
139
-      _kh_dir_diff (kq_mon->sub, local_monitor->source);
140
-    }
141
-  else
142
-    g_file_monitor_emit_event (mon, first, second, event);
143
+  g_file_monitor_emit_event (G_FILE_MONITOR (kq_mon), first, second, event);
144
 }
145
 
146
+/*
147
+ * _ke_is_excluded:
148
+ * @full_path - a path to file to check.
149
+ *
150
+ * Returns: TRUE if the file should be excluded from the kqueue-powered
151
+ *      monitoring, FALSE otherwise.
152
+ **/
153
+gboolean
154
+_ke_is_excluded (const char *full_path)
155
+{
156
+  GFile *f = NULL;
157
+  GMount *mount = NULL;
158
+
159
+  f = g_file_new_for_path (full_path);
160
+
161
+  if (f != NULL) {
162
+    mount = g_file_find_enclosing_mount (f, NULL, NULL);
163
+    g_object_unref (f);
164
+  }
165
+
166
+  if ((mount != NULL && (g_mount_can_unmount (mount))) || g_str_has_prefix (full_path, "/mnt/"))
167
+  {
168
+    g_warning ("Excluding %s from kernel notification, falling back to poll", full_path);
169
+    if (mount)
170
+      g_object_unref (mount);
171
+    return TRUE;
172
+  }
173
+
174
+  return FALSE;
175
+}
176
+#endif /* !O_EVTONLY */
177
 
178
 static void
179
 g_kqueue_file_monitor_finalize (GObject *object)
180
@@ -80,16 +140,18 @@ g_kqueue_file_monitor_finalize (GObject 
181
 
182
   if (kqueue_monitor->sub)
183
     {
184
-      _kh_cancel_sub (kqueue_monitor->sub);
185
-      _kh_sub_free (kqueue_monitor->sub);
186
+      _kqsub_cancel (kqueue_monitor->sub);
187
+      _kqsub_free (kqueue_monitor->sub);
188
       kqueue_monitor->sub = NULL;
189
     }
190
 
191
+#ifndef O_EVTONLY
192
   if (kqueue_monitor->fallback)
193
     g_object_unref (kqueue_monitor->fallback);
194
 
195
   if (kqueue_monitor->fbfile)
196
     g_object_unref (kqueue_monitor->fbfile);
197
+#endif
198
 
199
   if (G_OBJECT_CLASS (g_kqueue_file_monitor_parent_class)->finalize)
200
     (*G_OBJECT_CLASS (g_kqueue_file_monitor_parent_class)->finalize) (object);
201
@@ -103,21 +165,25 @@ g_kqueue_file_monitor_start (GLocalFileM
202
                              GFileMonitorSource *source)
203
 {
204
   GKqueueFileMonitor *kqueue_monitor = G_KQUEUE_FILE_MONITOR (local_monitor);
205
-  GObject *obj;
206
-  GKqueueFileMonitorClass *klass;
207
-  GObjectClass *parent_class;
208
-  kqueue_sub *sub = NULL;
209
-  gboolean ret_kh_startup = FALSE;
210
-  const gchar *path = NULL; 
211
-
212
-
213
-  ret_kh_startup = _kh_startup ();
214
-  g_assert (ret_kh_startup);
215
+  kqueue_sub *sub;
216
+  const gchar *path;
217
 
218
   path = filename;
219
-  if (!path)
220
+  if (path == NULL)
221
     path = dirname;
222
 
223
+#ifndef O_EVTONLY
224
+  if (_ke_is_excluded (path))
225
+    {
226
+      GFile *file = g_file_new_for_path (path);
227
+      kqueue_monitor->fbfile = file;
228
+      kqueue_monitor->fallback = _g_poll_file_monitor_new (file);
229
+      g_signal_connect (kqueue_monitor->fallback, "changed",
230
+			G_CALLBACK (_fallback_callback), kqueue_monitor);
231
+      return;
232
+    }
233
+#endif
234
+
235
   /* For a directory monitor, create a subscription object anyway.
236
    * It will be used for directory diff calculation routines. 
237
    * Wait, directory diff in a GKqueueFileMonitor?
238
@@ -125,33 +191,13 @@ g_kqueue_file_monitor_start (GLocalFileM
239
    * file, GIO uses a GKqueueFileMonitor object for that. If a directory
240
    * will be created under that path, GKqueueFileMonitor will have to
241
    * handle the directory notifications. */
242
+  sub = _kqsub_new (path, local_monitor, source);
243
+  if (sub == NULL)
244
+    return;
245
 
246
-  sub = _kh_sub_new (path, TRUE, source);
247
-
248
-  /* FIXME: what to do about errors here? we can't return NULL or another
249
-   * kind of error and an assertion is probably too hard (same issue as in
250
-   * the inotify backend) */
251
-  g_assert (sub != NULL);
252
   kqueue_monitor->sub = sub;
253
-
254
-  if (!_ke_is_excluded (path))
255
-    _kh_add_sub (sub);
256
-  else
257
-    {
258
-      GFile *file = g_file_new_for_path (path);
259
-      kqueue_monitor->fbfile = file;
260
-      kqueue_monitor->fallback = _g_poll_file_monitor_new (file);
261
-      g_signal_connect (kqueue_monitor->fallback,
262
-                        "changed",
263
-                        G_CALLBACK (_fallback_callback),
264
-                        kqueue_monitor);
265
-    }
266
-}
267
-
268
-static gboolean
269
-g_kqueue_file_monitor_is_supported (void)
270
-{
271
-  return _kh_startup ();
272
+  if (!_kqsub_start_watching (sub))
273
+    _km_add_missing (sub);
274
 }
275
 
276
 static void
277
@@ -175,24 +221,218 @@ g_kqueue_file_monitor_init (GKqueueFileM
278
 }
279
 
280
 static gboolean
281
+g_kqueue_file_monitor_callback (gint fd, GIOCondition condition, gpointer user_data)
282
+{
283
+  gint64 now = g_source_get_time (kq_source);
284
+  kqueue_sub *sub;
285
+  GFileMonitorSource *source;
286
+  struct kevent ev;
287
+  struct timespec ts;
288
+
289
+  memset (&ts, 0, sizeof(ts));
290
+  while (kevent(fd, NULL, 0, &ev, 1, &ts) > 0)
291
+    {
292
+        GFileMonitorEvent mask = 0;
293
+
294
+        if (ev.filter != EVFILT_VNODE || ev.udata == NULL)
295
+          continue;
296
+
297
+	sub = ev.udata;
298
+        source = sub->source;
299
+
300
+        if (ev.flags & EV_ERROR)
301
+          ev.fflags = NOTE_REVOKE;
302
+
303
+        if (ev.fflags & (NOTE_DELETE | NOTE_REVOKE))
304
+          {
305
+            _kqsub_cancel (sub);
306
+            _km_add_missing (sub);
307
+          }
308
+
309
+        if (sub->is_dir && ev.fflags & (NOTE_WRITE | NOTE_EXTEND))
310
+          {
311
+            _kh_dir_diff (sub);
312
+            ev.fflags &= ~(NOTE_WRITE | NOTE_EXTEND);
313
+          }
314
+
315
+        if (ev.fflags & NOTE_DELETE)
316
+          {
317
+            mask = G_FILE_MONITOR_EVENT_DELETED;
318
+          }
319
+        else if (ev.fflags & NOTE_ATTRIB)
320
+          {
321
+            mask = G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED;
322
+          }
323
+        else if (ev.fflags & (NOTE_WRITE | NOTE_EXTEND))
324
+          {
325
+            mask = G_FILE_MONITOR_EVENT_CHANGED;
326
+          }
327
+        else if (ev.fflags & NOTE_RENAME)
328
+          {
329
+            /* Since there’s apparently no way to get the new name of the
330
+             * file out of kqueue(), all we can do is say that this one has
331
+             * been deleted. */
332
+            mask = G_FILE_MONITOR_EVENT_DELETED;
333
+          }
334
+        else if (ev.fflags & NOTE_REVOKE)
335
+          {
336
+            mask = G_FILE_MONITOR_EVENT_UNMOUNTED;
337
+          }
338
+
339
+        if (mask)
340
+          g_file_monitor_source_handle_event (source, mask, NULL, NULL, NULL, now);
341
+    }
342
+
343
+  return TRUE;
344
+}
345
+
346
+static gboolean
347
+g_kqueue_file_monitor_is_supported (void)
348
+{
349
+  int errsv;
350
+
351
+  G_LOCK (kq_lock);
352
+
353
+  if (kq_queue == -1)
354
+    {
355
+      kq_queue = kqueue ();
356
+      errsv = errno;
357
+
358
+      if (kq_queue == -1)
359
+        {
360
+          g_warning ("Unable to create a kqueue: %s", g_strerror (errsv));
361
+          G_UNLOCK (kq_lock);
362
+          return FALSE;
363
+        }
364
+
365
+      kq_source = g_unix_fd_source_new (kq_queue, G_IO_IN);
366
+      g_source_set_callback (kq_source, (GSourceFunc) g_kqueue_file_monitor_callback, NULL, NULL);
367
+      g_source_attach (kq_source, GLIB_PRIVATE_CALL (g_get_worker_context) ());
368
+    }
369
+
370
+  G_UNLOCK (kq_lock);
371
+
372
+  return TRUE;
373
+}
374
+
375
+static gboolean
376
 g_kqueue_file_monitor_cancel (GFileMonitor *monitor)
377
 {
378
   GKqueueFileMonitor *kqueue_monitor = G_KQUEUE_FILE_MONITOR (monitor);
379
 
380
   if (kqueue_monitor->sub)
381
     {
382
-      _kh_cancel_sub (kqueue_monitor->sub);
383
-      _kh_sub_free (kqueue_monitor->sub);
384
+      _kqsub_cancel (kqueue_monitor->sub);
385
+      _kqsub_free (kqueue_monitor->sub);
386
       kqueue_monitor->sub = NULL;
387
     }
388
+#ifndef O_EVTONLY
389
   else if (kqueue_monitor->fallback)
390
     {
391
       g_signal_handlers_disconnect_by_func (kqueue_monitor->fallback, _fallback_callback, kqueue_monitor);
392
       g_file_monitor_cancel (kqueue_monitor->fallback);
393
     }
394
+#endif
395
 
396
   if (G_FILE_MONITOR_CLASS (g_kqueue_file_monitor_parent_class)->cancel)
397
     (*G_FILE_MONITOR_CLASS (g_kqueue_file_monitor_parent_class)->cancel) (monitor);
398
 
399
   return TRUE;
400
 }
401
+
402
+static kqueue_sub *
403
+_kqsub_new (const gchar *filename, GLocalFileMonitor *mon, GFileMonitorSource *source)
404
+{
405
+  kqueue_sub *sub;
406
+
407
+  sub = g_slice_new (kqueue_sub);
408
+  sub->filename = g_strdup (filename);
409
+  sub->mon = mon;
410
+  g_source_ref ((GSource *) source);
411
+  sub->source = source;
412
+  sub->fd = -1;
413
+  sub->deps = NULL;
414
+  sub->is_dir = 0;
415
+
416
+  return sub;
417
+}
418
+
419
+static void
420
+_kqsub_free (kqueue_sub *sub)
421
+{
422
+  g_assert (sub->deps == NULL);
423
+  g_assert (sub->fd == -1);
424
+
425
+  g_source_unref ((GSource *) sub->source);
426
+  g_free (sub->filename);
427
+  g_slice_free (kqueue_sub, sub);
428
+}
429
+
430
+static gboolean
431
+_kqsub_cancel (kqueue_sub *sub)
432
+{
433
+  struct kevent ev;
434
+
435
+  /* Remove the event and close the file descriptor to automatically
436
+   * delete pending events. */
437
+  if (sub->fd != -1)
438
+    {
439
+      EV_SET (&ev, sub->fd, EVFILT_VNODE, EV_DELETE, NOTE_ALL, 0, sub);
440
+      if (kevent (kq_queue, &ev, 1, NULL, 0, NULL) == -1)
441
+        {
442
+          g_warning ("Unable to remove event for %s: %s", sub->filename, g_strerror (errno));
443
+          return FALSE;
444
+        }
445
+      close (sub->fd);
446
+      sub->fd = -1;
447
+    }
448
+
449
+  _km_remove (sub);
450
+
451
+  if (sub->deps)
452
+    {
453
+      dl_free (sub->deps);
454
+      sub->deps = NULL;
455
+    }
456
+
457
+  return TRUE;
458
+}
459
+
460
+gboolean
461
+_kqsub_start_watching (kqueue_sub *sub)
462
+{
463
+  struct stat st;
464
+  struct kevent ev;
465
+
466
+  sub->fd = open (sub->filename, O_KQFLAG);
467
+  if (sub->fd == -1)
468
+      return FALSE;
469
+
470
+  if (fstat (sub->fd, &st) == -1)
471
+    {
472
+      g_warning ("fstat failed for %s: %s", sub->filename, g_strerror (errno));
473
+      close (sub->fd);
474
+      sub->fd = -1;
475
+      return FALSE;
476
+    }
477
+
478
+  sub->is_dir = (st.st_mode & S_IFDIR) ? 1 : 0;
479
+  if (sub->is_dir)
480
+    {
481
+      if (sub->deps)
482
+        dl_free (sub->deps);
483
+
484
+      sub->deps = dl_listing (sub->filename);
485
+    }
486
+
487
+  EV_SET (&ev, sub->fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, NOTE_ALL, 0, sub);
488
+  if (kevent (kq_queue, &ev, 1, NULL, 0, NULL) == -1)
489
+    {
490
+      g_warning ("Unable to add event for %s: %s", sub->filename, g_strerror (errno));
491
+      close (sub->fd);
492
+      sub->fd = -1;
493
+      return FALSE;
494
+    }
495
+
496
+  return TRUE;
497
+}
498
--- gio/kqueue/kqueue-helper.c.orig	2016-10-22 05:18:22 UTC
499
+++ gio/kqueue/kqueue-helper.c
500
@@ -34,81 +34,6 @@
501
 #include <errno.h>
502
 #include <pthread.h>
503
 #include "kqueue-helper.h"
504
-#include "kqueue-utils.h"
505
-#include "kqueue-thread.h"
506
-#include "kqueue-missing.h"
507
-#include "kqueue-exclusions.h"
508
-
509
-static gboolean kh_debug_enabled = FALSE;
510
-#define KH_W if (kh_debug_enabled) g_warning
511
-
512
-static GHashTable *subs_hash_table = NULL;
513
-G_LOCK_DEFINE_STATIC (hash_lock);
514
-
515
-static int kqueue_descriptor = -1;
516
-static int kqueue_socket_pair[] = {-1, -1};
517
-static pthread_t kqueue_thread;
518
-
519
-
520
-void _kh_file_appeared_cb (kqueue_sub *sub);
521
-
522
-/**
523
- * accessor function for kqueue_descriptor
524
- **/
525
-int
526
-get_kqueue_descriptor()
527
-{
528
-  return kqueue_descriptor;
529
-}
530
-
531
-/**
532
- * convert_kqueue_events_to_gio:
533
- * @flags: a set of kqueue filter flags
534
- * @done: a pointer to #gboolean indicating that the
535
- *      conversion has been done (out)
536
- *
537
- * Translates kqueue filter flags into GIO event flags.
538
- *
539
- * Returns: a #GFileMonitorEvent
540
- **/
541
-static GFileMonitorEvent
542
-convert_kqueue_events_to_gio (uint32_t flags, gboolean *done)
543
-{
544
-  g_assert (done != NULL);
545
-  *done = FALSE;
546
-
547
-  /* TODO: The following notifications should be emulated, if possible:
548
-   * - G_FILE_MONITOR_EVENT_PRE_UNMOUNT
549
-   */
550
-  if (flags & NOTE_DELETE)
551
-    {    
552
-      *done = TRUE;
553
-      return G_FILE_MONITOR_EVENT_DELETED;
554
-    }
555
-  if (flags & NOTE_ATTRIB)
556
-    {
557
-      *done = TRUE;
558
-      return G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED;
559
-    }
560
-  if (flags & (NOTE_WRITE | NOTE_EXTEND))
561
-    {
562
-      *done = TRUE;
563
-      return G_FILE_MONITOR_EVENT_CHANGED;
564
-    }
565
-  if (flags & NOTE_RENAME)
566
-    {
567
-      *done = TRUE;
568
-      return G_FILE_MONITOR_EVENT_MOVED;
569
-    }
570
-  if (flags & NOTE_REVOKE)
571
-    {
572
-      *done = TRUE;
573
-      return G_FILE_MONITOR_EVENT_UNMOUNTED;
574
-    }
575
-
576
-  /* done is FALSE */
577
-  return 0;
578
-}
579
 
580
 typedef struct {
581
   kqueue_sub *sub;
582
@@ -236,323 +161,21 @@ static const traverse_cbs cbs = {
583
 
584
 
585
 void
586
-_kh_dir_diff (kqueue_sub *sub, GFileMonitorSource *source)
587
+_kh_dir_diff (kqueue_sub *sub)
588
 {
589
   dep_list *was;
590
   handle_ctx ctx;
591
 
592
-  g_assert (sub != NULL);
593
-  g_assert (source != NULL);
594
-
595
   memset (&ctx, 0, sizeof (handle_ctx));
596
   ctx.sub = sub;
597
-  ctx.source = source;
598
+  ctx.source = sub->source;
599
 
600
   was = sub->deps;
601
   sub->deps = dl_listing (sub->filename);
602
- 
603
+
604
   dl_calculate (was, sub->deps, &cbs, &ctx);
605
 
606
   dl_free (was);
607
 }
608
 
609
 
610
-/**
611
- * process_kqueue_notifications:
612
- * @gioc: unused.
613
- * @cond: unused.
614
- * @data: unused.
615
- *
616
- * Processes notifications, coming from the kqueue thread.
617
- *
618
- * Reads notifications from the command file descriptor, emits the
619
- * "changed" event on the appropriate monitor.
620
- *
621
- * A typical GIO Channel callback function.
622
- *
623
- * Returns: %TRUE
624
- **/
625
-static gboolean
626
-process_kqueue_notifications (GIOChannel   *gioc,
627
-                              GIOCondition  cond,
628
-                              gpointer      data)
629
-{
630
-  struct kqueue_notification n;
631
-  kqueue_sub *sub = NULL;
632
-  GFileMonitorSource *source = NULL;
633
-  GFileMonitorEvent mask = 0;
634
-  
635
-  g_assert (kqueue_socket_pair[0] != -1);
636
-  if (!_ku_read (kqueue_socket_pair[0], &n, sizeof (struct kqueue_notification)))
637
-    {
638
-      KH_W ("Failed to read a kqueue notification, error %d", errno);
639
-      return TRUE;
640
-    }
641
-
642
-  G_LOCK (hash_lock);
643
-  sub = (kqueue_sub *) g_hash_table_lookup (subs_hash_table, GINT_TO_POINTER (n.fd));
644
-  G_UNLOCK (hash_lock);
645
-
646
-  if (sub == NULL)
647
-    {
648
-      KH_W ("Got a notification for a deleted or non-existing subscription %d",
649
-             n.fd);
650
-      return TRUE;
651
-    }
652
-
653
-  source = sub->user_data;
654
-  g_assert (source != NULL);
655
-
656
-  if (n.flags & (NOTE_DELETE | NOTE_REVOKE))
657
-    {
658
-      if (sub->deps)
659
-        {
660
-          dl_free (sub->deps);
661
-          sub->deps = NULL;  
662
-        }  
663
-      _km_add_missing (sub);
664
-
665
-      if (!(n.flags & NOTE_REVOKE))
666
-        {
667
-          /* Note that NOTE_REVOKE is issued by the kqueue thread
668
-           * on EV_ERROR kevent. In this case, a file descriptor is
669
-           * already closed from the kqueue thread, no need to close
670
-           * it manually */ 
671
-          _kh_cancel_sub (sub);
672
-        }
673
-    }
674
-
675
-  if (sub->is_dir && n.flags & (NOTE_WRITE | NOTE_EXTEND))
676
-    {
677
-      _kh_dir_diff (sub, source);
678
-      n.flags &= ~(NOTE_WRITE | NOTE_EXTEND);
679
-    }
680
-
681
-  if (n.flags)
682
-    {
683
-      gboolean done = FALSE;
684
-      mask = convert_kqueue_events_to_gio (n.flags, &done);
685
-      if (done == TRUE)
686
-        g_file_monitor_source_handle_event (source, mask, NULL, NULL, NULL, g_get_monotonic_time ());
687
-    }
688
-
689
-  return TRUE;
690
-}
691
-
692
-
693
-/*
694
- * _kh_startup_impl:
695
- * @unused: unused
696
- *
697
- * Kqueue backend startup code. Should be called only once.
698
- *
699
- * Returns: %TRUE on success, %FALSE otherwise.
700
- **/
701
-static gpointer
702
-_kh_startup_impl (gpointer unused)
703
-{
704
-  GIOChannel *channel = NULL;
705
-  gboolean result = FALSE;
706
-
707
-  kqueue_descriptor = kqueue ();
708
-  result = (kqueue_descriptor != -1);
709
-  if (!result)
710
-    {
711
-      KH_W ("Failed to initialize kqueue\n!");
712
-      return GINT_TO_POINTER (FALSE);
713
-    }
714
-
715
-  result = socketpair (AF_UNIX, SOCK_STREAM, 0, kqueue_socket_pair);
716
-  if (result != 0)
717
-    {
718
-      KH_W ("Failed to create socket pair\n!");
719
-      return GINT_TO_POINTER (FALSE) ;
720
-    }
721
-
722
-  result = pthread_create (&kqueue_thread,
723
-                           NULL,
724
-                           _kqueue_thread_func,
725
-                           &kqueue_socket_pair[1]);
726
-  if (result != 0)
727
-    {
728
-      KH_W ("Failed to run kqueue thread\n!");
729
-      return GINT_TO_POINTER (FALSE);
730
-    }
731
-
732
-  _km_init (_kh_file_appeared_cb);
733
-
734
-  channel = g_io_channel_unix_new (kqueue_socket_pair[0]);
735
-  g_io_add_watch (channel, G_IO_IN, process_kqueue_notifications, NULL);
736
-
737
-  subs_hash_table = g_hash_table_new (g_direct_hash, g_direct_equal);
738
-
739
-  KH_W ("started gio kqueue backend\n");
740
-  return GINT_TO_POINTER (TRUE);
741
-}
742
-
743
-
744
-/*
745
- * _kh_startup:
746
- * Kqueue backend initialization.
747
- *
748
- * Returns: %TRUE on success, %FALSE otherwise.
749
- **/
750
-gboolean
751
-_kh_startup (void)
752
-{
753
-  static GOnce init_once = G_ONCE_INIT;
754
-  g_once (&init_once, _kh_startup_impl, NULL);
755
-  return GPOINTER_TO_INT (init_once.retval);
756
-}
757
-
758
-
759
-/**
760
- * _kh_start_watching:
761
- * @sub: a #kqueue_sub
762
- *
763
- * Starts watching on a subscription.
764
- *
765
- * Returns: %TRUE on success, %FALSE otherwise.
766
- **/
767
-gboolean
768
-_kh_start_watching (kqueue_sub *sub)
769
-{
770
-  g_assert (kqueue_socket_pair[0] != -1);
771
-  g_assert (sub != NULL);
772
-  g_assert (sub->filename != NULL);
773
-
774
-  /* kqueue requires a file descriptor to monitor. Sad but true */
775
-#if defined (O_EVTONLY)
776
-  sub->fd = open (sub->filename, O_EVTONLY);
777
-#else
778
-  sub->fd = open (sub->filename, O_RDONLY);
779
-#endif
780
-
781
-  if (sub->fd == -1)
782
-    {
783
-      KH_W ("failed to open file %s (error %d)", sub->filename, errno);
784
-      return FALSE;
785
-    }
786
-
787
-  _ku_file_information (sub->fd, &sub->is_dir, NULL);
788
-  if (sub->is_dir)
789
-    {
790
-      /* I know, it is very bad to make such decisions in this way and here.
791
-       * We already do have an user_data at the #kqueue_sub, and it may point to
792
-       * GKqueueFileMonitor or GKqueueDirectoryMonitor. For a directory case,
793
-       * we need to scan in contents for the further diffs. Ideally this process
794
-       * should be delegated to the GKqueueDirectoryMonitor, but for now I will
795
-       * do it in a dirty way right here. */
796
-      if (sub->deps)
797
-        dl_free (sub->deps);
798
-
799
-      sub->deps = dl_listing (sub->filename);  
800
-    }
801
-
802
-  G_LOCK (hash_lock);
803
-  g_hash_table_insert (subs_hash_table, GINT_TO_POINTER (sub->fd), sub);
804
-  G_UNLOCK (hash_lock);
805
-
806
-  _kqueue_thread_push_fd (sub->fd);
807
-  
808
-  /* Bump the kqueue thread. It will pick up a new sub entry to monitor */
809
-  if (!_ku_write (kqueue_socket_pair[0], "A", 1))
810
-    KH_W ("Failed to bump the kqueue thread (add fd, error %d)", errno);
811
-  return TRUE;
812
-}
813
-
814
-
815
-/**
816
- * _kh_add_sub:
817
- * @sub: a #kqueue_sub
818
- *
819
- * Adds a subscription for monitoring.
820
- *
821
- * This funciton tries to start watching a subscription with
822
- * _kh_start_watching(). On failure, i.e. when a file does not exist yet,
823
- * the subscription will be added to a list of missing files to continue
824
- * watching when the file will appear.
825
- *
826
- * Returns: %TRUE
827
- **/
828
-gboolean
829
-_kh_add_sub (kqueue_sub *sub)
830
-{
831
-  g_assert (sub != NULL);
832
-
833
-  if (!_kh_start_watching (sub))
834
-    _km_add_missing (sub);
835
-
836
-  return TRUE;
837
-}
838
-
839
-
840
-/**
841
- * _kh_cancel_sub:
842
- * @sub a #kqueue_sub
843
- *
844
- * Stops monitoring on a subscription.
845
- *
846
- * Returns: %TRUE
847
- **/
848
-gboolean
849
-_kh_cancel_sub (kqueue_sub *sub)
850
-{
851
-  gboolean missing = FALSE;
852
-  g_assert (kqueue_socket_pair[0] != -1);
853
-  g_assert (sub != NULL);
854
-
855
-  G_LOCK (hash_lock);
856
-  missing = !g_hash_table_remove (subs_hash_table, GINT_TO_POINTER (sub->fd));
857
-  G_UNLOCK (hash_lock);
858
-
859
-  if (missing)
860
-    {
861
-      /* If there were no fd for this subscription, file is still
862
-       * missing. */
863
-      KH_W ("Removing subscription from missing");
864
-      _km_remove (sub);
865
-    }
866
-  else
867
-    {
868
-      /* fd will be closed in the kqueue thread */
869
-      _kqueue_thread_remove_fd (sub->fd);
870
-
871
-      /* Bump the kqueue thread. It will pick up a new sub entry to remove*/
872
-      if (!_ku_write (kqueue_socket_pair[0], "R", 1))
873
-        KH_W ("Failed to bump the kqueue thread (remove fd, error %d)", errno);
874
-    }
875
-
876
-  return TRUE;
877
-}
878
-
879
-
880
-/**
881
- * _kh_file_appeared_cb:
882
- * @sub: a #kqueue_sub
883
- *
884
- * A callback function for kqueue-missing subsystem.
885
- *
886
- * Signals that a missing file has finally appeared in the filesystem.
887
- * Emits %G_FILE_MONITOR_EVENT_CREATED.
888
- **/
889
-void
890
-_kh_file_appeared_cb (kqueue_sub *sub)
891
-{
892
-  GFile* child;
893
-
894
-  g_assert (sub != NULL);
895
-  g_assert (sub->filename);
896
-
897
-  if (!g_file_test (sub->filename, G_FILE_TEST_EXISTS))
898
-    return;
899
-
900
-  child = g_file_new_for_path (sub->filename);
901
-
902
-  g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
903
-                             child,
904
-                             NULL,
905
-                             G_FILE_MONITOR_EVENT_CREATED);
906
-
907
-  g_object_unref (child);
908
-}
909
--- gio/kqueue/kqueue-helper.h.orig	2016-10-22 05:18:22 UTC
910
+++ gio/kqueue/kqueue-helper.h
911
@@ -23,16 +23,31 @@
912
 #ifndef __KQUEUE_HELPER_H
913
 #define __KQUEUE_HELPER_H
914
 
915
-#include "kqueue-sub.h"
916
 #include <gio/glocalfilemonitor.h>
917
 #include <gio/gfilemonitor.h>
918
 
919
-gboolean _kh_startup        (void);
920
-gboolean _kh_add_sub        (kqueue_sub *sub);
921
-gboolean _kh_cancel_sub     (kqueue_sub *sub);
922
+#include "dep-list.h"
923
 
924
-gboolean _kh_start_watching (kqueue_sub *sub);
925
+/**
926
+ * kqueue_sub:
927
+ * @filename: a name of the file to monitor
928
+ * @fd: the associated file descriptor (used by kqueue)
929
+ *
930
+ * Represents a subscription on a file or directory.
931
+ */
932
+typedef struct
933
+{
934
+  GLocalFileMonitor   *mon;
935
+  GFileMonitorSource  *source;
936
+  gchar*    filename;
937
+  int       fd;
938
+  dep_list* deps;
939
+  int       is_dir;
940
+} kqueue_sub;
941
 
942
-void     _kh_dir_diff       (kqueue_sub *sub, GFileMonitorSource *source);
943
+gboolean _kqsub_start_watching (kqueue_sub *sub);
944
+void _kh_dir_diff    (kqueue_sub *sub);
945
+void _km_add_missing (kqueue_sub *sub);
946
+void _km_remove      (kqueue_sub *sub);
947
 
948
 #endif /* __KQUEUE_HELPER_H */
949
--- gio/kqueue/kqueue-missing.c.orig	2016-10-22 05:18:22 UTC
950
+++ gio/kqueue/kqueue-missing.c
951
@@ -23,12 +23,12 @@
952
 #include <glib.h>
953
 
954
 #include "kqueue-helper.h"
955
-#include "kqueue-sub.h"
956
-#include "kqueue-missing.h"
957
 
958
 
959
 #define SCAN_MISSING_TIME 4 /* 1/4 Hz */
960
 
961
+void _kh_file_appeared_cb (kqueue_sub *sub);
962
+
963
 static gboolean km_scan_missing (gpointer user_data);
964
 
965
 static gboolean km_debug_enabled = FALSE;
966
@@ -38,21 +38,6 @@ static GSList *missing_subs_list = NULL;
967
 G_LOCK_DEFINE_STATIC (missing_lock);
968
 
969
 static volatile gboolean scan_missing_running = FALSE;
970
-static on_create_cb file_appeared_callback;
971
-
972
-
973
-/**
974
- * _km_init:
975
- * @cb: a callback function. It will be called when a watched file
976
- *     will appear.
977
- *
978
- * Initialize the kqueue-missing module (optional).
979
- **/
980
-void
981
-_km_init (on_create_cb cb)
982
-{
983
-  file_appeared_callback = cb;
984
-}
985
 
986
 
987
 /**
988
@@ -83,6 +68,35 @@ _km_add_missing (kqueue_sub *sub)
989
     }
990
 }
991
 
992
+/**
993
+ * _kh_file_appeared_cb:
994
+ * @sub: a #kqueue_sub
995
+ *
996
+ * A callback function for kqueue-missing subsystem.
997
+ *
998
+ * Signals that a missing file has finally appeared in the filesystem.
999
+ * Emits %G_FILE_MONITOR_EVENT_CREATED.
1000
+ **/
1001
+void
1002
+_kh_file_appeared_cb (kqueue_sub *sub)
1003
+{
1004
+  GFile *child;
1005
+
1006
+  g_assert (sub != NULL);
1007
+  g_assert (sub->filename);
1008
+
1009
+  if (!g_file_test (sub->filename, G_FILE_TEST_EXISTS))
1010
+    return;
1011
+
1012
+  child = g_file_new_for_path (sub->filename);
1013
+
1014
+  g_file_monitor_emit_event (G_FILE_MONITOR (sub->mon),
1015
+                             child,
1016
+                             NULL,
1017
+                             G_FILE_MONITOR_EVENT_CREATED);
1018
+
1019
+  g_object_unref (child);
1020
+}
1021
 
1022
 /**
1023
  * km_scan_missing:
1024
@@ -114,11 +128,10 @@ km_scan_missing (gpointer user_data)
1025
       g_assert (sub != NULL);
1026
       g_assert (sub->filename != NULL);
1027
 
1028
-      if (_kh_start_watching (sub))
1029
+      if (_kqsub_start_watching (sub))
1030
         {
1031
           KM_W ("file %s now exists, starting watching", sub->filename);
1032
-          if (file_appeared_callback)
1033
-            file_appeared_callback (sub);
1034
+          _kh_file_appeared_cb (sub);
1035
           not_missing = g_slist_prepend (not_missing, head);
1036
         }
1037
     }
(-)files/patch-gio_kqueue_gkqueuefilemonitor.c (-45 lines)
Lines 1-45 Link Here
1
https://bugzilla.gnome.org/show_bug.cgi?id=739424
2
https://bug739424.bugzilla-attachments.gnome.org/attachment.cgi?id=351191
3
4
--- gio/kqueue/gkqueuefilemonitor.c.orig	2018-01-15 21:00:32.535064000 +0100
5
+++ gio/kqueue/gkqueuefilemonitor.c	2018-01-15 21:07:20.920334000 +0100
6
@@ -29,6 +29,15 @@
7
 #include <gio/gfile.h>
8
 #include <gio/giomodule.h>
9
 
10
+/*
11
+ * Because ``kqueue_sub'' are not refcounted, we need
12
+ * ensure no other thread is getting a reference to
13
+ * the element we want to free.
14
+ *
15
+ * That's why _kh_cancel_sub() must be called with
16
+ * this lock held to prevent a race.
17
+ */
18
+G_LOCK_EXTERN (hash_lock);
19
 
20
 struct _GKqueueFileMonitor
21
 {
22
@@ -80,9 +89,11 @@ g_kqueue_file_monitor_finalize (GObject *object)
23
 
24
   if (kqueue_monitor->sub)
25
     {
26
+      G_LOCK (hash_lock);
27
       _kh_cancel_sub (kqueue_monitor->sub);
28
       _kh_sub_free (kqueue_monitor->sub);
29
       kqueue_monitor->sub = NULL;
30
+      G_UNLOCK (hash_lock);
31
     }
32
 
33
   if (kqueue_monitor->fallback)
34
@@ -181,9 +192,11 @@ g_kqueue_file_monitor_cancel (GFileMonitor *monitor)
35
 
36
   if (kqueue_monitor->sub)
37
     {
38
+      G_LOCK (hash_lock);
39
       _kh_cancel_sub (kqueue_monitor->sub);
40
       _kh_sub_free (kqueue_monitor->sub);
41
       kqueue_monitor->sub = NULL;
42
+      G_UNLOCK (hash_lock);
43
     }
44
   else if (kqueue_monitor->fallback)
45
     {
(-)files/patch-gio_kqueue_kqueue-helper.c (-96 lines)
Lines 1-96 Link Here
1
This bug combines serveral patches:
2
https://bugzilla.gnome.org/show_bug.cgi?id=778515
3
and
4
https://bugzilla.gnome.org/show_bug.cgi?id=739424
5
https://bug739424.bugzilla-attachments.gnome.org/attachment.cgi?id=351191
6
7
https://bugzilla.gnome.org/show_bug.cgi?id=776147
8
https://git.gnome.org/browse/glib/commit/?id=76072a2dde4a4acc8be8d3c47efbc6811ebe0c1e
9
10
--- gio/kqueue/kqueue-helper.c.orig	2018-01-28 21:18:25.213606000 +0100
11
+++ gio/kqueue/kqueue-helper.c	2018-01-28 21:18:34.964780000 +0100
12
@@ -43,7 +43,7 @@ static gboolean kh_debug_enabled = FALSE;
13
 #define KH_W if (kh_debug_enabled) g_warning
14
 
15
 static GHashTable *subs_hash_table = NULL;
16
-G_LOCK_DEFINE_STATIC (hash_lock);
17
+G_LOCK_DEFINE (hash_lock);
18
 
19
 static int kqueue_descriptor = -1;
20
 static int kqueue_socket_pair[] = {-1, -1};
21
@@ -97,8 +97,10 @@ convert_kqueue_events_to_gio (uint32_t flags, gboolean
22
     }
23
   if (flags & NOTE_RENAME)
24
     {
25
+      /* Since there’s apparently no way to get the new name of the file out of
26
+       * kqueue(), all we can do is say that this one has been deleted. */
27
       *done = TRUE;
28
-      return G_FILE_MONITOR_EVENT_MOVED;
29
+      return G_FILE_MONITOR_EVENT_DELETED;
30
     }
31
   if (flags & NOTE_REVOKE)
32
     {
33
@@ -291,10 +293,10 @@ process_kqueue_notifications (GIOChannel   *gioc,
34
 
35
   G_LOCK (hash_lock);
36
   sub = (kqueue_sub *) g_hash_table_lookup (subs_hash_table, GINT_TO_POINTER (n.fd));
37
-  G_UNLOCK (hash_lock);
38
 
39
   if (sub == NULL)
40
     {
41
+      G_UNLOCK (hash_lock);
42
       KH_W ("Got a notification for a deleted or non-existing subscription %d",
43
              n.fd);
44
       return TRUE;
45
@@ -336,6 +338,7 @@ process_kqueue_notifications (GIOChannel   *gioc,
46
         g_file_monitor_source_handle_event (source, mask, NULL, NULL, NULL, g_get_monotonic_time ());
47
     }
48
 
49
+  G_UNLOCK (hash_lock);
50
   return TRUE;
51
 }
52
 
53
@@ -451,13 +454,14 @@ _kh_start_watching (kqueue_sub *sub)
54
 
55
   G_LOCK (hash_lock);
56
   g_hash_table_insert (subs_hash_table, GINT_TO_POINTER (sub->fd), sub);
57
-  G_UNLOCK (hash_lock);
58
 
59
   _kqueue_thread_push_fd (sub->fd);
60
   
61
   /* Bump the kqueue thread. It will pick up a new sub entry to monitor */
62
   if (!_ku_write (kqueue_socket_pair[0], "A", 1))
63
     KH_W ("Failed to bump the kqueue thread (add fd, error %d)", errno);
64
+  G_UNLOCK (hash_lock);
65
+
66
   return TRUE;
67
 }
68
 
69
@@ -498,22 +502,15 @@ _kh_add_sub (kqueue_sub *sub)
70
 gboolean
71
 _kh_cancel_sub (kqueue_sub *sub)
72
 {
73
-  gboolean missing = FALSE;
74
+  gboolean removed = FALSE;
75
   g_assert (kqueue_socket_pair[0] != -1);
76
   g_assert (sub != NULL);
77
 
78
-  G_LOCK (hash_lock);
79
-  missing = !g_hash_table_remove (subs_hash_table, GINT_TO_POINTER (sub->fd));
80
-  G_UNLOCK (hash_lock);
81
+  _km_remove (sub);
82
 
83
-  if (missing)
84
-    {
85
-      /* If there were no fd for this subscription, file is still
86
-       * missing. */
87
-      KH_W ("Removing subscription from missing");
88
-      _km_remove (sub);
89
-    }
90
-  else
91
+  removed = g_hash_table_remove (subs_hash_table, GINT_TO_POINTER (sub->fd));
92
+
93
+  if (removed)
94
     {
95
       /* fd will be closed in the kqueue thread */
96
       _kqueue_thread_remove_fd (sub->fd);

Return to bug 226920