X-Git-Url: https://git.saurik.com/apple/configd.git/blobdiff_plain/78403150fdf95618c483be7dbe6bca9459b92a9f..1ef45fa40afc16a8d224b7438ecefbd7c8b4fbfe:/IPMonitorControl/IPMonitorControlServer.c diff --git a/IPMonitorControl/IPMonitorControlServer.c b/IPMonitorControl/IPMonitorControlServer.c index e19ccbb..b4563fc 100644 --- a/IPMonitorControl/IPMonitorControlServer.c +++ b/IPMonitorControl/IPMonitorControlServer.c @@ -1,15 +1,15 @@ /* - * Copyright (c) 2013-2014 Apple Inc. All rights reserved. + * Copyright (c) 2013-2017 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ - * + * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. - * + * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, @@ -17,7 +17,7 @@ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. - * + * * @APPLE_LICENSE_HEADER_END@ */ @@ -45,10 +45,8 @@ #include "IPMonitorControlPrivate.h" #include -STATIC Boolean * S_verbose; - #ifdef TEST_IPMONITOR_CONTROL -#define my_log(__level, fmt, ...) SCPrint(TRUE, stdout, CFSTR(fmt "\n"), ## __VA_ARGS__) +#define my_log(__level, __format, ...) SCPrint(TRUE, stdout, CFSTR(__format "\n"), ## __VA_ARGS__) #else /* TEST_IPMONITOR_CONTROL */ #include "ip_plugin.h" @@ -83,8 +81,7 @@ InterfaceChangedListAddInterface(CFStringRef ifname) CFArrayAppendValue(S_if_changes, ifname); S_if_changes_range.length = 1; } - else if (CFArrayContainsValue(S_if_changes, S_if_changes_range, - ifname) == FALSE) { + else if (!CFArrayContainsValue(S_if_changes, S_if_changes_range, ifname)) { CFArrayAppendValue(S_if_changes, ifname); S_if_changes_range.length++; } @@ -113,14 +110,14 @@ InterfaceRankAssertionAdd(const void * key, const void * value, void * context) = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); - CFDictionarySetValue(*assertions_p, key, value); + CFDictionarySetValue(*assertions_p, key, rank); return; } existing_rank = CFDictionaryGetValue(*assertions_p, key); if (existing_rank == NULL || (CFNumberCompare(rank, existing_rank, NULL) == kCFCompareGreaterThan)) { - CFDictionarySetValue(*assertions_p, key, value); + CFDictionarySetValue(*assertions_p, key, rank); } return; } @@ -171,6 +168,8 @@ GenerateNotification(void) STATIC void AddChangedInterface(const void * key, const void * value, void * context) { +#pragma unused(value) +#pragma unused(context) InterfaceChangedListAddInterface((CFStringRef)key); return; } @@ -178,14 +177,12 @@ AddChangedInterface(const void * key, const void * value, void * context) STATIC void ControlSessionInvalidate(ControlSessionRef session) { - if (*S_verbose) { - my_log(LOG_NOTICE, "Invalidating %p", session); - } + my_log(LOG_DEBUG, "Invalidating %p", session); LIST_REMOVE(session, link); if (session->assertions != NULL) { my_log(LOG_DEBUG, "IPMonitorControlServer: %p pid %d removing assertions %@", - session->connection, + session->connection, xpc_connection_get_pid(session->connection), session->assertions); CFDictionaryApplyFunction(session->assertions, AddChangedInterface, @@ -200,9 +197,7 @@ ControlSessionInvalidate(ControlSessionRef session) STATIC void ControlSessionRelease(void * p) { - if (*S_verbose) { - my_log(LOG_NOTICE, "Releasing %p", p); - } + my_log(LOG_DEBUG, "Releasing %p", p); free(p); return; } @@ -224,9 +219,7 @@ ControlSessionCreate(xpc_connection_t connection) xpc_connection_set_finalizer_f(connection, ControlSessionRelease); xpc_connection_set_context(connection, session); LIST_INSERT_HEAD(&S_ControlSessions, session, link); - if (*S_verbose) { - my_log(LOG_NOTICE, "Created %p (connection %p)", session, connection); - } + my_log(LOG_DEBUG, "Created %p (connection %p)", session, connection); return (session); } @@ -261,7 +254,7 @@ ControlSessionSetInterfaceRank(ControlSessionRef session, } ifname_cf = CFStringCreateWithCString(NULL, ifname, kCFStringEncodingUTF8); - + if (rank == kSCNetworkServicePrimaryRankDefault) { CFDictionaryRemoveValue(session->assertions, ifname_cf); if (CFDictionaryGetCount(session->assertions) == 0) { @@ -320,17 +313,17 @@ IPMonitorControlServerHandleSetInterfaceRank(xpc_connection_t connection, xpc_object_t request, xpc_object_t reply) { +#pragma unused(reply) const char * ifname; SCNetworkServicePrimaryRank rank; ControlSessionRef session; - if (IPMonitorControlServerValidateConnection(connection) == FALSE) { - my_log(LOG_DEBUG, - "IPMonitorControlServer: %p pid %d permission denied", + if (!IPMonitorControlServerValidateConnection(connection)) { + my_log(LOG_INFO, "connection %p pid %d permission denied", connection, xpc_connection_get_pid(connection)); return (EPERM); } - ifname + ifname = xpc_dictionary_get_string(request, kIPMonitorControlRequestKeyInterfaceName); if (ifname == NULL) { @@ -351,8 +344,7 @@ IPMonitorControlServerHandleSetInterfaceRank(xpc_connection_t connection, } session = ControlSessionGet(connection); ControlSessionSetInterfaceRank(session, ifname, rank); - my_log(LOG_DEBUG, - "IPMonitorControlServer: %p pid %d set %s %u", + my_log(LOG_INFO, "connection %p pid %d set %s %u", connection, xpc_connection_get_pid(connection), ifname, rank); return (0); } @@ -375,7 +367,7 @@ IPMonitorControlServerHandleGetInterfaceRank(xpc_connection_t connection, /* no session, no rank assertion */ return (ENOENT); } - ifname + ifname = xpc_dictionary_get_string(request, kIPMonitorControlRequestKeyInterfaceName); if (ifname == NULL) { @@ -392,10 +384,7 @@ IPMonitorControlServerHandleDisconnect(xpc_connection_t connection) { ControlSessionRef session; - if (*S_verbose) { - my_log(LOG_NOTICE, "IPMonitorControlServer: client %p went away", - connection); - } + my_log(LOG_DEBUG, "IPMonitorControlServer: client %p went away", connection); session = ControlSessionLookup(connection); if (session == NULL) { /* never asserted anything */ @@ -410,15 +399,15 @@ IPMonitorControlServerHandleRequest(xpc_connection_t connection, xpc_object_t request) { xpc_type_t type; - + type = xpc_get_type(request); if (type == XPC_TYPE_DICTIONARY) { int error = 0; uint64_t request_type; xpc_connection_t remote; xpc_object_t reply; - - request_type + + request_type = xpc_dictionary_get_uint64(request, kIPMonitorControlRequestKeyType); reply = xpc_dictionary_create_reply(request); @@ -452,12 +441,11 @@ IPMonitorControlServerHandleRequest(xpc_connection_t connection, IPMonitorControlServerHandleDisconnect(connection); } else if (request == XPC_ERROR_CONNECTION_INTERRUPTED) { - my_log(LOG_NOTICE, - "IPMonitorControlServer: connection interrupted"); + my_log(LOG_INFO, "connection interrupted"); } } else { - my_log(LOG_NOTICE, "IPMonitorControlServer: unexpected event"); + my_log(LOG_NOTICE, "unexpected event"); } return; } @@ -471,6 +459,7 @@ IPMonitorControlServerHandleNewConnection(xpc_connection_t connection) IPMonitorControlServerHandleRequest(connection, event); }; xpc_connection_set_event_handler(connection, handler); + xpc_connection_set_target_queue(connection, S_IPMonitorControlServerQueue); xpc_connection_resume(connection); return; } @@ -488,26 +477,25 @@ IPMonitorControlServerCreate(dispatch_queue_t queue, const char * name) } handler = ^(xpc_object_t event) { xpc_type_t type; - + type = xpc_get_type(event); if (type == XPC_TYPE_CONNECTION) { IPMonitorControlServerHandleNewConnection(event); } else if (type == XPC_TYPE_ERROR) { const char * desc; - + desc = xpc_dictionary_get_string(event, XPC_ERROR_KEY_DESCRIPTION); if (event == XPC_ERROR_CONNECTION_INVALID) { - my_log(LOG_NOTICE, "IPMonitorControlServer: %s", desc); + my_log(LOG_NOTICE, "%s", desc); xpc_release(connection); } else { - my_log(LOG_NOTICE, "IPMonitorControlServer: %s", desc); - } + my_log(LOG_NOTICE, "%s", desc); + } } else { - my_log(LOG_NOTICE, "IPMonitorControlServer: unknown event %p", - type); + my_log(LOG_NOTICE, "unknown event %p", type); } }; S_IPMonitorControlServerQueue = queue; @@ -520,10 +508,10 @@ PRIVATE_EXTERN Boolean IPMonitorControlServerStart(CFRunLoopRef runloop, CFRunLoopSourceRef rls, Boolean * verbose) { +#pragma unused(verbose) dispatch_queue_t q; xpc_connection_t connection; - S_verbose = verbose; SetNotificationInfo(runloop, rls); q = dispatch_queue_create("IPMonitorControlServer", NULL); connection = IPMonitorControlServerCreate(q, kIPMonitorControlServerName);