Esempio n. 1
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 {
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);
	}