-
Notifications
You must be signed in to change notification settings - Fork 2
/
CFMessagePort.c
1220 lines (1132 loc) · 51.9 KB
/
CFMessagePort.c
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
/*
* Copyright (c) 2008-2012 Brent Fulgham <[email protected]>. All rights reserved.
*
* This source code is a modified version of the CoreFoundation sources released by Apple Inc. under
* the terms of the APSL version 2.0 (see below).
*
* For information about changes from the original Apple source release can be found by reviewing the
* source control system for the project at https://sourceforge.net/svn/?group_id=246198.
*
* The original license information is as follows:
*
* Copyright (c) 2011 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/* CFMessagePort.c
Copyright (c) 1998-2011, Apple Inc. All rights reserved.
Responsibility: Christopher Kane
*/
#if DEPLOYMENT_TARGET_MACOSX
#include <CoreFoundation/CFMessagePort.h>
#include <CoreFoundation/CFRunLoop.h>
#include <CoreFoundation/CFMachPort.h>
#include <CoreFoundation/CFDictionary.h>
#include <CoreFoundation/CFByteOrder.h>
#include <limits.h>
#include <CoreFoundation/CoreFoundation_Prefix.h>
#include "CFInternal.h"
#include <mach/mach.h>
#include <mach/message.h>
#include <mach/mach_error.h>
#include <bootstrap_priv.h>
#include <math.h>
#include <mach/mach_time.h>
#include <dlfcn.h>
#include <dispatch/dispatch.h>
extern pid_t getpid(void);
#define __kCFMessagePortMaxNameLengthMax 255
#if defined(BOOTSTRAP_MAX_NAME_LEN)
#define __kCFMessagePortMaxNameLength BOOTSTRAP_MAX_NAME_LEN
#else
#define __kCFMessagePortMaxNameLength 128
#endif
#if __kCFMessagePortMaxNameLengthMax < __kCFMessagePortMaxNameLength
#undef __kCFMessagePortMaxNameLength
#define __kCFMessagePortMaxNameLength __kCFMessagePortMaxNameLengthMax
#endif
#define __CFMessagePortMaxDataSize 0x60000000L
//#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
DISPATCH_HELPER_FUNCTIONS(mport, CFMessagePort)
//#pragma GCC diagnostic pop
static CFSpinLock_t __CFAllMessagePortsLock = CFSpinLockInit;
static CFMutableDictionaryRef __CFAllLocalMessagePorts = NULL;
static CFMutableDictionaryRef __CFAllRemoteMessagePorts = NULL;
struct __CFMessagePort {
CFRuntimeBase _base;
CFSpinLock_t _lock;
CFStringRef _name;
CFMachPortRef _port; /* immutable; invalidated */
CFMutableDictionaryRef _replies;
int32_t _convCounter;
int32_t _perPID; /* zero if not per-pid, else pid */
CFMachPortRef _replyPort; /* only used by remote port; immutable once created; invalidated */
CFRunLoopSourceRef _source; /* only used by local port; immutable once created; invalidated */
dispatch_source_t _dispatchSource; /* only used by local port; invalidated */
dispatch_queue_t _dispatchQ; /* only used by local port */
CFMessagePortInvalidationCallBack _icallout;
CFMessagePortCallBack _callout; /* only used by local port; immutable */
CFMessagePortCallBackEx _calloutEx; /* only used by local port; immutable */
CFMessagePortContext _context; /* not part of remote port; immutable; invalidated */
};
/* Bit 0 in the base reserved bits is used for invalid state */
/* Bit 1 of the base reserved bits is used for has-extra-port-refs state */
/* Bit 2 of the base reserved bits is used for is-remote state */
/* Bit 3 in the base reserved bits is used for is-deallocing state */
CF_INLINE Boolean __CFMessagePortIsValid(CFMessagePortRef ms) {
return (Boolean)__CFBitfieldGetValue(((const CFRuntimeBase *)ms)->_cfinfo[CF_INFO_BITS], 0, 0);
}
CF_INLINE void __CFMessagePortSetValid(CFMessagePortRef ms) {
__CFBitfieldSetValue(((CFRuntimeBase *)ms)->_cfinfo[CF_INFO_BITS], 0, 0, 1);
}
CF_INLINE void __CFMessagePortUnsetValid(CFMessagePortRef ms) {
__CFBitfieldSetValue(((CFRuntimeBase *)ms)->_cfinfo[CF_INFO_BITS], 0, 0, 0);
}
CF_INLINE Boolean __CFMessagePortExtraMachRef(CFMessagePortRef ms) {
return (Boolean)__CFBitfieldGetValue(((const CFRuntimeBase *)ms)->_cfinfo[CF_INFO_BITS], 1, 1);
}
CF_INLINE void __CFMessagePortSetExtraMachRef(CFMessagePortRef ms) {
__CFBitfieldSetValue(((CFRuntimeBase *)ms)->_cfinfo[CF_INFO_BITS], 1, 1, 1);
}
CF_INLINE void __CFMessagePortUnsetExtraMachRef(CFMessagePortRef ms) {
__CFBitfieldSetValue(((CFRuntimeBase *)ms)->_cfinfo[CF_INFO_BITS], 1, 1, 0);
}
CF_INLINE Boolean __CFMessagePortIsRemote(CFMessagePortRef ms) {
return (Boolean)__CFBitfieldGetValue(((const CFRuntimeBase *)ms)->_cfinfo[CF_INFO_BITS], 2, 2);
}
CF_INLINE void __CFMessagePortSetRemote(CFMessagePortRef ms) {
__CFBitfieldSetValue(((CFRuntimeBase *)ms)->_cfinfo[CF_INFO_BITS], 2, 2, 1);
}
CF_INLINE void __CFMessagePortUnsetRemote(CFMessagePortRef ms) {
__CFBitfieldSetValue(((CFRuntimeBase *)ms)->_cfinfo[CF_INFO_BITS], 2, 2, 0);
}
CF_INLINE Boolean __CFMessagePortIsDeallocing(CFMessagePortRef ms) {
return (Boolean)__CFBitfieldGetValue(((const CFRuntimeBase *)ms)->_cfinfo[CF_INFO_BITS], 3, 3);
}
CF_INLINE void __CFMessagePortSetIsDeallocing(CFMessagePortRef ms) {
__CFBitfieldSetValue(((CFRuntimeBase *)ms)->_cfinfo[CF_INFO_BITS], 3, 3, 1);
}
CF_INLINE void __CFMessagePortLock(CFMessagePortRef ms) {
__CFSpinLock(&(ms->_lock));
}
CF_INLINE void __CFMessagePortUnlock(CFMessagePortRef ms) {
__CFSpinUnlock(&(ms->_lock));
}
// Just a heuristic
#define __CFMessagePortMaxInlineBytes 4096*10
struct __CFMessagePortMachMessage0 {
mach_msg_base_t base;
int32_t magic;
int32_t msgid;
int32_t byteslen;
uint8_t bytes[0];
};
struct __CFMessagePortMachMessage1 {
mach_msg_base_t base;
mach_msg_ool_descriptor_t ool;
int32_t magic;
int32_t msgid;
int32_t byteslen;
};
#define MAGIC 0xF1F2F3F4
#define MSGP0_FIELD(msgp, ident) ((struct __CFMessagePortMachMessage0 *)msgp)->ident
#define MSGP1_FIELD(msgp, ident) ((struct __CFMessagePortMachMessage1 *)msgp)->ident
#define MSGP_GET(msgp, ident) \
((((mach_msg_base_t *)msgp)->body.msgh_descriptor_count) ? MSGP1_FIELD(msgp, ident) : MSGP0_FIELD(msgp, ident))
static mach_msg_base_t *__CFMessagePortCreateMessage(bool reply, mach_port_t port, mach_port_t replyPort, int32_t convid, int32_t msgid, const uint8_t *bytes, int32_t byteslen) {
if (__CFMessagePortMaxDataSize < byteslen) return NULL;
int32_t rounded_byteslen = ((byteslen + 3) & ~0x3);
if (rounded_byteslen <= __CFMessagePortMaxInlineBytes) {
int32_t size = sizeof(struct __CFMessagePortMachMessage0) + rounded_byteslen;
struct __CFMessagePortMachMessage0 *msg = CFAllocatorAllocate(kCFAllocatorSystemDefault, size, 0);
if (!msg) return NULL;
memset(msg, 0, size);
msg->base.header.msgh_id = convid;
msg->base.header.msgh_size = size;
msg->base.header.msgh_remote_port = port;
msg->base.header.msgh_local_port = replyPort;
msg->base.header.msgh_reserved = 0;
msg->base.header.msgh_bits = MACH_MSGH_BITS((reply ? MACH_MSG_TYPE_MOVE_SEND_ONCE : MACH_MSG_TYPE_COPY_SEND), (MACH_PORT_NULL != replyPort ? MACH_MSG_TYPE_MAKE_SEND_ONCE : 0));
msg->base.body.msgh_descriptor_count = 0;
msg->magic = MAGIC;
msg->msgid = CFSwapInt32HostToLittle(msgid);
msg->byteslen = CFSwapInt32HostToLittle(byteslen);
if (NULL != bytes && 0 < byteslen) {
memmove(msg->bytes, bytes, byteslen);
}
return (mach_msg_base_t *)msg;
} else {
int32_t size = sizeof(struct __CFMessagePortMachMessage1);
struct __CFMessagePortMachMessage1 *msg = CFAllocatorAllocate(kCFAllocatorSystemDefault, size, 0);
if (!msg) return NULL;
memset(msg, 0, size);
msg->base.header.msgh_id = convid;
msg->base.header.msgh_size = size;
msg->base.header.msgh_remote_port = port;
msg->base.header.msgh_local_port = replyPort;
msg->base.header.msgh_reserved = 0;
msg->base.header.msgh_bits = MACH_MSGH_BITS((reply ? MACH_MSG_TYPE_MOVE_SEND_ONCE : MACH_MSG_TYPE_COPY_SEND), (MACH_PORT_NULL != replyPort ? MACH_MSG_TYPE_MAKE_SEND_ONCE : 0));
msg->base.header.msgh_bits |= MACH_MSGH_BITS_COMPLEX;
msg->base.body.msgh_descriptor_count = 1;
msg->magic = MAGIC;
msg->msgid = CFSwapInt32HostToLittle(msgid);
msg->byteslen = CFSwapInt32HostToLittle(byteslen);
msg->ool.deallocate = false;
msg->ool.copy = MACH_MSG_VIRTUAL_COPY;
msg->ool.address = (void *)bytes;
msg->ool.size = byteslen;
msg->ool.type = MACH_MSG_OOL_DESCRIPTOR;
return (mach_msg_base_t *)msg;
}
}
static CFStringRef __CFMessagePortCopyDescription(CFTypeRef cf) {
CFMessagePortRef ms = (CFMessagePortRef)cf;
CFStringRef result;
const char *locked;
CFStringRef contextDesc = NULL;
locked = ms->_lock ? "Yes" : "No";
if (__CFMessagePortIsRemote(ms)) {
result = CFStringCreateWithFormat(kCFAllocatorSystemDefault, NULL, CFSTR("<CFMessagePort %p [%p]>{locked = %s, valid = %s, remote = %s, name = %@}"), cf, CFGetAllocator(ms), locked, (__CFMessagePortIsValid(ms) ? "Yes" : "No"), (__CFMessagePortIsRemote(ms) ? "Yes" : "No"), ms->_name);
} else {
if (NULL != ms->_context.info && NULL != ms->_context.copyDescription) {
contextDesc = ms->_context.copyDescription(ms->_context.info);
}
if (NULL == contextDesc) {
contextDesc = CFStringCreateWithFormat(kCFAllocatorSystemDefault, NULL, CFSTR("<CFMessagePort context %p>"), ms->_context.info);
}
void *addr = ms->_callout ? (void *)ms->_callout : (void *)ms->_calloutEx;
Dl_info info;
const char *name = (dladdr(addr, &info) && info.dli_saddr == addr && info.dli_sname) ? info.dli_sname : "???";
result = CFStringCreateWithFormat(kCFAllocatorSystemDefault, NULL, CFSTR("<CFMessagePort %p [%p]>{locked = %s, valid = %s, remote = %s, name = %@, source = %p, callout = %s (%p), context = %@}"), cf, CFGetAllocator(ms), locked, (__CFMessagePortIsValid(ms) ? "Yes" : "No"), (__CFMessagePortIsRemote(ms) ? "Yes" : "No"), ms->_name, ms->_source, name, addr, (NULL != contextDesc ? contextDesc : CFSTR("<no description>")));
}
if (NULL != contextDesc) {
CFRelease(contextDesc);
}
return result;
}
static void __CFMessagePortDeallocate(CFTypeRef cf) {
CFMessagePortRef ms = (CFMessagePortRef)cf;
__CFMessagePortSetIsDeallocing(ms);
CFMessagePortInvalidate(ms);
// Delay cleanup of _replies until here so that invalidation during
// SendRequest does not cause _replies to disappear out from under that function.
if (NULL != ms->_replies) {
CFRelease(ms->_replies);
}
if (NULL != ms->_name) {
CFRelease(ms->_name);
}
if (NULL != ms->_port) {
if (__CFMessagePortExtraMachRef(ms)) {
mach_port_mod_refs(mach_task_self(), CFMachPortGetPort(ms->_port), MACH_PORT_RIGHT_SEND, -1);
mach_port_mod_refs(mach_task_self(), CFMachPortGetPort(ms->_port), MACH_PORT_RIGHT_RECEIVE, -1);
}
CFMachPortInvalidate(ms->_port);
CFRelease(ms->_port);
}
// A remote message port for a local message port in the same process will get the
// same mach port, and the remote port will keep the mach port from being torn down,
// thus keeping the remote port from getting any sort of death notification and
// auto-invalidating; so we manually implement the 'auto-invalidation' here by
// tickling each remote port to check its state after any message port is destroyed,
// but most importantly after local message ports are destroyed.
__CFSpinLock(&__CFAllMessagePortsLock);
CFMessagePortRef *remotePorts = NULL;
CFIndex cnt = 0;
if (NULL != __CFAllRemoteMessagePorts) {
cnt = CFDictionaryGetCount(__CFAllRemoteMessagePorts);
remotePorts = CFAllocatorAllocate(kCFAllocatorSystemDefault, cnt * sizeof(CFMessagePortRef), __kCFAllocatorGCScannedMemory);
CFDictionaryGetKeysAndValues(__CFAllRemoteMessagePorts, NULL, (const void **)remotePorts);
for (CFIndex idx = 0; idx < cnt; idx++) {
CFRetain(remotePorts[idx]);
}
}
__CFSpinUnlock(&__CFAllMessagePortsLock);
if (remotePorts) {
for (CFIndex idx = 0; idx < cnt; idx++) {
// as a side-effect, this will auto-invalidate the CFMessagePort if the CFMachPort is invalid
CFMessagePortIsValid(remotePorts[idx]);
CFRelease(remotePorts[idx]);
}
CFAllocatorDeallocate(kCFAllocatorSystemDefault, remotePorts);
}
}
static CFTypeID __kCFMessagePortTypeID = _kCFRuntimeNotATypeID;
static const CFRuntimeClass __CFMessagePortClass = {
0,
"CFMessagePort",
NULL, // init
NULL, // copy
__CFMessagePortDeallocate,
NULL,
NULL,
NULL, //
__CFMessagePortCopyDescription
};
__private_extern__ void __CFMessagePortInitialize(void) {
__kCFMessagePortTypeID = _CFRuntimeRegisterClass(&__CFMessagePortClass);
}
CFTypeID CFMessagePortGetTypeID(void) {
return __kCFMessagePortTypeID;
}
static CFStringRef __CFMessagePortSanitizeStringName(CFStringRef name, uint8_t **utfnamep, CFIndex *utfnamelenp) {
uint8_t *utfname;
CFIndex utflen;
CFStringRef result = NULL;
utfname = CFAllocatorAllocate(kCFAllocatorSystemDefault, __kCFMessagePortMaxNameLength + 1, 0);
CFStringGetBytes(name, CFRangeMake(0, CFStringGetLength(name)), kCFStringEncodingUTF8, 0, false, utfname, __kCFMessagePortMaxNameLength, &utflen);
utfname[utflen] = '\0';
if (strlen(utfname) != utflen) {
/* PCA 9194709: refuse to sanitize a string with an embedded nul character */
CFAllocatorDeallocate(kCFAllocatorSystemDefault, utfname);
utfname = NULL;
utfnamelenp = 0;
} else {
/* A new string is created, because the original string may have been
truncated to the max length, and we want the string name to definitely
match the raw UTF-8 chunk that has been created. Also, this is useful
to get a constant string in case the original name string was mutable. */
result = CFStringCreateWithBytes(kCFAllocatorSystemDefault, utfname, utflen, kCFStringEncodingUTF8, false);
}
if (NULL != utfnamep) {
*utfnamep = utfname;
} else if (NULL != utfname) {
CFAllocatorDeallocate(kCFAllocatorSystemDefault, utfname);
}
if (NULL != utfnamelenp) {
*utfnamelenp = utflen;
}
return result;
}
static void __CFMessagePortDummyCallback(CFMachPortRef port, void *msg, CFIndex size, void *info) {
// not supposed to be implemented
}
static void __CFMessagePortInvalidationCallBack(CFMachPortRef port, void *info) {
// info has been setup as the CFMessagePort owning the CFMachPort
if (info) CFMessagePortInvalidate(info);
}
static CFMessagePortRef __CFMessagePortCreateLocal(CFAllocatorRef allocator, CFStringRef name, CFMessagePortCallBack callout, CFMessagePortContext *context, Boolean *shouldFreeInfo, Boolean perPID, CFMessagePortCallBackEx calloutEx) {
CFMessagePortRef memory;
uint8_t *utfname = NULL;
if (shouldFreeInfo) *shouldFreeInfo = true;
if (NULL != name) {
name = __CFMessagePortSanitizeStringName(name, &utfname, NULL);
}
__CFSpinLock(&__CFAllMessagePortsLock);
if (!perPID && NULL != name) {
CFMessagePortRef existing;
if (NULL != __CFAllLocalMessagePorts && CFDictionaryGetValueIfPresent(__CFAllLocalMessagePorts, name, (const void **)&existing)) {
CFRetain(existing);
__CFSpinUnlock(&__CFAllMessagePortsLock);
CFRelease(name);
CFAllocatorDeallocate(kCFAllocatorSystemDefault, utfname);
if (!CFMessagePortIsValid(existing)) { // must do this outside lock to avoid deadlock
CFRelease(existing);
existing = NULL;
}
return (CFMessagePortRef)(existing);
}
}
__CFSpinUnlock(&__CFAllMessagePortsLock);
CFIndex size = sizeof(struct __CFMessagePort) - sizeof(CFRuntimeBase);
memory = (CFMessagePortRef)_CFRuntimeCreateInstance(allocator, __kCFMessagePortTypeID, size, NULL);
if (NULL == memory) {
if (NULL != name) {
CFRelease(name);
}
CFAllocatorDeallocate(kCFAllocatorSystemDefault, utfname);
return NULL;
}
__CFMessagePortUnsetValid(memory);
__CFMessagePortUnsetExtraMachRef(memory);
__CFMessagePortUnsetRemote(memory);
memory->_lock = CFSpinLockInit;
memory->_name = name;
memory->_port = NULL;
memory->_replies = NULL;
memory->_convCounter = 0;
memory->_perPID = perPID ? getpid() : 0; // actual value not terribly useful for local ports
memory->_replyPort = NULL;
memory->_source = NULL;
memory->_dispatchSource = NULL;
memory->_dispatchQ = NULL;
memory->_icallout = NULL;
memory->_callout = callout;
memory->_calloutEx = calloutEx;
memory->_context.info = NULL;
memory->_context.retain = NULL;
memory->_context.release = NULL;
memory->_context.copyDescription = NULL;
if (NULL != name) {
CFMachPortRef native = NULL;
kern_return_t ret;
mach_port_t bs, mp;
task_get_bootstrap_port(mach_task_self(), &bs);
if (!perPID) {
ret = bootstrap_check_in(bs, (char *)utfname, &mp); /* If we're started by launchd or the old mach_init */
if (ret == KERN_SUCCESS) {
ret = mach_port_insert_right(mach_task_self(), mp, mp, MACH_MSG_TYPE_MAKE_SEND);
if (KERN_SUCCESS == ret) {
CFMachPortContext ctx = {0, memory, NULL, NULL, NULL};
native = CFMachPortCreateWithPort(allocator, mp, __CFMessagePortDummyCallback, &ctx, NULL);
__CFMessagePortSetExtraMachRef(memory);
} else {
CFLog(kCFLogLevelDebug, CFSTR("*** CFMessagePort: mach_port_insert_member() after bootstrap_check_in(): failed %d (0x%x) '%s', port = 0x%x, name = '%s'"), ret, ret, bootstrap_strerror(ret), mp, utfname);
mach_port_destroy(mach_task_self(), mp);
CFAllocatorDeallocate(kCFAllocatorSystemDefault, utfname);
// name is released by deallocation
CFRelease(memory);
return NULL;
}
}
}
if (!native) {
CFMachPortContext ctx = {0, memory, NULL, NULL, NULL};
native = CFMachPortCreate(allocator, __CFMessagePortDummyCallback, &ctx, NULL);
if (!native) {
CFAllocatorDeallocate(kCFAllocatorSystemDefault, utfname);
// name is released by deallocation
CFRelease(memory);
return NULL;
}
mp = CFMachPortGetPort(native);
ret = bootstrap_register2(bs, (char *)utfname, mp, perPID ? BOOTSTRAP_PER_PID_SERVICE : 0);
if (ret != KERN_SUCCESS) {
CFLog(kCFLogLevelDebug, CFSTR("*** CFMessagePort: bootstrap_register(): failed %d (0x%x) '%s', port = 0x%x, name = '%s'\nSee /usr/include/servers/bootstrap_defs.h for the error codes."), ret, ret, bootstrap_strerror(ret), mp, utfname);
CFMachPortInvalidate(native);
CFRelease(native);
CFAllocatorDeallocate(kCFAllocatorSystemDefault, utfname);
// name is released by deallocation
CFRelease(memory);
return NULL;
}
}
CFMachPortSetInvalidationCallBack(native, __CFMessagePortInvalidationCallBack);
memory->_port = native;
}
CFAllocatorDeallocate(kCFAllocatorSystemDefault, utfname);
__CFMessagePortSetValid(memory);
if (NULL != context) {
memmove(&memory->_context, context, sizeof(CFMessagePortContext));
memory->_context.info = context->retain ? (void *)context->retain(context->info) : context->info;
}
__CFSpinLock(&__CFAllMessagePortsLock);
if (!perPID && NULL != name) {
CFMessagePortRef existing;
if (NULL != __CFAllLocalMessagePorts && CFDictionaryGetValueIfPresent(__CFAllLocalMessagePorts, name, (const void **)&existing)) {
CFRetain(existing);
__CFSpinUnlock(&__CFAllMessagePortsLock);
CFRelease(memory);
return (CFMessagePortRef)(existing);
}
if (NULL == __CFAllLocalMessagePorts) {
__CFAllLocalMessagePorts = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeDictionaryKeyCallBacks, NULL);
}
CFDictionaryAddValue(__CFAllLocalMessagePorts, name, memory);
}
__CFSpinUnlock(&__CFAllMessagePortsLock);
if (shouldFreeInfo) *shouldFreeInfo = false;
return memory;
}
CFMessagePortRef CFMessagePortCreateLocal(CFAllocatorRef allocator, CFStringRef name, CFMessagePortCallBack callout, CFMessagePortContext *context, Boolean *shouldFreeInfo) {
return __CFMessagePortCreateLocal(allocator, name, callout, context, shouldFreeInfo, false, NULL);
}
CFMessagePortRef CFMessagePortCreatePerProcessLocal(CFAllocatorRef allocator, CFStringRef name, CFMessagePortCallBack callout, CFMessagePortContext *context, Boolean *shouldFreeInfo) {
return __CFMessagePortCreateLocal(allocator, name, callout, context, shouldFreeInfo, true, NULL);
}
CFMessagePortRef _CFMessagePortCreateLocalEx(CFAllocatorRef allocator, CFStringRef name, Boolean perPID, uintptr_t unused, CFMessagePortCallBackEx calloutEx, CFMessagePortContext *context, Boolean *shouldFreeInfo) {
return __CFMessagePortCreateLocal(allocator, name, NULL, context, shouldFreeInfo, perPID, calloutEx);
}
static CFMessagePortRef __CFMessagePortCreateRemote(CFAllocatorRef allocator, CFStringRef name, Boolean perPID, CFIndex pid) {
CFMessagePortRef memory;
CFMachPortRef native;
CFMachPortContext ctx;
uint8_t *utfname = NULL;
CFIndex size;
mach_port_t bp, port;
kern_return_t ret;
name = __CFMessagePortSanitizeStringName(name, &utfname, NULL);
if (NULL == name) {
return NULL;
}
__CFSpinLock(&__CFAllMessagePortsLock);
if (!perPID && NULL != name) {
CFMessagePortRef existing;
if (NULL != __CFAllRemoteMessagePorts && CFDictionaryGetValueIfPresent(__CFAllRemoteMessagePorts, name, (const void **)&existing)) {
CFRetain(existing);
__CFSpinUnlock(&__CFAllMessagePortsLock);
CFRelease(name);
CFAllocatorDeallocate(kCFAllocatorSystemDefault, utfname);
if (!CFMessagePortIsValid(existing)) { // must do this outside lock to avoid deadlock
CFRelease(existing);
existing = NULL;
}
return (CFMessagePortRef)(existing);
}
}
__CFSpinUnlock(&__CFAllMessagePortsLock);
size = sizeof(struct __CFMessagePort) - sizeof(CFMessagePortContext) - sizeof(CFRuntimeBase);
memory = (CFMessagePortRef)_CFRuntimeCreateInstance(allocator, __kCFMessagePortTypeID, size, NULL);
if (NULL == memory) {
if (NULL != name) {
CFRelease(name);
}
CFAllocatorDeallocate(kCFAllocatorSystemDefault, utfname);
return NULL;
}
__CFMessagePortUnsetValid(memory);
__CFMessagePortUnsetExtraMachRef(memory);
__CFMessagePortSetRemote(memory);
memory->_lock = CFSpinLockInit;
memory->_name = name;
memory->_port = NULL;
memory->_replies = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, NULL, NULL);
memory->_convCounter = 0;
memory->_perPID = perPID ? pid : 0;
memory->_replyPort = NULL;
memory->_source = NULL;
memory->_dispatchSource = NULL;
memory->_dispatchQ = NULL;
memory->_icallout = NULL;
memory->_callout = NULL;
memory->_calloutEx = NULL;
ctx.version = 0;
ctx.info = memory;
ctx.retain = NULL;
ctx.release = NULL;
ctx.copyDescription = NULL;
task_get_bootstrap_port(mach_task_self(), &bp);
ret = bootstrap_look_up2(bp, (char *)utfname, &port, perPID ? (pid_t)pid : 0, perPID ? BOOTSTRAP_PER_PID_SERVICE : 0);
native = (KERN_SUCCESS == ret) ? CFMachPortCreateWithPort(allocator, port, __CFMessagePortDummyCallback, &ctx, NULL) : NULL;
CFAllocatorDeallocate(kCFAllocatorSystemDefault, utfname);
if (NULL == native) {
// name is released by deallocation
CFRelease(memory);
return NULL;
}
memory->_port = native;
__CFMessagePortSetValid(memory);
__CFSpinLock(&__CFAllMessagePortsLock);
if (!perPID && NULL != name) {
CFMessagePortRef existing;
if (NULL != __CFAllRemoteMessagePorts && CFDictionaryGetValueIfPresent(__CFAllRemoteMessagePorts, name, (const void **)&existing)) {
CFRetain(existing);
__CFSpinUnlock(&__CFAllMessagePortsLock);
CFRelease(memory);
return (CFMessagePortRef)(existing);
}
if (NULL == __CFAllRemoteMessagePorts) {
__CFAllRemoteMessagePorts = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeDictionaryKeyCallBacks, NULL);
}
CFDictionaryAddValue(__CFAllRemoteMessagePorts, name, memory);
}
__CFSpinUnlock(&__CFAllMessagePortsLock);
CFMachPortSetInvalidationCallBack(native, __CFMessagePortInvalidationCallBack);
// that set-invalidation-callback might have called back into us
// if the CFMachPort is already bad, but that was a no-op since
// there was no callback setup at the (previous) time the CFMachPort
// went invalid; so check for validity manually and react
if (!CFMachPortIsValid(native)) {
CFRelease(memory); // does the invalidate
return NULL;
}
return (CFMessagePortRef)memory;
}
CFMessagePortRef CFMessagePortCreateRemote(CFAllocatorRef allocator, CFStringRef name) {
return __CFMessagePortCreateRemote(allocator, name, false, 0);
}
CFMessagePortRef CFMessagePortCreatePerProcessRemote(CFAllocatorRef allocator, CFStringRef name, CFIndex pid) {
return __CFMessagePortCreateRemote(allocator, name, true, pid);
}
Boolean CFMessagePortIsRemote(CFMessagePortRef ms) {
__CFGenericValidateType(ms, __kCFMessagePortTypeID);
return __CFMessagePortIsRemote(ms);
}
CFStringRef CFMessagePortGetName(CFMessagePortRef ms) {
__CFGenericValidateType(ms, __kCFMessagePortTypeID);
return ms->_name;
}
Boolean CFMessagePortSetName(CFMessagePortRef ms, CFStringRef name) {
CFAllocatorRef allocator = CFGetAllocator(ms);
uint8_t *utfname = NULL;
__CFGenericValidateType(ms, __kCFMessagePortTypeID);
if (ms->_perPID || __CFMessagePortIsRemote(ms)) return false;
name = __CFMessagePortSanitizeStringName(name, &utfname, NULL);
if (NULL == name) {
return false;
}
__CFSpinLock(&__CFAllMessagePortsLock);
if (NULL != name) {
CFMessagePortRef existing;
if (NULL != __CFAllLocalMessagePorts && CFDictionaryGetValueIfPresent(__CFAllLocalMessagePorts, name, (const void **)&existing)) {
__CFSpinUnlock(&__CFAllMessagePortsLock);
CFRelease(name);
CFAllocatorDeallocate(kCFAllocatorSystemDefault, utfname);
return false;
}
}
__CFSpinUnlock(&__CFAllMessagePortsLock);
if (NULL != name && (NULL == ms->_name || !CFEqual(ms->_name, name))) {
CFMachPortRef oldPort = ms->_port;
CFMachPortRef native = NULL;
kern_return_t ret;
mach_port_t bs, mp;
task_get_bootstrap_port(mach_task_self(), &bs);
ret = bootstrap_check_in(bs, (char *)utfname, &mp); /* If we're started by launchd or the old mach_init */
if (ret == KERN_SUCCESS) {
ret = mach_port_insert_right(mach_task_self(), mp, mp, MACH_MSG_TYPE_MAKE_SEND);
if (KERN_SUCCESS == ret) {
CFMachPortContext ctx = {0, ms, NULL, NULL, NULL};
native = CFMachPortCreateWithPort(allocator, mp, __CFMessagePortDummyCallback, &ctx, NULL);
__CFMessagePortSetExtraMachRef(ms);
} else {
mach_port_destroy(mach_task_self(), mp);
CFAllocatorDeallocate(kCFAllocatorSystemDefault, utfname);
CFRelease(name);
return false;
}
}
if (!native) {
CFMachPortContext ctx = {0, ms, NULL, NULL, NULL};
native = CFMachPortCreate(allocator, __CFMessagePortDummyCallback, &ctx, NULL);
if (!native) {
CFAllocatorDeallocate(kCFAllocatorSystemDefault, utfname);
CFRelease(name);
return false;
}
mp = CFMachPortGetPort(native);
ret = bootstrap_register2(bs, (char *)utfname, mp, 0);
if (ret != KERN_SUCCESS) {
CFLog(kCFLogLevelDebug, CFSTR("*** CFMessagePort: bootstrap_register(): failed %d (0x%x) '%s', port = 0x%x, name = '%s'\nSee /usr/include/servers/bootstrap_defs.h for the error codes."), ret, ret, bootstrap_strerror(ret), mp, utfname);
CFMachPortInvalidate(native);
CFRelease(native);
CFAllocatorDeallocate(kCFAllocatorSystemDefault, utfname);
CFRelease(name);
return false;
}
}
CFMachPortSetInvalidationCallBack(native, __CFMessagePortInvalidationCallBack);
ms->_port = native;
if (NULL != oldPort && oldPort != native) {
if (__CFMessagePortExtraMachRef(ms)) {
mach_port_mod_refs(mach_task_self(), CFMachPortGetPort(oldPort), MACH_PORT_RIGHT_SEND, -1);
mach_port_mod_refs(mach_task_self(), CFMachPortGetPort(oldPort), MACH_PORT_RIGHT_RECEIVE, -1);
}
CFMachPortInvalidate(oldPort);
CFRelease(oldPort);
}
__CFSpinLock(&__CFAllMessagePortsLock);
// This relocking without checking to see if something else has grabbed
// that name in the cache is rather suspect, but what would that even
// mean has happened? We'd expect the bootstrap_* calls above to have
// failed for this one and not gotten this far, or failed for all of the
// other simultaneous attempts to get the name (and having succeeded for
// this one, gotten here). So we're not going to try very hard here
// with the thread-safety.
if (NULL == __CFAllLocalMessagePorts) {
__CFAllLocalMessagePorts = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeDictionaryKeyCallBacks, NULL);
}
if (NULL != ms->_name) {
CFDictionaryRemoveValue(__CFAllLocalMessagePorts, ms->_name);
CFRelease(ms->_name);
}
ms->_name = name;
CFDictionaryAddValue(__CFAllLocalMessagePorts, name, ms);
__CFSpinUnlock(&__CFAllMessagePortsLock);
}
CFAllocatorDeallocate(kCFAllocatorSystemDefault, utfname);
return true;
}
void CFMessagePortGetContext(CFMessagePortRef ms, CFMessagePortContext *context) {
__CFGenericValidateType(ms, __kCFMessagePortTypeID);
//#warning CF: assert that this is a local port
CFAssert1(0 == context->version, __kCFLogAssertion, "%s(): context version not initialized to 0", __PRETTY_FUNCTION__);
memmove(context, &ms->_context, sizeof(CFMessagePortContext));
}
void CFMessagePortInvalidate(CFMessagePortRef ms) {
__CFGenericValidateType(ms, __kCFMessagePortTypeID);
if (!__CFMessagePortIsDeallocing(ms)) {
CFRetain(ms);
}
__CFMessagePortLock(ms);
if (__CFMessagePortIsValid(ms)) {
if (ms->_dispatchSource) {
dispatch_source_cancel(ms->_dispatchSource);
ms->_dispatchSource = NULL;
ms->_dispatchQ = NULL;
}
CFMessagePortInvalidationCallBack callout = ms->_icallout;
CFRunLoopSourceRef source = ms->_source;
CFMachPortRef replyPort = ms->_replyPort;
CFMachPortRef port = ms->_port;
CFStringRef name = ms->_name;
void *info = NULL;
__CFMessagePortUnsetValid(ms);
if (!__CFMessagePortIsRemote(ms)) {
info = ms->_context.info;
ms->_context.info = NULL;
}
ms->_source = NULL;
ms->_replyPort = NULL;
ms->_port = NULL;
__CFMessagePortUnlock(ms);
__CFSpinLock(&__CFAllMessagePortsLock);
if (0 == ms->_perPID && NULL != (__CFMessagePortIsRemote(ms) ? __CFAllRemoteMessagePorts : __CFAllLocalMessagePorts)) {
CFDictionaryRemoveValue(__CFMessagePortIsRemote(ms) ? __CFAllRemoteMessagePorts : __CFAllLocalMessagePorts, name);
}
__CFSpinUnlock(&__CFAllMessagePortsLock);
if (NULL != callout) {
callout(ms, info);
}
if (!__CFMessagePortIsRemote(ms) && NULL != ms->_context.release) {
ms->_context.release(info);
}
if (NULL != source) {
CFRunLoopSourceInvalidate(source);
CFRelease(source);
}
if (NULL != replyPort) {
CFMachPortInvalidate(replyPort);
CFRelease(replyPort);
}
if (__CFMessagePortIsRemote(ms)) {
// Get rid of our extra ref on the Mach port gotten from bs server
mach_port_deallocate(mach_task_self(), CFMachPortGetPort(port));
}
if (NULL != port) {
// We already know we're going invalid, don't need this callback
// anymore; plus, this solves a reentrancy deadlock; also, this
// must be done before the deallocate of the Mach port, to
// avoid a race between the notification message which could be
// handled in another thread, and this NULL'ing out.
CFMachPortSetInvalidationCallBack(port, NULL);
if (__CFMessagePortExtraMachRef(ms)) {
mach_port_mod_refs(mach_task_self(), CFMachPortGetPort(port), MACH_PORT_RIGHT_SEND, -1);
mach_port_mod_refs(mach_task_self(), CFMachPortGetPort(port), MACH_PORT_RIGHT_RECEIVE, -1);
}
CFMachPortInvalidate(port);
CFRelease(port);
}
} else {
__CFMessagePortUnlock(ms);
}
if (!__CFMessagePortIsDeallocing(ms)) {
CFRelease(ms);
}
}
Boolean CFMessagePortIsValid(CFMessagePortRef ms) {
__CFGenericValidateType(ms, __kCFMessagePortTypeID);
if (!__CFMessagePortIsValid(ms)) return false;
if (NULL != ms->_port && !CFMachPortIsValid(ms->_port)) {
CFMessagePortInvalidate(ms);
return false;
}
if (NULL != ms->_replyPort && !CFMachPortIsValid(ms->_replyPort)) {
CFMessagePortInvalidate(ms);
return false;
}
return true;
}
CFMessagePortInvalidationCallBack CFMessagePortGetInvalidationCallBack(CFMessagePortRef ms) {
__CFGenericValidateType(ms, __kCFMessagePortTypeID);
return ms->_icallout;
}
void CFMessagePortSetInvalidationCallBack(CFMessagePortRef ms, CFMessagePortInvalidationCallBack callout) {
__CFGenericValidateType(ms, __kCFMessagePortTypeID);
if (!__CFMessagePortIsValid(ms) && NULL != callout) {
callout(ms, ms->_context.info);
} else {
ms->_icallout = callout;
}
}
static void __CFMessagePortReplyCallBack(CFMachPortRef port, void *msg, CFIndex size, void *info) {
CFMessagePortRef ms = info;
mach_msg_base_t *msgp = msg;
mach_msg_base_t *replymsg;
__CFMessagePortLock(ms);
if (!__CFMessagePortIsValid(ms)) {
__CFMessagePortUnlock(ms);
return;
}
int32_t byteslen = 0;
Boolean invalidMagic = (MSGP_GET(msgp, magic) != MAGIC) && (CFSwapInt32(MSGP_GET(msgp, magic)) != MAGIC);
Boolean invalidComplex = (0 != msgp->body.msgh_descriptor_count) && !(msgp->header.msgh_bits & MACH_MSGH_BITS_COMPLEX);
invalidComplex = invalidComplex || ((msgp->header.msgh_bits & MACH_MSGH_BITS_COMPLEX) && (0 == msgp->body.msgh_descriptor_count));
Boolean wayTooBig = ((msgp->body.msgh_descriptor_count) ? sizeof(struct __CFMessagePortMachMessage1) : sizeof(struct __CFMessagePortMachMessage0) + __CFMessagePortMaxInlineBytes) < msgp->header.msgh_size;
Boolean wayTooSmall = msgp->header.msgh_size < sizeof(struct __CFMessagePortMachMessage0);
Boolean wrongSize = false;
if (!(invalidComplex || wayTooBig || wayTooSmall)) {
byteslen = CFSwapInt32LittleToHost(MSGP_GET(msgp, byteslen));
wrongSize = (byteslen < 0) || (__CFMessagePortMaxDataSize < byteslen);
if (0 != msgp->body.msgh_descriptor_count) {
wrongSize = wrongSize || (MSGP1_FIELD(msgp, ool).size != byteslen);
} else {
wrongSize = wrongSize || (msgp->header.msgh_size - sizeof(struct __CFMessagePortMachMessage0) < byteslen);
}
}
Boolean invalidMsgID = (0 <= msgp->header.msgh_id) && (msgp->header.msgh_id <= INT32_MAX); // conversation id
if (invalidMagic || invalidComplex || wayTooBig || wayTooSmall || wrongSize || invalidMsgID) {
CFLog(kCFLogLevelWarning, CFSTR("*** CFMessagePort: dropping corrupt reply Mach message (0b%d%d%d%d%d%d)"), invalidMagic, invalidComplex, wayTooBig, wayTooSmall, wrongSize, invalidMsgID);
mach_msg_destroy((mach_msg_header_t *)msgp);
__CFMessagePortUnlock(ms);
return;
}
if (CFDictionaryContainsKey(ms->_replies, (void *)(uintptr_t)msgp->header.msgh_id)) {
CFDataRef reply = NULL;
replymsg = (mach_msg_base_t *)msg;
if (0 == replymsg->body.msgh_descriptor_count) {
uintptr_t msgp_extent = (uintptr_t)((uint8_t *)msgp + msgp->header.msgh_size);
uintptr_t data_extent = (uintptr_t)((uint8_t *)&(MSGP0_FIELD(replymsg, bytes)) + byteslen);
if (0 <= byteslen && data_extent <= msgp_extent) {
reply = CFDataCreate(kCFAllocatorSystemDefault, MSGP0_FIELD(replymsg, bytes), byteslen);
} else {
reply = (void *)~0; // means NULL data
}
} else {
//#warning CF: should create a no-copy data here that has a custom VM-freeing allocator, and not vm_dealloc here
reply = CFDataCreate(kCFAllocatorSystemDefault, MSGP1_FIELD(replymsg, ool).address, MSGP1_FIELD(replymsg, ool).size);
vm_deallocate(mach_task_self(), (vm_address_t)MSGP1_FIELD(replymsg, ool).address, MSGP1_FIELD(replymsg, ool).size);
}
CFDictionarySetValue(ms->_replies, (void *)(uintptr_t)msgp->header.msgh_id, (void *)reply);
} else { /* discard message */
if (1 == msgp->body.msgh_descriptor_count) {
vm_deallocate(mach_task_self(), (vm_address_t)MSGP1_FIELD(msgp, ool).address, MSGP1_FIELD(msgp, ool).size);
}
}
__CFMessagePortUnlock(ms);
}
SInt32 CFMessagePortSendRequest(CFMessagePortRef remote, SInt32 msgid, CFDataRef data, CFTimeInterval sendTimeout, CFTimeInterval rcvTimeout, CFStringRef replyMode, CFDataRef *returnDatap) {
mach_msg_base_t *sendmsg;
CFRunLoopRef currentRL = CFRunLoopGetCurrent();
CFRunLoopSourceRef source = NULL;
CFDataRef reply = NULL;
int64_t termTSR;
uint32_t sendOpts = 0, sendTimeOut = 0;
int32_t desiredReply;
Boolean didRegister = false;
kern_return_t ret;
//#warning CF: This should be an assert
// if (!__CFMessagePortIsRemote(remote)) return -999;
if (data && __CFMessagePortMaxDataSize < CFDataGetLength(data)) {
CFLog(kCFLogLevelWarning, CFSTR("*** CFMessagePortSendRequest: CFMessagePort cannot send more than %lu bytes of data"), __CFMessagePortMaxDataSize);
return kCFMessagePortTransportError;
}
__CFMessagePortLock(remote);
if (!__CFMessagePortIsValid(remote)) {
__CFMessagePortUnlock(remote);
return kCFMessagePortIsInvalid;
}
CFRetain(remote); // retain during run loop to avoid invalidation causing freeing
if (NULL == remote->_replyPort) {
CFMachPortContext context;
context.version = 0;
context.info = remote;
context.retain = (const void *(*)(const void *))CFRetain;
context.release = (void (*)(const void *))CFRelease;
context.copyDescription = (CFStringRef (*)(const void *))__CFMessagePortCopyDescription;
remote->_replyPort = CFMachPortCreate(CFGetAllocator(remote), __CFMessagePortReplyCallBack, &context, NULL);
}
remote->_convCounter++;
desiredReply = -remote->_convCounter;
sendmsg = __CFMessagePortCreateMessage(false, CFMachPortGetPort(remote->_port), (replyMode != NULL ? CFMachPortGetPort(remote->_replyPort) : MACH_PORT_NULL), -desiredReply, msgid, (data ? CFDataGetBytePtr(data) : NULL), (data ? CFDataGetLength(data) : 0));
if (!sendmsg) {
__CFMessagePortUnlock(remote);
CFRelease(remote);
return kCFMessagePortTransportError;
}
if (replyMode != NULL) {
CFDictionarySetValue(remote->_replies, (void *)(uintptr_t)desiredReply, NULL);
source = CFMachPortCreateRunLoopSource(CFGetAllocator(remote), remote->_replyPort, -100);
didRegister = !CFRunLoopContainsSource(currentRL, source, replyMode);
if (didRegister) {
CFRunLoopAddSource(currentRL, source, replyMode);
}
}
if (sendTimeout < 10.0*86400) {
// anything more than 10 days is no timeout!
sendOpts = MACH_SEND_TIMEOUT;
sendTimeout *= 1000.0;
if (sendTimeout < 1.0) sendTimeout = 0.0;
sendTimeOut = floor(sendTimeout);
}
ret = mach_msg((mach_msg_header_t *)sendmsg, MACH_SEND_MSG|sendOpts, sendmsg->header.msgh_size, 0, MACH_PORT_NULL, sendTimeOut, MACH_PORT_NULL);
if (KERN_SUCCESS != ret) {
// need to deallocate the send-once right that might have been created
if (replyMode != NULL) mach_port_deallocate(mach_task_self(), ((mach_msg_header_t *)sendmsg)->msgh_local_port);
if (didRegister) {
CFRunLoopRemoveSource(currentRL, source, replyMode);
}
if (source) CFRelease(source);
__CFMessagePortUnlock(remote);
CFAllocatorDeallocate(kCFAllocatorSystemDefault, sendmsg);
CFRelease(remote);
return (MACH_SEND_TIMED_OUT == ret) ? kCFMessagePortSendTimeout : kCFMessagePortTransportError;
}
__CFMessagePortUnlock(remote);
CFAllocatorDeallocate(kCFAllocatorSystemDefault, sendmsg);
if (replyMode == NULL) {
CFRelease(remote);
return kCFMessagePortSuccess;
}
_CFMachPortInstallNotifyPort(currentRL, replyMode);
termTSR = mach_absolute_time() + __CFTimeIntervalToTSR(rcvTimeout);
for (;;) {
CFRunLoopRunInMode(replyMode, __CFTSRToTimeInterval(termTSR - mach_absolute_time()), true);
// warning: what, if anything, should be done if remote is now invalid?
reply = CFDictionaryGetValue(remote->_replies, (void *)(uintptr_t)desiredReply);
if (NULL != reply || termTSR < (int64_t)mach_absolute_time()) {
break;
}
if (!CFMessagePortIsValid(remote)) {
// no reason that reply port alone should go invalid so we don't check for that
break;
}
}
// Should we uninstall the notify port? A complex question...
if (didRegister) {
CFRunLoopRemoveSource(currentRL, source, replyMode);
}
if (source) CFRelease(source);
if (NULL == reply) {
CFDictionaryRemoveValue(remote->_replies, (void *)(uintptr_t)desiredReply);
CFRelease(remote);
return CFMessagePortIsValid(remote) ? kCFMessagePortReceiveTimeout : kCFMessagePortBecameInvalidError;
}
if (NULL != returnDatap) {
*returnDatap = ((void *)~0 == reply) ? NULL : reply;
} else if ((void *)~0 != reply) {
CFRelease(reply);
}
CFDictionaryRemoveValue(remote->_replies, (void *)(uintptr_t)desiredReply);
CFRelease(remote);
return kCFMessagePortSuccess;
}
static mach_port_t __CFMessagePortGetPort(void *info) {
CFMessagePortRef ms = info;
if (!ms->_port && __CFMessagePortIsValid(ms)) CFLog(kCFLogLevelWarning, CFSTR("*** Warning: A local CFMessagePort (%p) is being put in a run loop or dispatch queue, but it has not been named yet, so this will be a no-op and no messages are going to be received, even if named later."), info);
return ms->_port ? CFMachPortGetPort(ms->_port) : MACH_PORT_NULL;
}