forked from stevestreza/XPCKit
/
XPCConnection.m
329 lines (271 loc) · 10.5 KB
/
XPCConnection.m
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
//
// XPCConnection.m
// XPCKit
//
// Created by Steve Streza on 7/25/11. Copyright 2011 XPCKit.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "XPCConnection.h"
#import "XPCMessage+XPCKitInternal.h"
#import <xpc/xpc.h>
#import "NSObject+XPCParse.h"
#import "NSDictionary+XPCParse.h"
#import "XPCUtilities.h"
#define XPCLogMessages 1
#define XPCSendLogMessages 1
@implementation XPCConnection
@synthesize eventHandler=_eventHandler, dispatchQueue=_dispatchQueue, replyDispatchQueue=_replyDispatchQueue, connection=_connection;
- (id)initWithServiceName:(NSString *)serviceName{
xpc_connection_t connection = xpc_connection_create([serviceName cStringUsingEncoding:NSUTF8StringEncoding], NULL);
self = [self initWithConnection:connection];
if (connection) {
xpc_release(connection);
}
return self;
}
-(id)initWithConnection:(xpc_connection_t)connection{
if(!connection){
[self release];
return nil;
}
if(self = [super init]){
_connection = xpc_retain(connection);
[self receiveConnection:_connection];
dispatch_queue_t queue = dispatch_queue_create(xpc_connection_get_name(_connection), 0);
self.dispatchQueue = queue;
if (queue) {
dispatch_release(queue);
}
[self resume];
}
return self;
}
-(void)dealloc{
if(_connection){
xpc_connection_cancel(_connection);
xpc_release(_connection);
_connection = NULL;
}
if(_eventHandler){
[_eventHandler release];
_eventHandler = nil;
}
[super dealloc];
}
-(void)setDispatchQueue:(dispatch_queue_t)dispatchQueue{
if(dispatchQueue){
dispatch_retain(dispatchQueue);
}
if(_dispatchQueue){
dispatch_release(_dispatchQueue);
}
_dispatchQueue = dispatchQueue;
xpc_connection_set_target_queue(self.connection, self.dispatchQueue);
}
-(void)setReplyDispatchQueue:(dispatch_queue_t)dispatchQueue{
if(dispatchQueue){
dispatch_retain(dispatchQueue);
}
if(_replyDispatchQueue){
dispatch_release(_replyDispatchQueue);
}
_replyDispatchQueue = dispatchQueue;
}
-(void)receiveConnection:(xpc_connection_t)connection
{
// __block XPCConnection *this = self;
xpc_connection_set_event_handler(connection, ^(xpc_object_t object) {
XPCMessage *message = nil;
if (object == XPC_ERROR_CONNECTION_INTERRUPTED ||
object == XPC_ERROR_CONNECTION_INVALID ||
object == XPC_ERROR_KEY_DESCRIPTION ||
object == XPC_ERROR_TERMINATION_IMMINENT)
{
xpc_object_t errorDict = xpc_dictionary_create(NULL, NULL, 0);
xpc_dictionary_set_value(errorDict, "__XPCError", object);
message = [XPCMessage messageWithXPCDictionary:errorDict];
xpc_release(errorDict);
}else{
message = [XPCMessage messageWithXPCDictionary:object];
// Pick up log level that was sent accross the wire
XPCSetLogLevel([message logLevel]);
}
#if XPCSendLogMessages
if([message objectForKey:@"XPCDebugLog"]){
NSLog(@"LOG: %@", [message objectForKey:@"XPCDebugLog"]);
return;
}
#endif
// There are circumstances where self is already be deallocated at this point (and with it _eventHandler)
// if(this->_eventHandler){
// this.eventHandler(message, this);
// }
// Since the commented lines above lead to frequent crashes due to a race condition when quitting
// we will go with this crash free version instead (which does lead to a retain cycle due to use of
// self within the block). Since the app is about to quit anyway, though, it should be that bad...
XPCEventHandler handler = self.eventHandler;
if(handler) handler(message,self);
});
}
-(void)sendMessage:(XPCMessage *)inMessage
{
[self sendMessage:inMessage withReply:nil errorHandler:nil];
}
// Sends a message to the associated connection in the XPC service and invokes the reply handler
// on the dispatch queue of self once the associated connection sends a reply.
// Logs an error message to the console if the connection does not reply for whatever reasons.
-(void)sendMessage:(XPCMessage *)inMessage withReply:(XPCReplyHandler)replyHandler
{
[self sendMessage:inMessage withReply:replyHandler errorHandler:nil];
}
// Sends a message to the associated connection in the XPC service and invokes the reply handler
// on the dispatch queue of self once the associated connection sends a reply.
// Invokes the error handler if the associated connection does not reply for whatever reasons.
-(void)sendMessage:(XPCMessage *)inMessage
withReply:(XPCReplyHandler)inReplyHandler
errorHandler:(XPCErrorHandler)inErrorHandler
{
// Whenever sending a message we also send the current log level
// so the XPC service can set its log level accordingly
[inMessage setLogLevel:XPCGetLogLevel()];
#if XPCLogMessages
XPCLogAll(@"Sending message %@", inMessage);
#endif
if (inReplyHandler == nil) {
dispatch_async(self.dispatchQueue, ^{
xpc_connection_send_message(_connection, inMessage.XPCDictionary);
});
} else {
// Need to tell message that we want a direct reply
[inMessage setNeedsDirectReply:YES];
dispatch_queue_t replyQueue = self.replyDispatchQueue ? self.replyDispatchQueue : dispatch_get_current_queue();
dispatch_retain(replyQueue);
XPCReplyHandler replyHandler = [inReplyHandler copy];
XPCErrorHandler errorHandler = [inErrorHandler copy];
dispatch_async(self.dispatchQueue, ^{
xpc_connection_send_message_with_reply(_connection, inMessage.XPCDictionary, replyQueue, ^(xpc_object_t event){
xpc_type_t type = xpc_get_type(event);
if (type == XPC_TYPE_ERROR)
{
if (errorHandler) {
errorHandler([XPCMessage errorForXPCObject:event]);
} else {
if (event == XPC_ERROR_CONNECTION_INVALID) {
// The process on the other end of the connection has either
// crashed or cancelled the connection. After receiving this error,
// the connection is in an invalid state, and you do not need to
// call xpc_connection_cancel(). Just tear down any associated state
// here.
XPCLogError(@"Connection is invalid");
} else if (event == XPC_ERROR_CONNECTION_INTERRUPTED) {
// Handle per-connection termination cleanup.
XPCLogError(@"Connection was interrupted");
}
}
} else {
assert(type == XPC_TYPE_DICTIONARY);
XPCMessage *replyMessage = [XPCMessage messageWithXPCDictionary:event];
replyHandler(replyMessage);
}
[replyHandler release];
[errorHandler release];
dispatch_release(replyQueue);
});
});
}
}
// Sends a selector and a target and an optional object argument to the associated connection in the XPC service
// which will invoke the selector on the (copied) target with the optional (copied) argument. Invokes the
// return value handler on the dispatch queue of self with the (copied) return value and possibly
// a (copied) error object supplied.
-(void)sendSelector:(SEL)inSelector withTarget:(id)inTarget object:(id)inObject returnValueHandler:(XPCReturnValueHandler)inReturnHandler
{
// Copy return value handler onto the heap to make it stick around until we need it...
XPCReturnValueHandler returnHandler = [inReturnHandler copy];
XPCMessage* message = [XPCMessage messageWithSelector:inSelector target:inTarget object:inObject];
[self sendMessage:message withReply:^(XPCMessage* inReply)
{
NSError *error = nil;
id returnValue = [inReply invocationReturnValue:&error];
returnHandler(returnValue, error); // Handle method-level errors here
[returnHandler release];
}
errorHandler:^(NSError* inError) // Handle connection-level errors here
{
returnHandler(nil, inError);
[returnHandler release];
}];
}
-(NSString *)connectionName{
__block char* name = NULL;
dispatch_sync(self.dispatchQueue, ^{
name = (char*)xpc_connection_get_name(_connection);
});
if(!name) return nil;
return [NSString stringWithCString:name encoding:NSUTF8StringEncoding];
}
-(NSNumber *)connectionEUID{
__block uid_t uid = 0;
dispatch_sync(self.dispatchQueue, ^{
uid = xpc_connection_get_euid(_connection);
});
return [NSNumber numberWithUnsignedInt:uid];
}
-(NSNumber *)connectionEGID{
__block gid_t egid = 0;
dispatch_sync(self.dispatchQueue, ^{
egid = xpc_connection_get_egid(_connection);
});
return [NSNumber numberWithUnsignedInt:egid];
}
-(NSNumber *)connectionProcessID{
__block pid_t pid = 0;
dispatch_sync(self.dispatchQueue, ^{
pid = xpc_connection_get_pid(_connection);
});
return [NSNumber numberWithUnsignedInt:pid];
}
-(NSNumber *)connectionAuditSessionID{
__block au_asid_t auasid = 0;
dispatch_sync(self.dispatchQueue, ^{
auasid = xpc_connection_get_asid(_connection);
});
return [NSNumber numberWithUnsignedInt:auasid];
}
-(void)suspend{
dispatch_async(self.dispatchQueue, ^{
xpc_connection_suspend(_connection);
});
}
-(void)resume{
dispatch_async(self.dispatchQueue, ^{
xpc_connection_resume(_connection);
});
}
#pragma mark - Miscellaneous
-(NSString *) description
{
return [NSString stringWithFormat:@"<%@(%@, %@, %@, %@)>",
[self className],
[self connectionName],
[self connectionProcessID],
[self connectionEUID],
[self connectionEGID]];
}
-(void)sendLog:(NSString *)string{
#if XPCSendLogMessages
[self sendMessage:[XPCMessage messageWithObject:string forKey:@"XPCDebugLog"]];
#endif
}
@end