X-Git-Url: https://git.saurik.com/apple/security.git/blobdiff_plain/6b200bc335dc93c5516ccb52f14bd896d8c7fad7..dd5fb164cf5b32c462296bc65e289e100f74b59a:/OSX/utilities/src/debugging.c?ds=inline diff --git a/OSX/utilities/src/debugging.c b/OSX/utilities/src/debugging.c index 22503b41..124fae55 100644 --- a/OSX/utilities/src/debugging.c +++ b/OSX/utilities/src/debugging.c @@ -44,6 +44,7 @@ #include #include #include +#include const char *api_trace = "api_trace"; @@ -412,29 +413,33 @@ static char *copyScopeStr(CFStringRef scope, char *alternative) { return scopeStr; } -static os_log_t logObjForCFScope(CFStringRef scope) { - static dispatch_once_t onceToken = 0; - __block os_log_t retval = OS_LOG_DISABLED; - static dispatch_queue_t logObjectQueue = NULL; +os_log_t +secLogObjForCFScope(CFStringRef scope) +{ + os_log_t retval = OS_LOG_DISABLED; + static os_unfair_lock lock = OS_UNFAIR_LOCK_INIT; static CFMutableDictionaryRef scopeMap = NULL; - - if(scope == NULL) scope = CFSTR("logging"); - - dispatch_once(&onceToken, ^{ - logObjectQueue = dispatch_queue_create("logObjectQueue", DISPATCH_QUEUE_SERIAL); + + if (scope == NULL) { + scope = CFSTR("logging"); + } + + os_unfair_lock_lock_with_options(&lock, OS_UNFAIR_LOCK_DATA_SYNCHRONIZATION); + + if (scopeMap == NULL) { scopeMap = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFCopyStringDictionaryKeyCallBacks, NULL); - }); + } - dispatch_sync(logObjectQueue, ^{ - retval = (os_log_t) CFDictionaryGetValue(scopeMap, scope); - if (retval) return; - + retval = (os_log_t)CFDictionaryGetValue(scopeMap, scope); + if (retval == NULL) { CFStringPerformWithCString(scope, ^(const char *scopeStr) { CFDictionaryAddValue(scopeMap, scope, os_log_create("com.apple.securityd", scopeStr)); }); - retval = (os_log_t) CFDictionaryGetValue(scopeMap, scope); - }); - + retval = (os_log_t)CFDictionaryGetValue(scopeMap, scope); + } + + os_unfair_lock_unlock(&lock); + return retval; } @@ -460,17 +465,12 @@ void secLogEnable(void) { pthread_mutex_unlock(&loggingMutex); } -os_log_t logObjForScope(const char *scope) -{ - return secLogObjForScope(scope); -} - os_log_t secLogObjForScope(const char *scope) { if (!secLogEnabled()) return OS_LOG_DISABLED; CFStringRef cfscope = NULL; if(scope) cfscope = CFStringCreateWithCString(kCFAllocatorDefault, scope, kCFStringEncodingASCII); - os_log_t retval = logObjForCFScope(cfscope); + os_log_t retval = secLogObjForCFScope(cfscope); CFReleaseNull(cfscope); return retval; } @@ -506,7 +506,6 @@ CFStringRef SecLogAPICreate(bool apiIn, const char *api, CFStringRef format, ... } #if TARGET_OS_OSX -#ifdef NO_OS_LOG // Functions for weak-linking os_log functions #include @@ -548,6 +547,5 @@ weak_log_f(os_log_type_enabled, weak_os_log_type_enabled, bool, return false); #undef weak_log_f -#endif // NO_OS_LOG #endif // TARGET_OS_OSX