-
Notifications
You must be signed in to change notification settings - Fork 19
/
ionotifyCB.c
170 lines (145 loc) · 4.9 KB
/
ionotifyCB.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
/*
* Copyright (c) 2008 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@
*/
/*
cc ionotifyCB.c -o /tmp/ion -Wall -Wno-four-char-constants -framework IOKit -undefined warning
*/
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include <mach/mach_interface.h>
#include <IOKit/IOKitLib.h>
#include <CoreFoundation/CFRunLoop.h>
mach_port_t masterPort;
mach_port_t notifyPort;
CFRunLoopSourceRef cfSource;
void ServiceInterestCallback(void * refcon, io_service_t service,
natural_t messageType, void * messageArgument )
{
io_name_t name;
assert( refcon == (void *) masterPort );
if( KERN_SUCCESS == IORegistryEntryGetName( service, name ))
printf(name);
else
printf("???");
printf(": messageType %08lx, arg %08lx\n",
messageType, messageArgument);
}
void ServiceArrivalCallback( void * refcon, io_iterator_t iter )
{
kern_return_t kr;
io_object_t obj;
io_name_t name;
io_string_t path;
mach_port_t note;
CFDictionaryRef dict;
IONotificationPortRef notify = (IONotificationPortRef) refcon;
while( (obj = IOIteratorNext( iter))) {
assert( KERN_SUCCESS == (
kr = IORegistryEntryGetName( obj, name )
));
printf("name:%s(%d)\n", name, obj);
dict = IOCreateDisplayInfoDictionary( obj, kNilOptions );
if( dict) {
CFShow( dict );
CFRelease( dict );
}
kr = IORegistryEntryGetPath( obj, kIOServicePlane, path );
if( KERN_SUCCESS == kr) {
// if the object is detached, getPath will fail
printf("path:%s\n", path);
// as will IOServiceAddInterestNotification
if( KERN_SUCCESS != (
kr = IOServiceAddInterestNotification(
notify,
obj, kIOGeneralInterest,
&ServiceInterestCallback, (void *) masterPort,
¬e )
)) printf("IOServiceAddInterestNotification(%lx)\n", kr);
// can compare two kernel objects with IOObjectIsEqualTo() if we keep the object,
// otherwise, release
IOObjectRelease( obj );
}
}
}
void notifyTest( char * arg )
{
kern_return_t kr;
io_iterator_t note1, note2;
io_service_t obj;
const char * type;
IONotificationPortRef notify;
assert( 0 != (
notify = IONotificationPortCreate( masterPort )
));
type = kIOMatchedNotification;// or kIOPublishNotification;
assert( KERN_SUCCESS == (
kr = IOServiceAddMatchingNotification(
notify,
type,
IOServiceMatching( arg ),
// IOBSDNameMatching( masterPort, 0, arg ),
&ServiceArrivalCallback, (void *) notify,
¬e1 )
));
printf("IOServiceAddMatchingNotification: %s: ", type );
// dumping the iterator gives us the current list
// and arms the notification for changes
ServiceArrivalCallback ( (void *) notify, note1 );
printf("\n");
type = kIOBusyInterest;
obj = IORegistryEntryFromPath( masterPort, kIOServicePlane ":/");
assert( obj );
assert( KERN_SUCCESS == (
kr = IOServiceAddInterestNotification(
notify,
obj, type,
&ServiceInterestCallback, (void *) masterPort,
¬e2 )
));
CFRunLoopAddSource(CFRunLoopGetCurrent(),
IONotificationPortGetRunLoopSource(notify),
kCFRunLoopDefaultMode);
printf("waiting...\n");
CFRunLoopRun(); // Start silly state machine
IOObjectRelease( note1 );
IOObjectRelease( note2 );
}
int
main(int argc, char **argv)
{
kern_return_t kr;
/*
* Get master device port
*/
assert( KERN_SUCCESS == (
kr = IOMasterPort( bootstrap_port,
&masterPort)
));
if( argc > 1)
notifyTest( argv[1] );
else
printf("%s className\n", argv[0]);
printf("Exit\n");
}