OSX LaunchAgent LaunchDaemon Communication over XPC

330 Views Asked by At

I'm trying to communicate my xpc service from LaunchDaemon.

LaunchDaemon is command line executable and LaunchAgent is xpc service.

Initiating my connection from main service(LaunchDaemon) like this:

NSXPCConnection * _connectionToService;

 _connectionToService = [[NSXPCConnection alloc] initWithMachServiceName:@"com.XpcService" options:NSXPCConnectionPrivileged];


 _connectionToService.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(XpcServiceProtocol)];

 _connectionToService.interruptionHandler = ^{ NSLog(@"Connection Terminated"); };

 _connectionToService.invalidationHandler = ^{ NSLog(@"Connection Invalidated"); };

 [_connectionToService resume];

 //here calling required functions

Xpc-service listening like this:

@interface ServiceDelegate : NSObject <NSXPCListenerDelegate>
@end 

@implementation ServiceDelegate

-(BOOL)listener:(NSXPCListener *)listener shouldAcceptNewConnection: (NSXPCConnection *)newConnection { 

newConnection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(XpcServiceProtocol)]; 

XpcService *exportedObject = [XpcService new];

newConnection.exportedObject = exportedObject;

[newConnection resume]; 

return YES; 

} 

@end 

int main(int argc, const char *argv[]) { 

ServiceDelegate *delegate = [ServiceDelegate new];

NSXPCListener *listener = [[NSXPCListener alloc] initWithMachServiceName:@"com.XpcService"]; 

listener.delegate = delegate;

[listener resume]; 

return 0; 

}

In my case i'm getting Connection Invalidated error, my xpc couldn't even be started.

LaunchAgent & LaunchDaemon loaded perfectly, code signing was also be done. help me to find out what might be caused the problem? Thanks in advance.

0

There are 0 best solutions below