which handler process the reply from server in macos xpc

380 Views Asked by At

I have below demo code for XPC client:

xpc_connection_t _connection = xpc_connection_create_mach_service("mac.xpc.service.name",
                                                                  NULL,
                                                                  XPC_CONNECTION_MACH_SERVICE_PRIVILEGED);
if (_connection == NULL) {
    printf("connect daemon xpc service failed\n");
    return;
}

xpc_connection_set_event_handler(_connection, ^(xpc_object_t obj) {
    //event_handler
});

xpc_connection_resume(_connection);

xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0);
//create message here
//...

xpc_connection_send_message_with_reply(_connection, message, NULL, ^(xpc_object_t object) {
    //reply block
});

I can connect to the xpc service and also send message successfully. The only issue here is that the reply message is always correctly received by event handler but in the reply block the object is always XPC_TYPE_ERROR.

I am little bit confused here because I thought the reply block should receive the correct reply object since it is explicitly declared for xpc_connection_send_message_with_reply but looks that's not the truth.

Can anyone explain how it happens and how to make sure the reply block can always get the correct reply?

Really appreciate for your reply.

Thanks!

1

There are 1 best solutions below

0
On

I can connect to the xpc service and also send message successfully.

Creating a connection instance does not mean you connected to the XPC service. Returning from the xpc_connection_send_message_with_reply function does not mean a message was sent successfully. Unless you are able to confirm from the XPC service side that your message was received, I would not assume that's what is actually occurring.

If you try to send a message to a service which does not exist or could not be started, getting back an XPC error would make sense.


Looking at your code and your reference to an "XPC Service" I suspect your issue is passing in XPC_CONNECTION_MACH_SERVICE_PRIVILEGED. XPC Services by definition cannot be privileged, so if you truly have an XPC Service then do not pass in this flag. If you have an executable/application which runs as root and acts an XPC Mach server, then this is unlikely to be your issue.