-
Notifications
You must be signed in to change notification settings - Fork 0
/
IOKitLib.c
3297 lines (2711 loc) · 79.6 KB
/
IOKitLib.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) 1998-2014 Apple Computer, 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@
*/
#ifndef IOCONNECT_MAPMEMORY_10_6
#define IOCONNECT_MAPMEMORY_10_6 1
#endif
#include <IOKit/IOTypes.h>
#include <device/device_types.h>
#include <mach/mach.h>
#include <mach/mach_port.h>
#if TARGET_IPHONE_SIMULATOR
#include <servers/bootstrap.h>
#endif
#include <stdlib.h>
#include <stdarg.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <asl.h>
#include <dispatch/dispatch.h>
#include <dispatch/private.h>
#include <xpc/xpc.h>
#include <CoreFoundation/CoreFoundation.h>
#include <CoreFoundation/CFMachPort.h>
#include <libkern/OSAtomic.h>
#include <IOKit/IOBSD.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/IOKitServer.h>
#include <IOKit/IOCFSerialize.h>
#include <IOKit/IOCFUnserialize.h>
#include <IOKit/IOKitLibPrivate.h>
#include <IOKit/IOKitKeysPrivate.h>
#if __LP64__
typedef struct OSNotificationHeader64 NotificationHeader;
// XXX gvdl: Need to conditionalise this for LP64
#define mig_external __private_extern__
#include <iokitmig64.h>
#undef mig_external
#else
// To stage the IOKitUser map the 64 bit aware APIs locally
# if EMULATE_IOCONNECT
# define EMULATE_IOCONNECT_64 1
# define EMULATE_IOCONNECT_ASYNC_64 1
# endif
# if EMULATE_IOCONNECT_64
# define io_connect_method em_connect_method
# endif
# if EMULATE_IOCONNECT_ASYNC_64
# define io_connect_async_method em_connect_async_method
# endif
typedef struct OSNotificationHeader NotificationHeader;
#include <iokitmig32.h>
#endif
uint64_t
gIOKitLibServerVersion;
CFOptionFlags
gIOKitLibSerializeOptions = kIOCFSerializeToBinary;
/*
* Ports
*/
extern mach_port_t mach_task_self();
const mach_port_t kIOMasterPortDefault = MACH_PORT_NULL;
static mach_port_t
__IOGetDefaultMasterPort()
{
mach_port_t masterPort;
kern_return_t result = IOMasterPort(MACH_PORT_NULL, &masterPort);
if( KERN_SUCCESS != result)
masterPort = MACH_PORT_NULL;
return( masterPort );
}
kern_return_t
#if TARGET_IPHONE_SIMULATOR
IOMasterPort( mach_port_t bootstrapPort, mach_port_t * masterPort )
#else
IOMasterPort( mach_port_t bootstrapPort __unused, mach_port_t * masterPort )
#endif
{
kern_return_t result = KERN_SUCCESS;
mach_port_t host_port = 0;
#if TARGET_IPHONE_SIMULATOR
/* Defaulting to bypass until <rdar://problem/13141176> is addressed */
static boolean_t use_iokitsimd = 0;
static dispatch_once_t once;
dispatch_once(&once, ^{
const char *value = getenv("IOS_SIMULATOR_IOKITSIMD");
if (value) {
use_iokitsimd = (*value == '1');
}
/* Don't log about it until <rdar://problem/13141176> is addressed */
#if 0
if (!use_iokitsimd)
asl_log(NULL, NULL, ASL_LEVEL_NOTICE,
"IOKit.framework:IOMasterPort bypassing iokitsimd");
#endif
});
if (use_iokitsimd) {
if (bootstrapPort == MACH_PORT_NULL)
bootstrapPort = bootstrap_port;
return bootstrap_look_up(bootstrapPort, "com.apple.iokitsimd", masterPort);
}
#endif
host_port = mach_host_self();
result = host_get_io_master(host_port, masterPort);
static dispatch_once_t versionOnce;
dispatch_once(&versionOnce, ^{
#if IOKIT_SERVER_VERSION >= 20140421
kern_return_t kr;
kr = io_server_version(*masterPort, &gIOKitLibServerVersion);
if (KERN_SUCCESS != kr) gIOKitLibServerVersion = 0;
#endif /* IOKIT_SERVER_VERSION >= 20140421 */
if (gIOKitLibServerVersion < 20140421) gIOKitLibSerializeOptions &= ~kIOCFSerializeToBinary;
});
/* Dispose of the host port to prevent security breaches and port
* leaks. We don't care about the kern_return_t value of this
* call for now as there's nothing we can do if it fails.
*/
if (host_port) mach_port_deallocate(mach_task_self(), host_port);
return result;
}
kern_return_t
IOCreateReceivePort( uint32_t msgType, mach_port_t * recvPort )
{
kern_return_t res;
switch (msgType) {
case kOSNotificationMessageID:
case kOSAsyncCompleteMessageID:
res = mach_port_allocate(mach_task_self(),
MACH_PORT_RIGHT_RECEIVE, recvPort);
break;
default:
res = kIOReturnBadArgument;
}
return res;
}
/*
* IOObject
*/
kern_return_t
IOObjectRelease(
io_object_t object )
{
return( mach_port_deallocate( mach_task_self(), object ));
}
kern_return_t
IOObjectRetain(
io_object_t object )
{
return( mach_port_mod_refs(mach_task_self(),
object,
MACH_PORT_RIGHT_SEND,
1 ));
}
kern_return_t
IOObjectGetClass(
io_object_t object,
io_name_t className )
{
return _IOObjectGetClass(object, 0, className);
}
kern_return_t
_IOObjectGetClass(
io_object_t object,
uint64_t options,
io_name_t className )
{
CFTypeRef overrideType = NULL;
boolean_t override = false;
#if !TARGET_IPHONE_SIMULATOR
if ( (options & kIOClassNameOverrideNone) == 0 ) {
overrideType = IORegistryEntryCreateCFProperty(object, CFSTR(kIOClassNameOverrideKey), kCFAllocatorDefault, 0);
if ( overrideType ) {
if ( CFGetTypeID(overrideType) == CFStringGetTypeID() ) {
override = CFStringGetCString((CFStringRef)overrideType, className, sizeof(io_name_t), kCFStringEncodingUTF8);
}
CFRelease(overrideType);
}
}
#endif /* !TARGET_IPHONE_SIMULATOR */
return( override ? kIOReturnSuccess : io_object_get_class( object, className ));
}
CFStringRef
IOObjectCopyClass(
io_object_t object)
{
return _IOObjectCopyClass(object, 0);
}
CFStringRef
_IOObjectCopyClass(
io_object_t object,
uint64_t options)
{
io_name_t my_name;
CFStringRef my_str = NULL;
// if there's no argument, no point going on. Return NULL.
if (!object)
return my_str;
_IOObjectGetClass( object, options, my_name );
my_str = CFStringCreateWithCString (kCFAllocatorDefault, my_name, kCFStringEncodingUTF8);
return my_str;
}
CFStringRef
IOObjectCopySuperclassForClass(CFStringRef classname)
{
io_name_t my_name, orig_name;
CFStringRef my_str = NULL;
char * my_cstr;
kern_return_t kr;
// if there's no argument, no point going on. Return NULL.
if (classname == NULL) {
return my_str;
}
my_cstr = malloc(sizeof(char) * 128);
CFStringGetCString (classname, my_cstr, 128, kCFStringEncodingUTF8);
strncpy(orig_name, my_cstr, sizeof(io_name_t));
mach_port_t masterPort = __IOGetDefaultMasterPort();
kr = io_object_get_superclass(masterPort, orig_name, my_name);
if (masterPort != MACH_PORT_NULL)
mach_port_deallocate(mach_task_self(), masterPort);
if (kr == kIOReturnSuccess) {
my_str = CFStringCreateWithCString (kCFAllocatorDefault, my_name, kCFStringEncodingUTF8);
}
free(my_cstr);
return my_str;
}
CFStringRef
IOObjectCopyBundleIdentifierForClass(CFStringRef classname)
{
io_name_t my_name, orig_name;
CFStringRef my_str = NULL;
char * my_cstr;
kern_return_t kr;
// if there's no argument, no point going on. Return NULL.
if (classname == NULL) {
return my_str;
}
my_cstr = malloc(sizeof(char) * 128);
CFStringGetCString (classname, my_cstr, 128, kCFStringEncodingUTF8);
strncpy(orig_name, my_cstr, sizeof(io_name_t));
mach_port_t masterPort = __IOGetDefaultMasterPort();
kr = io_object_get_bundle_identifier(masterPort, orig_name, my_name);
if (masterPort != MACH_PORT_NULL)
mach_port_deallocate(mach_task_self(), masterPort);
if (kr == kIOReturnSuccess) {
my_str = CFStringCreateWithCString (kCFAllocatorDefault, my_name, kCFStringEncodingUTF8);
}
free(my_cstr);
return my_str;
}
boolean_t
IOObjectConformsTo(
io_object_t object,
const io_name_t className )
{
return _IOObjectConformsTo(object, className, 0);
}
boolean_t
_IOObjectConformsTo(
io_object_t object,
const io_name_t className,
uint64_t options)
{
boolean_t conforms;
if( kIOReturnSuccess != io_object_conforms_to(
object, (char *) className, &conforms ))
conforms = 0;
#if !TARGET_IPHONE_SIMULATOR
if ( !conforms && ((options & kIOClassNameOverrideNone) == 0) ) {
CFTypeRef overrideType = IORegistryEntryCreateCFProperty(object, CFSTR(kIOClassNameOverrideKey), kCFAllocatorDefault, 0);
if ( overrideType ) {
if ( CFGetTypeID(overrideType) == CFStringGetTypeID() ) {
CFStringRef classNameString = CFStringCreateWithCStringNoCopy(kCFAllocatorDefault, className, kCFStringEncodingUTF8, kCFAllocatorNull);
if ( classNameString ) {
conforms = kCFCompareEqualTo == CFStringCompare((CFStringRef)overrideType, classNameString, 0);
CFRelease(classNameString);
}
}
CFRelease(overrideType);
}
}
#endif /* !TARGET_IPHONE_SIMULATOR */
return( conforms );
}
boolean_t
IOObjectIsEqualTo(
io_object_t object,
io_object_t anObject )
{
return( object == anObject );
}
uint32_t
IOObjectGetKernelRetainCount(
io_object_t object )
{
uint32_t count;
if( kIOReturnSuccess != io_object_get_retain_count( object, &count))
count = 0;
return( count );
}
uint32_t
IOObjectGetRetainCount(
io_object_t object )
{
return( IOObjectGetKernelRetainCount(object) );
}
uint32_t
IOObjectGetUserRetainCount(
io_object_t object )
{
mach_port_urefs_t urefs;
if( kIOReturnSuccess != mach_port_get_refs( mach_task_self(), object, MACH_PORT_RIGHT_SEND, &urefs))
urefs = 0;
return( urefs );
}
/*
* IOIterator
*/
io_object_t
IOIteratorNext(
io_iterator_t iterator )
{
io_object_t next;
if( kIOReturnSuccess != io_iterator_next( iterator, &next))
next = 0;
return( next );
}
void
IOIteratorReset(
io_iterator_t iterator )
{
io_iterator_reset( iterator );
}
boolean_t
IOIteratorIsValid(
io_iterator_t iterator )
{
boolean_t valid;
if( kIOReturnSuccess != io_iterator_is_valid( iterator, &valid ))
valid = FALSE;
return( valid );
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* IOService
*/
io_service_t
IOServiceGetMatchingService(
mach_port_t _masterPort,
CFDictionaryRef matching )
{
kern_return_t kr;
CFDataRef data;
CFIndex dataLen;
mach_port_t masterPort;
io_service_t service = MACH_PORT_NULL;
bool ool;
if( !matching)
return( MACH_PORT_NULL);
if (MACH_PORT_NULL == _masterPort)
masterPort = __IOGetDefaultMasterPort();
else
masterPort = _masterPort;
data = IOCFSerialize( matching, gIOKitLibSerializeOptions );
CFRelease( matching );
if( !data)
return( MACH_PORT_NULL );
dataLen = CFDataGetLength(data);
ool = true;
#if IOKIT_SERVER_VERSION >= 20140421
if (kIOCFSerializeToBinary & gIOKitLibSerializeOptions)
{
if ((size_t) dataLen < sizeof(io_struct_inband_t))
{
kr = io_service_get_matching_service_bin(masterPort,
(char *) CFDataGetBytePtr(data), dataLen, &service );
ool = false;
}
}
else
#endif /* IOKIT_SERVER_VERSION >= 20140421 */
{
if ((size_t) dataLen < sizeof(io_string_t))
{
kr = io_service_get_matching_service( masterPort,
(char *) CFDataGetBytePtr(data), &service );
ool = false;
}
}
if (ool)
{
kern_return_t result;
kr = io_service_get_matching_service_ool( masterPort,
(char *) CFDataGetBytePtr(data), dataLen, &result, &service );
if (KERN_SUCCESS == kr)
kr = result;
}
CFRelease( data );
if ((masterPort != MACH_PORT_NULL) && (masterPort != _masterPort))
mach_port_deallocate(mach_task_self(), masterPort);
if (KERN_SUCCESS != kr)
service = MACH_PORT_NULL;
return( service );
}
kern_return_t
IOServiceGetMatchingServices(
mach_port_t _masterPort,
CFDictionaryRef matching,
io_iterator_t * existing )
{
kern_return_t kr;
CFDataRef data;
CFIndex dataLen;
mach_port_t masterPort;
bool ool;
if( !matching)
return( kIOReturnBadArgument);
if (MACH_PORT_NULL == _masterPort)
masterPort = __IOGetDefaultMasterPort();
else
masterPort = _masterPort;
data = IOCFSerialize(matching, gIOKitLibSerializeOptions);
CFRelease( matching );
if( !data)
return( kIOReturnUnsupported );
dataLen = CFDataGetLength(data);
ool = true;
#if IOKIT_SERVER_VERSION >= 20140421
if (kIOCFSerializeToBinary & gIOKitLibSerializeOptions)
{
if ((size_t) dataLen < sizeof(io_struct_inband_t))
{
kr = io_service_get_matching_services_bin(masterPort,
(char *) CFDataGetBytePtr(data), dataLen, existing);
ool = false;
}
}
else
#endif /* IOKIT_SERVER_VERSION >= 20140421 */
{
if ((size_t) dataLen < sizeof(io_string_t))
{
kr = io_service_get_matching_services(masterPort,
(char *) CFDataGetBytePtr(data), existing);
ool = false;
}
}
if (ool)
{
kern_return_t result;
kr = io_service_get_matching_services_ool( masterPort,
(char *) CFDataGetBytePtr(data), dataLen, &result, existing );
if (KERN_SUCCESS == kr)
kr = result;
}
CFRelease( data );
if ((masterPort != MACH_PORT_NULL) && (masterPort != _masterPort))
mach_port_deallocate(mach_task_self(), masterPort);
return( kr );
}
kern_return_t
IOServiceMatchPropertyTable( io_service_t service, CFDictionaryRef matching,
boolean_t * matches )
{
kern_return_t kr;
CFDataRef data;
CFIndex dataLen;
bool ool;
if( !matching)
return( kIOReturnBadArgument);
data = IOCFSerialize( matching, gIOKitLibSerializeOptions );
if( !data)
return( kIOReturnUnsupported );
dataLen = CFDataGetLength(data);
ool = true;
#if IOKIT_SERVER_VERSION >= 20140421
if (kIOCFSerializeToBinary & gIOKitLibSerializeOptions)
{
if ((size_t) dataLen < sizeof(io_struct_inband_t))
{
kr = io_service_match_property_table_bin(service,
(char *) CFDataGetBytePtr(data), dataLen, matches);
ool = false;
}
}
else
#endif /* IOKIT_SERVER_VERSION >= 20140421 */
{
if ((size_t) dataLen < sizeof(io_string_t))
{
kr = io_service_match_property_table(service,
(char *) CFDataGetBytePtr(data), matches);
ool = false;
}
}
if (ool)
{
kern_return_t result;
kr = io_service_match_property_table_ool( service,
(char *) CFDataGetBytePtr(data), dataLen, &result, matches );
if (KERN_SUCCESS == kr)
kr = result;
}
CFRelease( data );
return( kr );
}
static kern_return_t
InternalIOServiceAddNotification(
mach_port_t _masterPort,
const io_name_t notificationType,
CFDictionaryRef matching,
mach_port_t wakePort,
io_async_ref_t asyncRef,
mach_msg_type_number_t referenceCnt,
io_iterator_t * notification )
{
kern_return_t kr;
CFDataRef data;
CFIndex dataLen;
mach_port_t masterPort;
bool ool;
if( !matching)
return( kIOReturnBadArgument);
if (MACH_PORT_NULL == _masterPort)
masterPort = __IOGetDefaultMasterPort();
else
masterPort = _masterPort;
data = IOCFSerialize( matching, gIOKitLibSerializeOptions );
CFRelease( matching );
if( !data)
return( kIOReturnUnsupported );
dataLen = CFDataGetLength(data);
ool = true;
#if IOKIT_SERVER_VERSION >= 20140421
if (kIOCFSerializeToBinary & gIOKitLibSerializeOptions)
{
if ((size_t) dataLen < sizeof(io_struct_inband_t))
{
kr = io_service_add_notification_bin( masterPort, (char *) notificationType,
(char *) CFDataGetBytePtr(data), dataLen,
wakePort, asyncRef, referenceCnt,
notification );
ool = false;
}
}
else
#endif /* IOKIT_SERVER_VERSION >= 20140421 */
{
if ((size_t) dataLen < sizeof(io_string_t))
{
kr = io_service_add_notification( masterPort, (char *) notificationType,
(char *) CFDataGetBytePtr(data),
wakePort, asyncRef, referenceCnt,
notification );
ool = false;
}
}
if (ool)
{
kern_return_t result;
kr = io_service_add_notification_ool( masterPort, (char *) notificationType,
(char *) CFDataGetBytePtr(data), dataLen,
wakePort, asyncRef, referenceCnt,
&result, notification );
if (KERN_SUCCESS == kr) kr = result;
}
CFRelease( data );
if ((masterPort != MACH_PORT_NULL) && (masterPort != _masterPort))
mach_port_deallocate(mach_task_self(), masterPort);
return( kr );
}
kern_return_t
IOServiceAddNotification(
mach_port_t _masterPort,
const io_name_t notificationType,
CFDictionaryRef matching,
mach_port_t wakePort,
uintptr_t reference,
io_iterator_t *notification )
{
return (InternalIOServiceAddNotification(_masterPort, notificationType,
matching, wakePort,
(io_user_reference_t *) &reference, 1,
notification));
}
kern_return_t
IOServiceAddMatchingNotification(
IONotificationPortRef notifyPort,
const io_name_t notificationType,
CFDictionaryRef matching,
IOServiceMatchingCallback callback,
void * refcon,
io_iterator_t * notification )
{
io_user_reference_t asyncRef[kIOMatchingCalloutCount];
asyncRef[kIOMatchingCalloutFuncIndex] = (io_user_reference_t) callback;
asyncRef[kIOMatchingCalloutRefconIndex] = (io_user_reference_t) refcon;
return (InternalIOServiceAddNotification(notifyPort->masterPort, notificationType,
matching, notifyPort->wakePort,
&asyncRef[0], kIOMatchingCalloutCount,
notification));
}
kern_return_t
IOServiceAddInterestNotification(
IONotificationPortRef notifyPort,
io_service_t service,
const io_name_t interestType,
IOServiceInterestCallback callback,
void * refcon,
io_object_t * notification )
{
io_user_reference_t asyncRef[kIOInterestCalloutCount];
kern_return_t kr;
asyncRef[kIOInterestCalloutFuncIndex] = (io_user_reference_t) callback;
asyncRef[kIOInterestCalloutRefconIndex] = (io_user_reference_t) refcon;
asyncRef[kIOInterestCalloutServiceIndex] = (io_user_reference_t) service;
kr = io_service_add_interest_notification( service, (char *) interestType,
notifyPort->wakePort,
asyncRef, kIOInterestCalloutCount,
notification );
return( kr );
}
IONotificationPortRef
IONotificationPortCreate(
mach_port_t masterPort )
{
kern_return_t kr;
IONotificationPort *notify;
if (MACH_PORT_NULL == masterPort) {
masterPort = __IOGetDefaultMasterPort();
} else {
IOObjectRetain(masterPort);
}
notify = calloc( 1, sizeof( IONotificationPort));
if (!notify) {
return( 0 );
}
notify->masterPort = masterPort;
kr = IOCreateReceivePort(kOSNotificationMessageID, ¬ify->wakePort);
if( kr != kIOReturnSuccess) {
free( notify );
return( 0 );
}
return notify;
}
static void
IONotificationPortRelease(void *ctxt)
{
IONotificationPortRef notify = ctxt;
if (OSAtomicDecrement32(¬ify->refcount) < 0) {
/* Note: dispatch sources require resources they are monitoring to be
* destroyed after their corresponding kevent has been unregistered.
*
* We hence use an internal refcount to make sure the port destruction
* is delayed until it is safe: the IONotificationPort and each each
* created dispatch source own a reference.
*
* The refcount encoding is offset by 1 so that its initial value (a
* single refcount owned by the IONotificationPort) can be 0. So
* we free resources when the refcount encoding becomes -1.
*/
mach_port_mod_refs(mach_task_self(), notify->wakePort,
MACH_PORT_RIGHT_RECEIVE, -1);
mach_port_deallocate(mach_task_self(), notify->masterPort);
free( notify );
}
}
void
IONotificationPortDestroy(
IONotificationPortRef notify )
{
if (notify->cfmachPort) {
CFMachPortInvalidate(notify->cfmachPort);
CFRelease(notify->cfmachPort);
}
if (notify->source) {
CFRelease(notify->source);
}
if (notify->dispatchSource) {
dispatch_source_cancel(notify->dispatchSource);
dispatch_release(notify->dispatchSource);
}
IONotificationPortRelease(notify);
}
CFRunLoopSourceRef
IONotificationPortGetRunLoopSource(
IONotificationPortRef notify )
{
CFMachPortContext context;
Boolean cfReusedPort = false;
if (notify->source)
return (notify->source);
context.version = 1;
context.info = (void *) notify;
context.retain = NULL;
context.release = NULL;
context.copyDescription = NULL;
notify->cfmachPort = CFMachPortCreateWithPort(NULL, notify->wakePort,
IODispatchCalloutFromCFMessage, &context, &cfReusedPort);
if (!notify->cfmachPort)
return NULL;
if (cfReusedPort)
{
// We got a CFMachPortRef that CF re-used from its pool.
// This is probably a race condition, and this belongs
// to a recently dead port.
// We expect a new CFMachPortRef - we treat it as an error.
CFStringRef description = NULL;
char str[255];
if (notify->cfmachPort) {
description = CFCopyDescription(notify->cfmachPort);
if (description) {
CFStringGetCString(description, str, sizeof(str), kCFStringEncodingUTF8);
CFRelease(description);
}
}
asl_log(NULL, NULL, ASL_LEVEL_ERR,
"IOKit.framework:IONotificationPortGetRunLoopSource bad CFMachPort, %s\n",
description ? str : "No Description");
CFRelease(notify->cfmachPort);
notify->cfmachPort = NULL;
goto exit;
}
notify->source = CFMachPortCreateRunLoopSource(NULL, notify->cfmachPort, 0);
exit:
return (notify->source);
}
mach_port_t
IONotificationPortGetMachPort(
IONotificationPortRef notify )
{
return( notify->wakePort );
}
kern_return_t
IONotificationPortSetImportanceReceiver(IONotificationPortRef notify)
{
kern_return_t kr;
kr = mach_port_set_attributes(mach_task_self(), notify->wakePort, MACH_PORT_IMPORTANCE_RECEIVER, (mach_port_info_t)NULL, 0);
assert(kr == KERN_SUCCESS);
return (kr);
}
boolean_t _IODispatchCalloutWithDispatch(mach_msg_header_t *msg, mach_msg_header_t *reply)
{
mig_reply_setup(msg, reply);
((mig_reply_error_t*)reply)->RetCode = MIG_NO_REPLY;
IODispatchCalloutFromCFMessage(NULL, msg, msg->msgh_size, dispatch_mach_msg_get_context(msg));
return TRUE;
}
#define MAX_MSG_SIZE (8ul * 1024ul - MAX_TRAILER_SIZE)
void
IONotificationPortSetDispatchQueue(IONotificationPortRef notify, dispatch_queue_t queue)
{
dispatch_source_t dispatchSource;
if (notify->dispatchSource) {
dispatch_source_cancel(notify->dispatchSource);
dispatch_release(notify->dispatchSource);
notify->dispatchSource = NULL;
}
if (!queue) return;
OSAtomicIncrement32(¬ify->refcount);
dispatchSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_MACH_RECV, notify->wakePort, 0, queue);
dispatch_set_context(dispatchSource, notify);
dispatch_source_set_event_handler(dispatchSource, ^{
dispatch_mig_server(dispatchSource, MAX_MSG_SIZE, _IODispatchCalloutWithDispatch);
});
dispatch_source_set_cancel_handler_f(dispatchSource, IONotificationPortRelease);
notify->dispatchSource = dispatchSource;
dispatch_activate(dispatchSource);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* Matching creation helpers
*/
static CFMutableDictionaryRef
MakeOneStringProp(
CFStringRef key,
const char * name )
{
CFMutableDictionaryRef dict;
CFStringRef string;
dict = CFDictionaryCreateMutable( kCFAllocatorDefault, 0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
if( !dict)
return( dict);
string = CFStringCreateWithCString( kCFAllocatorDefault, name,
kCFStringEncodingMacRoman );
if( string) {
CFDictionarySetValue( dict, key, string );
CFRelease( string );
} else {
CFRelease( dict );
dict = 0;
}
return( dict );
}
static CFMutableDictionaryRef
MakeOneNumProp(
CFStringRef key,
uint64_t value )
{
CFMutableDictionaryRef dict;
CFNumberRef num;