int main(int argc, char *argv[]) { xpc_connection_t conn; if (argc < 2) { fprintf(stderr, "Usage: %s <mach service name>\n", argv[0]); return (1); } conn = xpc_connection_create_mach_service(argv[1], NULL, XPC_CONNECTION_MACH_SERVICE_LISTENER); xpc_connection_set_event_handler(conn, ^(xpc_object_t peer) { printf("New connection, peer=%p\n", peer); xpc_connection_set_event_handler(peer, ^(xpc_object_t event) { if (event == XPC_ERROR_CONNECTION_INVALID) { printf("Connection closed by remote end\n"); return; } if (xpc_get_type(event) != XPC_TYPE_DICTIONARY) { printf("Received something else than a dictionary!\n"); return; } printf("Message received: %p\n", event); printf("%s\n", xpc_copy_description(event)); xpc_object_t resp = xpc_dictionary_create(NULL, NULL, 0); xpc_dictionary_set_string(resp, "foo", "bar"); xpc_connection_send_message(peer, resp); });
void send_single_message(xpc_connection_t conn, xpc_object_t msg) { __block bool b_sent = false; xpc_connection_send_message_with_reply(conn, msg, NULL, ^(xpc_object_t event) { char *desc = xpc_copy_description(event); printf("Reply: %s\n", desc); free(desc); });
static void log_xpc_object(const char *msg, xpc_object_t obj) { char *desc; desc = xpc_copy_description(obj); SCLog(TRUE, LOG_DEBUG, CFSTR("%s = %s"), msg, desc); free(desc); }
static void LogDebug(const char *prefix, xpc_object_t o) { if (!LogDebugEnabled()) return; char *desc = xpc_copy_description(o); os_log_info(OS_LOG_DEFAULT, "%s: %s", prefix, desc); free(desc); }
static void log_xpc_object(const char *msg, xpc_object_t obj) { char *desc; desc = xpc_copy_description(obj); asl_log(NULL, NULL, ASL_LEVEL_ERR, "%s = %s", msg, desc); free(desc); }
static void log_xpc_object(const char *msg, xpc_object_t obj) { char *desc; desc = xpc_copy_description(obj); if (*S_debug) { SCLoggerLog(S_logger, LOG_INFO, CFSTR("%s = %s"), msg, desc); } free(desc); }
void send() { xpc_object_t reply = xpc_connection_send_message_with_reply_sync(service, obj); xpc_release(obj); obj = NULL; xpc_type_t type = xpc_get_type(reply); if (type == XPC_TYPE_DICTIONARY) { obj = reply; if (int64_t error = xpc_dictionary_get_int64(obj, "error")) MacOSError::throwMe(error); } else if (type == XPC_TYPE_ERROR) { const char *s = xpc_copy_description(reply); printf("Error returned: %s\n", s); free((char*)s); MacOSError::throwMe(errSecCSInternalError); } else { const char *s = xpc_copy_description(reply); printf("Unexpected type of return object: %s\n", s); free((char*)s); } }