XPC service and mac application do not connect to each other

122 views Asked by At

I am currently writing an agent for endpoint security. I cannot connect the application and the xpc service. I start the plist with launchctl and then open the application, but it does not connect and the application runs dysfunctionally. I leave the code of the ViewController in the application and the XPCConnection in the xpc service below.

Note: I create the XPC service in Xcode like a normal application and write it as a service application with "Application is background only"

ViewController.swift

    func establishConnection() {
        XPCConnection.shared.connectToDaemon(bundle: Bundle.main, delegate: self) { success in
            DispatchQueue.main.async { [self] in
                if !success {
                    controlButton.isEnabled = false
                    configMenuStatus(start: false, stop: false)
                    alertWithError(error: "Unable to start monitoring for broken connection with daemon.")
                } else {
                    Logger(.Info, "Connect to daemon successfully.")
                }
            }
        }
    }

XPCConnection.swift

    func connectToDaemon(bundle: Bundle, delegate: ClientXPCProtocol, handler: @escaping (Bool) -> Void) {
        guard connection == nil else {
            Logger(.Info, "Client already connected.")
            handler(true)
            return
        }
        guard getMachServiceName(from: bundle) == ClientBundle else {
            handler(false)
            return
        }
        
        let newConnection = NSXPCConnection(machServiceName: DaemonBundle)
        newConnection.exportedObject = delegate
        newConnection.exportedInterface = NSXPCInterface(with: ClientXPCProtocol.self)
        newConnection.remoteObjectInterface = NSXPCInterface(with: DaemonXPCProtocol.self)
        newConnection.invalidationHandler = {
            self.connection = nil
            Logger(.Info, "Daemon disconnected.")
            handler(false)
        }
        newConnection.interruptionHandler = {
            self.connection = nil
            Logger(.Error, "Daemon interrupted.")
            handler(false)
        }
        connection = newConnection
        newConnection.resume()
        
        let proxy = newConnection.remoteObjectProxyWithErrorHandler { error in
            Logger(.Error, "Failed to connect with error [\(error)]")
            self.connection?.invalidate()
            self.connection = nil
            handler(false)
        } as? DaemonXPCProtocol
        
        proxy!.connectResponse(handler)
        handler(true)
    }

This is the error photo, the application continues to work

enter image description here

Thanks!

First, I checked to see if I had made a mistake in the bundle identifier, but I could not find an error, and then I realized that I had not run the launchd service. Then I ran it, but it did not make any sense. What I am trying to do is to connect and run the network extension and endpoint security with this service, but the xpc service does not connect to each other.

After looking into it a little more, I think it's related to Sandbox.

"[Error] XPCConnection.swift: 61 [-] Failed to connect with error [Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service named *********** was invalidated: failed at lookup with error 159 - Sandbox restriction." UserInfo={NSDebugDescription=The connection to service named *********** was invalidated: failed at lookup with error 159 - Sandbox restriction.}]"

0

There are 0 answers