Пример #1
0
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);
        });
Пример #2
0
void Noble::setupXpcConnection() {
  this->dispatchQueue = dispatch_queue_create("com.apple.blued", 0);
  this->xpcConnnection = xpc_connection_create_mach_service("com.apple.blued", this->dispatchQueue, XPC_CONNECTION_MACH_SERVICE_PRIVILEGED);

  xpc_connection_set_event_handler(this->xpcConnnection, ^(xpc_object_t event) {
    xpc_retain(event);
    this->handleXpcEvent(event);
  });
void XpcConnection::setup() {
  this->dispatchQueue = dispatch_queue_create(this->serviceName.c_str(), 0);
  this->xpcConnnection = xpc_connection_create_mach_service(this->serviceName.c_str(), this->dispatchQueue, XPC_CONNECTION_MACH_SERVICE_PRIVILEGED);

  xpc_connection_set_event_handler(this->xpcConnnection, ^(xpc_object_t event) {
    xpc_retain(event);
//    NSLog(@"event = %@", event);
    this->handleEvent(event);
  });
Пример #4
0
static xpc_connection_t swca_create_connection(const char *name) {
    if (!name)
        name = kSWCAXPCServiceName;
    xpc_connection_t connection;
    connection = xpc_connection_create_mach_service(name, NULL, 0);
    xpc_connection_set_event_handler(connection, ^(xpc_object_t event) {
        const char *description = xpc_dictionary_get_string(event, XPC_ERROR_KEY_DESCRIPTION);
        secnotice("xpc", "got event: %s", description);
    });
__private_extern__
libSC_info_client_t *
libSC_info_client_create(dispatch_queue_t	q,
			 const char		*service_name,
			 const char		*service_description)
{
	xpc_connection_t	c;
	libSC_info_client_t	*client;
#if	!TARGET_IPHONE_SIMULATOR
	const uint64_t		flags	=	XPC_CONNECTION_MACH_SERVICE_PRIVILEGED;
#else	// !TARGET_IPHONE_SIMULATOR
	const uint64_t		flags	=	0;
#endif	// !TARGET_IPHONE_SIMULATOR

	if (_has_forked) {
		return NULL;
	}

	client = malloc(sizeof(libSC_info_client_t));
	client->active = TRUE;
	client->service_description = strdup(service_description);
	client->service_name = strdup(service_name);

	c = xpc_connection_create_mach_service(service_name, q, flags);

	xpc_connection_set_event_handler(c, ^(xpc_object_t xobj) {
		xpc_type_t	type;

		type = xpc_get_type(xobj);
		if (type == XPC_TYPE_DICTIONARY) {
			asl_log(NULL, NULL, ASL_LEVEL_ERR, "%s: unexpected message", client->service_name);
			log_xpc_object("  dict = ", xobj);
		} else if (type == XPC_TYPE_ERROR) {
			if (xobj == XPC_ERROR_CONNECTION_INVALID) {
				asl_log(NULL, NULL, ASL_LEVEL_ERR, "%s: server not available", client->service_name);
				client->active = FALSE;
			} else if (xobj == XPC_ERROR_CONNECTION_INTERRUPTED) {
				asl_log(NULL, NULL, ASL_LEVEL_DEBUG, "%s: server failed", client->service_name);
			} else {
				const char	*desc;

				desc = xpc_dictionary_get_string(xobj, XPC_ERROR_KEY_DESCRIPTION);
				asl_log(NULL, NULL, ASL_LEVEL_DEBUG,
					"%s: connection error: %d : %s",
					client->service_name,
					xpc_connection_get_pid(c),
					desc);
			}

		} else {
			asl_log(NULL, NULL, ASL_LEVEL_ERR,
				"%s: unknown event type : %p",
				client->service_name,
				type);
		}
	});
Пример #6
0
int main(){
  xpc_connection_t conn = xpc_connection_create_mach_service("com.apple.networkd", NULL, XPC_CONNECTION_MACH_SERVICE_PRIVILEGED);

  xpc_connection_set_event_handler(conn, ^(xpc_object_t event) {
    xpc_type_t t = xpc_get_type(event);
    if (t == XPC_TYPE_ERROR){
      printf("err: %s\n", xpc_dictionary_get_string(event, XPC_ERROR_KEY_DESCRIPTION));
    }
    printf("received an event\n");
  });
Пример #7
0
//
// connect to XPC service
//
xpc_connection_t XpcConnect(char *service, void *ctx) {
    dispatch_queue_t queue = dispatch_queue_create(service, 0);
    xpc_connection_t conn = xpc_connection_create_mach_service(service, queue, XPC_CONNECTION_MACH_SERVICE_PRIVILEGED);

    // making a local copy, that should be made "persistent" with the following Block_copy
    GoInterface ictx = *((GoInterface*)ctx);

    xpc_connection_set_event_handler(conn,
        Block_copy(^(xpc_object_t event) {
            handleXpcEvent(event, (void *)&ictx);
        })
Пример #8
0
//
// Your standard server-side xpc main
//
int main (int argc, char * const argv[])
{
    const char *name = serviceName;
    if (const char *s = getenv("SYSPOLICYNAME"))
        name = s;

//    extern char *optarg;
    extern int optind;
    int arg;
    while ((arg = getopt(argc, argv, "v")) != -1)
        switch (arg) {
        case 'v':
            break;
        case '?':
            usage();
        }
    if (optind < argc)
        usage();

    dispatch_queue_t queue = dispatch_queue_create("server", 0);
    xpc_connection_t service = xpc_connection_create_mach_service(name, queue, XPC_CONNECTION_MACH_SERVICE_LISTENER /* | XPC_CONNECTION_MACH_SERVICE_PRIVILEGED */);

    xpc_connection_set_event_handler(service, ^(xpc_object_t cmsg) {
        if (xpc_get_type(cmsg) == XPC_TYPE_CONNECTION) {
            xpc_connection_t connection = xpc_connection_t(cmsg);
            syslog(LOG_DEBUG, "Connection from pid %d", xpc_connection_get_pid(connection));
            xpc_connection_set_event_handler(connection, ^(xpc_object_t msg) {
                if (xpc_get_type(msg) == XPC_TYPE_DICTIONARY) {
                    const char *function = xpc_dictionary_get_string(msg, "function");
                    syslog(LOG_DEBUG, "pid %d requested %s", xpc_connection_get_pid(connection), function);
                    xpc_object_t reply = xpc_dictionary_create_reply(msg);
                    try {
                        if (function == NULL) {
                            xpc_dictionary_set_int64(reply, "error", errSecCSInternalError);
                        } else if (!strcmp(function, "assess")) {
                            doAssess(msg, reply);
                        } else if (!strcmp(function, "update")) {
                            doUpdate(msg, reply);
                        } else if (!strcmp(function, "record")) {
                            doRecord(msg, reply, connection);
                        } else {
                            xpc_dictionary_set_int64(reply, "error", errSecCSInternalError);
                        }
                    } catch (...) {
                        xpc_dictionary_set_int64(reply, "error", errSecCSInternalError);
                    }
                    xpc_connection_send_message(connection, reply);
                    xpc_release(reply);
                }
            });
            xpc_connection_resume(connection);
        } else {
Пример #9
0
int main(int argc, const char * argv[])
{
    uint64_t action = 0;
    OSStatus status = noErr;
    uint8_t testkey[128] = "\xde\xad\xbe\xef\xde\xad\xbe\xef\xde\xad\xbe\xef\xde\xad\xbe\xef";
    xpc_connection_t connection = xpc_connection_create_mach_service(SECURITYD_SERVICE_NAME, NULL, XPC_CONNECTION_MACH_SERVICE_PRIVILEGED);
    xpc_object_t message = NULL, reply = NULL;

    xpc_connection_set_event_handler(connection, ^(xpc_object_t event) {
        if (xpc_get_type(event) == XPC_TYPE_ERROR) {
            printf("XPC error\n");
        }
    });
Пример #10
0
static xpc_connection_t
create_connection(dispatch_queue_t queue)
{
	xpc_connection_t new_connection;

	new_connection = xpc_connection_create_mach_service(kSNHelperService, queue, XPC_CONNECTION_MACH_SERVICE_PRIVILEGED);
	if (isa_xpc_connection(new_connection)) {
		xpc_connection_set_event_handler(new_connection,
			^(xpc_object_t message) {
				if (isa_xpc_error(message)) {
					syslog(LOG_INFO, "Got an error on the snhelper connection");
				} else if (isa_xpc_dictionary(message)) {
					syslog(LOG_INFO, "Got an unexpected message on the snhelper connection");
				}
			});
		xpc_connection_resume(new_connection);
	}