From 3f168f926f1d932c0256a5d5b3abba8cf92da852 Mon Sep 17 00:00:00 2001 From: Nareg Sinenian Date: Wed, 21 Dec 2016 00:52:30 +0800 Subject: [PATCH] Fixes issue where running discovery may prevent access to preferences, causing iscsid to crash. --- Source/User/iscsid/iSCSIDaemon.c | 39 ++++++++++++++----- iSCSIInitiator.xcodeproj/project.pbxproj | 12 +++--- .../xcschemes/iSCSI.framework.xcscheme | 2 +- .../xcschemes/iSCSI.kext.xcscheme | 2 +- .../xcshareddata/xcschemes/iscsictl.xcscheme | 2 +- .../xcshareddata/xcschemes/iscsid.xcscheme | 2 +- 6 files changed, 40 insertions(+), 19 deletions(-) diff --git a/Source/User/iscsid/iSCSIDaemon.c b/Source/User/iscsid/iSCSIDaemon.c index cc8f093e..72e064f8 100644 --- a/Source/User/iscsid/iSCSIDaemon.c +++ b/Source/User/iscsid/iSCSIDaemon.c @@ -75,6 +75,12 @@ IONotificationPortRef powerNotifyPortRef; // Used to fire discovery timer at specified intervals CFRunLoopTimerRef discoveryTimer = NULL; +// Used by discovery to notify the main daemon thread that data is ready +CFRunLoopSourceRef discoverySource = NULL; + +// Used to point to discovery records +CFDictionaryRef discoveryRecords = NULL; + // Mutex lock when discovery is running pthread_mutex_t discoveryMutex = PTHREAD_MUTEX_INITIALIZER; @@ -366,10 +372,8 @@ errno_t iSCSIDLoginCommon(SessionIdentifier sessionId, // Update target alias in preferences (if one was furnished) else { - pthread_mutex_lock(&preferencesMutex); iSCSIPreferencesSetTargetAlias(preferences,targetIQN,iSCSITargetGetAlias(target)); iSCSIPreferencesSynchronzeAppValues(preferences); - pthread_mutex_unlock(&preferencesMutex); } if(sessCfg) @@ -1302,8 +1306,21 @@ CFDictionaryRef iSCSIDCreateRecordsWithSendTargets(iSCSISessionManagerRef manage void * iSCSIDRunDiscovery(void * context) { - CFDictionaryRef discoveryRecords = iSCSIDCreateRecordsWithSendTargets(sessionManager,preferences); + if(discoveryRecords != NULL) + CFRelease(discoveryRecords); + + discoveryRecords = iSCSIDCreateRecordsWithSendTargets(sessionManager,preferences); + + + // Clear mutex created when discovery was launched + pthread_mutex_unlock(&discoveryMutex); + CFRunLoopSourceSignal(discoverySource); + return NULL; +} + +void iSCSIDProcessDiscoveryData(void * info) +{ // Process discovery results if any if(discoveryRecords) { @@ -1314,19 +1331,15 @@ void * iSCSIDRunDiscovery(void * context) const void * keys[count]; const void * values[count]; CFDictionaryGetKeysAndValues(discoveryRecords,keys,values); - + for(CFIndex i = 0; i < count; i++) iSCSIDUpdatePreferencesWithDiscoveredTargets(sessionManager,preferences,keys[i],values[i]); - + iSCSIPreferencesSynchronzeAppValues(preferences); pthread_mutex_unlock(&preferencesMutex); CFRelease(discoveryRecords); } - - pthread_mutex_unlock(&discoveryMutex); - - return NULL; } @@ -2112,6 +2125,14 @@ int main(void) reqInfo->socket = socket; reqInfo->socketSourceRead = sockSourceRead; reqInfo->fd = 0; + + // Runloop source signal by deamoen when discovery data is ready to be processed + CFRunLoopSourceContext discoveryContext; + bzero(&discoveryContext,sizeof(discoveryContext)); + discoveryContext.info = &discoveryRecords; + discoveryContext.perform = iSCSIDProcessDiscoveryData; + discoverySource = CFRunLoopSourceCreate(kCFAllocatorDefault,1,&discoveryContext); + CFRunLoopAddSource(CFRunLoopGetMain(),sockSourceRead,kCFRunLoopDefaultMode); asl_log(NULL,NULL,ASL_LEVEL_INFO,"daemon started"); diff --git a/iSCSIInitiator.xcodeproj/project.pbxproj b/iSCSIInitiator.xcodeproj/project.pbxproj index 8bdd7fe8..f9262407 100644 --- a/iSCSIInitiator.xcodeproj/project.pbxproj +++ b/iSCSIInitiator.xcodeproj/project.pbxproj @@ -522,7 +522,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0730; - LastUpgradeCheck = 0700; + LastUpgradeCheck = 0820; TargetAttributes = { 2B1A490B1D3D40FE00D3ED0D = { CreatedOnToolsVersion = 7.3.1; @@ -709,7 +709,7 @@ CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; CODE_SIGN_IDENTITY = ""; COPY_PHASE_STRIP = YES; - DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + DEBUG_INFORMATION_FORMAT = dwarf; DYLIB_CURRENT_VERSION = 1.0.0; ENABLE_NS_ASSERTIONS = NO; GCC_C_LANGUAGE_STANDARD = "compiler-default"; @@ -789,7 +789,7 @@ CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; CODE_SIGN_IDENTITY = "-"; COPY_PHASE_STRIP = NO; - DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + DEBUG_INFORMATION_FORMAT = dwarf; ENABLE_NS_ASSERTIONS = NO; ENABLE_STRICT_OBJC_MSGSEND = YES; GCC_C_LANGUAGE_STANDARD = gnu99; @@ -922,7 +922,7 @@ COMBINE_HIDPI_IMAGES = YES; COPY_PHASE_STRIP = NO; CURRENT_PROJECT_VERSION = 1; - DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + DEBUG_INFORMATION_FORMAT = dwarf; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = ""; DYLIB_CURRENT_VERSION = 1.0.0; @@ -1023,7 +1023,7 @@ COMBINE_HIDPI_IMAGES = YES; COPY_PHASE_STRIP = YES; CURRENT_PROJECT_VERSION = "1.0.0-beta5"; - DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + DEBUG_INFORMATION_FORMAT = dwarf; DYLIB_CURRENT_VERSION = 1.0.0; ENABLE_NS_ASSERTIONS = NO; GCC_C_LANGUAGE_STANDARD = gnu99; @@ -1114,7 +1114,7 @@ CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; COPY_PHASE_STRIP = YES; CURRENT_PROJECT_VERSION = "1.0.0-beta5"; - DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + DEBUG_INFORMATION_FORMAT = dwarf; DYLIB_CURRENT_VERSION = 1.0.0; ENABLE_NS_ASSERTIONS = NO; GCC_C_LANGUAGE_STANDARD = "compiler-default"; diff --git a/iSCSIInitiator.xcodeproj/xcshareddata/xcschemes/iSCSI.framework.xcscheme b/iSCSIInitiator.xcodeproj/xcshareddata/xcschemes/iSCSI.framework.xcscheme index a8ec144c..ce2c1c24 100644 --- a/iSCSIInitiator.xcodeproj/xcshareddata/xcschemes/iSCSI.framework.xcscheme +++ b/iSCSIInitiator.xcodeproj/xcshareddata/xcschemes/iSCSI.framework.xcscheme @@ -1,6 +1,6 @@