rb-fsevent 0.9.2 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/Gemfile +3 -0
  4. data/Guardfile +8 -0
  5. data/{LICENSE → LICENSE.txt} +3 -1
  6. data/README.md +260 -0
  7. data/Rakefile +33 -0
  8. data/bin/fsevent_watch +0 -0
  9. data/ext/{fsevent_watch/LICENSE → LICENSE} +1 -1
  10. data/ext/fsevent_watch/FSEventsFix.c +626 -0
  11. data/ext/fsevent_watch/FSEventsFix.h +105 -0
  12. data/ext/fsevent_watch/{fsevent_watch/TSICTString.c → TSICTString.c} +34 -55
  13. data/ext/fsevent_watch/{fsevent_watch/TSICTString.h → TSICTString.h} +0 -0
  14. data/ext/fsevent_watch/{fsevent_watch/cli.c → cli.c} +48 -7
  15. data/ext/fsevent_watch/{fsevent_watch/cli.h → cli.h} +3 -3
  16. data/ext/fsevent_watch/{fsevent_watch/common.h → common.h} +1 -13
  17. data/ext/fsevent_watch/compat.c +41 -0
  18. data/ext/fsevent_watch/compat.h +100 -0
  19. data/ext/fsevent_watch/defines.h +42 -0
  20. data/ext/fsevent_watch/{fsevent_watch/main.c → main.c} +101 -62
  21. data/ext/fsevent_watch/signal_handlers.c +66 -0
  22. data/ext/fsevent_watch/signal_handlers.h +16 -0
  23. data/ext/rakefile.rb +225 -41
  24. data/lib/otnetstring.rb +85 -0
  25. data/lib/rb-fsevent/fsevent.rb +53 -7
  26. data/lib/rb-fsevent/version.rb +3 -1
  27. data/lib/rb-fsevent.rb +2 -1
  28. data/rb-fsevent.gemspec +26 -0
  29. metadata +53 -56
  30. data/README.rdoc +0 -255
  31. data/ext/fsevent_watch/Info.plist +0 -38
  32. data/ext/fsevent_watch/fsevent_watch/compat.c +0 -20
  33. data/ext/fsevent_watch/fsevent_watch/compat.h +0 -40
  34. data/ext/fsevent_watch/fsevent_watch.xcodeproj/project.pbxproj +0 -254
  35. data/ext/fsevent_watch/xcconfig/Common.xcconfig +0 -82
  36. data/ext/fsevent_watch/xcconfig/Debug.xcconfig +0 -19
  37. data/ext/fsevent_watch/xcconfig/Release.xcconfig +0 -23
  38. data/ext/fsevent_watch/xcconfig/fsevent_watch.xcconfig +0 -17
  39. data/ext/rb-fsevent.xcconfig +0 -33
@@ -0,0 +1,626 @@
1
+ /*
2
+ * FSEventsFix
3
+ *
4
+ * Resolves a long-standing bug in realpath() that prevents FSEvents API from
5
+ * monitoring certain folders on a wide range of OS X released (10.6-10.10 at least).
6
+ *
7
+ * The underlying issue is that for some folders, realpath() call starts returning
8
+ * a path with incorrect casing (e.g. "/users/smt" instead of "/Users/smt").
9
+ * FSEvents is case-sensitive and calls realpath() on the paths you pass in, so
10
+ * an incorrect value returned by realpath() prevents FSEvents from seeing any
11
+ * change events.
12
+ *
13
+ * See the discussion at https://github.com/thibaudgg/rb-fsevent/issues/10 about
14
+ * the history of this bug and how this library came to exist.
15
+ *
16
+ * This library uses Facebook's fishhook to replace a custom implementation of
17
+ * realpath in place of the system realpath; FSEvents will then invoke our custom
18
+ * implementation (which does not screw up the names) and will thus work correctly.
19
+ *
20
+ * Our implementation of realpath is based on the open-source implementation from
21
+ * OS X 10.10, with a single change applied (enclosed in "BEGIN WORKAROUND FOR
22
+ * OS X BUG" ... "END WORKAROUND FOR OS X BUG").
23
+ *
24
+ * Include FSEventsFix.{h,c} into your project and call FSEventsFixInstall().
25
+ *
26
+ * It is recommended that you install FSEventsFix on demand, using FSEventsFixIsBroken
27
+ * to check if the folder you're about to pass to FSEventStreamCreate needs the fix.
28
+ * Note that the fix must be applied before calling FSEventStreamCreate.
29
+ *
30
+ * FSEventsFixIsBroken requires a path that uses the correct case for all folder names,
31
+ * i.e. a path provided by the system APIs or constructed from folder names provided
32
+ * by the directory enumeration APIs.
33
+ *
34
+ * Copyright (c) 2015 Andrey Tarantsov <andrey@tarantsov.com>
35
+ * Copyright (c) 2003 Constantin S. Svintsoff <kostik@iclub.nsu.ru>
36
+ *
37
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
38
+ * of this software and associated documentation files (the "Software"), to deal
39
+ * in the Software without restriction, including without limitation the rights
40
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
41
+ * copies of the Software, and to permit persons to whom the Software is
42
+ * furnished to do so, subject to the following conditions:
43
+ *
44
+ * The above copyright notice and this permission notice shall be included in
45
+ * all copies or substantial portions of the Software.
46
+ *
47
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
48
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
49
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
50
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
51
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
52
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
53
+ * THE SOFTWARE.
54
+ *
55
+ * Based on a realpath implementation from Apple libc 498.1.7, taken from
56
+ * http://www.opensource.apple.com/source/Libc/Libc-498.1.7/stdlib/FreeBSD/realpath.c
57
+ * and provided under the following license:
58
+ *
59
+ * Copyright (c) 2003 Constantin S. Svintsoff <kostik@iclub.nsu.ru>
60
+ *
61
+ * Redistribution and use in source and binary forms, with or without
62
+ * modification, are permitted provided that the following conditions
63
+ * are met:
64
+ * 1. Redistributions of source code must retain the above copyright
65
+ * notice, this list of conditions and the following disclaimer.
66
+ * 2. Redistributions in binary form must reproduce the above copyright
67
+ * notice, this list of conditions and the following disclaimer in the
68
+ * documentation and/or other materials provided with the distribution.
69
+ * 3. The names of the authors may not be used to endorse or promote
70
+ * products derived from this software without specific prior written
71
+ * permission.
72
+ *
73
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
74
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
77
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
78
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
79
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
80
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
81
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
82
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
83
+ * SUCH DAMAGE.
84
+ */
85
+
86
+
87
+ #include "FSEventsFix.h"
88
+
89
+ #include <dispatch/dispatch.h>
90
+ #include <stddef.h>
91
+ #include <stdint.h>
92
+ #include <stdbool.h>
93
+ #include <string.h>
94
+ #include <stdio.h>
95
+ #include <stdlib.h>
96
+ #include <stdarg.h>
97
+ #include <ctype.h>
98
+ #include <dlfcn.h>
99
+
100
+
101
+ const char *const FSEventsFixVersionString = "0.11.0";
102
+
103
+
104
+ #pragma mark - Forward declarations
105
+
106
+ static char *(*orig_realpath)(const char *restrict file_name, char resolved_name[PATH_MAX]);
107
+ static char *CFURL_realpath(const char *restrict file_name, char resolved_name[PATH_MAX]);
108
+ static char *FSEventsFix_realpath_wrapper(const char *restrict src, char *restrict dst);
109
+
110
+ static void _FSEventsFixHookInstall();
111
+ static void _FSEventsFixHookUninstall();
112
+
113
+
114
+ #pragma mark - Internal state
115
+
116
+ static dispatch_queue_t g_queue = NULL;
117
+
118
+ static int64_t g_enable_refcount = 0;
119
+
120
+ static bool g_in_self_test = false;
121
+ static bool g_hook_operational = false;
122
+
123
+ static void(^g_logging_block)(FSEventsFixMessageType type, const char *message);
124
+ static FSEventsFixDebugOptions g_debug_opt = 0;
125
+
126
+ typedef struct {
127
+ char *name;
128
+ void *replacement;
129
+ void *original;
130
+ uint hooked_symbols;
131
+ } rebinding_t;
132
+
133
+ static rebinding_t g_rebindings[] = {
134
+ { "_realpath$DARWIN_EXTSN", (void *) &FSEventsFix_realpath_wrapper, (void *) &realpath, 0 }
135
+ };
136
+ static const uint g_rebindings_nel = sizeof(g_rebindings) / sizeof(g_rebindings[0]);
137
+
138
+
139
+ #pragma mark - Logging
140
+
141
+ static void _FSEventsFixLog(FSEventsFixMessageType type, const char *__restrict fmt, ...) __attribute__((__format__ (__printf__, 2, 3)));
142
+
143
+ static void _FSEventsFixLog(FSEventsFixMessageType type, const char *__restrict fmt, ...) {
144
+ if (g_logging_block) {
145
+ char *message = NULL;
146
+ va_list va;
147
+ va_start(va, fmt);
148
+ vasprintf(&message, fmt, va);
149
+ va_end(va);
150
+
151
+ if (message) {
152
+ if (!!(g_debug_opt & FSEventsFixDebugOptionLogToStderr)) {
153
+ fprintf(stderr, "FSEventsFix: %s\n", message);
154
+ }
155
+ if (g_logging_block) {
156
+ g_logging_block(type, message);
157
+ }
158
+ free(message);
159
+ }
160
+ }
161
+ }
162
+
163
+
164
+ #pragma mark - API
165
+
166
+ void _FSEventsFixInitialize() {
167
+ static dispatch_once_t onceToken;
168
+ dispatch_once(&onceToken, ^{
169
+ g_queue = dispatch_queue_create("FSEventsFix", DISPATCH_QUEUE_SERIAL);
170
+ });
171
+ }
172
+
173
+ void FSEventsFixConfigure(FSEventsFixDebugOptions debugOptions, void(^loggingBlock)(FSEventsFixMessageType severity, const char *message)) {
174
+ _FSEventsFixInitialize();
175
+ loggingBlock = Block_copy(loggingBlock);
176
+ dispatch_sync(g_queue, ^{
177
+ g_debug_opt = debugOptions;
178
+ g_logging_block = loggingBlock;
179
+ });
180
+ }
181
+
182
+ // Must be called from the private serial queue.
183
+ void _FSEventsFixSelfTest() {
184
+ g_in_self_test = true;
185
+ g_hook_operational = false;
186
+ static char result[1024];
187
+ realpath("/Etc/__!FSEventsFixSelfTest!__", result);
188
+ g_in_self_test = false;
189
+ }
190
+
191
+ void FSEventsFixEnable() {
192
+ _FSEventsFixInitialize();
193
+ dispatch_sync(g_queue, ^{
194
+ if (++g_enable_refcount == 1) {
195
+ orig_realpath = dlsym(RTLD_DEFAULT, "realpath");
196
+ _FSEventsFixHookInstall();
197
+ _FSEventsFixSelfTest();
198
+ if (g_hook_operational) {
199
+ _FSEventsFixLog(FSEventsFixMessageTypeStatusChange, "Enabled");
200
+ } else {
201
+ _FSEventsFixLog(FSEventsFixMessageTypeFatalError, "Failed to enable (hook not called)");
202
+ }
203
+ }
204
+ });
205
+ }
206
+
207
+ void FSEventsFixDisable() {
208
+ _FSEventsFixInitialize();
209
+ dispatch_sync(g_queue, ^{
210
+ if (g_enable_refcount == 0) {
211
+ abort();
212
+ }
213
+ if (--g_enable_refcount == 0) {
214
+ _FSEventsFixHookUninstall();
215
+ _FSEventsFixSelfTest();
216
+ if (!g_hook_operational) {
217
+ _FSEventsFixLog(FSEventsFixMessageTypeStatusChange, "Disabled");
218
+ } else {
219
+ _FSEventsFixLog(FSEventsFixMessageTypeFatalError, "Failed to disable (hook still called)");
220
+ }
221
+ }
222
+ });
223
+ }
224
+
225
+ bool FSEventsFixIsOperational() {
226
+ _FSEventsFixInitialize();
227
+ __block bool result = false;
228
+ dispatch_sync(g_queue, ^{
229
+ result = g_hook_operational;
230
+ });
231
+ return result;
232
+ }
233
+
234
+ bool _FSEventsFixIsBroken_noresolve(const char *resolved) {
235
+ if (!!(g_debug_opt & FSEventsFixDebugOptionSimulateBroken)) {
236
+ if (strstr(resolved, FSEventsFixSimulatedBrokenFolderMarker)) {
237
+ return true;
238
+ }
239
+ }
240
+
241
+ char *reresolved = realpath(resolved, NULL);
242
+ if (reresolved) {
243
+ bool broken = (0 != strcmp(resolved, reresolved));
244
+ free(reresolved);
245
+ return broken;
246
+ } else {
247
+ return true;
248
+ }
249
+ }
250
+
251
+ bool FSEventsFixIsBroken(const char *path) {
252
+ char *resolved = CFURL_realpath(path, NULL);
253
+ if (!resolved) {
254
+ return true;
255
+ }
256
+ bool broken = _FSEventsFixIsBroken_noresolve(resolved);
257
+ free(resolved);
258
+ return broken;
259
+ }
260
+
261
+ char *FSEventsFixCopyRootBrokenFolderPath(const char *inpath) {
262
+ if (!FSEventsFixIsBroken(inpath)) {
263
+ return NULL;
264
+ }
265
+
266
+ // get a mutable copy of an absolute path
267
+ char *path = CFURL_realpath(inpath, NULL);
268
+ if (!path) {
269
+ return NULL;
270
+ }
271
+
272
+ for (;;) {
273
+ char *sep = strrchr(path, '/');
274
+ if ((sep == NULL) || (sep == path)) {
275
+ break;
276
+ }
277
+ *sep = 0;
278
+ if (!_FSEventsFixIsBroken_noresolve(path)) {
279
+ *sep = '/';
280
+ break;
281
+ }
282
+ }
283
+
284
+ return path;
285
+ }
286
+
287
+ static void _FSEventsFixAttemptRepair(const char *folder) {
288
+ int rv = rename(folder, folder);
289
+
290
+ if (!!(g_debug_opt & FSEventsFixDebugOptionSimulateRepair)) {
291
+ const char *pos = strstr(folder, FSEventsFixSimulatedBrokenFolderMarker);
292
+ if (pos) {
293
+ char *fixed = strdup(folder);
294
+ fixed[pos - folder] = 0;
295
+ strcat(fixed, pos + strlen(FSEventsFixSimulatedBrokenFolderMarker));
296
+
297
+ rv = rename(folder, fixed);
298
+ free(fixed);
299
+ }
300
+ }
301
+
302
+ if (rv != 0) {
303
+ if (errno == EPERM) {
304
+ _FSEventsFixLog(FSEventsFixMessageTypeResult, "Permission error when trying to repair '%s'", folder);
305
+ } else {
306
+ _FSEventsFixLog(FSEventsFixMessageTypeExpectedFailure, "Unknown error when trying to repair '%s': errno = %d", folder, errno);
307
+ }
308
+ }
309
+ }
310
+
311
+ FSEventsFixRepairStatus FSEventsFixRepairIfNeeded(const char *inpath) {
312
+ char *root = FSEventsFixCopyRootBrokenFolderPath(inpath);
313
+ if (root == NULL) {
314
+ return FSEventsFixRepairStatusNotBroken;
315
+ }
316
+
317
+ for (;;) {
318
+ _FSEventsFixAttemptRepair(root);
319
+ char *newRoot = FSEventsFixCopyRootBrokenFolderPath(inpath);
320
+ if (newRoot == NULL) {
321
+ _FSEventsFixLog(FSEventsFixMessageTypeResult, "Repaired '%s' in '%s'", root, inpath);
322
+ free(root);
323
+ return FSEventsFixRepairStatusRepaired;
324
+ }
325
+ if (0 == strcmp(root, newRoot)) {
326
+ _FSEventsFixLog(FSEventsFixMessageTypeResult, "Failed to repair '%s' in '%s'", root, inpath);
327
+ free(root);
328
+ free(newRoot);
329
+ return FSEventsFixRepairStatusFailed;
330
+ }
331
+ _FSEventsFixLog(FSEventsFixMessageTypeResult, "Partial success, repaired '%s' in '%s'", root, inpath);
332
+ free(root);
333
+ root = newRoot;
334
+ }
335
+ }
336
+
337
+
338
+ #pragma mark - FSEventsFix realpath wrapper
339
+
340
+ static char *FSEventsFix_realpath_wrapper(const char * __restrict src, char * __restrict dst) {
341
+ if (g_in_self_test) {
342
+ if (strstr(src, "__!FSEventsFixSelfTest!__")) {
343
+ g_hook_operational = true;
344
+ }
345
+ }
346
+
347
+ // CFURL_realpath doesn't support putting where resolution failed into the
348
+ // dst buffer, so we call the original realpath here first and if it gets a
349
+ // result, replace that with the output of CFURL_realpath. that way all the
350
+ // features of the original realpath are available.
351
+ char *rv = NULL;
352
+ char *orv = orig_realpath(src, dst);
353
+ if (orv != NULL) { rv = CFURL_realpath(src, dst); }
354
+
355
+ if (!!(g_debug_opt & FSEventsFixDebugOptionLogCalls)) {
356
+ char *result = rv ?: dst;
357
+ _FSEventsFixLog(FSEventsFixMessageTypeCall, "realpath(%s) => %s\n", src, result);
358
+ }
359
+
360
+ if (!!(g_debug_opt & FSEventsFixDebugOptionUppercaseReturn)) {
361
+ char *result = rv ?: dst;
362
+ if (result) {
363
+ for (char *pch = result; *pch; ++pch) {
364
+ *pch = (char)toupper(*pch);
365
+ }
366
+ }
367
+ }
368
+
369
+ return rv;
370
+ }
371
+
372
+
373
+ #pragma mark - realpath
374
+
375
+ // naive implementation of realpath on top of CFURL
376
+ // NOTE: doesn't quite support the full range of errno results one would
377
+ // expect here, in part because some of these functions just return a boolean,
378
+ // and in part because i'm not dealing with messy CFErrorRef objects and
379
+ // attempting to translate those to sane errno values.
380
+ // NOTE: the OSX realpath will return _where_ resolution failed in resolved_name
381
+ // if passed in and return NULL. we can't properly support that extension here
382
+ // since the resolution happens entirely behind the scenes to us in CFURL.
383
+ static char* CFURL_realpath(const char *file_name, char resolved_name[PATH_MAX])
384
+ {
385
+ char* resolved;
386
+ CFURLRef url1;
387
+ CFURLRef url2;
388
+ CFStringRef path;
389
+
390
+ if (file_name == NULL) {
391
+ errno = EINVAL;
392
+ return (NULL);
393
+ }
394
+
395
+ #if __DARWIN_UNIX03
396
+ if (*file_name == 0) {
397
+ errno = ENOENT;
398
+ return (NULL);
399
+ }
400
+ #endif
401
+
402
+ // create a buffer to store our result if we weren't passed one
403
+ if (!resolved_name) {
404
+ if ((resolved = malloc(PATH_MAX)) == NULL) return (NULL);
405
+ } else {
406
+ resolved = resolved_name;
407
+ }
408
+
409
+ url1 = CFURLCreateFromFileSystemRepresentation(NULL, (const UInt8*)file_name, (CFIndex)strlen(file_name), false);
410
+ if (url1 == NULL) { goto error_return; }
411
+
412
+ url2 = CFURLCopyAbsoluteURL(url1);
413
+ CFRelease(url1);
414
+ if (url2 == NULL) { goto error_return; }
415
+
416
+ url1 = CFURLCreateFileReferenceURL(NULL, url2, NULL);
417
+ CFRelease(url2);
418
+ if (url1 == NULL) { goto error_return; }
419
+
420
+ // if there are multiple hard links to the original path, this may end up
421
+ // being _completely_ different from what was intended
422
+ url2 = CFURLCreateFilePathURL(NULL, url1, NULL);
423
+ CFRelease(url1);
424
+ if (url2 == NULL) { goto error_return; }
425
+
426
+ path = CFURLCopyFileSystemPath(url2, kCFURLPOSIXPathStyle);
427
+ CFRelease(url2);
428
+ if (path == NULL) { goto error_return; }
429
+
430
+ bool success = CFStringGetCString(path, resolved, PATH_MAX, kCFStringEncodingUTF8);
431
+ CFRelease(path);
432
+ if (!success) { goto error_return; }
433
+
434
+ return resolved;
435
+
436
+ error_return:
437
+ if (!resolved_name) {
438
+ // we weren't passed in an output buffer and created our own. free it
439
+ int e = errno;
440
+ free(resolved);
441
+ errno = e;
442
+ }
443
+ return (NULL);
444
+ }
445
+
446
+
447
+ #pragma mark - fishhook
448
+
449
+ // Copyright (c) 2013, Facebook, Inc.
450
+ // All rights reserved.
451
+ // Redistribution and use in source and binary forms, with or without
452
+ // modification, are permitted provided that the following conditions are met:
453
+ // * Redistributions of source code must retain the above copyright notice,
454
+ // this list of conditions and the following disclaimer.
455
+ // * Redistributions in binary form must reproduce the above copyright notice,
456
+ // this list of conditions and the following disclaimer in the documentation
457
+ // and/or other materials provided with the distribution.
458
+ // * Neither the name Facebook nor the names of its contributors may be used to
459
+ // endorse or promote products derived from this software without specific
460
+ // prior written permission.
461
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
462
+ // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
463
+ // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
464
+ // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
465
+ // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
466
+ // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
467
+ // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
468
+ // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
469
+ // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
470
+ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
471
+
472
+ #import <stdlib.h>
473
+ #import <string.h>
474
+ #import <sys/types.h>
475
+ #import <mach-o/dyld.h>
476
+ #import <mach-o/loader.h>
477
+ #import <mach-o/nlist.h>
478
+
479
+ #ifdef __LP64__
480
+ typedef struct mach_header_64 mach_header_t;
481
+ typedef struct segment_command_64 segment_command_t;
482
+ typedef struct section_64 section_t;
483
+ typedef struct nlist_64 nlist_t;
484
+ #define LC_SEGMENT_ARCH_DEPENDENT LC_SEGMENT_64
485
+ #else
486
+ typedef struct mach_header mach_header_t;
487
+ typedef struct segment_command segment_command_t;
488
+ typedef struct section section_t;
489
+ typedef struct nlist nlist_t;
490
+ #define LC_SEGMENT_ARCH_DEPENDENT LC_SEGMENT
491
+ #endif
492
+
493
+ static volatile bool g_hook_installed = false;
494
+
495
+ static void _FSEventsFixHookUpdateSection(section_t *section, intptr_t slide, nlist_t *symtab, char *strtab, uint32_t *indirect_symtab)
496
+ {
497
+ uint32_t *indirect_symbol_indices = indirect_symtab + section->reserved1;
498
+ void **indirect_symbol_bindings = (void **)((uintptr_t)slide + section->addr);
499
+ for (uint i = 0; i < section->size / sizeof(void *); i++) {
500
+ uint32_t symtab_index = indirect_symbol_indices[i];
501
+ if (symtab_index == INDIRECT_SYMBOL_ABS || symtab_index == INDIRECT_SYMBOL_LOCAL ||
502
+ symtab_index == (INDIRECT_SYMBOL_LOCAL | INDIRECT_SYMBOL_ABS)) {
503
+ continue;
504
+ }
505
+ uint32_t strtab_offset = symtab[symtab_index].n_un.n_strx;
506
+ char *symbol_name = strtab + strtab_offset;
507
+ for (rebinding_t *cur = g_rebindings, *end = g_rebindings + g_rebindings_nel; cur < end; ++cur) {
508
+ if (strcmp(symbol_name, cur->name) == 0) {
509
+ if (g_hook_installed) {
510
+ if (indirect_symbol_bindings[i] != cur->replacement) {
511
+ indirect_symbol_bindings[i] = cur->replacement;
512
+ ++cur->hooked_symbols;
513
+ }
514
+ } else if (cur->original != NULL) {
515
+ if (indirect_symbol_bindings[i] == cur->replacement) {
516
+ indirect_symbol_bindings[i] = cur->original;
517
+ if (cur->hooked_symbols > 0) {
518
+ --cur->hooked_symbols;
519
+ }
520
+ }
521
+ }
522
+ goto symbol_loop;
523
+ }
524
+ }
525
+ symbol_loop:;
526
+ }
527
+ }
528
+
529
+ static void _FSEventsFixHookUpdateImage(const struct mach_header *header, intptr_t slide) {
530
+ Dl_info info;
531
+ if (dladdr(header, &info) == 0) {
532
+ return;
533
+ }
534
+
535
+ segment_command_t *cur_seg_cmd;
536
+ segment_command_t *linkedit_segment = NULL;
537
+ struct symtab_command* symtab_cmd = NULL;
538
+ struct dysymtab_command* dysymtab_cmd = NULL;
539
+
540
+ uintptr_t cur = (uintptr_t)header + sizeof(mach_header_t);
541
+ for (uint i = 0; i < header->ncmds; i++, cur += cur_seg_cmd->cmdsize) {
542
+ cur_seg_cmd = (segment_command_t *)cur;
543
+ if (cur_seg_cmd->cmd == LC_SEGMENT_ARCH_DEPENDENT) {
544
+ if (strcmp(cur_seg_cmd->segname, SEG_LINKEDIT) == 0) {
545
+ linkedit_segment = cur_seg_cmd;
546
+ }
547
+ } else if (cur_seg_cmd->cmd == LC_SYMTAB) {
548
+ symtab_cmd = (struct symtab_command*)cur_seg_cmd;
549
+ } else if (cur_seg_cmd->cmd == LC_DYSYMTAB) {
550
+ dysymtab_cmd = (struct dysymtab_command*)cur_seg_cmd;
551
+ }
552
+ }
553
+
554
+ if (!symtab_cmd || !dysymtab_cmd || !linkedit_segment ||
555
+ !dysymtab_cmd->nindirectsyms) {
556
+ return;
557
+ }
558
+
559
+ // Find base symbol/string table addresses
560
+ uintptr_t linkedit_base = (uintptr_t)slide + linkedit_segment->vmaddr - linkedit_segment->fileoff;
561
+ nlist_t *symtab = (nlist_t *)(linkedit_base + symtab_cmd->symoff);
562
+ char *strtab = (char *)(linkedit_base + symtab_cmd->stroff);
563
+
564
+ // Get indirect symbol table (array of uint32_t indices into symbol table)
565
+ uint32_t *indirect_symtab = (uint32_t *)(linkedit_base + dysymtab_cmd->indirectsymoff);
566
+
567
+ cur = (uintptr_t)header + sizeof(mach_header_t);
568
+ for (uint i = 0; i < header->ncmds; i++, cur += cur_seg_cmd->cmdsize) {
569
+ cur_seg_cmd = (segment_command_t *)cur;
570
+ if (cur_seg_cmd->cmd == LC_SEGMENT_ARCH_DEPENDENT) {
571
+ if (strcmp(cur_seg_cmd->segname, SEG_DATA) != 0) {
572
+ continue;
573
+ }
574
+ for (uint j = 0; j < cur_seg_cmd->nsects; j++) {
575
+ section_t *sect =
576
+ (section_t *)(cur + sizeof(segment_command_t)) + j;
577
+ if ((sect->flags & SECTION_TYPE) == S_LAZY_SYMBOL_POINTERS) {
578
+ _FSEventsFixHookUpdateSection(sect, slide, symtab, strtab, indirect_symtab);
579
+ }
580
+ if ((sect->flags & SECTION_TYPE) == S_NON_LAZY_SYMBOL_POINTERS) {
581
+ _FSEventsFixHookUpdateSection(sect, slide, symtab, strtab, indirect_symtab);
582
+ }
583
+ }
584
+ }
585
+ }
586
+ }
587
+
588
+ static void _FSEventsFixHookSaveOriginals() {
589
+ for (rebinding_t *cur = g_rebindings, *end = g_rebindings + g_rebindings_nel; cur < end; ++cur) {
590
+ void *original = cur->original = dlsym(RTLD_DEFAULT, cur->name+1);
591
+ if (!original) {
592
+ const char *error = dlerror();
593
+ _FSEventsFixLog(FSEventsFixMessageTypeFatalError, "Cannot find symbol %s, dlsym says: %s\n", cur->name, error);
594
+ }
595
+ }
596
+ }
597
+
598
+ static void _FSEventsFixHookUpdate() {
599
+ uint32_t c = _dyld_image_count();
600
+ for (uint32_t i = 0; i < c; i++) {
601
+ _FSEventsFixHookUpdateImage(_dyld_get_image_header(i), _dyld_get_image_vmaddr_slide(i));
602
+ }
603
+ }
604
+
605
+ static void _FSEventsFixHookInstall() {
606
+ static bool first_rebinding_done = false;
607
+
608
+ if (!g_hook_installed) {
609
+ g_hook_installed = true;
610
+
611
+ if (!first_rebinding_done) {
612
+ first_rebinding_done = true;
613
+ _FSEventsFixHookSaveOriginals();
614
+ _dyld_register_func_for_add_image(_FSEventsFixHookUpdateImage);
615
+ } else {
616
+ _FSEventsFixHookUpdate();
617
+ }
618
+ }
619
+ }
620
+
621
+ static void _FSEventsFixHookUninstall() {
622
+ if (g_hook_installed) {
623
+ g_hook_installed = false;
624
+ _FSEventsFixHookUpdate();
625
+ }
626
+ }